Python for loops - School of Information Technologies

advertisement
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
Download