python for loop
Python hosting: Host, run, and code Python in the cloud!
Loops are fundamental in programming, allowing for repetitive code execution based on specific conditions. Python provides versatile looping mechanisms that cater to various programming needs. This article delves into the primary looping structures in Python: for
loops, while
loops, and nested loops.
Related Course: Python Programming Bootcamp: Go from zero to hero
For Loop in Python
The for
loop in Python provides a concise way to iterate over iterable objects such as lists, strings, and tuples.
1 | items = ["Abby", "Brenda", "Cindy", "Diddy"] |
Additionally, using the range()
function, you can easily generate a sequence of numbers, making it simple to repeat a block of code N
times.
1 | for i in range(1, 10): |
While Loop in Python
The while
loop offers a way to execute a block of code as long as a specific condition remains true. It’s particularly useful when the number of repetitions isn’t known beforehand.
1 | correctNumber = 5 |
Nested Loops in Python
For advanced use-cases, Python supports nesting loops within loops. While this allows for more sophisticated patterns, it’s essential to use with caution as nested loops can increase computational complexity.
1 | for x in range(1, 10): |
To truly harness the power of loops in Python, practice and real-world application are key. Download Python Exercises to get started.
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.