Week7(Functions & Modules) python computer
Week7(Functions & Modules) python computer
Information Technology
3 12/28/2024
Some Examples of Built-in Functions
4 12/28/2024
Some Examples of Built-in Math Functions
5 12/28/2024
Why Functions?
Creating a new function gives you an opportunity
to name a group of statements, which makes your
program easier to read and debug.
Functions can make a program smaller by
eliminating repetitive code. Later, if you make a
change, you only need to make it in one place.
Well-designed functions are often useful for many
programs. Once you write and debug one, you can
reuse it.
6 12/28/2024
Function Syntax
7 12/28/2024
Creating/defining a Function
8 12/28/2024
Number of Arguments
By default, a function must be called with the correct number of
arguments. Meaning that if your function expects 2 arguments, you
must call the function with 2 arguments, not more, and not less.
If you try to call the function with 1 or 3 arguments, you will get an
error.
9 12/28/2024
Default Argument/Parameter Value
The following example shows how to use a default
parameter value.
If we call the function without argument, it uses
10 12/28/2024
Fruitful Functions and Void Functions
Some functions, such as the math functions,
11 12/28/2024
Void Function
Defining a
function
Calling a
function
Avoid having a variable and a function with the
same name
Empty parentheses after the name indicates
12 12/28/2024
Fruitful Function
13 12/28/2024
The return Statement
14 12/28/2024
Example of return (Absolute Value)
15 12/28/2024
Return Multiple Values
16 12/28/2024
How Function works in Python
17 12/28/2024
Class Tasks
Write a function named “average()” that takes three integers as input and returns the
average of the three numbers.
Write a function named “area_circle()” that takes the radius of a circle as input and
returns the area of the circle.
Write a function named “slope()” that takes the X and Y coordinates of two points on a
line as input and returns the slope of the line: 𝑦2 − 𝑦1 = 𝑚 ( 𝑥2 − 𝑥1 ).
Write a function named “isEven()” that takes a positive integer as input and returns 1 if
the number is even and returns 0 if the number is odd
Write a function named “factorial()” that takes a positive integer as input and returns
the
factorial of the input number.
18 12/28/2024
Scope and Lifetime of Variables
Global and nonlocal keywords
Try to limit the global variables in program, global
variables can be accessed and modified anywhere,
and this may result in unexpected behavior’s
Variables declared within a function are scoped
only to that function – called Local Variables
Local Variables are not accessible/ visible outside
of that function
100
101
101
24 12/28/2024
Modules Help
After importing a module, can see what functions
25 12/28/2024
Modules (Online Help)
26 12/28/2024
Some of Frequently Used built-in Modules
io
random
string
27 12/28/2024
User Created Module
28 12/28/2024
Importing and Using of Modules
Multiple Modules can be imported in single line
30 12/28/2024
Hiding Some Elements of a Module
Any element in a module starts with an underbar
31 12/28/2024
Hiding Some Elements of a Module
However, if we explicitly import the function then
32 12/28/2024
Lambda Function
A lambda function is an anonymous
function (i.e., defined without a name)
that can take any number of arguments
But, unlike normal functions, evaluates
and returns only one expression.
Syntax
lambda parameters : expression
33 12/28/2024
Examples
1. Add 10 to argument a, and return the result:
x = lambda a : a + 10
print(x(5))
2. Multiply argument a with argument b and return the result:
x = lambda a, b : a * b
print(x(5, 6))
nest them:
(lambda x: x * 10 if x > 10 else (x * 5 if x < 5 else x))(11)
35 12/28/2024