Python Loops

Loops are one essential part of programming.

In Python we have 2 kinds of loops: while loops and for loops.

while loops

while loops are defined using the while keyword, and they repeat their block until the condition is evaluated as False…


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

Loops are one essential part of programming.

In Python we have 2 kinds of loops: while loops and for loops.

while loops

while loops are defined using the while keyword, and they repeat their block until the condition is evaluated as False:

condition = True
while condition == True:
    print("The condition is True")

This is an infinite loop. It never ends.

Let’s halt the loop right after the first iteration:

condition = True
while condition == True:
    print("The condition is True")
    condition = False

print("After the loop")

In this case, the first iteration is ran, as the condition test is evaluated to True, and at the second iteration the condition test evaluates to False, so the control goes to the next instruction, after the loop.

It’s common to have a counter to stop the iteration after some number of cycles:

count = 0
while count < 10:
    print("The condition is True")
    count = count + 1

print("After the loop")

for loops

Using for loops we can tell Python to execute a block for a pre-determined amount of times, up front, and without the need of a separate variable and conditional to check its value.

For example we can iterate the items in a list:

items = [1, 2, 3, 4]
for item in items:
    print(item)

Or, you can iterate a specific amount of times using the range() function:

for item in range(04):
    print(item)

range(4) creates a sequence that starts from 0 and contains 4 items: [0, 1, 2, 3].

To get the index, you should wrap the sequence into the enumerate() function:

items = [1, 2, 3, 4]
for index, item in enumerate(items):
    print(index, item)

Break and continue

Both while and for loops can be interrupted inside the block, using two special keywords: break and continue.

continue stops the current iteration and tells Python to execute the next one.

break stops the loop altogether, and goes on with the next instruction after the loop end.

The first example here prints 1, 3, 4. The second example prints 1:

items = [1, 2, 3, 4]
for item in items:
    if item == 2:
        continue
    print(item)
items = [1, 2, 3, 4]
for item in items:
    if item == 2:
        break
    print(item)


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2020-12-24T05:00:00+00:00) Python Loops. Retrieved from https://www.scien.cx/2020/12/24/python-loops/

MLA
" » Python Loops." flaviocopes.com | Sciencx - Thursday December 24, 2020, https://www.scien.cx/2020/12/24/python-loops/
HARVARD
flaviocopes.com | Sciencx Thursday December 24, 2020 » Python Loops., viewed ,<https://www.scien.cx/2020/12/24/python-loops/>
VANCOUVER
flaviocopes.com | Sciencx - » Python Loops. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2020/12/24/python-loops/
CHICAGO
" » Python Loops." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2020/12/24/python-loops/
IEEE
" » Python Loops." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2020/12/24/python-loops/. [Accessed: ]
rf:citation
» Python Loops | flaviocopes.com | Sciencx | https://www.scien.cx/2020/12/24/python-loops/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.