Loops in Python: Explained With Examples

21 Likes Comment
Loops in Python

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:

  1. Definition of Loops
  2. Why loops are used and where to use?
  3. Different types of loops with syntax
  4. More Examples
  5. Inner and Outer loop in python
  6. 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 example:

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

  1. While Loop
  2. 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-

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-

Comparison of For Loop and While Loop-

  1. For loops are faster than while loops.
  2. For loop average about 2.95 to 3.02 ms
  3. . 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

OUTPUT:
15

Example 2- Print all letters of a string

Output:
Input: abcdefg
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-

Indentation is important here, otherwise, Error will be generated.

Examples of Inner & Outer Loops –

Output:
Apple
SAMSUNG
REAL ME
REDMI
ASUS
NOKIA

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

*
**
***
****

Output:
*
**
***
****

If you like this tutorial and if you are preparing for the placement then you must follow our Instagram page Shout Coders.

Recommended Posts-

  1. Introduction to Python and Environment Setup
  2. Python Program to Check a Year is Leap Year or Not
  3. Python Program to Check a Palindrome Number
  4. Word Guessing Game in Python

You might like

TASHNEEM YESMIN

About the Author: TASHNEEM YESMIN

Leave a Reply

Your email address will not be published. Required fields are marked *

Close Bitnami banner
Bitnami