You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
They can be used to iterate the number of elements in the lists,
They can also be used to iterate the strings
range function is used to iterate through numbers
foriteminiterable_object:
print(item)
# This is the syntax for loops in python# Item represents the iterator's position# You can call it anything you want# printing numbers using for loopsfornumberinrange(1,100):
print(number)
# This will print numbers from 1 to 99forletterin"Hello":
print(letter)
# This for loop will print out the characters in the word hello
Ranges
They are usually used with for loops
range(7) if we give in one parameter this will only print out numbers 0 to 6
range(1,8) if we give in two parameters this will print out numbers 1 to 7
range(1,10,2) In this example the thir parameter is used to tell how many steps should be skipped and this will print odd numbers
range helps you generate the sequence of number its a python built in function
x=input('How many times I have told you clean the room ')
y=int(x)
fortimeinrange(y):
print(f'{time}: Clean Your room')
# A simple example of loops
While Loops
While loops are just like for loops
While loops only run if the conditional statement is true otherwise it will stop
whilesome_condition:
#do this# An example user_input=Nonewhileuser_input!='please':
print('Sorry Access Denied')
We need to be careful with while loops since they will continue forever if the condition is not true It will ruin the program
#Another While Loop Examplenum=0whilenum<10:
num+=1print(num)
Importance of Break keyword in Python
It gives us the ability to get out of the loop
whileTrue:
command=input('Type exit to get out of this')
ifcommand=='exit':
breakforxinrange(1,10):
ifx==3:
break