For Loop in Python


The for loop is used to repeat a specific block of code which you want to repeat a fixed number of times. The for loop is a control flow statement that is used to repeatedly execute a group of statements as long as the condition is satisfied.

Syntax:
   for variable_name in sequence :
              body of loop

This program is a Python script that demonstrates the use of the for loop and the built-in range() function.

  • The first for loop uses the range() function to create an iterator that generates a sequence of numbers from 0 (inclusive) to 21 (exclusive) with a step of 2. The for loop iterates over the numbers in the sequence and prints each one to the screen.
  • The second for loop uses the range() function to create an iterator that generates a sequence of numbers from 0 to 4. The for loop iterates five times, each time prompting the user to input a number using the built-in input() function.
  • The values entered by the user are stored in variables a and b, and then the program print the sum of a and b as output. So the program is using the for loop and the range() function to iterate over a sequence of numbers and for each iteration, it prompts the user to enter two numbers and print the sum of those numbers.
  • The first for loop iterates over a sequence of even numbers and the second for loop iterates 5 times and each iteration it prompts user to enter two numbers.

Source Code

# For Loop in Python
for i in range(0, 21, 2):
    print(i)

for i in range(5):
    a=int(input("Enter a No : "))
    b=int(input("Enter a No : "))
    print(a+b)
To download raw file Click Here

Output


0
2
4
6
8
10
12
14
16
18
20
Enter a No : 3
Enter a No : 2
5
Enter a No : 2
Enter a No : 2
4
Enter a No : 3
Enter a No : 3
6
Enter a No : 3
Enter a No : 3
6
Enter a No : 3
Enter a No : 3
6

List of Programs


Sample Programs


Python Database Connection


Python Flask


Python Tkinder Tutorial