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 loop
 
member= [ "Abby","Brenda,"Clindy","Diddy" ]
 
for i in member:
    print(i)

for loop


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,

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')



We can combine for loops using nesting. If we want to iterate over an (x,y) field we could use:
for example:

 
for x in range(1,10):
    for y in range(1,10):
        print("(" + str(x) + "," + str(y) + ")")

Nesting is very useful, but it increases complexity the deeper you nest


thanks - anonymous

Comments

Popular posts from this blog

wifi attack with kali linux

how to clone a simcard ???