-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path55_Python_For_Loops.py
72 lines (61 loc) · 1.18 KB
/
55_Python_For_Loops.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Python For Loops
print("Python For Loops")
# for loop
print("\nfor loop")
print("Example:")
fruits = ['apple', 'banana', 'mango']
for x in fruits:
print(x)
# Looping through a string
print("\nLooping through a string")
print("Example:")
for x in "car":
print(x)
# The break statement
print("\nThe break statement")
print("Example:")
car = ['toyota', 'mazda', 'nissan']
for x in car:
print(x)
if x == 'mazda':
break
# The continue statement
print("\nThe continue statement")
print("Example:")
values = [1, 2, 3, 4, 5]
for x in values:
if x == 2:
continue
print(x)
# The range() function
print("\nThe range() function")
print("Example:")
for x in range(4):
print(x)
print("Example:")
for x in range(2, 5):
print(x)
print("Example:")
for x in range(3, 12, 3):
print(x)
# Else in for loop
print("\nElse in for loop")
print("Example:")
for x in range(4):
print(x)
else:
print("Loop finished!")
# Nested loop
print("\nNested loop")
print("Example:")
colors = ['red', 'green']
fruits = ['apple', 'cherry']
for a in colors:
for b in fruits:
print(a, b)
# The pass statement
print("\nThe pass statement")
print("Example:")
for x in [0, 1, 2]:
pass
# Error will not occur