• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Python » For Loop in Python
Next →
← Prev

What is for-loop in Python With examples?

By Dinesh Thakur

In this session of the Python tutorial, you’ll learn in detail about for loop in Python 3. you’ll also know how to use the range() function for loops. You’ll even know how to use else statements with loop, nested for loop, and loop.

We’ll be covering the following topics in this tutorial:

  • For-Loop Control Flow Statements in Python 3
  • What’s iteration inside a Python for loop?
  • Use of For Loop in Python 3
  • Python for loop syntax
  • The range() Function In Python For Loop
  • Using Python for-loops with the range() function:
  • The break and continue Statements
  • Else in Python For Loop
  • Nested For Loop in Python

For-Loop Control Flow Statements in Python 3

Python for Loop Statements is another control flow statement. The program’s control is always moved to the start of the for-loop to perform specific blocks of statements for a definite time, which control through an iterable expression. After a while, the condition becomes false, the ‘for’ loop suspends.

Though other languages include conditions and increment expression in the syntax of both for-loop, the iteration, and incrementing value commanded by creating a Python sequence, in this module, we will learn about for-loops in Python.

What’s iteration inside a Python for loop?

Repetitive implementation of the identical block of code over and over is known as iteration.

There are two types of iteration:

• Definite iteration: where the number of repetitions is given explicitly in advance.

• Indefinite iteration: where the code block executes until a condition fulfill.

  In Python, indefinite iteration did use a while loop.

Use of For Loop in Python 3

For loops in Python are called iterators. Python for-loop can iterate over the sequences (such as string, array, list, tuples, dictionary) is called traversal.

Python for loop syntax

Let’s see the Python Syntax of for-loop with examples:

for val in sequence:
  statements(s) 

If a sequence includes an expression list, it evaluates first. Then, the very first item in the series will assign to the iterating variable Val. Then, the statements block executed. Each item in the list assigned to Val, and the statement(s) block executed until the whole sequence using up.

The below flowchart demonstrates the working of for loop:

flowchart of For Loop in Python

According to the flowchart, the loop will continue to do before the previous item in the order is reached. The whole body of the for loop, like the entire body of this Python while-loop, is indented from the rest of the code from the program.

Let’s take a look at Python for-loop examples for better understanding.

Sq_root = 1 
count_list = [1,2,3,4,5,6,7] 
for val in count_list: 
   Sq_root = val*val 
   print("The Square of", val, "is", Sq_root) 

Output: 

The square of 1 is 1 
The square of 2 is 4 
The square of 3 is 9 
The square of 4 is 16 
The square of 5 is 25 
The square of 6 is 36 
The square of 7 is 49  

The range() Function In Python For Loop

Python range() function: We can specify the sequence of numbers within a given range, starting from 0 and increments by 1.

Example:

range(100) 

Note:  The range is not out of 1 to 100 but from 0 to 99 (100 numbers). Specifying Start and stop points from the range() function:

Example:

range(1,5) 

Note: In this example, the Beginning point is 1 and Also the End point is 5.

So, the list will be created from 1 to 4 (4 numbers).

Using Python for-loops with the range() function:

Example:

We can simply use Python for loop with the range() function as shown in the example below.

For Val in range(10,15): 
    print(Val)
 
Output: 
10 
11 
12 
13 
14 

By default, the increment from the range() function when used with loops set to 1; Nonetheless, we Could Alter or Define a Specific increment by Adding the third parameter from the range() function, as illustrated in the next example:

For i in range(10,20,2) 
    print(i) 
Output: 
10 12 14 16 18 

The break and continue Statements

Break and continue work precisely the identical manner with for loops as with while loops. Break terminates the loop entirely and proceeds into the first statement after the loop:

Break Statement

Like while loops, for loops, may also be terminated with the break statement, the break will immediately terminate the execution of this loop and then move the control of the program towards the end of the loop.

Example:

# creating a list of numbers 
count_list = [2,3,4,5,6,7,8] 
for Val in count_list: 
  print(Val) 
  if Val == 6: 
    break 

Output:2 3 4 5 6 

Continue Statement

Like while loops, continue statement can be used in Python for loops to complete the ongoing iteration and transfer the control to the start of the loop to keep another iteration.

count_list = [2,3,4,5,6,7,8] 
for Val in count_list: 
   if Val == 5: 
     continue
     print(Val) 
Output: 2 3 4 6 7 8 

Note that the fifth iteration was interrupted as we’ve used the continue statement when the value of Val is 5. Consequently, the control passed to the start of the loop, and the program began executing the sixth iteration and did not print the value of Val when it was 5.

Else in Python For Loop

For loop in Python can have an optional else block. The else block will be executed when all iterations completed. When the break used for loop to terminate the loop before all the iterations completed, the else block ignored.

Example:

for Val in range(1,6):
   print(Val)
else:
   print("All iterations complete") 

Output:1 2 3 4 5 

All iterations completed

Nested For Loop in Python

The nested loops would be the loops that nested inside an existing loop; that is, nested loops would be the body of another loop.

Example:

for V1 in range(1,9,2): 
   for V2 in range(V1): 
       print( V1, end = ' ') 
       print()  
Output:  
1 
3 3 3 
5 5 5 5 5 
7 7 7 7 7 7 7   

From the preceding example, we’ve used nested loops to publish some pyramid design. The outer loop utilizes the number of rows from the design, and the inner loop and the nested loop used to control the number of columns from the design. Note that we used the third parameter in range() function and set the increment to 2. That’s why the number in each column is incremented by 2.

You’ll also like:

  1. For Loop | Java Examples
  2. Infinite For loop Example | Java Examples
  3. Java Nested For Loop Examples
  4. While loop in Python
  5. Discuss how and when the values of the loop index change throughout the processing of the loop
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Python

Python Tutorials

  • Python - Home
  • Python - Features
  • Python - Installation
  • Python - Hello World
  • Python - Operators Types
  • Python - Data Types
  • Python - Variable Type
  • Python - Switch Case
  • Python - Line Structure
  • Python - String Variables
  • Python - Condition Statement
  • Python - if else Statement
  • Python - for-loop
  • Python - while loop
  • Python - Command Line
  • Python - Regular Expression

Python Collections Data Types

  • Python - List
  • Python - Sets
  • Python - Tuples
  • Python - Dictionary

Python Functions

  • Python - Functions
  • Python - String Functions
  • Python - Lambda Function
  • Python - map() Function

Python Object Oriented

  • Python - Oops Concepts
  • Python - File Handling
  • Python - Exception Handling
  • Python - Multithreading
  • Python - File I/O

Python Data Structure

  • Python - Linked List
  • Python - Bubble Sort
  • Python - Selection Sort
  • Python - Linear Search
  • Python - Binary Search

Python Programs

  • Python - Armstrong Number
  • Python - Leap Year Program
  • Python - Fibonacci Series
  • Python - Factorial Program

Other Links

  • Python - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW