forked from dscnsec/Python-Bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
9_loops.py
86 lines (73 loc) · 1.63 KB
/
9_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#This is a basic python script to demonstrate loops
#This is a for loop
#For loops are used to iterate over a sequence
for i in range(5):
print(i)
#This is a while loop
#While loops are used to iterate over a condition
i = 0
while i < 5:
print(i)
i += 1
#This is a nested loop
#Nested loops are loops within loops
for i in range(5):
for j in range(5):
print(i,j)
#This is a break statement
#Break statements are used to break out of a loop
for i in range(5):
if i == 3:
break
print(i)
#This is a continue statement
#Continue statements are used to skip the current iteration of a loop
for i in range(5):
if i == 3:
continue
print(i)
#This is a pass statement
#Pass statements are used to do nothing
for i in range(5):
if i == 3:
pass
print(i)
#This is a loop with an else statement
#Else statements are executed when the loop is finished
for i in range(5):
print(i)
else:
print("Loop is finished")
#This is a loop with an else statement
#Else statements are executed when the loop is finished
i = 0
while i < 5:
print(i)
i += 1
else:
print("Loop is finished")
#Iterating over a list
#Lists are mutable
list = [1,2,3,4,5]
for i in list:
print(i)
#Iterating over a dictionary
#Dictionaries are key value pairs
dict = {"name":"GDSC","age":1}
for i in dict:
print(i)
#Iterating over a tuple
#Tuples are immutable
tuple = (1,2,3,4,5)
for i in tuple:
print(i)
#Iterating over a set
#Sets are unordered and unindexed
set = {1,2,3,4,5}
for i in set:
print(i)
#Iterating over a string
#Strings are immutable
string = "Hello"
for i in string:
print(i)