Python Tutorials
Python File Handling
Python Modules
A for
loop is used for repetition in sequence (i.e. list, tuple, dictionary, set, or character unit).
This is similar to the for
keyword in some editing languages, and serves as a repetition method as found in other object-focused editing languages.
With a for
loop we can make a set of statements, as well as each item in the list, tuple, set etc.
Print each fruit in a fruit list:
fruits = ["apple", "banana", "cherry"]
for
x in fruits:
print(x)
The for
loop does not require variable indexing to be pre-configured.
Even cables are controllable, consisting of a series of letters:
Loop through the letters in the word "banana":
for x in "banana":
print(x)
With a break
statement we can stop the loop before it goes into all things:
Exit the loop when x
is "banana":
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x ==
"banana":
break
Exit the loop when x
is "banana", but this time the break comes before the print:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x ==
"banana":
break
print(x)
With an continue
statement we can stop the current duplicate of the loop, and proceed to the following:
Do not print banana:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x ==
"banana":
continue
print(x)
To connect to a set of code a certain number of times, we can use the range()
function,
The range()
function returns the sequence of numbers, from 0 by default, to increments by 1 (by default), and ends at a certain number.
Using the range() function:
for x in range(6):
print(x)
Note that the range(6)
is not a value of 0 to 6, but a value of 0 to 5.
range()
function varies by 0 as the first value, however it is possible to specify the first value by adding a parameter: range(2, 6)
, meaning values from 2 to 6 (but excluding 6):
Using the start parameter:
for x in range(2, 6):
print(x)
The range()
function is automatically adjusted to increase the sequence by 1, however it is possible to specify an additional value by adding a third parameter: range(2, 30, 3)
:
Increment the sequence with 3 (default is 1):
for x in range(2, 30, 3):
print(x)
else
keyword in the for
loop specifies the code block to be used when the loop is complete:
Print all numbers from 0 to 5, and print a message when the loop has ended:
for x in range(6):
print(x)
else:
print("Finally finished!")
Note: Else
block will NOT be used if the loop is stopped by a break
statement.
Break the loop when x
is 3, and see what happens with the else
block:
for x in range(6):
if x == 3: break
print(x)
else:
print("Finally finished!")
A loop with a nest is a loop inside a loop.
"Internal loop" will be used simultaneously for each duplicate of "external loop":
Print each adjective for every fruit:
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)
because the for
loops cannot be empty, but if for some reason you have a for
loop with no content, enter a pass
statement to avoid finding error.
Print each adjective for every fruit:
for x in [0, 1, 2]:
pass