So far, you have been introduced to integers (e.g. 1) strings (e.g. 'hello'), lists (e.g. [1, 'a', 3] , and sets (e.g. {1, 2, 'Python'}). You have learned how to create and inspect them using methods and built-in functions.
However, you'll find that, most of the time, you just want to carry out some operation for all the items in a sequence. Built-in functions and methods are then not always the answer. However, the for-loop is!
The goal of this chapter is to introduce you to the for-loop, which is the most commonly used loop.
At the end of this chapter, you will be able to:
If you want to learn more about these topics, you might find the following links useful:
If you have questions about this chapter, please refer to the forum on Canvas.
In [ ]:
for number in [1,2,3]:
print(number)
This loop prints the numbers 1, 2, and 3, each on a new line. The variable name number is just something I have chosen. It could have been anything, even something like sugar_bunny. But number is nice and descriptive. OK, so how does the loop work?
Please note that if a container (in this case a list) is empty, we will not print anything.
In [ ]:
for number in []:
print(number)
Loops work extremely well with if-statements
In [ ]:
for item in ['Python', 'is', 'really', 'nice', 'right?']:
if item.startswith('r'):
print(item, 'startswith the letter r')
else:
print(item, 'does not start with the letter r')
In [ ]:
a_list = [1,2,3,4]
a_list.reverse()
for item in a_list:
print(item)
In [ ]:
a_list = [1,2,3,4]
for item in a_list[::-1]:
print(item)
In [ ]:
a_set = {'sets', 'are', 'unordered'}
for item in a_set:
print(item)
In [ ]:
word = "hippopotamus"
for letter in word:
print(letter)
One data structure (a new one!) is very frequently used with for loops. This data structure is a tuple. It's very simple to create a tuple.
In [ ]:
a_tuple = (1, 'a', 2)
print(a_tuple)
Important for use
In [ ]:
help(tuple)
A tuple can be empty. You can use () or tuple():
In [ ]:
an_empty_tuple = ()
print(an_empty_tuple)
In [ ]:
another_empty_tuple = tuple()
print(another_empty_tuple)
In [ ]:
a_tuple = (1, 'a', 2)
first_el, second_el, third_el = a_tuple
print(first_el)
print(second_el)
print(third_el)
In [ ]:
language_data = [('the', 'determiner'),
('house', 'noun'),
('is', 'verb'),
('big', 'adjective')]
Usually, you would like to loop through such an example. One way of doing that is the following:
In [ ]:
for item in language_data:
print(item[0], item[1])
However, we can unpack the tuple within the for-loop to make it more readable:
In [ ]:
for word, part_of_speech in language_data:
print(word, part_of_speech)
Note about unpacking
Unpacking is an operation that can also be used with other containers, for instance with lists:
In [ ]:
a_list = [1,2,3]
first_el, second_el, third_el = a_list
print(first_el)
print(second_el)
print(third_el)
break
The break statement lets us escape a loop.
In [ ]:
word = "hippopotamus"
for letter in word:
print(letter)
if letter =="o":
break
continue
The continue statement ends the current iteration and jumps to the top of the loop and starts the next iteration.
In [ ]:
word = "hippopotamus"
for letter in word:
if letter =="o":
continue
print(letter)
In the two examples above, not all letters in the word 'hippopotamus' are printed. Both break and continue teleport you to another part of the code. break teleports out of the loop, continue teleports you to the next iteration of the loop.
In [ ]:
sentence = 'Now I know a lot about looping'
word_list = sentence.split()
# your code here
In [ ]:
number_collection = [1,2,2,3,50, 50, 60, 57, 58, 3, 37, 37]
unique_number = # your code here
# print each number on a single line
# your code here
In [ ]:
a_list = [1,2,3]
first_el, second_el, third_el = a_list
print(first_el)
print(second_el)
print(third_el)
a_list.append(5)
first_el, second_el, third_el = a_list
print(first_el)
print(second_el)
print(third_el)
In [ ]:
a_list.append(5)
first_el, second_el, third_el = a_list
print(first_el)
print(second_el)
print(third_el)
In [ ]:
a_set = {1,2,3,}
first_el, second_el, third_el = a_set
print(first_el)
print(second_el)
print(third_el)
In [ ]:
['Python', 'is', 'really', 'nice', 'right?']
# your code here
Please use the continue statement to make sure that words that start with the letter 'r' are not printed
In [ ]:
['Python', 'is', 'really', 'nice', 'right?']
# your code here
In [ ]: