Python for loops Girls’ Programming Network School of Information Technologies University of Sydney Mini-lecture 7 Lists for loops More Strings Summary 2 Outline 1 Lists 2 for loops 3 More Strings 4 Summary Girls’ Programming Network for loops Mini-lecture 7 Lists for loops More Strings Summary 3 Storing groups of things in variables Imagine we want to store a list of strings or integers We can try to do this with variables 1 2 3 4 >>> >>> >>> >>> word1 word2 word3 word4 = = = = 'This' 'is' ' a' 'sentence' If the list was long we’d need thousands of variables And we’d need separate code to do things with each variable: 1 >>> print word1, word2, word3, ... Girls’ Programming Network for loops Mini-lecture 7 Lists for loops More Strings Summary 4 Lists can store multiple things 1 2 3 4 5 6 >>> words = ['This', 'is', 'a', 'sentence'] >>> words[0] 'This' >>> words[1] 'is' >>> A list is created using square brackets in Python The words list holds four strings in order Each item in a list is called an element Each element can be accessed using an index or subscript Notice that indices start from zero! Girls’ Programming Network for loops Mini-lecture 7 Lists for loops More Strings Summary 5 What happens if we fall off the end? 1 2 3 4 5 6 7 8 >>> words = ['This', 'is', 'a', 'sentence'] >>> words[4] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range >>> words[-1] 'sentence' >>> Python complains about accessing off the end of a list But, negative indices work! They return elements from the end of the list What would word[3] and word[-3] return? Girls’ Programming Network for loops Mini-lecture 7 Lists for loops More Strings Summary 6 You can put anything in a list You can have a list of integers: 1 >>> primes = [1, 2, 3, 5, 7, 11] You can have lists with mixed integers and strings 1 >>> l = [1, 'two', 3, 4, 'five'] But this is almost never a good idea You should treat every element of the list the same Girls’ Programming Network for loops Mini-lecture 7 Lists for loops More Strings Summary 7 A Python for loop in action 1 2 3 4 5 6 7 >>> for i in [0, 1, 2]: ... print i, 'is an element' ... 0 is an element 1 is an element 2 is an element >>> A for loop starts with the keyword for Remember, all Python control structures start with a keyword Next is the loop variable i which will hold each element Then the keyword in followed by the list to loop over Watch out for missing the colon! Girls’ Programming Network for loops Mini-lecture 7 Lists for loops More Strings Summary 7 A Python for loop in action 1 2 3 4 5 6 7 >>> for i in [0, 1, 2]: ... print i, 'is an element' ... 0 is an element 1 is an element 2 is an element >>> Each element of the list is assigned to the variable i in turn Then the body is run When it runs out of elements in the list, the loop stops Notice you don’t need to update i yourself now Girls’ Programming Network for loops Mini-lecture 7 Lists for loops More Strings Summary 8 for loops can run over lists of strings too 1 2 3 4 5 6 7 8 9 >>> words = ['This', 'is', 'a', 'sentence'] >>> for word in words: ... print word, 'is an element' ... This is an element is is an element a is an element sentence is an element >>> The list can be stored in a variable too (here words) Girls’ Programming Network for loops Mini-lecture 7 Lists for loops More Strings Summary 9 range and xrange create lists of integers 1 2 3 4 5 6 7 8 9 >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> for i in range(3): ... print i ... 0 1 2 >>> range(n) creates a list up to but not including n range(n) starts from 0 This matches list indexing from zero xrange(n) is a more efficient version for large lists Girls’ Programming Network for loops Mini-lecture 7 Lists for loops More Strings Summary 10 Appending items to lists We can add elements to an existing list using append 1 2 3 4 >>> words = ['This', 'is', 'a', 'sentence'] >>> words.append('blah') >>> words ['This', 'is', 'a', 'sentence', 'blah'] append is a special kind of function called a method The thing we are operating on appears before the function name In this case, we’re appending to the words list A period (.) separates the object from the function name Girls’ Programming Network for loops Mini-lecture 7 Lists Strings in for for loops More Strings Summary 11 loops You can use strings in for loops in the same way you use lists Instead of iterating over the elements of the list, you iterate over the characters in the string 1 2 for letter in "mosey": print letter, What will be the output of the code above? You can also access characters in a string by their indices, like elements of a list. 1 2 3 >>> last_char = "smiley"[-1] >>> last_char ' y' Girls’ Programming Network for loops Mini-lecture 7 Lists for loops More Strings Summary 12 String Methods Like the list method append, there are lots of special methods defined for strings. You can find out about these by using the builtin Python documentation. 1 >>> help(str) You’ll have to scroll a bit to get to the bits you want. Look for stuff like 1 2 3 4 5 | | | | | capitalize(...) S.capitalize() -> string Girls’ Programming Network Return a copy of the string S with only its first character capitalized. for loops Mini-lecture 7 Lists for loops More Strings Summary 13 You should now be able to: Create Python lists Access individual list elements using indices Write Python for loops Append elements to a list Use help in the Python interpreter Girls’ Programming Network for loops Mini-lecture 7