Lecture 3: Conditional Statements and Loops
Lecture Contents
• Control Structures
• Sequence Structures
• Decision Structures
• if Statement
• if else Statement
• elif Statement ( Chained Conditionals)
• Nested Conditionals
• Repetition Structures
• Loops
• for loop
• while loop
Lecture 3: Conditional Statements and Loops in Python 2
Control Structure
• A control structure controls the order in which a set of statements execute
• Sequence Structure
• All statements are executed in a sequential order
• Sequence structure cannot handle all types of real word scenarios
• Decision Structure / Conditional Statements
• Allow to execute different statements based on a condition
• If condition is True execute statement 1 otherwise execute statement 2
• Examples / Types
• if Statement
• if-else Statement
• elif Statement
• Repetition Structures / Loops
• Execute a block of code specified number of times
• for loop and while loop
Lecture 3: Conditional Statements and Loops in Python 3
Decision Structure : Conditional
Statements
• Conditional Statement
• A line of code that includes a Boolean expression
• Evaluates to True or False
• Includes Relational Operators
Lecture 3: Conditional Statements and Loops in Python 4
Arithmetic Operators
Lecture 3: Conditional Statements and Loops in Python 5
if Statement ( Lab Task 1.1)
• if statement is used to execute a block of code based on a Boolean expression
• Syntax:
• Things to Remember :
• if is in lower case
• There is a colon after the Boolean expression
• Body of if should be indented
• Can contain a single or multiple statements in the body
Lecture 3: Conditional Statements and Loops in Python 6
if Statement Example / Flow Chart
Lecture 3: Conditional Statements and Loops in Python 7
Problem Solving
Lecture 3: Conditional Statements and Loops in Python 8
if else Statement (Lab Task 1.2)
• Sometimes we need to execute a specified block of code when condition is
true and another block of code when condition is false
Lecture 3: Conditional Statements and Loops in Python 9
Problem Solving
Lecture 3: Conditional Statements and Loops in Python 10
Chained Conditionals – elif
• Sometimes there are more than two possibilities .
Lecture 3: Conditional Statements and Loops in Python 11
Problem Solving
Lecture 3: Conditional Statements and Loops in Python 12
Nested Conditional Statements ( Lab
Task 1.3)
• An if statement or an if-else statement can be nested within another if or else
block
Lecture 3: Conditional Statements and Loops
13
in Python
Checking Multiple Conditions
• Logical Operators can be used to combine multiple conditions
Lecture 3: Conditional Statements and Loops in Python 14
Multiple Conditions – Examples
Lecture 3: Conditional Statements and Loops in Python 15
Switch Case Statement
• Languages like C , C++ , Java and Others have Switch–Case statement that can
be used as an alternative to elif statement to check multiple conditions.
• Earlier versions of python do no have any switch-case functionality
• The same can be implemented with the help of dictionaries and classes and functions
• Python version 3.10 and later have match-case functionality
Lecture 3: Conditional Statements and Loops
16
in Python
match – case ( Syntax)
Lecture 3: Conditional Statements and Loops in Python 17
match-case ( Example)
Lecture 3: Conditional Statements and Loops in Python 18
Problem Solving
Lecture 3: Conditional Statements and Loops in Python 19
Repetition Structures / Loops
Lecture 3: Conditional Statements and Loops in Python 20
Repetition Structure
• A repetition structure causes a statement or group of statements to execute
repeatedly
• Two Types
• Counter Controlled Loops
• Number of iterations is known
• A Counter variable is used to count the number of iterations
• for loop is a counter-controlled loop
• Example: Print first 10 odd numbers
• Condition Controlled Loops
• Number of iterations is not known in advanced
• Before each iteration a condition is checked
• while loop is a condition-controlled loop
• For example : play a game until choice ==‘No’
Lecture 3: Conditional Statements and Loops in Python 21
for loop
• A counter controlled loop
• Can be used to process each element in a sequence
• Strings
• List
• Dictionaries
• Sets and Tuples
• Syntax
Lecture 3: Conditional Statements and Loops
22
in Python
for loop – Examples
Lecture 3: Conditional Statements and Loops
23
in Python
range() – a useful built-in function
• range()
• Creates a type of object called an iterable
• iterable
• An object that contains a sequence of values that can be iterated over
• Used in counter-controlled loops ( for loop for example)
• Syntax
• range( start, stop, step)
• By default , start=0 . It is an optional argument
• stop is required . Specifies the end point. Itself not included
• step specifies the increment. By default, it is 1
Lecture 3: Conditional Statements and Loops
24
in Python
range() – syntax ( Lab Task 2.1)
Lecture 3: Conditional Statements and Loops
25
in Python
Nested Loops
Lecture 3: Conditional Statements and Loops
26
in Python
Nested for Loops
Lecture 3: Conditional Statements and Loops
27
in Python
HCF of Two Numbers
Lecture 3: Conditional Statements and Loops
28
in Python
Print this pattern using for loops
Lecture 3: Conditional Statements and Loops
29
in Python
while loop
• While loop is used when number of iterations is not known
• Number of iterations depends on some condition
• Syntax:
• Example
Lecture 3: Conditional Statements and Loops
30
in Python
Game: Guess the number ( Lab Task
2.2)
• Requirements
• A secret number
• Ask the user to guess the number
• Check if the number matches
• If number is greater – display “ You are too high”
• If the number is less – display “ You are too low”
• Terminate when the number matches with a congratulations message
Lecture 3: Conditional Statements and Loops
31
in Python
Which one is better? for or while loop
• Understandability
• for loop is easy to understand and implement as compared to for loop
• Potential for infinite looping
• Less chances in for loop
• Can run into infinite iterations based on the condition
• Execution time
• Both have initialization , test condition and increment expression
• Actual execution time depends on the logic to be implemented
• Any for loop task can be implemented using a while loop but not vice versa
Lecture 3: Conditional Statements and Loops
32
in Python
Loop Control Statements
• Loop control statements change execution from its normal sequence.
Lecture 3: Conditional Statements and Loops
33
in Python
Continue Statement – Examples
Lecture 3: Conditional Statements and Loops
34
in Python
Pass Statement
• The pass statement in Python is a null statement. It does not do anything. It is
used as a placeholder for future code.
• For example, you might use the pass statement to define a function that you
have not yet implemented, or to create a loop that you will fill in later.
• The pass statement is also used to avoid errors in situations where a
statement is required syntactically, but you do not want any code to execute.
• For example, you might use the pass statement in an if statement where you
do not want to take any action if the condition is met.
Lecture 3: Conditional Statements and Loops
35
in Python
pass statement
Lecture 3: Conditional Statements and Loops
36
in Python
break statement-examples
Lecture 3: Conditional Statements and Loops
37
in Python
pass and continue
Lecture 3: Conditional Statements and Loops
38
in Python
pass Statement - Example
The pass statement is a useful tool for avoiding errors and for creating
placeholders for future code.
Lecture 3: Conditional Statements and Loops
39
in Python
while True Loop
• The loop will run at least once
• A break statement is included to quit the loop based on a condition
• If there is no break statement , the loop will continue indefinitely
Lecture 3: Conditional Statements and Loops
40
in Python
Python Literals
• A fixed value that is represented directly in python source code
• Literals are constants that are self-explanatory and don’t need to be
computed or evaluated.
• They are used to provide values to variable or to directly utilize them in
expressions
Lecture 3: Conditional Statements and Loops
41
in Python
References / Resources/
Acknowledgments
• Starting out with Python , 3rd Edition
• Chapter 3 Decision Structure and Boolean Logic
• Chapter 4 Repetition Structures
• https://code4coding.com
• https://www.geeksforgeeks.org/python-while-loop/
Lecture 3: Conditional Statements and Loops
42
in Python