loops in python
LOOPS:
Code can be repeated using a loop. Lines of code can be repeated N times, where N is manually configurable. In practice, it means code will be repeated until a condition is met.
Python has 3 types of loops: while loops , nested loops and for loops.
For loop
We can iterate a list using a for loopmember= [ "Abby","Brenda,"Clindy","Diddy" ] for i in member: print(i) |
The for loop can be used to repeat N times too:
for a in range(1,10):
print(a)
WHILE LOOP :
If you are unsure how many times a code should be repeated, use a while loop.
For example,
For example,
correctNumber = 5
guess = 0
while guess != correctNumber:
guess = int(input("Guess the number: "))
if guess != correctNumber:
print('False guess')
print('You guessed the correct number')
guess = 0
while guess != correctNumber:
guess = int(input("Guess the number: "))
if guess != correctNumber:
print('False guess')
print('You guessed the correct number')
for example:
for x in range(1,10): for y in range(1,10): print("(" + str(x) + "," + str(y) + ")") |
Comments
Post a Comment