Loops In Python (for, while loop)

Example 1:

# 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.
#Examples
s = 'PYTHON'
for x in s : 
    print(x)
    

P

Y

T

H

O

N

Example 2:

List Iteration

books

bags

pens

Example 3:

String Iteration

P

y

t

h

o

n

_

i

s

_

a

w

e

s

o

m

e

Example 4:

Dictionary Iteration

xyz 123

abc 345

Example 5:

0

1

2

3

4

Practice problems:

Example 6:

3

4

5

Example 7:

4

6

8

Example 8:

0

-2

-4

-6

-8

Example 9:

0

1

2

0

Example 10:

No Output

Example 11:

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:

W

E

L

O

V

E

P

Y

T

H

O

N

Example 13:

one

two

three

four

five

Example 14:

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:

1

2 2

3 3 3

4 4 4 4

Example 16:

hello

hi

1

2

3

4

3.6j

['hi', 'bye', 'bye bye']

123

23

3

2

Example 17:

Output

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Good to Go

Example 18:

Output

Enter a number : 5

5

10

15

20

25

30

35

40

45

50

Example 19:

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:

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:

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:

Output

Iteration 1

Value 1 = 1

Value 2 = 2

Value 3 = 3

Iteration 2

Value 1 = hello

Value 2 = hi

Value 3 = how are you

Iteration 3 Value 1 = good

Value 2 = bye

Value 3 = bye

Iteration 4

Value 1 = hello

Value 2 = world

Value 3 = !!

Iteration 5

Value 1 = python

Value 2 = java

Value 3 = c

Iteration 6

Value 1 = c

Value 2 = +

Value 3 = +

Iteration 7

Value 1 = oh

Value 2 = my

Value 3 = god

Example 23:

Output

val1 = 1

val2 = 1

val1 = 34

val2 = 3

val1 = 56

val2 = 4

val1 = 34

val2 = 42

val1 = 2

val2 = 67

val1 = 364

val2 = 346

val1 = 57

val2 = 57

val1 = 42

val2 = 5434

val1 = 13

val2 = 2

val1 = 4

val2 = 34

val1 = 65

val2 = 657

val1 = 63

val2 = 434

val1 = 3

val2 = 2

val1 = 12

val2 = 346

val1 = 2

val2 = 34

val1 = 3456

val2 = 34

Example 24:

Output

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,

Example 25:

Output

990, 960, 930, 900, 870, 840, 810, 780, 750, 720, 690, 660, 630, 600, 570, 540, 510, 480, 450, 420, 390, 360, 330, 300, 270, 240, 210, 180, 150, 120, 90, 60, 30,

Example 26:

Output:

[2, 10, 12, 12, 23, 34, 34, 44, 45, 56, 76, 213, 232, 345, 456, 567]

Last updated

Was this helpful?