Python For loop

For loop :

  • In all the programming language loop is used to iterate the statement or a part of the program.

For loop in python :

  • In python For loop is also used to iterate the part of the program.
  • it is used to traverse the data structure like list,tuples,or dictionary.

Syntax :

                    for iterating_var in Sequence:
                              Statements.....

Examples :

     >>> for i in range(0,10):
         print(i)
  
     0 
     1
     2
     3  
     4
     5
     6
     7
     8
     9
     >>> 

printing table of the given number :


     >>> n = int(input("Enter number :"))
     Enter number : 5
     >>> for i in range(1,11):
         print(n * i)
     5
     10
     15 
     20
     25
     30
     35
     40
     45
     50
     >>> 

Exercise : comment your Answer ,
Write a program to sum of integer from 1 to 10 using for loop.



Comments

  1. sum = 0
    for i in range(1,11):
    sum = sum + i
    print("The sum of numbers from 1 to 10 is : ",sum)

    ReplyDelete

Post a Comment