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

Lecture Unit 4

Intro to python

Uploaded by

hashimabdulatwif
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)
18 views

Lecture Unit 4

Intro to python

Uploaded by

hashimabdulatwif
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/ 33

UNIVERSITY OF LUSAKA

SCHOOL OF SOCIAL SCIENCES AND TECHNOLOGY

LECTURE~UNIT 4: Python - Control Flow Statements 1


NAME: Longwani C Perry (Mr.)
CONTACT: +260 966589151
What are Control Flow Statements?
Python program control flow is regulated by various types of conditional statements,
loops, and function calls. By default, the instructions in a computer program are executed
in a sequential manner, from top to bottom, or from start to end. However, such sequentially
executing programs can perform only simplistic tasks.

Therefore, control flow statements makes the program to have a decision-making ability, so that
it performs different steps depending on different conditions.

Normally, there are two type of control flow statements in any programming language and Python
also supports them. Namely:

• Decision-making Statements
• Loops or Iteration Statements

2
What are Control Flow Statements?
Decision-making Statements
Decision making statements are used in the Python programs to make them able to decide
which of the alternative group of instructions to be executed, depending on value of a certain
Boolean expression.

The following diagram illustrates how decision-making statements work: −

3
What are Control Flow Statements?
Types of Decision-making Statements in Python

• The if statement
• The if-else statement
• The nested-if statement
• The if-elif-else ladder
• Switch statements (match)

4
What are Control Flow Statements?
Python if statement
The if statement is the most simple decision-making statement. It is used to decide
whether a certain statement or block of statements will be executed or not.

Here, the condition after evaluation will be either true or false. if the statement accepts
Boolean values – if the value is true then it will execute the block of statements below it
otherwise not.

5
What are Control Flow Statements?
As we know, python uses indentation to identify a block. So the block under an if statement
will be identified as shown in the below example:

6
What are Control Flow Statements?
Flowchart of Python if statement
Let’s look at the flow of code in the If statement

7
What are Control Flow Statements?
Example of Python if Statement
As the condition present in the if statement is false. So, the block below the if statement is executed.

8
What are Control Flow Statements?
Python If-Else Statement
The if statement alone tells us that if a condition is true it will execute a block of statements
and if the condition is false it won’t. But if we want to do something else if the condition is false,
we can use the else statement with the if statement to execute a block of code when the if condition
is false.

9
What are Control Flow Statements?
Flowchart of Python if-else statement
Let’s look at the flow of code in an if-else statement

10
What are Control Flow Statements?

Using Python if-else statement


The block of code following the else statement is executed as the condition present in the if
statement is false after calling the statement which is not in the block(without spaces).

11
What are Control Flow Statements?
Nested-If Statement in Python
A nested if is an if statement that is the target of another if statement. Nested if statements mean
an if statement inside another if statement.

Yes, Python allows us to nest if statements within if statements. i.e., we can place an if statement
inside another if statement.

12
What are Control Flow Statements?
Flowchart of Python Nested if Statement
Let’s look at the flow of control in Nested if Statements

13
What are Control Flow Statements?
Example of Python Nested if statement
In this example, we are showing nested if conditions in the code, All the If conditions will be
executed one by one.

14
What are Control Flow Statements?
Python if-elif-else Ladder
Here, a user can decide among multiple options. The if statements are executed from the top down.

As soon as one of the conditions controlling the if is true, the statement associated with that if is
executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final
“else” statement will be executed.

15
What are Control Flow Statements?
Flowchart of Python if-elif-else ladder
Let’s look at the flow of control in if-elif-else ladder:

16
What are Control Flow Statements?
Example of Python if-elif-else ladder
In the example, we are showing single if condition, multiple elif conditions, and single
else condition.

17
What are Control Flow Statements?
Short Hand if statement
Whenever there is only a single statement to be executed inside the if block then shorthand if
can be used. The statement can be put on the same line as the if statement.

18
What are Control Flow Statements?
Example of Python if shorthand
In the given example, we have a condition that if the number is less than 15, then further code
will be executed.

19
What are Control Flow Statements?
Short Hand if-else statement
This can be used to write the if-else statements in a single line where only one statement is
needed in both the if and else blocks.

20
What are Control Flow Statements?
Example of Python if else shorthand
In the given example, we are printing True if the number is 15, or else it will print False.

21
What are Control Flow Statements?
Chaining comparison operators in Python

The most common syntax to do it is as follows:

In Python, there is a better way to write this using the Comparison operator Chaining.
The chaining of operators can be written as follows:

22
What are Control Flow Statements?
Here’s an example of chaining comparison operators:

23
What are Control Flow Statements?

24
What are Control Flow Statements?
Switch Case in Python
In Python 3.10 and after that, Python will support this by using match in place of switch:

25
What are Control Flow Statements?
Loops or Iteration Statements
Most of the processes require a group of instructions to be repeatedly executed. In programming
terminology, it is called a loop. Instead of the next step, if the flow is redirected towards any
earlier step, it constitutes a loop.

The following diagram illustrates how the looping works −

26
What are Control Flow Statements?
Types of Loop Statements in Python

• For loop
• While loop

For Loops in Python


Python For loop is used for sequential traversal i.e. it is used for iterating over an iterable
like String, Tuple, List, Set, or Dictionary.

27
What are Control Flow Statements?
Flowchart of for loop

28
What are Control Flow Statements?
Examples of Python For Loop

29
What are Control Flow Statements?
Python While Loop
Python While Loop is used to execute a block of statements repeatedly until a given condition
is satisfied. When the condition becomes false, the line immediately after the loop in the program
is executed.

30
What are Control Flow Statements?
Flowchart of Python While Loop

31
What are Control Flow Statements?
Python While Loop
In this example, the condition for while will be True as long as the counter variable (count)
is less than 3.

32
Questions????

Subscribe to my YouTube Channel !!!


For more ICT lessons

Perlongs Computing

33

You might also like