CTPS Module III Updated
CTPS Module III Updated
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
▪ But sometimes, there may arise a condition where you want to exit the
loop completely, skip an iteration or ignore that condition.
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.
▪ If the element is found, then you would exit from the loop without
traversing the remaining elements.
▪ 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.
▪ 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.
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
1. Built-in Functions
2. User-Defined 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
honest
honest
honest
honest
honest_ratings
(honest_ratings)
honest_ratings
(honest_ratings)
(honest_ratings)
(honest_ratings)
(honest_ratings)
(honest_ratings)
(honest_ratings)
(honest_ratings)
(honest_ratings)
honest_ratings
_honest_ratings (honest_ratings)
honest_ratings
honest_ratings
honest_rating (honest_ratings)
honest_ratings
honest_ratings
honest_ratins: (honest_ratings)
honest_ratings:
honest_ratings:
honest_ratings
honest_ratings.sort()
honest_ratings
honest_ratings
honest_ratings.sort()
honest_ratings
honest_ratings
2* “Humble Disciplined”
honest_ratings
printStuff (honest_ratings)
“Student”,
honest_ratings
printStuff(honest_ratings)
Student
Student
Student
def studentNames
Lambda Functions
Lambda Functions
Lambda Functions
▪ This function can have any number of arguments but only one
expression, which is evaluated and returned.
Lambda Functions
Lambda Functions
More Examples
▪ In the example, we defined a lambda function(upper) to convert a
string to its upper case using upper().
Lambda Functions
▪ The code defines a cube function using both the ‘def' keyword and a
lambda function.
▪ The output is 125 for both the ‘def' and lambda functions,
demonstrating that they achieve the same cube calculation.
Lambda Functions
Lambda Functions
Lambda Functions
print(add(2,2)== prod(2,2))