Computational Thinking and
Problem Solving with Python
[Link]. I Year I Semester (2024-2028)
All Sections
COMPUTATIONAL THINKING AND PROBLEM-
SOLVING USING PYTHON
Module – III
Control Flow And Functions
Module - III
‾ Conditionals, if, elif, else;
‾ Iteration/Control Statement; Break, Continue,
‾ Functions: Types and Definition, Flow of execution;
‾ Function Prototype; Parameters and Arguments, Fruitful
Functions.
10/28/2024 Introduction Python Using Capstone Project 3
Conditional Statements
▪ Conditional Statements are statements in Python that provide a
choice for the control flow based on a condition.
▪ It means that the control flow of the Python program will be
decided based on the outcome of the condition.
Conditional Statements
1. if Conditional Statement
▪ If the simple code of block is to be performed if the condition holds then
the if statement is used. Here the condition mentioned holds then the
code of the block runs otherwise not.
▪ Syntax:
if <expr>:
<statement>
<expr> is an expression evaluated in a Boolean context, as Logical
Operators in the Operators and Expressions.
<statement> is a valid Python statement, which must be indented.
Conditional Statements
1. if Conditional Statement
▪ If <expr> is true (evaluates to a value that is “truthy”), then
<statement> is executed.
▪ If <expr> is false, then <statement> is skipped over and
not executed.
▪ Note that the colon (:) following <expr> is required. Some
programming languages require <expr> to be enclosed in
parentheses, but Python does not.
Conditional Statements
1. if Conditional Statement
▪ Flowchart of if Statement in Python. Below is the flowchart by
which we can understand how to use if statement in Python:
Conditional Statements
1. if Conditional Statement
▪ Examples of this type of if statement:
Conditional Statements
1. if Conditional Statement
▪ Examples of this
type of if statement:
Conditional Statements
1. if Conditional
Statement
▪ Examples of
this type of
if statement:
Conditional Statements
1. if Conditional Statement
▪ Examples of this type of if statement:
Conditional Statements
1. if Conditional Statement
Python: It’s All About the Indentation
▪ Python follows a convention known as the off-side rule, a term
coined by British computer scientist Peter J. Landin. (The term
is taken from the offside law in association football.)
▪ Languages that adhere to the off-side rule define blocks by
indentation.
▪ Python is one of a relatively small set of off-side rule languages.
Conditional Statements
1. Compound if Statement
Python: It’s All About the Indentation
▪ Python follows a convention known as the off-side rule, a term
coined by British computer scientist Peter J. Landin. (The term
is taken from the offside law in association football.)
▪ Languages that adhere to the off-side rule define blocks by
indentation.
▪ Python is one of a relatively small set of off-side rule languages.
Conditional Statements
1. Compound if Statement
Python: It’s All About the
Indentation
▪ Here, all the statements at the
matching indentation level (lines 2
to 5) are considered part of the
same block.
▪ The entire block is executed if
<expr> is true, or skipped over if
<expr> is false.
▪ Either way, execution proceeds
with <following_statement> (line 6)
afterward.
Conditional Statements
1. Compound if Statement
Python: It’s All About the
Indentation
Conditional Statements: if-else
▪ Now we know how to use an if statement to conditionally
execute a single statement or a block of several statements.
▪ Sometimes, you want to evaluate a condition and take one path if
it is true but specify an alternative path if it is not.
▪ This is accomplished with an else clause:
Conditional Statements: if-else
▪ If <expr> is true, the first suite is executed, and the second is
skipped.
▪ If <expr> is false, the first suite is skipped and the second is
executed.
▪ Either way, execution then resumes after the second suite.
▪ Both suites are defined by indentation, as described above.
Conditional Statements: if-else
Conditional Statements: if-else
Example
Conditional Statements if-elif-else
▪ if checks a condition and executes a
if, elif, else: block of code if it’s true.
These are control
structures used to make ▪ elif (short for “else if”) allows you to
decisions based on check additional conditions.
conditions.
▪ else provides an alternative block of
code to execute if none of the previous
conditions are true.
Conditional Statements: if-elif-else
Conditional Statements: if-elif-else
Example
Example 2
Ternary Expression Conditional Statements in Python
▪ The Python ternary Expression determines if a condition is true
or false and then returns the appropriate value in accordance
with the result.
▪ The ternary Expression is useful in cases where we need to
assign a value to a variable based on a simple condition, and we
want to keep our code more concise — all in just one line of
code.
Ternary Expression Conditional Statements
Iteration/looping Statements
▪ while loop: Repeats a block of code as long as a condition
is true.
▪ for loop: Iterates over a sequence (e.g., a list, string,
or range).
10/28/2024 Introduc tion Python Using Capstone Projec t 27
10/28/2024 Introduc tion Python Using Capstone Projec t 28
10/28/2024 Introduc tion Python Using Capstone Projec t 29
10/28/2024 Introduc tion Python Using Capstone Projec t 30
10/28/2024 Introduc tion Python Using Capstone Projec t 31
10/28/2024 Introduc tion Python Using Capstone Projec t 32
10/28/2024 Introduc tion Python Using Capstone Projec t 33
10/28/2024 Introduc tion Python Using Capstone Projec t 34
10/28/2024 Introduc tion Python Using Capstone Projec t 35
10/28/2024 Introduc tion Python Using Capstone Projec t 36
10/28/2024 Introduc tion Python Using Capstone Projec t 37
for loop examples
for i in range(1, 6):
print(f"Number: {i}")
Coding Snippet
for i in range(10):
if i % 2 == 0:
continue
print(i)
10/28/2024 Introduc tion Python Using Capstone Projec t 40
10/28/2024 Introduc tion Python Using Capstone Projec t 41
10/28/2024 Introduc tion Python Using Capstone Projec t 42
10/28/2024 Introduc tion Python Using Capstone Projec t 43
10/28/2024 Introduc tion Python Using Capstone Projec t 44
10/28/2024 Introduc tion Python Using Capstone Projec t 45
10/28/2024 Introduc tion Python Using Capstone Projec t 46
10/28/2024 Introduc tion Python Using Capstone Projec t 47
10/28/2024 Introduc tion Python Using Capstone Projec t 48
10/28/2024 Introduc tion Python Using Capstone Projec t 49
Coding Snippet
count = 0
while count < 5:
print(f"Count: {count}")
count += 1
Module III
Control Flow and Functions
Loop Control Statements
break, continue, and pass
▪ Using loops in Python automates and repeats the tasks in an efficient
manner.
▪ But sometimes, there may arise a condition where you want to exit the
loop completely, skip an iteration or ignore that condition.
▪ These can be done by “loop control statements”.
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 51
break, continue and pass
▪ Loop control statements change execution from their normal
sequence.
▪ When execution leaves a scope, all automatic objects that were
created in that scope are destroyed.
▪ Python supports the following control statements:
▪ break statement
▪ Continue statement
▪ Pass statement
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 52
break, continue and pass
. A placeholder statement that does nothing (useful for
creating empty loops or functions).
11/1/2024 Introduc tion Python Using Capstone Projec t 53
break, continue and pass
“break” Statement Deatils
▪ Python provides us with a particular purpose statement – break. It is
worth noting that the break statement can only be used within the
for and while loops.
▪ Once the program encounters the break statement, it terminates the
loop immediately, and the lines of code written right after the body
of the loop continue to execute.
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 54
for loop with break example
for i in range(10):
if i == 5:
break
print(i)
break, continue, and pass
“break” Statement
▪ The break statement in Python will cause the loop to end even if
the while loop condition evaluates to True, or in the case of
for loop. It will cause the control of the program to jump out of
the for loop even if it satisfies the condition.
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 56
break, continue and pass in Python
“break” Statement in Python
▪ A typical example of when the break statement is used is during a
sequential search.
▪ For example, you’re searching for an element in a list of elements
using a for a loop. You will add in a comparison condition, to check if
the element is found.
▪ If the element is found, then you would exit from the loop without
traversing the remaining elements.
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 57
break, continue and pass
“break” Statement
• Here is a flowchart of how the break
statement works in Python to give you
better clarity.
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 58
break, continue and pass
“break” Statement in Python
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 59
break, continue and pass
Example: “break” statement in for loop
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 60
break, continue and pass
“continue” Statement in Python
▪ Continue is also one of the useful loop control statements in
Python.
▪ It is almost the opposite of the break statement. Break terminates
the loop, and the continue statement forces the program to execute
the next iteration of the loop.
▪ When the program control encounters the continue statement,
the code following the continue statement (if any) will be skipped,
and the next iteration of the loop will begin.
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 61
break, continue and pass
“continue” Statement in Python
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 62
break, continue and pass
“continue” Statement in Python
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 63
break, continue and pass
Example: continue statement in for loop
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 64
break, continue and pass
“pass” Statement in Python
▪ The “pass” is the keyword In Python, which won’t do anything.
Sometimes there is a situation in programming where we need to define
a syntactically empty block.
▪ We can define that block with the pass keyword.
▪ A pass statement is a Python null statement. When the
interpreter finds a pass statement in the program, it returns no operation.
Nothing happens when the pass statement is executed.
▪ It is useful in a situation where we are implementing new methods or
also in exception handling. It plays a role like a placeholder.
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 65
break, continue and pass
“pass” Statement in Python
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 66
break, continue and pass
“pass” Statement in Python
▪ But this doesn’t seem useful, does it? Not writing the pass statement there
wouldn’t make any difference in the code, and it would also make the
code shorter.
▪ Why does the python syntax have a statement that tells the interpreter to
do nothing?
▪ The statement can be used to fulfill a place in a block of code that needs at
least 1 statement.
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 67
break, continue and pass
“pass” Statement in Python
▪ You have this piece of code, and you really do not know at this point of
time, what the values of a and b will be, and what you will be doing if the
condition evaluates to True.
▪ You want to leave the body empty, but you cannot do that because just
running this piece of code would give you an indentation error.
▪ Why? Because after the colon (:), Python expects an indented block
of code, even if it is a single line or a single statement.
▪ Here, we can make use of the pass statement in Python.
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 68
10/28/2024 Introduc tion Python Using Capstone Projec t 69
Functions
Function Definition:
▪ A function is “a reusable block of code that performs a specific
task”.
▪ Defined using the def keyword, followed by the function name
and parameters.
Flow of Execution:
▪ Functions execute from top to bottom.
▪ When a function is called, control transfers to that function.
▪ After the function completes, control returns to where it was
called.
Functions
Function Prototype:
▪ Specifies the function name, parameters, and return type (if
any).
Parameters and Arguments:
▪ Parameters are placeholders in the function definition.
▪ Arguments are actual values passed to the function when it’s
called.
Fruitful Functions:
▪ Functions that return a value (using the return statement).
10/28/2024 Introduc tion Python Using Capstone Projec t 72
10/28/2024 Introduc tion Python Using Capstone Projec t 73
Types of Functions
In Python typically functions are classified into two main categories:
1. Built-in Functions
2. User-Defined Functions
1. Built-in Functions: These are functions that are provided by Python
itself and are always available for use.
▪ They are part of the Python standard library, and we don’t need
to define them or import anything to use them.
Types of Functions
1. Built-in Functions:
Examples:
▪ print() - Outputs text or data to the console.
▪ len() - Returns the length of an object.
▪ sum() - Returns the sum of elements in an iterable.
▪ max() - Returns the maximum value from a set of values.
Types of Functions
2. User-Defined Functions: These are functions that we, the
programmer, define to perform specific tasks.
▪ They are created using the def keyword followed by the function
name and parentheses ().
▪ However, under user-defined functions, you can further categorize
into more specific types (like recursive, lambda, generator, etc.)
based on their functionality.
▪ Examples of user-defined functions:
▪ Custom logic to greet users.
▪ Calculating the area of a shape.
▪ Performing data processing operations.
Types of Functions
2. User-Defined Functions: Examples
Module III
10/28/2024 Introduc tion Python Using Capstone Projec t 78
Module III
honest
honest
honest
11/4/2024 Introduc tion Python Using Capstone Projec t 79
Module III
honest
honest_ratings
(honest_ratings)
11/4/2024 Introduc tion Python Using Capstone Projec t 80
Module III
honest_ratings
(honest_ratings)
11/4/2024 Introduc tion Python Using Capstone Projec t 81
Module III
(honest_ratings)
(honest_ratings)
(honest_ratings)
11/4/2024 Introduc tion Python Using Capstone Projec t 82
Module III
(honest_ratings)
(honest_ratings)
11/4/2024 Introduc tion Python Using Capstone Projec t 83
Module III
(honest_ratings)
(honest_ratings)
11/4/2024 Introduc tion Python Using Capstone Projec t 84
Module III
honest_ratings
_honest_ratings (honest_ratings)
honest_ratings
11/4/2024 Introduc tion Python Using Capstone Projec t 85
Module III
honest_ratings
honest_rating (honest_ratings)
honest_ratings
11/4/2024 Introduc tion Python Using Capstone Projec t 86
Module III
honest_ratings
honest_ratins: (honest_ratings)
honest_ratings:
honest_ratings:
11/4/2024 Introduc tion Python Using Capstone Projec t 87
Module III
honest_ratings
honest_ratings.sort()
honest_ratings
11/4/2024 Introduc tion Python Using Capstone Projec t 88
Module III
honest_ratings
honest_ratings.sort()
honest_ratings
honest_ratings
11/4/2024 Introduc tion Python Using Capstone Projec t 89
Module III
10/28/2024 Introduc tion Python Using Capstone Projec t 90
Module III
Making Functions
11/1/2024 Introduc tion Python Using Capstone Projec t 91
Module III
Making Functions
11/1/2024 Introduc tion Python Using Capstone Projec t 92
Module III
10/28/2024 Introduc tion Python Using Capstone Projec t 93
Module III
11/4/2024 Introduc tion Python Using Capstone Projec t 94
Module III
2* “Humble Disciplined”
“Humble Disciplined Humble Disciplined”
Mult(2, “Humble Disciplined”)
“Humble Disciplined Humble Disciplined”
11/4/2024 Introduc tion Python Using Capstone Projec t 95
Module III
10/28/2024 Introduc tion Python Using Capstone Projec t 96
Module III
10/28/2024 Introduc tion Python Using Capstone Projec t 97
Module III
11/4/2024 Introduc tion Python Using Capstone Projec t 98
Module III
print(“honest”, i, “rating is”, s)
honest_ratings
printStuff (honest_ratings)
11/4/2024 Introduc tion Python Using Capstone Projec t 99
Module III
“Student”,
honest_ratings
printStuff(honest_ratings)
Student
Student
Student
11/4/2024 Introduc tion Python Using Capstone Projec t 100
Module III
def studentNames
11/4/2024 Introduc tion Python Using Capstone Projec t 101
Module III
10/28/2024 Introduc tion Python Using Capstone Projec t 102
Module III
10/28/2024 Introduc tion Python Using Capstone Projec t 103
Module III
10/28/2024 Introduc tion Python Using Capstone Projec t 104
Module III
10/28/2024 Introduc tion Python Using Capstone Projec t 105
Module III
10/28/2024 Introduc tion Python Using Capstone Projec t 106
Module III
10/28/2024 Introduc tion Python Using Capstone Projec t 107
Module III
10/28/2024 Introduc tion Python Using Capstone Projec t 108
Module III
11/4/2024 Introduc tion Python Using Capstone Projec t 109
Module III
Variables inside the “Global Scope” can have the same
name as variables inside the “Local Scope”.
10/28/2024 Introduc tion Python Using Capstone Projec t 110
Module III
10/28/2024 Introduc tion Python Using Capstone Projec t 111
Module III
If the variable is not defined in the
function Python will check in
“Global Scope”.
10/28/2024 Introduc tion Python Using Capstone Projec t 112
Module III
10/28/2024 Introduc tion Python Using Capstone Projec t 113
Module III
Lambda Functions
10/28/2024 Introduc tion Python Using Capstone Projec t 114
Module III
Lambda Functions
▪ Lambda Functions are “anonymous functions means that the
function is without a name”.
▪ As we already know the def keyword is used to define a normal
function in Python.
▪ Similarly, the lambda keyword is used to define an anonymous
function in Python.
▪ Syntax: lambda arguments : expression
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 115
Module III
Lambda Functions
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 116
Module III
Lambda Functions
▪ This function can have any number of arguments but only one
expression, which is evaluated and returned.
▪ One is free to use lambda functions wherever function objects
are required.
▪ You need to keep in your knowledge that lambda functions are
syntactically restricted to a single expression.
▪ It has various uses in particular fields of programming, besides
other types of expressions in functions.
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 117
Module III
Lambda Functions
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 118
Module III
Lambda Functions
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 119
Module III
Lambda Functions
More Examples
▪ In the example, we defined a lambda function(upper) to convert a
string to its upper case using upper().
▪ This code defines a lambda function named upper that takes a
string as its argument and converts it to uppercase using the
upper() method.
▪ It then applies this lambda function to the string ‘Python Developers’
and prints the result
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 120
Module III
Lambda Functions
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 121
Module III
Lambda Functions
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 122
Module III
Lambda Functions
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 123
Module III
Difference Between lambda functions and
def defined function
▪ The code defines a cube function using both the ‘def' keyword and a
lambda function.
▪ It calculates the cube of a given number (5 in this case) using both
approaches and prints the results.
▪ The output is 125 for both the ‘def' and lambda functions,
demonstrating that they achieve the same cube calculation.
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 124
Module III
Lambda Functions
10/28/2024 Introduc tion Python Using Capstone Projec t 125
Module III
Lambda Functions
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 126
Module III
Lambda Functions
1 0 /2 8 /2 0 2 4 Intro d uctio n P ytho n Using Cap sto ne P ro j ect 127
Exercise: Coding Snippet
What is the output of the below program?
def add(a, b):
return a + b
def prod(a, b):
return a * b
int aplusb = add(5,6)
int amulsb = prod(5,6)
print(add(2,2)== prod(2,2))