Control Constructs in Python
Control Constructs in Python
1
Lesson Learning Outcome
Pass Merit Distinction
P3 Write a program that M3 Use the IDE to manage D3 Evaluate the use of an
implements an algorithm the development process of IDE for development of
using an IDE. the program. applications in contrasted
with not using an IDE.
2
Control Constructs
3
Selection Example
Selection Example OUIPUT
x = int(input("Number ? ")) Number ? 20
if x % 2 == 0: 20 is EVEN
print(x,"is EVEN")
else: Input A? 12
print(x,"is ODD") Input B? 19
print() 12 is Less than 19
A = int(input("Input A? "))
B = int(input("Input B? ")) OUIPUT
if A > B: Number ? 13
print(A,"is Greater than",B) 13 is ODD
elif A < B:
print(A,"is Less than",B) Input A? 25
else: Input B? 25
print(A,"is Equal to",B) 25 is Equal to 25
4
Repetition Example
x = 1
while x <= 10: OUIPUT
print(x,end=",")
x = x + 1 1,2,3,4,5,6,7,8,9,10,
print()
for x in range(1,10):
print(x,end=",") 1,2,3,4,5,6,7,8,9,
print()
for x in range(1,10,2):
print(x,end=",") 1,3,5,7,9,
print()
for x in range(10,1,-3):
print(x,end=",") 10,7,4,
print()
for x in "Welcome":
print(x,end=",") W,e,l,c,o,m,e,
5
Break keyword
6
Continue keyword
7
Break & Continue Example
Break & Continue Example OUIPUT
# Use of break in a For Loop s
for val in "string": t
if val == "i": r
break The end
print(val)
print("The end") OUIPUT
# Use of continue in While Loop 1
x = 0 2
while x < 10: 4
x = x + 1 5
if x % 3 == 0: 7
continue 8
print(x) 10
print("The end") The end
8
Else in Loops
Else in Loops Example OUIPUT
x = 1 1
while x<=5: 2
print(x) 3
x = x + 1 4
else: 5
print("The end value of x = ",x) The end value of x = 6
print("** The End **") ** The End **
▪ In almost every programming language ‘else’ can be used only with conditional
statements
▪ Python allows us to use else statements with loops too.
▪ Else block is executed just after just after final iteration
9
Else in Loops
Else in Loops Example OUIPUT
for x in range(1, 5): 1
if x == 3: 2
break ** The End ** OUIPUT
print(x) 1
2
else: 4
print("The end value of x = ",x) The end value of x = 4
print("** The End **") ** The End **
▪ Else block is not executed break keyword is used, as it terminates a loop before
its normal completion
▪ Now change the above program by replacing the break keyword with continue
keyword and run the program again.
10
print() Function
11
print() Example1
Print() Example OUIPUT
print('Hello','Welcome’) Hello Welcome
a = 5
print("Value of a is",a) Value of a is 5
print(1,2,3,4) 1 2 3 4
print(1,2,3,4,sep='*’) 1*2*3*4
print(1,2,3,4,sep='#',end='&') 1#2#3#4&
12
print() Example1
Print() Example OUIPUT
x = 5; y = 10 Value of x is 5, y is 10
print(“Value of x is {}, y is {}“
.format(x,y))
print("I love {0} and {1}“
.format("bread","butter")) I love bread and butter
print("I love {1} and {0}".
format("bread","butter")) I love butter and bread
a = 12.3456789
print("Value of a = ",a) Value of a = 12.3456789
print("Value of a = %3.1f "%a) Value of a = 12.3
print("Value of a = %3.2f "%a) Value of a = 12.35
13
input() Function
14
Lesson Summary
15