Python Notes
Python Notes
Advantages of Functions:-
1. Once defined, Python functions can be called multiple times
and from any location in a program.
2. Our Python program can be broken up into numerous, easy-
to-follow functions if it is significant.
3. The ability to return as many outputs as we want using a
variety of arguments is one of Python's most significant
achievements.
Syntax:-
def function_name(parameters):
# code block
Function Calling:
• To call a function, simply write that function's
name followed by parenthesis(), and by placing
required arguments within the parenthesis.
• Eg
def greet():
print("Hello, world!")
greet()
# Output: Hello, world!
Function Arguments and Parameter:
Parameters:
• Parameters are variables that are used in the
function definition.
• They act as placeholders for the values that will be
passed to the function when it is called.
• Parameters are specified in the function signature.
Arguments:
• Arguments are the actual values or expressions
that are passed to a function when it is called.
• They are the values assigned to the parameters
during the function call.
• Arguments are specified in the function call.
Function Arguments and Parameter:
• Eg
def check_even_odd(num):
if num % 2 == 0:
print(num, "is even")
else:
print(num, "is odd")
check_even_odd(15)
check_even_odd(20)
#15 is odd
#20 is even
Return Statement:
• The keyword ‘return’ is used to return value.
• The ‘return’ statement in Python is used in a function to
send a value back to the caller.
• It is not mandatory to have a return statement in every
function, but if return statement is used then it must be
the last statement of Python function.
• Because a function is considered as terminated when
return statement appears in a function.
• Eg def add_numbers(x, y):
result = x + y
return result
sum_result = add_numbers(3, 5)
print(sum_result) # Output: 8
Return multiple values:
• functions can return multiple values also. It is done by
same return statement but followed by values to be
returned separated by comma (,).
• Eg def function():
a=10
b=10
return a,b
x = function()
print("Output multiple values: ", x)
print("Output type: ",type(x))
Local Variables:
• Variables defined inside a function have local
scope.
• They are accessible only within the function where
they are defined.
• They are created when the function is called and
destroyed when the function exits.
Scope of Variables:
• Eg
def my_function():
x = 10
print("Inside the function:", x)
my_function()
• Global Variables :
• Variables defined outside of any function or block have
global scope.
• They are accessible from any part of the code, both
inside and outside functions.
Scope of Variables:
• Eg:- z = 15
def my_function():
print(z)
my_function()
• if you want to declare a variable as global within a
function you need to use the ‘global’ keyword.
• Eg:- global_variable = 10
def my_function():
global global_variable
global_variable += 1
my_function()
print(global_variable) # Output will be 11
Scope of Variables:
• Enclosing (Non-local) Variables :
• When a function is defined inside another function, the
inner function can access variables from the outer
function.
• Eg
def outer_function():
y=5
def inner_function():
print(y)
inner_function()
outer_function() #5
Function with Default Arguments/Parameters:
1. Pass by Value:-
• When you pass an immutable object (e.g.,
numbers, strings, or tuples) to a function, a
new reference to the object is created
inside the function. Changes made to the
reference inside the function do not affect
the original object outside the function.
• A copy of that value is made and passed to
the function. Modifying the copy within the
function does not affect the original value.
Pass by Value & Pass by Reference:
• Eg def val(x):
x = 15
print(x)
x = 10
val(x) #15
print(x) #10
2. Pass by Reference:-
• When you pass a mutable object (e.g., a list,
dictionary) to a function, you are passing a
reference to that object. Changes made to the
object inside the function will affect the original
object outside the function.
• A reference or pointer to the original object is
passed to the function. As a result, any
modifications made within the function directly
impact the original object itself.
Pass by Value & Pass by Reference:
• Eg def val(I):
I.append(4)
print(I)
lst = [1, 2, 3]
print(lst) #[1, 2, 3]
val(lst) #[1, 2, 3, 4]
print(lst, ) #[1, 2, 3, 4]
• Mathematical Function:-
Python Modules:
result_addition = mymodule.add(5, 3)
result_subtraction = mymodule.subtr(10, 4)
result_multiplication = mymodule.mult(6, 7)
result_division = mymodule.div(20, 5)
import mymodule as m
result_addition = m.add(5, 3)
print("Result of addition:", result_addition)
from...import Statement:
• We can import specific attribute/member
from a module without importing the
module as a whole.
• Eg from mymodule import add, subtr
result = add(2,5)
print(result)
print(subtr(5,3))
#Output
#7
#2
Import everything using *
• To import all the objects from a module within
the present namespace, use the * symbol and
the from and import keyword.
• Eg from mymodule import *
result = add(2,5)
print(result)
print(subtr(5,3))
print(div(30,3))
#Output
#7
#2
#10.0
Importing Multiple Modules:
2. Map Function:
• The ‘map()’ function in Python is a built-in
function used to apply a given function to
each item of an iterable (like a list, tuple, or
dictionary) and return a map object, which
is an iterator.
• Syntax: map(function, iterable)
Functional Programming Module:
3. Filter Function:
• The ‘filter()’ function in Python is another
built-in function that allows you to filter
elements from an iterable (like a list, tuple,
or dictionary) based on a specified function
and returns an iterator of the elements for
which the function returns ‘true’.
• Syntax: filter(function, iterable)
Functional Programming Module:
4. Reduce Function:
• The ‘reduce’ function in Python is used to apply a
function to a sequence (such as a list) cumulatively
to reduce the sequence to a single value.
• It is part of the ‘functools’ module in Python 3 and
above and must be imported before use.
• Syntax: from functools import reduce
result = reduce(function, sequence)
Functional Programming Module:
2. Global Namespace:
This namespace includes names from various
imported modules that are accessible throughout the
module. It's created when the module is imported
and lasts until the script ends.
3. Built-in Namespace:
This namespace includes built-in functions,
exceptions, and attributes provided by Python itself.
It's automatically available in all Python scripts.
Python Packages: