Python For Loop | The break Statement | The continue Statement | The range() perform | Else sure Loop | Nested Loops

Python For Loop | The break Statement | The continue Statement | The range() perform | Else sure Loop | Nested Loops

Python For Loops

A for loop is employed for iterating over a sequence (that is either an inventory, a tuple, a lexicon, a set, or a string).

This is less just like the for keyword in different programming languages, associate degreed works a lot of like an iterator technique as found in different object-orientated programming languages.

With the for loop we are able to execute a group of statements, once for every item in an exceedingly list, tuple, set etc.

Example

Print every fruit in an exceedingly fruit list:

fruits = [“apple”, “banana”, “cherry”]
for x in fruits:
print(x)

The for loop doesn’t need associate degree compartmentalisation variable to line beforehand.

Looping Through a String
Even strings ar iterable objects, they contain a sequence of characters:

Example
Loop through the letters within the word “banana”:

for x in “banana”:
print(x)

The break Statement

With the break statement we are able to stop the loop before it’s whorled through all the items:

Example
Exit the loop once x is “banana”:

fruits = [“apple”, “banana”, “cherry”]
for x in fruits:
print(x) 
if x == “banana”:
break

Example
Exit the loop once x is “banana”, however this point the break comes before the print:

fruits = [“apple”, “banana”, “cherry”]
for x in fruits:
if x == “banana”:
break
print(x)

The continue Statement

With the continue statement we are able to stop the present iteration of the loop, and continue with the next:

Example
Do not print banana:

fruits = [“apple”, “banana”, “cherry”]
for x in fruits:
if x == “banana”:
continue
print(x)

The range() perform

To loop through a group of code a specific variety of times, we are able to use the range() perform,

The range() perform returns a sequence of numbers, ranging from zero by default, and increments by one (by default), and ends at a specific variety.

Example
Using the range() function:

for x in range(6):
print(x)

Note that range(6) isn’t the values of zero to sixhowever the values zero to five.

The range() perform defaults to zero as a beginning pricebut it’s potential to specify the beginning price by adding a parameter: range(2, 6), which implies values from a pair of to six (but not together with 6):

Example
Using the beginning parameter:

for x in range(2, 6):
print(x)

The range() perform defaults to increment the sequence by onebut it’s potential to specify the increment price by adding a 3rd parameter: range(2, 30, 3):

Example
Increment the sequence with three (default is 1):

for x in range(2, 30, 3):
print(x)

Else sure Loop

The else keyword in an exceedingly for loop specifies a block of code to be dead once the loop is finished:

Example
Print all numbers from zero to five, and print a message once the loop has ended:

for x in range(6):
print(x)
else:
print(“Finally finished!”)

Nested Loops

A nested loop could be a loop within a loop.

The “inner loop” are going to be dead just once for every iteration of the “outer loop”:

Example
Print every adjective for each fruit:

adj = [“red”, “big”, “tasty”]
fruits = [“apple”, “banana”, “cherry”]

for x in adj:
for y in fruits:
print(x, y)

Python For Loop | The break Statement | The continue Statement | The range() perform | Else sure Loop | Nested Loops

Leave a Comment

Your email address will not be published. Required fields are marked *