Loops in Python 3 with Examples

In this article, We are going to cover Loops in Python with Examples, Break Statement in Python, Continue Statement in Python, Pass Statement in Python, range() function in Python, for-else loop in Python.

Loops in Python with Examples

#1: For Loop in Python

In simple language for loop is a programming language statement which allows code to be repeatedly executed. For loop execute a sequence of statements multiple times and abbreviates the code that manages the loop variable. For loop is a control flow statement for specifying iteration. Which allows code to be executed repeatedly.

Syntax:

for var in sequence:
	#statement

Example of for loop in python:

x = ["FOSSTechNix"]
for y in x:
    print(y)

Output:

FOSSTechNix
Loops in Python 3 with Examples 1

Another Example of for loop (string) in Python

s = "FOSS"
for i in s :  
    print(i)  

Output:

F
O
S
S
Loops in Python 3 with Examples 2
num = [2,3,5,7,0,2,9,1]
addition = 0
for var in num:
	addition = addition + var

print("The addition is", addition)

Output:

The addition is 29
Loops in Python 3 with Examples 3

Loop Control Statements in Python

Python supports following control statements

  • Break Statement
  • Continue Statement
  • Pass Statement

#1: Break Statement in Python

Break condition is used to terminate the loop when a certain condition is met. The break statement is used a for loop. The break statement in python terminates the current loop ans resume execution at the next statement. Break statement can be used in both for and while loops. The break keyword is used to out a for loop.

Syntax:  

break

Example of break statement in Python

for s in "FOSSTechNix":
    if s == "T":
        break
    print(s)

print("The end")

Output:

F
O
S
S
The end
Loops in Python 3 with Examples 4

#2: Continue Statement in Python

In continue Statement we can stop the iteration and continue. Continue statement is opposite to break statement This statement is used to skip the rest of code inside a loop and execute the next iteration of the loop. This loop does not terminate and continue with next iteration.

Syntax:

Continue

Example of continue statement in Python:

for s in "Python":
    if s == "t":
        continue
    print(s)

print("The end")

Output:

P
y
h
o
n
The end
Loops in Python 3 with Examples 5

#3: Pass Statement in Python

The pass statement is a null statement. Pass statement is generally used as a placeholder. Pass is used to when we don’t want execute any code, so simply places pass at  that line. Difference between pass and comment comment is ignored by interpreter and pass is not ignored.

Syntax:

Pass

Example of pass statement in Python:

x = ["FOSSTechNix"]
for y in x:
    pass
Loops in Python 3 with Examples 6

Another example of pass statement in Python:

fruit =['mango', 'apple', 'cherry', 'orange'] 
  
for i in fruit: 
    if(i =='apple'): 
        pass 
    else: 
        print(i)

Output:

mango
cherry
orange
Loops in Python 3 with Examples 7

#2: range() function in Python

range function used to perform action for specific number of times. The range() function used to generate sequence. We can define start, stop and step size like range(start, stop, step_size) start from 0 by default, step_size increments 1 by default  and ends at specific number.

This function remember only start stop and step size and generate the next number. Does not store all values in memory.

Example of range() function in Python:

print(range(8))

print(list(range(8)))

print(list(range(2, 6)))

print(list(range(2, 18, 2)))

Output:

range(0, 8)
[0, 1, 2, 3, 4, 5, 6, 7]
[2, 3, 4, 5]
[2, 4, 6, 8, 10, 12, 14, 16]
Loops in Python 3 with Examples 8

Another Example of range() function in Python:

for num in range(4):
    for i in range(num):
        print(num, end="")
    print("\n")

Output:

1
22
333
Loops in Python 3 with Examples 9

#3: for-else loop in Python

In this for-else loop there is keyword break can be used to stop a for loop. Some cases else part is ignored. else block after for is executed when the loop is not end with break statement.

Example of for-else loop in Python:

for s in range(1, 5):  
    print(s)  
else: 
    print("No Break")

Output:

1
2
3
4
No Break
Loops in Python 3 with Examples 10

Another Example of for-else loop:

for s in range(1, 5):  
    print(s)  
    break
else: 
    print("No Break")

Output:

1
Loops in Python 3 with Examples 11

We have covered Loops in Python with Examples.

Conclusion:

We have covered Loops in Python with Examples, For Loop in Python, Break Statement in Python, Continue Statement in Python, Pass Statement in Python, range() function in Python, for-else loop in Python.

Related Articles:

While Loop in Python with examples

7 Python Operators with Examples

Python Introduction for Programmers [Part 1]

6 Python Conditional Statements with Examples

Functions in Python with Examples

classes and objects in python with examples

Reference:

Python official page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

1 thought on “Loops in Python 3 with Examples”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap