Type Here to Get Search Results !

ads

For Loop in Python: Examples

For Loop in Python:

Python Programming


#All the stmts within the for loop should follow the indentation.


x=[10,20,30,40,50]

for p in x:

    print(p)


#Printing all the values within a single line


for p in x:

    print(p,end=" ")


#Printing a msg for multiple times using for loop


x=[10,20,30,40,50]

for p in x:

    print("Good Evening!!!")


#To square each element of a list


x=[10,20,30,40,50]

for p in x:

    print(p**2)


#Squaring only the element 30

print(x[2]*x[2])

print("\n")


for p in x:

    if(p==40):

        print(p**2)

    else:

        print(p)

        

#To compute sum of list elements


x=[10,20,30,40,50]

sum=0

for p in x:

    sum=sum+p

print("sum=",sum)


To practice Python program, you can practice online by clicking on the link given below.👇

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.