ICT01-Course Outcome 6 - Module 4
ICT01-Course Outcome 6 - Module 4
Functions, Tuples,
Dictionaries, Exceptions,
Data Processing
Official Business
Learning Objectives
By the end of this session, you will be able to:
• Create, use, and call your own functions.
• Learn about parameterless and parameterized functions
• Learn the return expression and the None value.
• Learn about scopes in Python and the global keyword.
• Analyze some multi-parameter functions
• Learn about sequence types and the concept of mutability.
• Learn about tuples and dictionaries.
• Know how to prevent program termination.
• Know tips on testing and debugging codes.
Official Business
Functions
• A function is a block of code
that performs a specific task
when the function is called
(invoked).
• You can use functions to make
your code reusable, better
organized, and more readable.
• Functions can have parameters
and return values.
Official Business
• Lambda Functions:
These are small, anonymous functions defined using the
lambda keyword. They are often used for short, simple
operations, especially when a full function definition is
unnecessary.
Official Business
Defining a Function
• You can define your own function using the def
keyword and the following syntax:
Examples of Defining a
Function
• define a function which doesn't take any arguments
Parameter Function
• parameters exist only inside functions in which they
have been defined, and the only place where the
parameter can be defined is a space between a pair of
parentheses in the def statement
Parameter Function
One Two
Parameter Parameter
Three
Parameters
Official Business
• positional argument passing: the order of arguments passed
matters
Example:
None in Python
• It is commonly used to signal that a variable has no
value or a function doesn't return anything.
• There are only two kinds of circumstances when None
can be safely used:
− when you assign it to a variable (or return it as a function's
result)
− when you compare it with a variable to diagnose its internal
state.
Official Business
Results:
Official Business
The global
keyword
• The global keyword allows a
function to modify a variable
that is defined outside of its
local scope, essentially
extending the variable's scope
to include the function.
• Without global, any assignment
inside a function creates a local
variable with the same name,
leaving the global variable
unchanged.
Official Business
Creating multi-parameter
functions
Evaluating the BMI
• BMI(Body Mass Index): Measures body weight in relation to height.
• convert imperial units to metric ones.
Official Business
Creating multi-
parameter functions
• Triangles: Validating
Triangle Sides
-Check if three sides can
form a triangle.
-The sum of any two
sides must be greater
than the third side.
Official Business
• Triangle's area
• Heron's formula:
𝐴=
𝑠 𝑠 − 𝑎 𝑠 − 𝑏 (𝑠 − 𝑐)
𝑎+𝑏+𝑐
wherein 𝑠 =
2
• Factorials
- 𝑛! = 1×2×⋯×n
- Uses for loop to find the
product
Official Business
Creating multi-parameter
functions
Fibonnaci
• They are a sequence of integer numbers built
using a very simple rule:
• the first element of the sequence is
equal to one
• the second is also equal to one
• every subsequent number is the sum
of the two preceding numbers
Official Business
Recursion
▪ A function that calls
itself.
▪ Cleaner, more elegant
code.
▪ Breaks complex
problems into smaller
parts.
▪ Recursion is powerful
but memory-intensive.
▪ Commonly used in:
• Factorials
• Fibonacci sequence
Official Business
Dictionaries
•Dictionaries: Mutable, unordered collections of key-value
pairs.
•Keys must be unique and immutable, values can be of
any type.
Official Business