# For loop statement# It has the ability to iterate over the items of any sequence, such as a list or a string.# Syntax# for iterating_var in sequence:# statements(s)# If a sequence contains an expression list, it is evaluated first. # Then, the first item in the sequence is assigned to the iterating variable iterating_var. # Next, the statements block is executed. Each item in the list is assigned to iterating_var, and the statement(s) block is executed until the entire sequence is exhausted.
#Exampless ='PYTHON'for x in s :print(x)
P
Y
T
H
O
N
Example 2:
# It can be used to iterate over iterators and a range.# Iterating over a list print("List Iteration")l = ["books","bags","pens"] for i in l:print(i)
List Iteration
books
bags
pens
Example 3:
# Iterating over a String print("\nString Iteration")s ="Python_is_awesome"for i in s :print(i)#String Iteration
String Iteration
P
y
t
h
o
n
_
i
s
_
a
w
e
s
o
m
e
Example 4:
# Iterating over dictionary print("\nDictionary Iteration")d =dict()d['xyz']=123d['abc']=345for i in d :print("%s%d"%(i, d[i]))
Dictionary Iteration
xyz 123
abc 345
Example 5:
# One parameterfor i inrange(5):print(i)
0
1
2
3
4
Practice problems:
Example 6:
# Two parametersfor i inrange(3, 6):print(i)
3
4
5
Example 7:
# Three parametersfor i inrange(4, 10, 2):print(i)
4
6
8
Example 8:
# Going backwardsfor i inrange(0, -10, -2):print(i)
0
-2
-4
-6
-8
Example 9:
i =0while i <3:print(i) i +=1else:print(0)
0
1
2
0
Example 10:
my_string ="Python"i ="i"while i in my_string:#No output why?print(i, end =" ")
No Output
Example 11:
my_string ='Python is Awesome'for i inrange(my_string):print(i)
TypeError Traceback (most recent call last)
in () 1 my_string = 'Python is Awesome' ----> 2 for i in range(my_string): 3 print(i)
TypeError: 'str' object cannot be interpreted as an integer
Example 12:
my_string ='We love Python'for i inrange(len(my_string)):print(my_string[i].upper())
W
E
L
O
V
E
P
Y
T
H
O
N
Example 13:
my_list = ['one','two','three','four','five']my_list_len =len(my_list)for i inrange(0, my_list_len):print(my_list[i])
one
two
three
four
five
Example 14:
for i inrange(5, 0, -1):if i ==1:print('1 bottle of beer on the wall, 1 bottle of beer!')print('So take it down, pass it around, no more bottles of beer on the wall!')elif i ==2:print('2 more bottles of beer on the wall, 2 more bottles of beer!')print('So take one down, pass it around, 1 more bottle of beer on the wall!')else:print('{0} bottles of beer on the wall, {0} bottles of beer!'.format(i))print('So take it down, pass it around, {0} more bottles of beer on the wall!'.format(i -1))
5 bottles of beer on the wall, 5 bottles of beer!
So take it down, pass it around, 4 more bottles of beer on the wall!
4 bottles of beer on the wall, 4 bottles of beer!
So take it down, pass it around, 3 more bottles of beer on the wall!
3 bottles of beer on the wall, 3 bottles of beer!
So take it down, pass it around, 2 more bottles of beer on the wall!
2 more bottles of beer on the wall, 2 more bottles of beer!
So take one down, pass it around, 1 more bottle of beer on the wall!
1 bottle of beer on the wall, 1 bottle of beer!
So take it down, pass it around, no more bottles of beer on the wall!
Example 15:
# Nested Loops# Python programming language allows to use one loop inside another loop.# Syntax:# for iterator_var in sequence:# for iterator_var in sequence:# statements(s)# statements(s)# Example:-for i inrange(1, 5):for j inrange(i):print(i, end=' ')print()
1
2 2
3 3 3
4 4 4 4
Example 16:
l = [ 'hello','hi',1,2,3,4,3.6j,['hi','bye','bye bye'],123,23,3,2]for var in l :print(var)
hello
hi
1
2
3
4
3.6j
['hi', 'bye', 'bye bye']
123
23
3
2
Example 17:
for var inrange(1,20,2):if var %2==0:breakelif var %3==0:continueprint("Hello World")else:print("Good to Go ")
Output
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Good to Go
Example 18:
l = [ 1,2,3,4,5,6,7,8,9,10 ] n =int(input("Enter a number : "))for var in l :print(var*n)
Output
Enter a number : 5
5
10
15
20
25
30
35
40
45
50
Example 19:
for letter in'Python':print('Current Letter :', letter)fruits = ['banana','apple','mango']for fruit in fruits:print('Current fruit :', fruit)print("Good bye!")
Output
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!
Example 20:
# Iterating by Sequence Indexfruits = ['banana','apple','mango']for index inrange(len(fruits)):print('Current fruit :', fruits[index])print("Good bye!")
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!
Dictionary doesn't remember order (sequence), that's why we use another data type in python orderedDict
Example 21:
d ={'name':'python','version':3.6,'father':'Guido Van Rossum','named on ':'Monty Python','method':'OOPS','type':'interpreted','year':1991,}for var in d:print(var,'=',d[var])s ={'hello','hi',1,2,3,4,5,6,'good to go','bye bye'}for var in s :print(var)
Output
name = python
version = 3.6
father = Guido Van Rossum
named on = Monty Python
method = OOPS
type = interpreted
year = 1991
1
2
3
hello
4
5
6
hi
bye bye
good to go
Example 22:
l = [ (1,2,3),('hello','hi','how are you'),('good','bye','bye',),('hello','world','!!',),('python','java','c'),'c++',['oh','my','god' ] ]
c =1for val1,val2,val3 in l :print("Iteration ",c)print("Value 1 = ",val1)print("Value 2 = ",val2)print("Value 3 = ",val3) c = c +1print("\n")