Skip to content

Commit 1a06996

Browse files
committed
files of video 18 added
1 parent 4187c4d commit 1a06996

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

video_18/while_loop.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
from os import system
2+
system('clear')
3+
4+
5+
6+
7+
### why we need loops in programming
8+
# l = [1, 1.1, 'hello', True, 1+2j,
9+
# 'this world', 'this is a test', 'https://youtube.com/hello_world_media']
10+
#
11+
# counter = 0
12+
# if isinstance(l[0], str) and len(l[0]) > 5:
13+
# counter += 1
14+
# if isinstance(l[1], str) and len(l[1]) > 5:
15+
# counter += 1
16+
# if isinstance(l[2], str) and len(l[2]) > 5:
17+
# counter += 1
18+
# if isinstance(l[3], str) and len(l[3]) > 5:
19+
# counter += 1
20+
# if isinstance(l[4], str) and len(l[4]) > 5:
21+
# counter += 1
22+
# ...
23+
24+
25+
26+
### while loop
27+
# l1 = [1, 1.1, 'hello', True, 1+2j,
28+
# 'this world', 'this is a test', 'https://youtube.com/hello_world_media']
29+
30+
# counter = 0
31+
# i = 0
32+
# while i < len(l1):
33+
# if isinstance(l1[i], str) and len(l1[i]) > 5:
34+
# counter += 1
35+
# i += 1
36+
37+
# print(counter)
38+
39+
40+
### flow control
41+
# l2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
42+
43+
## break
44+
# i = 0
45+
# while i < len(l2):
46+
# if l2[i] == 5:
47+
# break
48+
# print(l2[i])
49+
# i += 1
50+
# print('end of the loop')
51+
52+
## continue
53+
# i = 0
54+
# while i < len(l2):
55+
# if l2[i] < 5:
56+
# i += 1
57+
# continue
58+
# print(l2[i])
59+
# i += 1
60+
# print('end of the loop')
61+
62+
63+
### loop run time steps
64+
# '''
65+
# l = [1, 2, ..., 10]
66+
# i = 0
67+
# while i < len(l):
68+
# print(l[i])
69+
# i += 1
70+
71+
# runtime steps:
72+
# step.1: check while condition
73+
# step.2: execute the loop body
74+
# step.3: increment i (usually)
75+
# step.4: go to step.1
76+
# '''
77+
# # i = 0
78+
# # while i < -1:
79+
# # print(i)
80+
# # i += 1
81+
82+
83+
# ### else for while loop
84+
# l3 = [1, 2, 3, 4, 5, 6, 7]
85+
# i = 0
86+
# while i < len(l3):
87+
# if l3[i] == 5:
88+
# i += 1
89+
# continue
90+
# # break
91+
# print(f'l3[{i}] = {l3[i]}')
92+
# i += 1
93+
# else:
94+
# # todo: do something
95+
# print('end of the loop')
96+
97+
98+
### ifinite loop
99+
# while True:
100+
# print('hello world', end=' ')

0 commit comments

Comments
 (0)