Python Loops: A Complete Guide with Codes and Examples (2023)

Article by: Manish Methani

Last Updated: October 6, 2021 at 8:04am IST
5 min 30 sec read

Introduction:

Python is a high-level, interpreted programming language that provides a variety of features to help developers write code efficiently. One of the most useful features of Python is its ability to iterate over collections of data using loops. In this tutorial, we'll take a closer look at loops in Python, including their syntax, usage, and examples.

Types of Loops in Python:

Python has two types of loops:

  1. For Loop
  2. While Loop

For Loop:

The for loop is used to iterate over a sequence of elements. It can be used to iterate over a range of values, a list of elements, or any other iterable object in Python.

Syntax of For Loop:

for variable in sequence:
    statement(s)

In the above syntax, the variable takes the value of each element in the sequence and the statement(s) are executed for each value of the variable.

Example:

Let's take an example to print the numbers from 1 to 5 using a for loop.

for i in range(1, 6):
    print(i)

Output:

1
2
3
4
5

Explanation:

In the above example, we have used the range() function to generate a sequence of values from 1 to 5, which is then iterated over using the for loop. The value of the variable i changes for each iteration of the loop, and the print() statement is executed for each value of i.

While Loop:

The while loop is used to iterate over a block of code as long as a certain condition is true. It is typically used when we don't know the exact number of iterations required in advance.

Syntax of While Loop:

while condition:
    statement(s)

In the above syntax, the condition is evaluated before each iteration of the loop. If the condition is true, the statement(s) are executed, and the loop continues. If the condition is false, the loop terminates.

Example:

Let's take an example to print the numbers from 1 to 5 using a while loop.

i = 1
while i <= 5:
    print(i)
    i += 1

Output:

1
2
3
4
5

Explanation:

In the above example, we have initialized the variable i to 1 before the loop. The condition i <= 5 is evaluated before each iteration of the loop. If the condition is true, the print() statement is executed and i is incremented by 1. This process is repeated until the condition becomes false.

enumerate function

The enumerate function in Python is a built-in function that allows you to loop through an iterable and track both the index and the corresponding value of each element. This function is very useful when working with lists, tuples, and other sequences in Python. In this tutorial, we will cover the basics of using the enumerate function in Python and some of the common use cases.

Syntax:

The syntax for the enumerate function in Python is as follows:

enumerate(iterable, start=0)

The enumerate() function takes two arguments:

  • iterable: The sequence that you want to iterate through.
  • start (optional): The value of the index to start from. The default value is 0.

Example 1: Looping through a list with the enumerate function

Suppose we have a list of fruits and we want to loop through it and print out each fruit along with its index:

fruits = ['apple', 'banana', 'orange', 'grape']

for index, fruit in enumerate(fruits):
    print(index, fruit)

Output:

0 apple
1 banana
2 orange
3 grape

Example 2: Starting from a different index

Suppose we want to start counting from 1 instead of 0. We can do this by passing the start argument to the enumerate() function:

fruits = ['apple', 'banana', 'orange', 'grape']

for index, fruit in enumerate(fruits, start=1):
    print(index, fruit)

Output:

1 apple
2 banana
3 orange
4 grape

In conclusion, loops in Python are a powerful tool for iterating through data structures and performing repetitive tasks. There are two main types of loops in Python: for loops and while loops. For loops are used for iterating over sequences like lists, tuples, and strings, while while loops are used for iterating until a specific condition is met.

By mastering loops in Python, you can write more efficient and effective code for a wide range of applications. With the examples and code snippets provided in this tutorial, you should be well on your way to becoming proficient with loops in Python.

 

Watch Video Tutorials at Codzify YouTube Channel:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com