0% found this document useful (0 votes)
3 views2 pages

Python For Loop

The document explains the use of for loops in Python to iterate over collections such as lists and strings. It provides examples of printing elements, repeating messages, squaring list elements, and computing the sum of list elements. Additionally, it demonstrates how to manipulate string characters using for loops.

Uploaded by

srinivas maddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Python For Loop

The document explains the use of for loops in Python to iterate over collections such as lists and strings. It provides examples of printing elements, repeating messages, squaring list elements, and computing the sum of list elements. Additionally, it demonstrates how to manipulate string characters using for loops.

Uploaded by

srinivas maddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#For loop:for loop executes set of stmts for every element of collection object.

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

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

for p in x:

print(p)

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 print a msg for multiple times using for loop

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

for p in x:

print("Good Morning!!!")

#To square each element of a list

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

for p in x:

print(p*p) #or print(p**2)

#To compute the sum of list elements


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

sum1=0

for p in x:

sum1=sum1+p

print("sum=",sum1)

#For loop on a string

x="python"

for p in x:

print(p)

#printing each character for 3 times

for p in x:

print(p*3)

#Printing a msg for multiple times

for p in x:

print("Good Evening!!!")

You might also like