forked from Akuli/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloops.py
More file actions
404 lines (256 loc) · 6.85 KB
/
loops.py
File metadata and controls
404 lines (256 loc) · 6.85 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#### LOOPS #####
### While Loop
Having below 3 parts
1) #Initialisation / Assign the value
2) #Condition
3) #Increment/Decrement
i=1 #Initialisation
while i<=5: #Condition
print("Raja",i)
i=i+1 #Increment/Decrement
## reverse while loop
i=10
while i>=1:
print("Mango",i)
i=i-1
-----
i=1
while i<=5:
print(i)
i=i+1
i=1
while i<=20:
print(i)
i=i+1
## nested while loop
i=1
while i<=5:
print(i, "Raja", end="")
j=1
while(j<=3):
print("Gopal", end="")
j=j+1
i=i+1
print()
#Example
i=1
while i<=5:
print(i,"Raja : ", end="")
j=1
while j<=5:
print(j,"Monkey, ",end="")
j=j+1
i=i+1
print()
#Example
i=1
while i<=5:
print(i,"Man: ",end="")
j=1
while j<=5:
print("Monkey",j,"; " , end="")
j=j+1
i=i+1
print()
##3 NAME RAJAGOPALNAIDU with while loop ###
i=1
while i<=3:
print(i, "Raja", end="")
j=1
while(j<=1):
print("Gopal", end="")
k=1
while(k<=1):
print("naidu", end="")
k=k+1
j=j+1
i=i+1
print()
#### For loop ########
##String
s = "RAJAGOPALNAIDU"
for i in s:
print(i) # this code will provide each "i" value in seperate lines
#to retrive above string results in a single line
s = "RAJAGOPALNAIDU"
for i in s:
print(i,end="")
## For loop with "List":
##LIST
l=["Raja", 99, 99, 8.7] # this list is having str,int,flot
for i in l:
print(i)
## For loop with "Tuple":
##Tuple
t=(1,2,2,3,4,6,7,8)
for i in t:
print(i)
T=(11)
for i in range(T):
print(i)
for i in range(23,37): # here range starting value consider as 23 and end value is n-1 which is 37-1=36
print(i)
for i in range(1, 50, 5): # Here 1 is starting value, 50-1 is ending value & 5 is itreation (difference) which means every 5th number will be in results
print (i)
#reverse
for i in range(11,0,-2):
print(i)
## which we don't want to print the values are Divisible by 5
for i in range(1,50):
if i%5!=0:
print(i) # if you observe this results 5 divisible numbers will not display means 5,10,15,20,25........
#
for i in range(1,10):
if i%2!=0:
print(i) # 2 divisables are not displayed
#
for i in range(1,10):
if i%2==0:
print(i) # 2 divisables only will display
#
for i in range (41,20,-2):
print(i)
###### NESTED FOR LOOP#####
for i in range(1,100):
if i%3!=0:
if i%5!=0:
print(i)
### Above Same query we can write as below:
for i in range(1,50):
if i%3!=0 and i%5!=0:
print(i)
### by using or both divisable values will not display
for i in range(1,61):
if i%3!=0 or i%5!=0:
print(i) # from this out put 15,30,45,60 will be missing as these nubers will divisible by both 3, 5
for i in range(1,61):
if i%3==0 and i%5==0:
print(i) # will display 15,30,45,60 these nubers only as these will divisible by both 3, 5
for i in range(1,61):
if i%3==0 or i%5==0:
print(i) #3,5 both divisible numbers will dislay
############ Key words "break", "Continue", "Pass" ###########
# 1) Break
# 2) Continue
# 3) Pass
# 1) Break
while using wending michene if we try to enter morethen avilable items in the mechene then
# if with in range by using FOR LOOP
for i in range(5):
if i>=10:
print("Given count is out of range")
break
print(i)
# # if out of range by using FOR LOOP
for i in range(11):
if i<10:
print("Given count is out of range")
break
print(i)
### if with in range by using WHILE LOOP (with out "input" option)
#ex: wending mechene has 3 items only but upto 5 items that will give so
# if customer enter/looking for 5 items from the mechene, will give 3 items and for 4th system will diplay not avilable message
av=3 ## av = avilability
i=1
while i<=5:
if i>av:
print("Input items are not avilable in the mechene")
break
print(i)
i+=1 # 3 items will get but for 4th item will display "Input items are not avilable in the mechene" message
##### To run same queries by using same WHILE & FOR LOOPS #####
for i in range(31):
if i%3!=0: #divisible by 3 number are not displayed from the results
print(i)
for i in range(1,31):
if i%3==0: #divisible by 3 number will displayed from the results
print(i)
#Reverse for above
for i in range(31,1,-1):
if i%3==0: #divisible by 3 number will displayed from the results
print(i)
##Same testing using while loop
i=1
while i<=30:
if i%3!=0: # Except divisible 3 numbers remining will display
print(i)
i=i+1
#Reverse order
i=30
while i>=1:
if i%3==0: #all 3 divisible numbers will display
print(i)
i=i-1
### if out of range by using FOR LOOP with "INPUT" value
av=5
x=int(input("Enter how many items required :"))
i=1
while i<=x:
#if i>av: ### if user enter morethen avilable items with this condition michene will provide avilable items in the michene and will give below mentioned error message.
#print(("Out of stock"))
#break
if x>av: ### with this condition michene will not provide any item if user enter morethen avilable items and stright away will give below mentioned error message.
print("These many items are not avilable in michene")
break
print("Item :",i)
i=i+1
# 2) Continue
for i in range(1,31):
if i%3!=0 and i%5!=0: #divisible by 3,5 number will displayed from the results
continue
print(i)
print("Done")
for i in range(1,31):
if i%3!=0 or i%5!=0: #will displayed divisible by both 3 & 5 numbers only in result
continue
print(i)
print("Done")
for i in range(1,11):
if i%3==0 or i%5==0: #divisible by 3 & 5 numbers will not displayed from the results
continue
print(i)
print("Done")
#Reverse for above
for i in range(31,1,-1):
if i%3==0: #divisible by 3 number will displayed from the results
print(i)
if i<3:
continue
print("this is end")
##Same testing using while loop
i=1
while i<=30:
if i%3!=0: # Except divisible 3 numbers remining will display
print(i)
i=i+1
if i>30:
continue
print("Done with the 3rd table")
#Reverse order
i=30
while i>=3:
if i%3==0: #all 3 divisible numbers will display
print(i)
i=i-1
if i<3:
print("No 3 divisibls after this number")
continue
## 3) Pass ##
#where which we don't need to print odd numbers between 1 - 100
for i in range (1,100):
if i%2==0:
pass
else:
print(i)
print("above list is odd values between 1 to 100")
for i in range (1,20):
if i%2!=0:
continue
else:
print(i)
for i in range (1,21):
if i%10!=0:
break
else:
print(i)
print("Myavvv")