Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engineering Computing

Similar presentations


Presentation on theme: "Engineering Computing"— Presentation transcript:

1 Engineering Computing
Week 3

2 Lists Often we want to store multiple values.
Record how many students show up to class each day Record the grades of students gets on their assignments Lists contain 0 or more objects Elements in the loop are separated by commas and contained within square brackets [] Ex. # Number of whales seen per day [6, 2, 4, 7, 3, 6, 64, 23, 5, 3, 7, 3, 8]

3 Lists >>>whales = [6, 2, 4, 7, 3, 6, 64, 23, 5, 3, 7, 3, 8]
How do we access individual elements? 1 2 3 4 5 6 7 8 9 10 11 12 whales 6 2 4 7 3 6 64 23 5 3 7 3 8

4 Accessing List Elements
>>>whales = [6, 2, 4, 7, 3, 6, 64, 23, 5, 3, 7, 3, 8] 1 2 3 4 5 6 7 8 9 10 11 12 whales 6 2 4 7 3 6 64 23 5 3 7 3 8 >>>whales[0] >>>whales[12] 6 8 >>>whales[4] >>>whales[103] 3 IndexError: list index out of range >>>whales[8] 5

5 Accessing List Elements
>>>whales = [6, 2, 4, 7, 3, 6, 64, 23, 5, 3, 7, 3, 8] 1 2 3 4 5 6 7 8 9 10 11 12 whales 6 2 4 7 3 6 64 23 5 3 7 3 8 >>>whales[-1] >>>whales[-4] 8 >>>whales[-2] >>>whales[-13] 3 6 >>>whales[-3] 7

6 Slicing Lists >>>whales = [6, 2, 4, 7, 3, 6, 64, 23, 5, 3, 7, 3, 8] 1 2 3 4 5 6 7 8 9 10 11 12 whales 6 2 4 7 3 6 64 23 5 3 7 3 8 >>>whales[1 : 4] >>>whales[:] [2,4,7] [6, 2, 4, 7, 3, 6, 64, 23, 5, 3, 7, 3, 8] >>>whales[ : 7] >>>whales[1:-1] [6,2,4,7,3,6,64] [2, 4, 7, 3, 6, 64, 23, 5, 3, 7, 3] >>>whales[2 : ] [4, 7, 3, 6, 64, 23, 5, 3, 7, 3, 8]

7 Empty List emptyList= [] >>>emptyList[0]
The length of the empty list is 0 There are no legal indices in the emtpy list and there is no data to access >>>emptyList[0] IndexError: list index out of range >>>emptyList[10]

8 Heterogeneity The elements in the list don’t just have to be numbers
>>> vowels = [`a’, `e’, `i’, `o’, `u’, `y’] [`a’, `e’, `i’, `o’, `u’, `y’] >>>vowels[0] ‘a’ >>>vowels[4] ‘u’ You can also mix and max elements >>>stuff= [`a’, 1, ’hello’, -5.0] [`a’, 1, ’hello’, -5.0] >>>stuff[3] -5.0 >>>stuff[0] ‘a’

9 Modifying List We can use the same notation that we use to access lists to modify lists >>> nobles = [‘helium’, ‘none’, ‘argon’, ’krypton’, ‘xenon’, ‘radon’] >>>nobles[1] ‘none’ >>>nobles[1] = ‘neon’ >>>nobles [‘helium’, ‘neon’, ‘argon’, ’krypton’, ‘xenon’, ‘radon’]

10 Built in List Functions
>>>numbers = [1, 5, 6, 3, 8, 2, 9, 20, 12, 20] >>>len(numbers) #Length of the list 10 >>>max(numbers) #Maximum value in the list 20 >>> min(numbers) #Minimum number in the list 1 >>>sum(numbers) #Sum of the numbers in the list 86

11 Processing List Items 1 ‘a’ third 4
In Python, for loops are used to iterate over elements of a list. They have a very familiar syntax. for variable in list: block Example list = [‘1’, 1, ‘a’, ‘third’, 4] for i in list: print i 1 ‘a’ third 4

12 Common For Loops >>> list = [1,2,3,4,5]
>>>for i in list: … print i*2 2 4 6 8 10 >>>for i in range(1,6,1) : … print I

13 Other Common For Loops >>>for i in “abcdefg”: …. print i a b c d e f g >>>outer = [‘Li’, ‘Na’, ‘K’] >>>inner =[‘F’, ‘Cl’, ‘Br’] #Nested For Loop for metal in outer: for gas in inner: print metal + gas LiF LiCl LiBr NaF NaCl NaBr KF KCl KBr

14 List Functions >>>list = [‘abc’, ’def’, ‘jkl’, ‘mno’, ‘pqr’]
>>>list.append(‘stu’); print list # append(v) appends v To End of List [‘abc’, ’def’, ‘jkl’, ‘mno’, ‘pqr’, ‘stu’] >>>list.insert(1,‘ghi’); print list #insert(i,v) inserts value v at index i [‘abc’, ‘ghi’, ’def’, ‘jkl’, ‘mno’, ‘pqr’, ‘stu’] >>>list.remove(‘abc’); print list #remove(v) removes first instance of v [‘ghi’, ’def’, ‘jkl’, ‘mno’, ‘pqr’, ‘stu’] >>>list.reverse(); print list [‘stu’, ’pqr’, ’mno’, ’jkl’, ’def’, ’ghi’]

15 More List Functions >>>list.sort(); print list #Sorts alphabetically or lo->hi [’def’, ’ghi’, ‘jkl’, ‘mno’, ‘pqr’, ‘stu’] >>>list.pop(); print list #Removes and returns last element [’def’, ’ghi’, ‘jkl’, ‘mno’, ‘pqr’]


Download ppt "Engineering Computing"

Similar presentations


Ads by Google