python for loop
Python hosting: Host, run, and code Python in the cloud!
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. This condition is usually (x >=N) but it’s not the only possible condition.
Python has 3 types of loops: for loops, while loops and nested loops.
Related Course:
Python Programming Bootcamp: Go from zero to hero
For loop
We can iterate a list using a for loop#!/usr/bin/python |
Visualization of for loop:
The for loop can be used to repeat N times too:
#!/usr/bin/python |
While loop
If you are unsure how many times a code should be repeated, use a while loop.
For example,
|
Nested loops
We can combine for loops using nesting. If we want to iterate over an (x,y) field we could use:
#!/usr/bin/python |
Nesting is very useful, but it increases complexity the deeper you nest.
If you are new to Python programming, I highly recommend this book.
Posted in beginner
Leave a Reply:
simple and understandable examples. thanks
I notice that the loops terminate without executing for the last value (10 in your example). Also, how do you control the number of statements executed by the loop? There don't seem to be any block boundaries.
Hi Ron, That's correct, the parameters of range() are: range(start, stop[, step]) or range(start, stop). If you want to have 1 to 10, use range(1, 11).
To define block boundaries you use 4 spaces. This applies for functions, loops, conditonal statements etc. The way we define the for loop is equivalent to C/C++ for (i = 1; i < 10; i++). I hope you enjoy the site :-)
Do you have any examples of the continue/break statements that C-style languages have?
Hi Ryan, you can simply use the break keyword in Python.
And example of continue:
Hi Frank, How about a-z? I wrote range (a,z), it showed me "name 'a' is not defined"
Hi Ben, there are several ways to iterate through a character set:
or:
or
I hope that helps :-)
Can you explain the drive ()? Thanks!
Sure, this is an example of a condition in a while loop. It may be that while you do not press the break in a car, the car keeps moving forward.
A while loops code continues to be executed until the condition is True.
For the driving example in the comments section, it makes more sense to put the drive() command, in the while loop, before getting new input for the variable, action. As the code stands now, when one enters "stop", one sees "driving" as the output response. Switching the order of the statements in the while loop, just terminates the loop silently when "stop" is entered.
nice examples thx .
Thanks! I'm happy you enjoy the site
Thank you so much
From where I should start I can't understand? How to save, How to edit, How to run, These are my problems.
Hi Amina, start from here: https://pythonspot.com/getting-started/
Download an editor like PyCharm to edit, save and run your code.
why are you using str() in print statement i am getting even i used variable directly
sometimes that may lead to problems as they have different datatypes,just being cautious.
whats wrong with this code not generating primes as i expected
The conditions in the while loops are never true. I fixed your code:
You can see the prime numbers being added to the array in the loop.
Hi Frank.
There is a little mistake in the "While loop" example. It should be:
[python]
guess = int(input("Guess the number: "))
[/python]
Otherwise, the compiler will compare between string and integer, and '5'!=5.
Except it - excellent!
Thanks Sagi!
I've updated the article.