t_02.4up

t_02.4up - Questions from Week 01 Engineering Computation...

This preview shows pages 1–5. Sign up to view the full content.

Engineering Computation: Tutorial 02 Problem Solving with Computing, using the Python Language January 13, 2011 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 Built-in functions 5 Math library functions 6 Straight-line (non-branching) programs 7 Branching programs 3 Today’s Topics: 1 Lists 2 For loops 3 Strings 4

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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 ﬁrst 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 List methods You can do things to your list using build-in list methods. [Demo] >>> colours = ["red", "white", "orange"] >>> colours.append("purple") >>> colours.insert(2, "green") >>> colours.sort() >>> colours.remove("red") 10 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 . range ( start , n) # l i s t of i n t e g e r s from s t a r t up to , but not # i n c l u d i n g n with step d i f f e r e n c e between # l i s t elements . range ( start , n , step ) 11 range function example uses of range function >>> range (5) [0 , 1 , 2 , 3 , 4] >>> range (1 ,5) [1 , 2 , 3 , 4] >>> range (1 ,5 ,2) [1 , 3] # you can bind the r e s u l t to a v a r i a b l e >>> myList = range (5) >>> myList [0 , 1 , 2 , 3 , 4] 12

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Built in Functions The max( < sequence > ) gives the maximum element in the sequence. Can take any number of parameters.
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 09/14/2011.

Page1 / 13

t_02.4up - Questions from Week 01 Engineering Computation...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online