Just like other high level language Python also have same control statements.....

They are

  • if … Else

  • For Loops

  • While Loops

  • Arrays

IF, ELIF AND ELSE STATEMENT

Example 09: Write a program to find which number is greater using IF statement alone.

x = 5

y = 10

if x > y:

print("x is greater than y")

if y > x:

print("y is greater than x")

if x == y:

print("x is equal to y")

Result

y is greater than x

Example 10: Write a program to find which number is greater using IF and ELIF statement.

x = 5

y = 10

if x > y:

print("x is greater than y")

elif y > x:

print("y is greater than x")

elif x == y:

print("x is equal to y")

Result

y is greater than x

Example 11: Write a program to find which number is greater using IF, ELIF and ELSE statement.

x = 10

y = 10

if x > y:

print("x is greater than y")

elif y > x:

print("y is greater than x")

else:

print("x is equal to y")

Result

x is greater than y

FOR LOOP STATEMENT

For Loop Statement example

for i in range(1,20):

print(i)

Different Ways of representing the for loop Statement

N = 10

for x in range (N):

print(x)

start = 1

stop= 10

for x in range (start,stop):

print(x)

start = 1

stop= 10

step=2

for x in range (start,stop,step):

print(x)

Example 11: Write a program to find sum and average of the given number using FOR LOOP statement

data = [ 1,4,7,10,13]

sum = 0

#Find the Sum of the given number

for x in data:

sum = sum + x

print(sum)

#Find the Average of the given number

N = len(data)

avg = sum/N

print('n')

print(avg)

Result

1

5

12

22

35

7.0