Tuesday, October 22, 2019

FOR loop in python

in loops concept For loop plays major role

range is inbuilt function which is having 3 integers values
range(start,stop,step)
start---from where to start the iteration
stop---upto which value(max value) to execute
step---how much increment is looking for each iteration


example 1:

for x in range(1,10):
    print(x)

out put:
1
2
3
4
5
6
7
8
9



example 2:

for x in range(1,10):

    print("*")

output:
*
*
*
*
*
*
*
*
*

nested for loop

for x in range(1,5):
    for y in range (1,5):

          print("*")

output:
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*

example 4:
for x in range(2, 30, 3):
  print(x)

output:
2
5
8
11
14
17
20
23
26
29

example 5:
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]

for x in adj:
  for y in fruits:

    print(x, y)

output:
red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry

example 6:
sentence = ["the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"]

for i in sentence:
    print(i)
    i=+1

output:
the
quick
brown
fox
jumped
over
the
lazy
dog

No comments:

Post a Comment

python class topic video