Home » For Loop In Python

For Loop In Python

For Loop In Python

A for loop in python is like a repetitive action that you do for a certain number of times. Imagine you have a basket of apples, and you want to count how many apples are in the basket. You can use a for loop to help you do this.

Here’s How it works

  1. You start by saying “for every apple in the basket, do this:”
  2. Then you count one apple.
  3. You repeat step 2 until you’ve counted all the apples in the basket.

So, if you had 5 apples in the basket, the for loop would help you count each apple until you reach 5.

Here’s a simple code example in for loop in Python example:


# Let's say we have 5 apples in a basket
basket = ['apple', 'apple', 'apple', 'apple', 'apple']

# We want to count how many apples are in the basket
count = 0  # We start counting from zero

# Now we'll use a for loop to count each apple
for apple in basket:
    count = count + 1  # Increase the count by 1 for each apple

# Finally, we print out the total count
print("Total number of apples in the basket:", count)
Python

In this example, the for loop goes through each apple in the basket and increases the count by 1 for each apple. So, at the end, we know how many apples are in the basket.

Python for Loop

Sometimes a programmer wants to execute a group of statements a certain number of times. This can be done using loops. Based on this loops are further classified into following types; for loop, while loop, nested loops.

For Loop in Python

for loops can iterate over a sequence of iterable objects in python. Iterating over a sequence is nothing but iterating over strings, lists, tuples, sets and dictionaries.

Example: iterating over a string:

name = 'Abhishek'
for i in name:
    print(i, end=", ")
Python

Output:

A, b, h, i, s, h, e, k,
Python

Example: iterating over a tuple:

colors = ("Red", "Green", "Blue", "Yellow")
for x in colors:
    print(x)
Python

Output:

Red
Green
Blue
Yellow
Python

Similarly, we can use loops for lists, sets and dictionaries.

python-for-loop.png

What if we do not want to iterate over a sequence? What if we want to use for loop for a specific number of times?

Here, we can use the range() function.

Example:

for k in range(5):
    print(k)
Python

Output:

0
1
2
3
4
Python

Here, we can see that the loop starts from 0 by default and increments at each iteration.

But we can also loop over a specific range.

Example:

for k in range(4,9):
    print(k)
Python

Output:

4
5
6
7
8
Python

Conclusion

In conclusion, the for loop is a fundamental programming construct that allows for repetitive execution of code blocks. It simplifies tasks such as iterating over sequences of elements or executing a block of code a fixed number of times. By understanding its syntax and functionality, developers can efficiently handle various repetitive tasks in their programs. Whether it’s processing data, performing calculations, or iterating over collections, the for loop remains a versatile tool in the programmer’s arsenal.

Frequently Asked Questions

Q1. What is a for loop?

Ans: A for loop is a programming construct used to repeatedly execute a block of code for a fixed number of times or over a sequence of elements.


Q2. How does a for loop work?

Ans: A for loop typically consists of an initialization step, a condition for iteration, and an update step. It iterates over a sequence of elements or executes a block of code a fixed number of times until the specified condition is met.


Q3. What is the syntax of a for loop?

Ans: In most programming languages, the syntax for a for loop is: scssCopy code for (initialization; condition; update) { // Code block to be executed }


Q4. What are common use cases for for loops?

Ans: For loops are commonly used for iterating over arrays, lists, or other collections of data. They are also used for executing a block of code a fixed number of times, such as when performing repetitive tasks or calculations.


Q5. Can a for loop be nested within another for loop?

Ans: Yes, for loops can be nested within one another. This is useful for iterating over multi-dimensional arrays or performing complex iterations over nested data structures.