Python Module 5 Crisp Notes
Python Module 5 Crisp Notes
Table of contents
Module 5
1. What is class? How to define class in python? How to initiate a class and how
the class members are accessed?
* A class is programmer defined data type which binds data and functions
together into a single entity
* The process of creating a new object is called as instantiation and the object is
called as an instance of a class
* Class members that is the attributes and methods can be accessed using the
object of a class
Syntax:
objectname.attributename
objectname.methodname()
2. Define class and object? What are programmer defined types? Explain with
examples?
* Class is user defined data type which binds data and function together into a
single entity. A class can have a set of variables also known as attributes and
member function also known as methods.
*programmer defined types ie. a class in python can be created using a keyword
class. Here, we are creating an empty class without any members by just using
the keyword pass within it
class point :
pass
print(point)
output
<class __ main __ point>
The term __ main __ indicates that the class
point is in the main scope of the current module
The process of the creating a new object is called as instantiation and the object
is instance of a class
point (p)
< __ main __ point object at 0x003C1BF0
3) Develop a program that uses class student which prompts the user to enter the
marks in three subjects, calculates total marks, percentage and displays the
details.
class Student:
def __init__(self, name = " ", usn = " ", marks = [0,0,0,0]):
self.name = name
self.usn = usn
self.marks = marks
def getMarks(self):
self.name = input(" Enter the student name :")
self.usn = input(" Enter the usn name :")
self.marks[0] = int(input(" Enter the marks in subject 1 :"))
self.marks[1] = int(input(" Enter the marks in subject 2 :"))
self.marks[2] = int(input(" Enter the marks in subject 3 :"))
self.marks[3] = self.marks[0] + self.marks[1] + self.marks[2]
def display(self):
print(self.name)
print(self.usn)
print(self.marks[0:3])
print("Total marks is :", self.marks[3])
Return sum
t1=time()
t1.hour=10
t1.minute=1
t1.second=14
t2=time()
t2.hour=2
t2.minute=4
t2.second=6
t3=time()
t3=add_time(t1,t2)
In the above example we have a class time and function add_time(t1,t2) it takes
two objects t1 and t2 adds the time (hour, min, sec) and prints without
modifying the original value of t1and t2.
def __str__(self):
return ‘%.2d:%.2d:%.2d’%(self.hour,self.minute,self.second)
>> print(time)
o/p 09:45:00
Here the function add is used to add the numbers. When the function is called
with the name and passing two arguments it will add two numbers and returns
the sum. The function is used to add three numbers also.
b) Addition operator
num1=1
num2=2
print(num1+num2)
o/p=3
str1=”python “
str2=”programming “
print(str1+” ” str2)
o/p=python programming
here we can see that single operators “ t” is used to carry addition operation on
integer data type and string data type (string concatenation)so the “+” operator
has different forms which is an occurrence of polymorphism in python
Question bank
1) What is class? How to define class in python? How to initiate a class and
how the class members are accessed explain with examples?
2) Explain __init__ and __str__ method with an example.
4) Define classes and objects in python. Create a class called Employee and
initialize with employee id and name. Design method to: