0% found this document useful (0 votes)
2 views

Control Constructs in Python

This document covers advanced Python programming concepts, focusing on control constructs such as selection and repetition, as well as the use of break and continue keywords. It also explains the print() and input() functions, providing examples for clarity. The lesson aims to enhance understanding of implementing algorithms using an Integrated Development Environment (IDE).

Uploaded by

mohsulai2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Control Constructs in Python

This document covers advanced Python programming concepts, focusing on control constructs such as selection and repetition, as well as the use of break and continue keywords. It also explains the print() and input() functions, providing examples for clarity. The lesson aims to enhance understanding of implementing algorithms using an Integrated Development Environment (IDE).

Uploaded by

mohsulai2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

UNIT1 - Programming

Week [06] – Python – Part II

1
Lesson Learning Outcome
Pass Merit Distinction

LO3 Implement basic algorithms in code using an


Integrated Development Environment (IDE)

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

Python supports following control constructs.


Week 3 we have done them in detail.
▪ if Statement
▪ if…elif Statement
▪ while loop
▪ for loop

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

▪ The break statement


terminates the loop
containing it.
▪ If break statement is inside a
nested loop (loop inside
another loop), break will
terminate the innermost loop.

6
Continue keyword

▪ The continue statement is


used to skip the rest of the
code inside a loop for the
current iteration only.
▪ Loop does not terminate
but continues on with the
next iteration.

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

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

▪ objects – is the value(s) to be printed.


▪ sep – is the separator used between the values. It defaults into
a space character.
▪ end – is printed after all values are printed. It defaults into a new
line.
▪ file – is the object where the values are printed and its default
value is sys.stdout (screen).

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

>>> num = input('Enter a number: ')


input([prompt]) Enter a number: 10
>>> num

Python provides this function to read a '10’


line of text from standard input, which by
default comes from the keyboard. Here the Value is String. This can be
converted as follows:
>>> int(num)
10
>>> float(num)
10.0

14
Lesson Summary

▪ Control constructs - recap


▪ Selections in Python
▪ Repetitions in Python
▪ Break / Continue Keywords
▪ Else in Loops
▪ print() Function
▪ input() Function

15

You might also like