-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditional statement while
More file actions
231 lines (180 loc) · 6.24 KB
/
Conditional statement while
File metadata and controls
231 lines (180 loc) · 6.24 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
#looping/iteration
def: to repeat instruction of set of instruction(program)
C/C++/java/javascript/php
1.while 2.do..while 3.for 4.(C++/java) for each
1.while ---> Iteration
syntax:
1.
Declaration loop var
while test-condition:
-------------------
-------------------
modify/update counter
------------------
2.
Declaration loop var
while test-condition:
-------------------
-------------------
modify/update counter
------------------
else:
--------------
--------------
2.for each----
(i).(for range() )
a. //group of value
for var in range(start,stop,end): #for(start;test;modify(indecrement)
-----------------------------
-----------------------------
-----------------------------
b. //group of value
for var in range(start,stop,end): #for(start;test;modify(indecrement)
-----------------------------
-----------------------------
-----------------------------
else: #(optional)
------------------
------------------
------------------
---- each
(ii).(for each)
//List,Tuple,set,dict(keys/values)
a.
for var in groupofvalue:
--------------------
--------------------
--------------------
--------------------
b.
for var in groupofvalue:
--------------------
--------------------
--------------------
--------------------
else:
----------------
----------------
----------------
count=1
while count<=3:
print("sushil")
#count=count+1 ---shorhand--->count+=1
count+=1 # count++ (wrong) no increment and decrement operator found in python
''' tracing----strong----looping very very strong
count=1
while 1<=3: #true
print("sushil") ---sushil
#count=count+1 ---shorhand--->count+=1
#count=1+1---=2
count+=1 # count++ (wrong) no increment and decrement operator found in python
----------------------------------------
count=2
while 2<=3: #true
print("sushil") ---sushil
#count=count+1 ---shorhand--->count+=1
#count=2+1---=3
count+=1 # count++ (wrong) no increment and decrement operator found in python
-----------------------------------------
count=3
while 3<=3: #true
print("sushil") ---sushil
#count=count+1 ---shorhand--->count+=1
#count=3+1---=4
count+=1 # count++ (wrong) no increment and decrement operator found in python
--------------------------------------
count=4
while 4<=3: #false
--------------
--------------
---------------
exit from loop '''
n=1
while n<=10:
print(2**n,end=' ')
n+=1
print()
n=1
while n<=10:
print(pow(2,n),end=' ')
n+=1
print()
import math
n=1
while n<=10:
print(math.pow(2,n),end=' ')
n+=1
cont=0;
while True:
n=int(input("Enter any number:"))
i=1
while i<=10:
print(n,"x",i,"=",n*i)
i+=1
cont=int(input("press 1 to cont.."))
if cont!=1:
break
''sum of series ----> even number print
find of even number
fixed range //1-20----even value sum---2,4,6,....18,20
Dynamic end
dynamic start end of range'''
r=1
sum=0
while r<=20:
if r%2==0:
print(r,end=" ")
sum+=r
r+=1
print("Sum of Even value=",sum)
print()
r=2
last=int(input("Enter range end point:"))
while r<=last:
print(r,end=" ")
r+=2
print()
first=int(input("Enter range end point:"))
last=int(input("Enter range end point:"))
while first<=last:
print(first,end=" ")
first+=2
sum=0
st=int(input("Enter range end point:")) #5
ed=int(input("Enter range end point:")) #7
while st<=ed: # 8<=7 --5 6,7 8(end)
sqr=st*st #7*7=49
sum+=sqr #sum+=49==sum=sum+sqr=61+49=110
print(sqr,end=" ") #49
st+=1 #8
print("Sum of square of given range value:",sum)
'''find factorial of number----> import math-----math.factorial(5)---120
//input----- -ve value are not allowed to factorial
num=5 n*(n-1)*(n-2) ...(n-(n-1))
5*4*3*2*1 --*1 one loop cycle waste
5*4*3*2
f=1
//reverse loop 5----->2(end) fix
f*=num
num-=1
'''
num=int(input("Enter any value:"))
if num>=0:
#find factorial
f=1
while num>=2:
f*=num
num-=1
print("Factorial of number=",f)
else:
print("-ve value is not allwoed to calc factorial")
#print factorial between range----nested while loop
# range-----2-10---while ----Outter
#---calc---while factorial ---inner
H\W
while range:
while factorial:
calc
modify/udpate inner loop
print("Factoril",f)
modify/update outter loop