Hey Shouters!! Today we have come up with the types of loops in python with good examples to understand.
If you are a complete beginner and does not the very basics of python. Please check Introduction To Python and How to write the first code in python.
Loops in Python-
In this tutorial, we will learn the following things:
- Definition of Loops
- Why loops are used and where to use?
- Different types of loops with syntax
- More Examples
- Inner and Outer loop in python
- Pattern Printing Examples
Definition of Loops-
A statement that can saves programmer time, eliminates large programming codes, can able to perform repeated actions, pre or post condition based execution is called Loop.
for i in [1,2,3]:
1 2 3
Why loops are used and where to use?
Why we use loops in our program, that is because to easy our repetitive tasks such as the addition of a series of number or calculating marks, etc.
Where to use, is a tricky logic. We must understand the logic to use that. Suppose we want to print patterns but the number of rows and columns are user-dependent then loops execute this for us.
Pattern printing programs are discussed later in this tutorial.
Loops are executed based on conditions. This condition is based on arithmetic calculations.
Different types of loops:
According to the user requirement, there are two most popular types of loops in python.
- While Loop
- For loop
1. While Loop-
While loops are basically used for satisfying a condition or to reach a satisfiable condition(i.e. until a specified condition is a meet). In while loop updating part(modification) is important. Otherwise, Infinite loops should occur.
While loops can also be used as long as a specified condition is true.
Syntax of While Loop-
"while" expression ":"
body of while loop
["else" ":" body of else block]
2. For Loop-
For loops have fixed number of steps that is defined by the program and can be altered by the user.
A For loop has 3 parts i.e. Initialization, condition, and change. In python, It is fully handled by Iterator.
A for loop can be used using third-party library numpy.
Syntax of For Loop-
"for" target_list "in" expression_list ":"
"body of the loop"
["else" ":" body of the else part]
Comparison of For Loop and While Loop-
- For loops are faster than while loops.
- For loop average about 2.95 to 3.02 ms
- . The While loop averaged about 3.05 to 3.37 ms.
Conclusion: The for loop in python is better
Some More Examples to Understand-
In this section, we will see some actual programs where loops are used and will understand deeply.
Example 1- Addition of N numbers
# Additon of N numbers
for i in num:
total = total + i
Example 2- Print all letters of a string
# Print all letters from a string
# input takes string value
userWord = userWord.upper()
# convert into uppercase
for letter in userWord:
letter: a b c d e f g
Inner and Outer Loops-
Basically, the Nested Looping concept is used in the matrix, pattern printing programs, sorting, etc.
The outer loop is basically used to represent rows and the inner loop is used for columns.
There is no special syntax for nested loops. Basically, For loop is used for the Nesting concept (more preferable).
Syntax of Inner and Outer Loops-
for var1 in Outer_loop_expression:
#body of outer loop
for var2 in Inner_loop_expression:
#body of Inner loop
Indentation is important here, otherwise, Error will be generated.
Examples of Inner & Outer Loops –
for patners in mobile:
for eachCompany in patners:
Pattern Printing With Loops in Python-
Patterns can be printed using simple for loops. Outer for loop is used for Row indexing and Inner for loop is used for Column indexing.
Example 1- Design this pattern
for i in range(0, size):
#outer loop executes size times
for j in range(0,i+1):
#inner loop executes 0 to i+1 times
If you like this tutorial and if you are preparing for the placement then you must follow our Instagram page Shout Coders.