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.
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
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.