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

Lecture 6 - Python

The document discusses different types of control flow statements in Python including decision making statements like if, elif and else as well as iterative statements like while and for loops. It provides examples of using if, elif, else, while, for, nested loops, break, continue and range.

Uploaded by

amila udayanga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lecture 6 - Python

The document discusses different types of control flow statements in Python including decision making statements like if, elif and else as well as iterative statements like while and for loops. It provides examples of using if, elif, else, while, for, nested loops, break, continue and range.

Uploaded by

amila udayanga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

APPLICATION LABORATORY II

Sherina Sally

Flow Control Department of ICT


Faculty of Technology,
University of Colombo

OUTLINE
• Decision Making Statements

• Iterative Statements
All Rights Reserved

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 2

1
DECISION MAKING
• Checks for the validity of a condition

• Branches for many levels depending on the initial condition

All Rights Reserved


• Indentation is useful to represent a block of code

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 3

if STATEMENT
Single if condition: Alternative else condition:

if x > 5: if x > 5:
print(“x is greater than 5”) print(“x is greater than 5”)
else:
print(“x is less than 5”)
if x > 5: print(“x is greater than 5”)
All Rights Reserved

Ultimate else condition:


Alternative elif condition:
if x > 2:
if x > 2: print(“x is greater than 2”)
print(“x is greater than 2”) elif x > 0:
elif x > 0: print(“x is greater than 0”)
print(“x is greater than 0”) else:
print(“x is less than 0”)

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 4

2
CONDITIONAL EXPRESSION
• Known as the Ternary Operator

• Short hand method of writing a “if-else” condition

• The “if-else” condition is placed in one line

All Rights Reserved


• Interpretation is similar to the natural language

• Useful if there exists only one statement to be executed after each condition: if, elif, else

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 5

#if-else in a single line

print(“x is greater than 5”) if x > 5 else print(“x is less than 5”)

#if-elif-else in a single line

print(“Excellent”) if x > 70 else print(“Satisfactory”) if x > 40 else print(“Fail”)

Department of ICT, Faculty of Technology, University of Colombo


6

3
NESTED if STATEMENT
• Insert an “if” statement within another “if” statement

if x >= 10:
if x <= 15:

All Rights Reserved


print(“x is a number between 10 and 15”)
else:
print(“x is greater than 15”)
else:
print(“x is less than 10”)

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 7

pass STATEMENT
• The body of a conditional statement, function, class cannot be empty

• Works as a placeholder with no operation performed


All Rights Reserved

if x > 5:
pass

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 8

4
ITERATIVE STATEMENTS
• Iterates until a condition is unsatisfactory or iterates over a sequence

• Looping statements:

• while statement: The loop iterates until a condition becomes False

All Rights Reserved


• for statement: The loop iterates over all the items in a sequence

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 9

while LOOP
x = 2
while x < 10:
print(x)
x += 2
All Rights Reserved

2
4
6
8
Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 10

5
for LOOP

for x in “Pet”: for x in [“red”, “blue”, “yellow”]:


print(x) print(x)

All Rights Reserved


P red
e blue
yellow
t
Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 11

NESTED for LOOP

for x in [“red”, “blue”]:


for y in [“hat”, “scarf”]:
print(x, y)
All Rights Reserved

red hat
red scarf
blue hat
blue scarf
Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 12

6
break STATEMENT
• Terminates from the existing loop and moves to the next statement

for x in ‘programming’:

All Rights Reserved


if x == ‘g’:
break
print(x)

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 13

continue STATEMENT
• Ignores the statements of the current iteration and continues with the next iteration

for x in ‘programming’:
All Rights Reserved

if x == ‘g’:
continue
print(x)

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 14

7
range()
incremental
• Iterates over a set number of times start factor

• The default behavior:

• Starting number: 0 for x in range(10, 30, 5):

All Rights Reserved


• Incremental factor: 1
print(x)

• The starting number, ending number and the incremental factor can be controlled

for x in range(30):
print(x)
Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 15

else STATEMENT IN LOOPS


• Executes a block of code after the occurrence of the loop

for x in range(30):
print(x)
else:
All Rights Reserved

print(“End of printing numbers 0-30”)

x = 0
while x < 5:
print(x)
else:
print(“x is now 5”)

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 16

8
Department of ICT, Faculty of Technology, University of Colombo

You might also like