python
python
2
FUNCTIO 3
NS
A function is a block of organized, reusable code that is
used to perform a single, related action
better modularity
def sabir(string) :
print (string)
return
Sabir(4)
Print(Sabir(3))
PASSING ARGUMENTS TO 6
In FUNCTIONS
programming, there are two ways in which arguments can be passed to functions :-
Pass by Value:
Pass by Reference:
All the changes made to the object inside the function affect its original value.
Object is mutable type, as mutable objects can be changed, the passed objects
EXAMPLES OF PASSING IMMUTABLE
7
ARGUMENTS
Value of my_list is
changed after
function call.
Address is same
9
FUNCTION ARGUMENTS
DEFAULT ARGUMENT 10
VALUES
The default value is evaluated only once.
EXAMPLE:
KEYWORD 11
ARGUMENTS
Keyword arguments are related to the function calls.
Example:
Output:
KEYWORD 12
Example 1:
ARGUMENTS
Possbile Error:
ARBITRARY ARGUMENT 13
LISTS
Variable-Length Arguments
The syntax of such arguments?
This tuple remains empty if no additional arguments are specified during
the function call.
Syntax :
def sabir( marks, *values) : This is called
print(marks) arbitrary argument
print(marks , values) and asterisk sign
is placed before
the variable name.
Sabir(23)
Sabir(23,20,2,3,4,5)
DOCUMENTATION 14
STRINGS
‘’’ Hello my name is Muhammad sabir and I am teaching the
python to the bscs-2017-2021n “””
‘.
15
Lambda Functions
# Program to show the use of 16
lambda functions
Filter ()
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(filter(lambda x: (x%2 == 0) , my_list))
print(new_list)
# Output: [4, 6, 8, 12]
17
Map ()
OOP
20
21
22
23
24
25
26
27
28
#definig the class
class Sabir: 29
def __init__(self, Name, RollNumber,Session,Subject):
self.Name=Name
self.RollNumber=RollNumber
self.Session=Session
self.Subject=Subject
Name='Sabir'
RollNumber='BSCS_17_04'
Session= '2017_2021'
Subject='Python_programming'
s1= Sabir(Name,RollNumber,Session,Subject)
s2= Sabir(Name,RollNumber,Session,Subject)
print(s1.Name)
print(s1.RollNumber)
print(s1.Session)
30
OOP Vs Functional
Programming
31