Intialization start point
Condition to stop
Increment or decrement
In [ ]: range(2,20,3)
# start=2 stop 20-1=19 increment by 3
In [ ]: for i in range(2,10,1):
print('hello')
# I want to say hello using a loop
# I want to say hello using a for loop
# I want to say hello using a while loop
In [ ]: # intial point
# condition to enter the loop / come out to the loop
# increment or decrement
for i in range(1,10,1) # start=1 end=9 increment
print(i)
i=1 # start point
while <condition>: # condition
i=i+1 # increment
print(i)
In [4]: #i=1 i=10 i+1
# 1,2,3,4,5,6,7,8,9,10
# range(1,11,1)
# i=1
# while cond:
# print(i)
# i=i+1
# case-1 : i>0
i=1
while i<11:
print(i,end=' ')
i=i+1
# step-1: i=1 while 1<11 T print(1) i=1+1=2
# step-2: i=2 while 2<11 T p(2) i=2+1=3
# step-10: i=10 while 10<11 T p(10) i=10+1=11
# step-11: i=11 while 11<11 F
1 2 3 4 5 6 7 8 9 10
In [6]: i=1
while i<11:
i=i+1
print(i,end=' ')
2 3 4 5 6 7 8 9 10 11
In [16]: for i in range(1,10,1): # 1 to 10
print(i,end=' ')
for i in range(10,0,-1): # 10 to 1
print(i,end=' ')
for i in range(-1,-11,-1): # -1 to -10
print(i,end=' ')
for i in range(-10,0,1): # -10 to -1
print(i,end=' ')
1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -10 -9 -8 -
7 -6 -5 -4 -3 -2 -1
In [18]: # for i in range(10,0,-1):
# print(i,end=' ')
i=10
while i>0:
print(i,end=' ')
i=i-1
10 9 8 7 6 5 4 3 2 1
In [20]: # for i in range(-1,-11,-1):
# print(i,end=' ')
i=-1
while i>-11:
print(i,end=' ')
i=i-1
-1 -2 -3 -4 -5 -6 -7 -8 -9 -10
In [ ]: # i=1 i==10 i need to stop
i=1
while True:
print(i,end=' ')
i=i+1
if i==10:
break
# i=10 i==1 i need to stop
i=10
while True:
print(i,end=' ')
i=i-1
if i==1:
break
# i=-1 i==-10 i need to stop
i=-1
while True:
print(i,end=' ')
i=i-1
if i==-10:
break
# i=-10 i==0 i need to stop
i=-10
while True:
print(i,end=' ')
i=i+1
if i==0:
break
Note
while loop prefer when we want to execute the code till condition satisfy
with out consider how many times it is running
unlimited chances
I will conduct the exam 3 times : for and while works
I will conduct the exam till pass: only while works
In [25]: import random
for i in range(0,3,1):
num=random.randint(1,10)
user_num=eval(input('enter the number'))
if num==user_num:
print('won')
break
else:
print('loss')
loss
loss
loss
In [29]: import random
i=0
while True:
num=random.randint(1,10)
print(num)
user_num=eval(input('enter the number'))
if num==user_num:
print('won')
break
else:
print('loss')
i=i+1
9
loss
4
loss
3
loss
3
loss
7
loss
10
loss
6
won
In [ ]: # wap ask the user take 5 numbers and find the square of the number
# wap ask the user print the 7th table
# wap ask the find the divisors of 75
# idea any number if we divide 75 the reminder shoud be zero
# 75/1 75/2 75/3 ... till what number we need to divid
# which condition we need to verify
# wap ask the user get a random number 5 times
# and print it is evern or odd
# wap ask the user get a random number1
# enter a number from keyboard number2
# if both numbers are match print won otherwise loss
# give 3 chances
# wap ask the user get sum of squares between 1 to 10
# wap ask the user get sum of even numbers between 1 to 100
# wap ask the user get 5 random numbers print sum of even and sum of add
# wap ask the user enter digit and get the sum of digits