Tutorial Playlist

Python Tutorial for Beginners

Overview

How to Install Python on Windows?

Lesson - 1

Python OOPs Concept: Here's What You Need to Know

Lesson - 2

Getting Started With Jupyter Network

Lesson - 3

PyCharm Tutorial: Getting Started with PyCharm

Lesson - 4

A Beginner’s Guide To Python Variables

Lesson - 5

Python Numbers: Integers, Floats, Complex Numbers

Lesson - 6

Learn A to Z About Python Functions

Lesson - 7

The Basics of Python Loops

Lesson - 8

Introduction to Python While Loop

Lesson - 9

Python For Loops Explained With Examples

Lesson - 10

Understanding Python If-Else Statement

Lesson - 11

Introduction to Python Strings

Lesson - 12

Everything You Need to Know About Python Slicing

Lesson - 13

All You Need To Know About Python List

Lesson - 14

Python Regular Expression (RegEX)

Lesson - 15

An Introduction to Python Threading

Lesson - 16

Objects and Classes in Python: Create, Modify and Delete

Lesson - 17

The Best Python Pandas Tutorial

Lesson - 18

A Beginner's Guide To Web Scraping With Python

Lesson - 19

A Handy Guide to Python Tuples

Lesson - 20

How to Easily Implement Python Sets and Dictionaries

Lesson - 21

Everything You Need to Know About Python Arrays

Lesson - 22

An Introduction to Matplotlib for Beginners

Lesson - 23

An Introduction to Scikit-Learn: Machine Learning in Python

Lesson - 24

Top 10 Python IDEs in 2020: Choosing The Best One

Lesson - 25

The Best NumPy Tutorial for Beginners

Lesson - 26

Python Django Tutorial: The Best Guide on Django Framework

Lesson - 27

Top 10 Reason Why You Should Learn Python

Lesson - 28

How To Become a Python Developer

Lesson - 29

Top 50 Python Interview Questions and Answers in 2020

Lesson - 30
Python For Loops Explained With Examples

Python is a general-purpose, high-level programming language designed to be easy to read and execute. It is open-source, which means it is free to use. In this article, we will learn one of Python programming's fundamental looping statements: the Python for loop.

The following topics that will be discussed in this article include:

  • What is Python for loop?
  • Break statement in for loop
  • Continue in for loop
  • range() function in for loop
  • Else in for loop
  • Nested for loop
  • Access index in a Python for loop
  • Looping through multiple lists
  • For loop exercise

What is Python for Loop?

The for loop in Python is used to iterate over a sequence, which could be a list, tuple, array, or string.

Syntax: FOR COUNTER IN SEQUENCE:

  STATEMENT(S)

Block Diagram:

Fig: Flowchart of for loop

Example:

basic

Fig: Basic example of Python for loop

The program operates as follows: We have assigned a variable, x, which is going to be a placeholder for every item in our iterable object. In this case, the variable “x” actually represents the elements in that list. Then, we print our variable, x. This process continues until all items are printed.

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

Break Statement in for Loop

The Python break statement is used to exit from the loop immediately after a certain condition is met.

Example:

break-statement

Fig: break statement

The program above operates as follows: The loop continues until the specified element is encountered. As soon as the ‘green’ element is encountered, the loop breaks. 

Continue Statement in for Loop

The function of the continue statement is to skip the current iteration of a loop and continue with the next one.

Example:

continue

Fig: continue statement

The Range() Function

The range() function executes a group of statements for a specified number of times.

Example:

range

Fig: range() function in Python for loop

The program operates as follows. When the for structure begins executing, the function

range creates a sequence of values, which range from zero to four. The first value in this sequence is assigned to the variable x, and the body of the for structure executes. For each subsequent value in the sequence, the value is assigned to variable x, and the body of the for structure executes. This process continues until all values in the sequence have been processed.

Else in for Loop

Python enables an else clause at the end of a for loop. The else part is executed if the loop terminates naturally.

Example:

else

Fig: else statement

In the above example, the for loop is executed first. After that, the else part is executed.

Nested Loops

A loop inside another loop is called a nested loop. The inner loop will be performed once for each iteration of the outer loop.

Example:

nested

Fig: Nested loop in Python for loop

Recommended programming practice:

Excessive nesting levels can make it difficult to follow the program. As a general rule, try to avoid using more than three levels of indentation.

Access Index in for Loop

To iterate over the indices in a sequence, we use the enumerate() function.

Example:

enumerate

Fig: enumerate() function

The enumerate() function adds the counter to iterable, which starts from zero by default.

Looping Through Multiple Lists

We can loop through multiple lists at once. For this, we use the zip() function, which is built in the Python loop.

Example:

looping.

Fig: Looping through multiple lists

For loop exercise

Question 1: Display a “simplilearn” message after successfully executing for loop

Solution:

q1

Fig: Question 1 solution

Question 2: You are given a list of integer elements. Make a new list that will store squares of elements from the previous list.

Solution:

q2

Fig: Question 2 solution

Looking forward to make a move to programming? Take up the Python Training Course and begin your career as a professional Python programmer.

Conclusion

In this article, we have discussed Python for loop and its syntax different statements. We also provided a visual example that demonstrated how to use the break and continue statements in a Python for loop. 

If you have any questions or comments, please post them below, and we'll have our experts get back to you as soon as possible.

Want to Learn More About Python?

Python is an essential tool aspiring data scientists and analysts must know. If you’re interested in adding this skill to your professional tool belt, enroll in out Python Training Course today!

About the Author

Aryan GuptaAryan Gupta

Aryan is a tech enthusiast who likes to stay updated about trending technologies of today. He is passionate about all things technology, a keen researcher, and writes to inspire. Aside from technology, he is an active football player and a keen enthusiast of the game.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.