Python for loop

Everything about for loop in python with practice Problems

In computer programming languages, loops are structures that are used for repeating a block of code again and again based on some specified conditions.

In Python programming, loops are used Python for loops are used for iterating over sequences or collections such as iterating over a list, a tuple, a dictionary, etc as long as a specified condition is true.

Loops in Python

There are two main types of loops in Python Programming.

  1. for loop

  2. while loop

Python for Loop

for loops are used for iterating over a sequence ( e.g., lists, strings, tuples, or any iterable objects.

for loop is useful particularly when we want to print to perform a specific task for each item in a list, tuple, dictionary, or any other iterable data structure.

Python for loop syntax

For Example, Printing numbers from 1 to 5 using a for loop.

# The "for" keyword marks as the beginning of the for loop.
# The loop variable "num" takes the value of each element in the sequence (range in this case).
# Here,the range(1, 6) generates a sequence of numbers from 1 to 5.
# The colon ":" signifies the start of the loop block.
for num in range(1, 6):
    # This block will be executed for each value of "num" in the specified range.

    # The "print" statement displays the current value of "num".
    # In the first iteration, "num" is 1; in the second iteration, "num" is 2, and so on.
    print(num)

# The loop has completed its execution, and the program continues with the next statement.

Output :

1
2
3
4
5

here, the for loop is used to iterate through a sequence of numbers generated by the Python for loop range ( range() ) function.

The for loop in Python with range range(1, 6) the function generates a sequence of numbers from 1 to 5, and the loop variable num takes each value from the range one by one.

The print(num) statement within the loop block displays the value of num at each iteration, resulting in the output of numbers 1 to 5.

The loop variable takes the value of each element in the sequence, allowing us to process, analyze, or manipulate data with ease.

Python for loop range

The range() function generates a sequence of numbers, often used as the basis for the loop's iteration.

The range() function can take up to three arguments: start, stop, and step. The start argument is optional and specifies the starting value of the sequence (default is 0).

The stop the argument specifies the ending value of the sequence and the step argument determines the increment between values in the sequence (default is 1).

Python for loop range example

for i in range(5, 0, -1):
    print(i)  # Output: 5, 4, 3, 2, 1
# the loop iterates because here starting(5) is greater than the stopping value(0).
# the step -1 decreases by 1 in each repetition.

here, the 'range()' function generates a sequence of numbers from 5 to 1 with step -1.

Python for loop index

In Python, we can use the built-in enumerate() function to get both the index and the corresponding value while iterating through a sequence using a for loop.

example:

python fruits = ['apple', 'banana', 'cherry'] for index, value in enumerate(fruits): print("Index:", index, "Value:", value)
#Output:
#Index: 0 Value: apple Index: 1 Value: banana Index: 2 Value: cherry

here, the enumerate() function generates pairs of index and value for each element in the fruits list. The for loop then iterates through these pairs, and accesses the index using the index variable and the value using the value variable.

To learn more about it please refer: Best way to learn python