for loop in Python

for loop is a definite loop and works with all types of values like integer, float, string, list in python.
Syntax:
for in :
Statements

Example: for loop with number values
Example 1:
# For Loop – Prints the number from 1 to 10
for num in range(1,11):
print(num)
range() function is used to specify the range of values for integer and float datatype.

Example 2:
# For Loop – Prints the number from 1 to 10 in reverse order
for num in reversed (range(1,11,2)):
print(num)

Example 3:
# For loop with String value
strN=”Win Corporate”

for ch in strN:
print (“Current Letter :” + ch)
#prints one character at a time from starting to the end of the string

Example: 4
# For loop with List
Company = [‘Win’, ‘Corporate’, ‘Training’]
for c in Company:
print (“Current value :” + c)
# prints one item at a time from the list.

Happy Learning
Win Corporate Training
website: http://www.wincorptrg.com
View My Profile

Advertisements