0% found this document useful (0 votes)
9 views54 pages

Python Control Flow Explained

The document provides an overview of control flow in Python, detailing how code execution is directed through sequential, selective, and iterative constructs. It covers key concepts such as Boolean expressions, logical operators, and various control statements including if, if-else, and loops. Additionally, it explains the use of core collection types like lists, tuples, and dictionaries, along with the use of break and continue statements in loops.

Uploaded by

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

Python Control Flow Explained

The document provides an overview of control flow in Python, detailing how code execution is directed through sequential, selective, and iterative constructs. It covers key concepts such as Boolean expressions, logical operators, and various control statements including if, if-else, and loops. Additionally, it explains the use of core collection types like lists, tuples, and dictionaries, along with the use of break and continue statements in loops.

Uploaded by

haon31676
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Control Flow in Python

Directing the Execution Path of Your Programs


Presenter:
Tesfaye Fenta
PhD Candidate
Beijing Institute of Technology
Date: November 26, 2025

12/01/2025 1
Learning Objectives
• Understand how control flow determines the order in which Python code runs.
• Write and evaluate Boolean expressions and logical operators.
• Use selection statements: if, if-else, if-elif-else, and nested if.
• Use iteration statements: while and for loops with range().
• Work with core collection types: list, tuple, and dictionary.
• Control loops using break and continue.

12/01/2025 2
Control Flow

• Control flow describes the order in which your lines of code are run.
• This order is usually different than the sequence in which the lines of code
appear.
• Execution can flow from one place in the code to another, as suggested in
the diagram below.

12/01/2025 3
Boolean expressions - conditions
• A condition is an expression that evaluates to either True or False.
• An expression that evaluates to either True or False is called a
Boolean expression.

12/01/2025 4
Cont…

12/01/2025 5
Logical NOT (not) - Unary Operator

12/01/2025 6
Logical AND (and) - Binary Operator

12/01/2025 7
Logical OR (or) - Binary Operator

12/01/2025 8
Combining Multiple Logical Operators

12/01/2025 9
Truth Tables in Python

12/01/2025 10
Practical Real-World Example

12/01/2025 11
Cont…

12/01/2025 12
Cont…

12/01/2025 13
Cont…

12/01/2025 14
Python Control Statements
• In any programming language a program may execute
sequentially, selectively or iteratively.
• Every programming language provides constructs to
support Sequence, Selection and Iteration.
• In Python all these construct can broadly categorized in 2
categories.
A. Conditional Control Construct (Selection, Iteration)
B. Un- Conditional Control Construct ( break, continue)
12/01/2025 15
Cont…
• Three Ways Programs Execute:
[Link] - Code runs line by line, top to bottom (default behavior)
[Link] - Code chooses which path to take based on conditions
(if/else)
[Link] - Code repeats a block multiple times (loops)

12/01/2025 16
Cont…
• What are Constructs?
• Constructs are the building blocks or structures that programming languages
provide to control how code executes.
• Think of them as control flow tools.
• In Python, constructs are keywords and statements that allow you to:
Make decisions
Repeat actions
Jump or skip code
• Two Categories of Control Constructs in Python:
A. Conditional Control Constructs
• These depend on conditions being met (True/False)
12/01/2025 17
Cont….

12/01/2025 18
Python If statements

12/01/2025 19
Cont…

Flow Chart: it is a graphical representation of steps an algorithm to solve a


problem.

12/01/2025 20
Example

12/01/2025 21
Python if - else statements

12/01/2025 22
Example 1

12/01/2025 23
Example 2

12/01/2025 24
Python Ladder if else statements (if-elif-else)
• In addition to the if clause, there are two other optional clauses often
used with an if statement. For example:

12/01/2025 25
Cont…
• if: An if statement must always start with an if clause, which contains the
first condition that is checked. If this evaluates to True, Python runs the
code indented in this if block and then skips to the rest of the code after
the if statement.

• elif: elif is short for "else if." An elif clause is used to check for an
additional condition if the conditions in the previous clauses in the if
statement evaluate to False. As you can see in the example, you can have
multiple elif blocks to handle different situations.

• else: Last is the else clause, which must come at the end of an if statement
if used. This clause doesn't require a condition. The code in an else block
is run if all conditions above that in the if statement evaluate to False. 26
12/01/2025
Cont…

12/01/2025 27
Example 1

12/01/2025 28
Example 2

12/01/2025 29
Cont…

12/01/2025 30
Python Nested if statements

12/01/2025 31
Cont

12/01/2025 32
Cont…

12/01/2025 33
Example

12/01/2025 34
Indentation
• Some other languages use braces to show where blocks of code begin and
end.
• In Python we use indentation to enclose blocks of code.
• For example, if statements use indentation to tell Python what code is
inside and outside of different clauses.

• In Python, indents conventionally come in multiples of four spaces. Be


strict about following this convention, because changing the indentation
can completely change the meaning of the code. If you are working on a
team of Python programmers, it's important that everyone follows the
same indentation convention!
12/01/2025 35
Python Iteration Statements

12/01/2025 36
Python while loop

12/01/2025 37
Cont…

12/01/2025 38
Cont…

12/01/2025 39
Example: print 1 to 10 numbers

12/01/2025 40
Example: Sum of 1 to 10 numbers.

12/01/2025 41
Python range( ) Function

12/01/2025 42
Python for loop
• A for loop in Python repeats a block of code once for each
item in an iterable (a list, range, dict, etc.).

12/01/2025 43
Lists

• A list is a collection data type that is ordered and mutable.


• Unlike Sets, Lists allow duplicate elements.
• They are useful for preserving a sequence of data and further iterating over
it.
• Lists are created with square brackets.

• my_list = ["banana", "cherry", "apple"]

12/01/2025 44
Cont..

12/01/2025 45
Cont…

12/01/2025 46
Tuple
• A tuple is a collection of objects which is ordered and immutable.
• Tuples are similar to lists, the main difference is the immutability.
• In Python tuples are written with round brackets and comma
separated values.

• my_tuple = ("Max", 28, "New York")

12/01/2025 47
Cont…
• Reasons to use a tuple over a list
• Generally used for objects that belong together.
• Use tuple for heterogeneous (different) datatypes and list for homogeneous
(similar) datatypes.
• Since tuple are immutable, iterating through tuple is slightly faster than with
list.
• Tuples with their immutable elements can be used as key for a dictionary. This
is not possible with lists.
• If you have data that doesn't change, implementing it as tuple will guarantee
that it remains write-protected.
12/01/2025 48
Cont..

12/01/2025 49
Dictionaries
• A dictionary is a collection which is unordered, changeable and
indexed.
• A dictionary consists of a collection of key-value pairs.
• Each key-value pair maps the key to its associated value. A
dictionary is written in braces.
• Each key is separated from its value by a colon (:), and the items
are separated by commas.
• my_dict = {"name":"Max", "age":28, "city":"New York"}

12/01/2025 50
Looping through dictionary

12/01/2025 51
Key Differences: while and for loop

12/01/2025 52
Un- Conditional Control Construct ( break, continue)
 Break: This statement forcefully terminates the loop
immediately when encountered
 Continue - This statement skips the current iteration of the loop
and proceeds to the next one.
 Example -

12/01/2025 53
!
u
yo
k
an
Th

12/01/2025 54

You might also like