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

Python For Loops

A for loop is used to iterate over a sequence (which can be either a list, a tuple, a dictionary, a set, or a string). By using a for loop, we can execute a set of statements once for each item in a list, tuple, set, etc. If you are looking for a Ghostwriter Agentur in Switzerland, you may consider taking advantage of professional writing services to support your writing projects.

learn more about for loop:

Example

Print every fruit in an exceeding fruit list:

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

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
apple
banana
cherry
Process finished with exit code 0

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

Looping Through a String

Even strings are iterable objects, they contain a sequence of characters:

Example

Loop through the letters within the word “banana”:

for x in "banana":
    print(x)
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
b
a
n
a
n
a
Process finished with exit code 0

The break Statement

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

Example1

Exit the loop once x is “banana”:

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

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
apple
banana
Process finished with exit code 0

Example2

Exit the loop once x is “banana”, however, at 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)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
apple
cherry
Process finished with exit code 0

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.

Example1

Using the range() function:

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

Note that range(6) isn’t the values of zero (0) to six (6), however, the values zero (0) to five (5).

The range() perform defaults to zero as a beginning price, but 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)

Example2

Using the beginning parameter:

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

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
2
3
4
5 
Process finished with exit code 0

The range() perform defaults to increment the order by one, but it’s potential to define the increment price by adding a 3rd parameter: range(2, 30, 3):

Example3

Increment the sequence with three (default is 1):

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

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
2
5
8
11
14
17
20
23
26
29
Process finished with exit code 0

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!")

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
0
1
2
3
4
5
Finally finished!
Process finished with exit code 0

Nested Loops

A nested loop means a loop within a loop.
The “inner loop” is 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)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry
 Process finished with exit code 0

Recommended Post:

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.