For Loops In Python- Python Tutorials For Beginners #9

Overview

A for loop is a powerful construct that allows you to repeat a block of code for each element in a sequence (like a list, tuple, or string). Here’s the basic structure:

1for element in sequence:
2    # Code to execute for each element
python
  • Step 1: For loops are always starts with for keyword.
  • Step 2: Create a variable as per your need.
  • Step 3: Iterate element as per you want.

Now, let’s break it down step by step:

Suppose we have a list of programming languages: languages = ['Swift', 'Python', 'Go']. We can use a for loop to access each language one by one:

1for language in languages:
2    print(language)
python

Output:

1Swift
2Python
3Go

You can also loop through each character in a string:

1language = 'Python'
2for char in language:
3    print(char)
python

Output:

1P
2y
3t
4h
5o
6n

The range() function generates a sequence of numbers. You can iterate over it using a for loop.This code is used to print the numbers as per given limit. Here, The limit is 0 to 4. Note that, The output will start from 0 and ends on 4. Here, the variable i contains numbers 0, 1, 2, and 3.

1# Iterate from 0 to 3
2for i in range(4):
3    print(i)
python

Output:

10
21
32
43

You can add an optional else block that executes after the loop completes:

1digits = [0, 1, 5]
2for digit in digits:
3    print(digit)
4else:
5    print("No items left.")
python

In this example, I used For Loops along with the If-Else statement. Here, i is List and k is a variable containing elements of list. k is used to access elements in the List. I assign conditions also with elements of the List.

 1i = ['fish','cat','dog']
 2
 3for k in i:
 4    print(k)
 5    if (k=="dog"):
 6        print("dogs are so loyal animals")
 7
 8    elif (k == 'cat'):
 9        print("cats are so cute.")
10
11    elif (k=='fish'):
12        print("fish is live in the water")
13    else:
14        print(k,"is are nice animal")
15
16        for i in k:
17            print(i)
...
python
1# Output:
2
3fish
4fish is live in the water
5cat
6cats are so cute.
7dog
8dogs are so loyal animals
...

In this given example pass keyword works as a placeholder. Pass keyword use when we add code in Loops, Functions, etc... later, But we do not want errors now.

1for x in "ABCD":
2    pass
python

In Python, the break statement is used to exit a loop prematurely. When encountered, it terminates the loop immediately. Here’s how it works:

1for i in range(5):
2    if i == 3:
3        break
4    print(i)
python

Output:

10
21
32
For Loops With Break Statement
For Loops With Break Statement
1Colours=("Red","Green","Blue")
2for i in Colours:
3    print(i)
python

Output:

1Red
2Green
3Blue
1Colours=["Red","Green","Blue"]
2for i in Colours:
3    print(i)
4  
python

Output:

1Red
2Green
3Blue
1Colours={ 
2    "1":"Red"
3        "2":"Green"
4        "3":"Blue" 
5}
6
7for i in Colours:
8    print(i)
...
python

Output:

1Red
2Green
3Blue
  1. Variables And DataTypes - Python Tutorials For Beginners #3
  2. Operators In Python - Python Tutorials For Beginners #5
  3. Type Casting In Python- Python Tutorials For Beginners #10