-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoops.py
72 lines (61 loc) · 1.69 KB
/
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
#execute a block of code several nosof times.
'''
#while - true is any non-zero value
cnt =0
while(cnt<9):
print(cnt)
cnt = cnt+1
print("Bye")
# for- used to iterate over any seq such as list,tuple,string etc
#for with list and 'in'
langs = ['C','Cpp','Java','Python']
for l in langs: #using 'in' operator
print(l)
#for with range()
val= range(4) #range() to def range of vals between 0-3
for i in val:
print(i)
#for loop with else
digits = [1,4,7,8]
for j in digits:
print(j)
else:
print("No item left")
#break
for i in range(1, 10):
if(i==4):
break
print(i)
for letter in "ketan":
if letter == 't' or letter == 'a':
break
print ('Current Letter :', letter)
#else bloack will not be executed if 1.exception occurs,2.break,3.os.exit()
#case1- exception occurs
for i in range(10):
print("Magic",i)
# print(10/0)
else:
print("Exited with else")
#case2 -by break
for i in range(8):
print("Hi",i)
if(i==4):
break
else:
print("else of for loop")
#else is always executed when while/for loop terminated normally.
'''
#Nested Loop - prog to check prime nos in given range
lowerd = int(input("Enter lower range"))
upperd = int(input("enter higher range"))
print("Prime numbers between", lowerd, "and", upperd, "are:")
for num in range(lowerd,upperd+1):
if num>1:
for i in range(2,num):
if num%i==0:
break
else:
print(num)
#Pass is an empty statement in python.If you want declare the func now w/t
#implementation, but you want provide the implementation in future use pass statement.