for Loops
for loops is a type of loop that allows us walking on the lists, tuples, strings, and even dictionaries.
The syntax of this loop is:
for item in object(it can be a list, a tuple, a dictionary etc.):
statements
Here is a breakdown of the syntax:
for➜ the keyword that indicates that the for statement begins.
item ➜ specifies the name of the variable that will hold a single element of a sequence.
in ➜ indicates that the sequence comes next.
object ➜ the sequence that will be stepped through.
statements ➜ the statements that will be executed in each iteration.
Walking on the lists:
In [1]: my_list = [1,2,3,4,5,6]
for element in my_list:
print("Item",element)
Item
Item
Item
Item
Item
Item
1
2
3
4
5
6
In [2]: # Adding elements using for loop
list1 = [1,2,3,4,5,6]
sum = 0
for item in list1:
sum = sum + item
print("Sum =",sum)
Sum = 21
The Complete Python 3 Programming Course: (Beginner to Advanced)
In [3]: # Printing even numbers
list2 = [1,2,3,4,5,6,7,8]
for item in list2:
if item % 2 == 0:
print(item)
2
4
6
8
In [4]: # Printing odd numbers
list2 = [1,2,3,4,5,6,7,8]
for item in list2:
if item % 2 == 1:
print(item)
1
3
5
7
Walking on the strings:
In [5]: s = "Hello world!"
for letter in s:
print(letter)
H
e
l
l
o
w
o
r
l
d
!
In [6]: # Replicate strings using loops
s = "Python"
for letter in s:
print(letter * 5)
PPPPP
yyyyy
ttttt
hhhhh
ooooo
nnnnn
The Complete Python 3 Programming Course: (Beginner to Advanced)
Walking on the tuples:
In [7]: # Walking on the tuples
t = (1,2,3,4,5,6)
for item in t:
print(item)
1
2
3
4
5
6
We can walkthrough the multidimensional tuples like this:
In [8]: # Two-dimensional tuples
list3 = [(1,2),(3,4),(5,6),(7,8)]
for item in list3:
print(item)
(1,
(3,
(5,
(7,
2)
4)
6)
8)
We can see that each element is printed as tuple. What if we want to reach that items?
In [9]: # Tuple unpacking
list3 = [(1,2),(3,4),(5,6),(7,8)]
for (i,j) in list3:
print("i:",i,"j:",j)
i:
i:
i:
i:
1
3
5
7
j:
j:
j:
j:
2
4
6
8
In [10]: #Unpacking three dimensional tuples
list4 = [(1,2,3),(4,5,6),(7,8,9),(10,11,12)]
for (i,j,k) in list4:
print(i * j * k)
6
120
504
1320
Walk through the dictionaries:
We have learned 3 methods of the dictionaries in our dictionaries lesson.
1) keys() method
2) values() method
3) items() method
The Complete Python 3 Programming Course: (Beginner to Advanced)
In [11]: my_dict = {"one":1,"two":2,"three":3,"four":4}
my_dict.keys()
Out[11]: dict_keys(['one', 'two', 'three', 'four'])
In [12]: my_dict.values()
Out[12]: dict_values([1, 2, 3, 4])
In [13]: my_dict.items()
Out[13]: dict_items([('one', 1), ('two', 2), ('three', 3), ('four', 4)])
We can walk through the dictionaries with using these methods.
If we don't write any dict methods, we get keys of the dictionary as default.
Let's see this:
In [14]: dict1 = {"one":1,"two":2,"three":3,"four":4}
for i in dict1:
print(i)
one
two
three
four
In [15]: #using keys method
dict1 = {"one":1,"two":2,"three":3,"four":4}
for i in dict1.keys():
print(i)
one
two
three
four
In [16]: #using values method
dict1 = {"one":1,"two":2,"three":3,"four":4}
for i in dict1.values():
print(i)
1
2
3
4
In [17]: #using items method
dict1 = {"one":1,"two":2,"three":3,"four":4}
for i in dict1.items():
print(i)
('one', 1)
('two', 2)
('three', 3)
('four', 4)
The Complete Python 3 Programming Course: (Beginner to Advanced)
In [18]: #getting the keys and values with items()
dict1 = {"one":1,"two":2,"three":3,"four":4}
for k,v in dict1.items():
print("Key:",k,"Value:",v)
Key:
Key:
Key:
Key:
one Value: 1
two Value: 2
three Value: 3
four Value: 4
This is the end of this lesson.
See you in our next lessons.
In [ ]:
The Complete Python 3 Programming Course: (Beginner to Advanced)