This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Engineering Computation: Tutorial 02 Problem Solving with Computing, using the Python Language September 27, 2010 Questions from Week 01? Programming Exercises from Zelle textbook 1 pg. 49 # 1, 2, 4, 6, 8. 2 What’s in your toolbox? 1 Print statement, string literals 2 Basic calculator, integer division 3 Variables 4 Builtin functions 5 Math library functions 6 Straightline (nonbranching) programs 7 Branching programs 3 Today’s Topics: 1 Lists 2 For loops 3 Strings 4 Lists We’ve used variables to name only a single piece of data . Sometimes, you want to refer to a whole bunch of data all at once. A new type of variable: the list . 5 Lists We’ve used variables to name only a single piece of data . Sometimes, you want to refer to a whole bunch of data all at once. A new type of variable: the list . A list is a ”collection variable” or data structure . Lists were invented so that we wouldn’t have to create 1,000 variables to store a thousand values. list example my family = [ ”mom” , ”dad” , ” s i s ” , ”bro” , ”dog” ] 5 Lists You can think of a list as a data train. Each car in the train is called a list element Each car can store a single piece of data. Usually, you would only store one type of element in a list. A list is itself just another variable . you can give it its own variable name. 6 Lists Pieces of data can be read from inside the list using an index . Python lists are indexed with positive and negative numbers. For positive indices, 0 is the first index, 1 is the second index and so forth. reading list elements >>> m y l i s t = [7 , 24 , 131] >>> m y l i s t [ 0 ] 7 >>> m y l i s t [ 1 ] 24 >>> m y l i s t [ 2 ] 131 >>> 7 Lists For negative indices 1 is the last index, 2 is the second last index and so forth. reading list elements >>> m y l i s t = [7 , 24 , 131] >>> m y l i s t [ 1] 131 >>> m y l i s t [ 2] 24 >>> m y l i s t [ 3] 7 >>> 8 Lists Lists are mutable . This means you can change the value of list elements. mutate list element >>> m y l i s t = [7 , 24 , 131] >>> m y l i s t [ 1 ] = 2.71 >>> m y l i s t [7 , 2.71 , 131] 9 Lists: append You can also add to a list using the append method. append a list >>> m y l i s t [7 , 2.71 , 131] >>> m y l i s t . append (3) >>> m y l i s t [7 , 2.71 , 131 , 3] 10 range function The range function generates a list of integers. 11 range function The range function generates a list of integers. uses # generate a l i s t of i n t e g e r s from 0 up to , # but not i n c l u d i n g n . range (n) 11 range function The range function generates a list of integers. uses # generate a l i s t of i n t e g e r s from 0 up to , # but not i n c l u d i n g n . range (n) # l i s t of i n t e g e r s from start , up to , but # not i n c l u d i n g n ....
View
Full Document
 Spring '10
 SMITH
 Fibonacci number

Click to edit the document details