Programming With Python I Notes2
Programming With Python I Notes2
Maya Nair
A function is a set of statements that take inputs, do some
specific computation and produces output.
Python
Types of functions:
Functions
• Built-in functions- pre-defined functions available with various
modules in python
• User defined functions – user-specific function defined by the
user
Python e.g.
Functions- def personal_details(name,phoneno,age=None):
Default if age==None:
arguments print(f"Age not provided by {name}")
else:
print(f"Age of {name} is {age}")
Output:
personal_details("Suresh",895653883) Age not provided by Suresh
Age of Ramesh is 29
personal_details("Ramesh",767767623,29)
16
Python Classes and Objects: Python is an object oriented language where every
variable you create is an object of a particular class. In fact, everything is a class
in Python.
E.g
>>> a=1.3
Python >>> type(a)
Object
<class 'float'>
Oriented
>>> l=[1,2,3]
Concepts
>>> type(l)
<class 'list'>
17
Syntax for creating a class:
All methods have an instance
class class_name: reference parameter ’ self ’ in
variables addition to other parameters
methods
Python e.g.:
Object Initialiser method which
Oriented class person: is automatically called
when an object of the
Concepts def __init__(self,name,age): class is created
self.name=name
self.age=age
def display(self):
print(f'Name is {self.name} and age is {self.age}’)
18
Syntax for creating objects :
Object_name=class_name(parameters to initializer if any)
Python e.g.
Object person1=Person(“Andrews”,35)
Oriented
person2=Person(“Maria”,25)
Concepts
person1.display()
Person2.display()
Output:
Name is Andrews and age is 35
Name is Maria and age is 25
19
• Write a program to create a class Department with instance variables
Dept_id, Dept_name and Manager. Apart from constructor two methods
have to be defined namely
• display() –displays all the information about the Department.
• updatemanager()-changes manager as per parameter.
Python self.__name=newname
a1=A()
Object Output:
a2=A("ABC")
Oriented XYZ
a1.setName("XYZ")
PQR
Concept print(a1.getName())
AttributeError: 'A' object has no
a2.setName("PQR")
s print(a2.getName()) attribute '__name'
print(a1.__name)# here name can not be accessed
#outside the class without
#getter/setter function
#print(a1._A__name)# But we can access the private variable by using
# the notation _classname__variablename 24
Concepts
Subclass Subclass
Manager(empcode,department,no_of_emp) Typist(empcode,department,typingspeed)
25
Syntax for inheritance
class subclass_name(superclass_name):
additional attributes
additional methods
e.g.
class Employee(object):
def __init__(self,empcode,department):
self.empcode=empcode
self.department=department
def display(self):
Oriented Employee.__init__(self,empcode,department)
Output:
self.no_of_emp=no_of_emp
Concepts def display(self): Emp code :Emp001 ,Department :IT
Employee.display(self) Number of Employees managed 30
print(f" Number of Employees managed {self.no_of_emp}")
manager1=Manager("Emp001","IT",30)
manager1.display() 26
Filter() and Map()functions
The filter() function in Python takes in a function and a list as arguments.
This offers an elegant way to filter out all the elements of a sequence
“sequence”, for which the function returns True.
e.g.
List List1=[1,2,3,4,5,6,7,8,9,10]
Manipulation even_list=list( filter( ( lambda x:x%2==0),list1) )
and >>> even_list
Comprehension
[2, 4, 6, 8, 10]
>>> odd_list=list( filter( ( lambda x:x%2!=0),list1) )
>>> odd_list Let Marks=[45,90,25,50,85,35] be a list of marks obtained by
certain students. Write Filter function to create a list of students
[1, 3, 5, 7, 9] with marks>40.
Advanced Python Session 2020 Maya Nair 27
The map() function in Python takes in a function and a list as argument. The
function is called with a lambda function and a list and a new list is returned
which contains all the lambda modified items returned by that function for
List each item.
Manipulation
e.g.
and
Comprehension list1=[1,2,3,4,5,6,7,8,9,10]
power_list=list(map(lambda x: x**2,list1))
>>> power_list
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Rather than filtering and mapping lists using filter() and reduce() functions with the help of lamda functions ,
Python provides a very compact method called as list comprehensions.
Syntax:
output_list = [output_exp for var in input _list if (var satisfies this condition)]
List #list comprehension may or may not contain an if condition. List
Manipulati #comprehensions can contain multiple for (nested list
on and #comprehensions
Comprehen list1=[1,2,3,4,5,6,7,8,9,10]