Tutorial Playlist

Python Tutorial for Beginners


The Best Tips for Learning Python

Lesson - 1

How to Install Python on Windows?

Lesson - 2

Top 10 Python IDEs in 2022: Choosing The Best One

Lesson - 3

A Beginner’s Guide To Python Variables

Lesson - 4

Understanding Python If-Else Statement

Lesson - 5

Python Numbers: Integers, Floats, Complex Numbers

Lesson - 6

Python Strings | Simplilearn Python Tutorial

Lesson - 7

The Basics of Python Loops

Lesson - 8

Python For Loops Explained With Examples

Lesson - 9

Introduction to Python While Loop

Lesson - 10

Everything You Need to Know About Python Arrays

Lesson - 11

All You Need To Know About Python List

Lesson - 12

How to Easily Implement Python Sets and Dictionaries

Lesson - 13

A Handy Guide to Python Tuples

Lesson - 14

Everything You Need to Know About Python Slicing

Lesson - 15

Python Regular Expression (RegEX)

Lesson - 16

Learn A to Z About Python Functions

Lesson - 17

Objects and Classes in Python: Create, Modify and Delete

Lesson - 18

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

Lesson - 19

An Introduction to Python Threading

Lesson - 20

Getting Started With Jupyter Network

Lesson - 21

PyCharm Tutorial: Getting Started with PyCharm

Lesson - 22

The Best NumPy Tutorial for Beginners

Lesson - 23

The Best Python Pandas Tutorial

Lesson - 24

An Introduction to Matplotlib for Beginners

Lesson - 25

The Best Guide to Time Series Analysis In Python

Lesson - 26

An Introduction to Scikit-Learn: Machine Learning in Python

Lesson - 27

A Beginner's Guide To Web Scraping With Python

Lesson - 28

Python Django Tutorial: The Best Guide on Django Framework

Lesson - 29

Top 10 Reason Why You Should Learn Python

Lesson - 30

10 Cool Python Project Ideas For Beginners in 2021

Lesson - 31

The Best Ideas for Python Automation Projects

Lesson - 32

12 Tips On How To Become a Python Developer

Lesson - 33

The Best Guide for RPA Using Python

Lesson - 34

Comprehending Web Development With PHP vs. Python

Lesson - 35

The Best Tips for Learning Python - REMOVE

Lesson - 36

The Best Way to Learn About Box and Whisker Plot

Lesson - 37

An Interesting Guide to Visualizing Data Using Python Seaborn

Lesson - 38

The Complete Guide to Data Visualization in Python

Lesson - 39

Everything You Need to Know About Game Designing With Pygame in Python

Lesson - 40

The Complete Simplified Guide to Python Bokeh

Lesson - 41

Top 150 Python Interview Questions and Answers for 2022

Lesson - 42

The Supreme Guide to Understand the Workings of CPython

Lesson - 43
The Basics of Python Loops

Python is one of the most popular and versatile programming languages available today, deployed across many industries and used for web development, machine learning, and data science. Given its widespread use, particularly in such in-demand (and interrelated) fields as machine learning and big data, it's not surprising that Python has surpassed Java as the top programming language. 

In this article, we will be learning about Python loops, including the different types of loops and how conditional statements work.

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

What Are Python loops?

A loop is an instruction that repeats multiple times as long as some condition is met.



Fig: Flowchart of Python loop

Significance of indentation

Indentation is significant in Python. It is used to define a block of code; without indentation, the program will show an error.

Type of Loops

There are mainly two types of loops. Let’s discuss them one by one.

1. For Loop

A for loop in Python is used to iterate over a sequence (list, tuple, set, dictionary, and string).



Fig: Flowchart of a for loop

Syntax: for iterating_var in sequence:  




Fig: for loop example

The preceding code executes as follows: The variable i is a placeholder for every item in your iterable object. The loop iterates as many times as the number of elements and prints the elements serially.

2. While Loop

The while loop is used to execute a set of statements as long as a condition is true.



Fig: While loop flowchart

Syntax: while expression:  




Fig: While loop

The preceding code executes as follows: We assign the value to variable x as 1. Until the value of x is less than 3, the loop continues and prints the numbers. 

3. Nested Loop

If a loop exists inside the body of another loop, it is called a nested loop.



Fig: Nested loop

The preceding code executes as follows: The program first encounters the outer loop, performing its first iteration. This first iteration triggers the inner, nested loop, which then runs to completion. Then the program returns to the top of the outer loop, completing the second iteration and again triggers the nested loop. The nested loop runs to completion, and the program returns to the top of the outer loop until the sequence is complete.

Conditional Statements in Python

Decision-making statements are used to execute the statements only when a particular condition is fulfilled.



Fig: Conditional statement flowchart

There are three main types of conditional statements. They are:

1. If statement: The if statement is used to test a specific condition. If the condition is true, a block of code is executed.

Syntax: if expression:




Fig: flowchart of if statement in Python loop



Fig: if statement in Python loop

2. Else statement: The else statement is executed when the expression in the if condition is false.



Fig: else flowchart in Python loop



Fig: else statement

3. Elif statement: The elif statement in Python enables you to check multiple conditions and execute specific blocks of statements if the previous conditions were false.



Fig: elif statement in Python

Practice Exercises

It’s time to test our understanding with these practice exercises. We will suggest trying to solve the questions on your own.

Question 1: Write a program to check if the given number is prime or not.



Fig: Solution 1

The preceding code executes as follows: You assign value to a variable number. If the number entered is greater than 1, you check for factors of that number. If a factor is found, it is not a prime number. Otherwise, the given number is a prime number.

Question 2: Write a program to print the numbers from 1 to 10. Exit the loop if the number becomes equal to 6.


Fig: Solution 2

The preceding code executes as follows: Until the value of myNum does not become equal to myVar, the while loop iterates. As soon as the value becomes equal, the loop breaks. 

Learn data operations in Python, strings, conditional statements, error handling, and the commonly used Python web framework Django. Check out Simplilearn's Python Training course.

Master Python and Increase Your Stock as a Programmer

Mastering the popular and highly versatile Python programming language is a great way to enter the exciting fields of data science, artificial intelligence, and machine learning. Consider signing up for Simplilearn’s Python Training Course, which provides a great foundation for this dynamic programming language.

About the Author


Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

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