❮ Previous Next ❯

Python For Loop

for loop is very popular and widely used loop. In general when loop concept coming to the picture then immediately for loop concept come to our mind.

A for loop in Python is such type of loop which is especially used for interacting over a sequence(thar is either a list, a tuple,
a set, a string, or a dictionary)
python for loop


for i in range(5):
    print(“Hello”)

->Here i is the loop variable, and the meaning is do something 5 times. That means Hello will print 5 times, like:
Output:
Hello
Hello
Hello
Hello
Hello

Here one question may come to our mind what is range(5)

If we write print(list(range(5))), then we will get [0,1,2,3,4]

So I can put list instead of range(), like

for i in [0,1,2,3,4]:
    print(“Hello”)
for number in range(20,26):
    print(number)
Output:
20
21
22
23
24
25
Example:
x=5
   for i in range(x):
  print(i)

Output:
0
1
2
3
4

#Python program to find the sum of numbers
x=[1,2,3,4,5]
sum=0
for i in x:
   sum=sum+i
print("summation is:",sum)

Output:
summation is:15

#Program to find the factorial of a number using recursion:

def fact(n):
   if n==0:
     return 1
   else:
     return n*fact(n-1)
x=int (input("enter a number"))
y=fact(x)
print("factorial is",y)

Output:
enter a number 5
factorial is 120
❮ Previous Next ❯

Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved