-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path030_ForLoop.py
More file actions
40 lines (33 loc) · 843 Bytes
/
030_ForLoop.py
File metadata and controls
40 lines (33 loc) · 843 Bytes
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
"""
FOR LOOP
For loop is used to iterate over collections (such as list, tuple, set, dictionary) or strings.
It is very much similar to other programming language.
"""
# define a list
list1 = ["1","2","3","4"]
list2 = ["A","B","C","D"]
# print each item from list
print("Loop through list")
for x in list1:
print(x)
# print each charater from string
print("\nLoop through string")
for x in "STRING":
print(x)
# using 'break' keyword to stop loop
print("\nusing 'break' keyword")
for x in list1:
print(x)
if x == "3":
break
# using 'continue' keyword to stop current iteration and continue with next
print("\nusing 'continue' keyword")
for x in list1:
if x == "3":
continue
print(x)
# nested for loop
print("\nNested For Loop")
for x in list1:
for y in list2:
print(x, y)