0% found this document useful (0 votes)
2 views10 pages

PWP Assignment 5

The document explains fundamental concepts of Python programming, including classes, objects, data hiding, data abstraction, inheritance, method overloading, and overriding. It provides syntax examples for defining classes, using default constructors, and demonstrates the creation of a Student and Employee class with methods for reading and printing information. Additionally, it discusses the format() method for string formatting with examples.

Uploaded by

st5617067
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

PWP Assignment 5

The document explains fundamental concepts of Python programming, including classes, objects, data hiding, data abstraction, inheritance, method overloading, and overriding. It provides syntax examples for defining classes, using default constructors, and demonstrates the creation of a Student and Employee class with methods for reading and printing information. Additionally, it discusses the format() method for string formatting with examples.

Uploaded by

st5617067
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Define class and object in python

Class: A class is a user-defined blueprint or prototype from which


objects are created. Classes provide a means of bundling data
and functionality together.

Object: An object is an instance of a class that has Some


attributes and behavior. Objects can be used to access the
attributes of the class.
e) Write syntax of defining class in Python.
Ans class <ClassName>:
<statement l >
<statement2>

<statemnentN>
With neat example explain default constructor concept in Python.
The default constructor is simple constructor which does not accept any arguments. It's
definition has only one argument which is a reference to the instance being constructed.
Example 1: Display Hello message using default constructor.
class Student:

def init_ _(self):

print("This is non parametrized constructor")


def show(self,name):
print("Hello",name)

sl = Student()

sl.show("Student1")

Output:
This is non parametrized constructor
Hello Student1
Define Data Hiding concept? Write two advantages of Data Hiding.
ns Data hiding is a concept which underlines the hiding of data or information from the
user.

Data hiding is a software development technique specifically used in Object-Oriented


Programming (0OP) to hide internal object details (data members).
Data hiding includes a process of combining the data and functions into a single unit
to conceal data within a class by restricting direct access to the data from outside the
class.
Data Abstraction in Python
Data Abstraction is one of the key concepts of Object-Oriented Programming (0OP) that hides the
internal implementation details of an object and only shows the essential features to the user.
c) With suitable example explain inheritance in Python.
Ans In inheritance objects of one class procure the properties of objects of another class.
Inheritance provide code usability, which means that some of the new features can be
added to the code while using the existing code. The mechanism of designing or
constructing classes from other classes is called inheritance.
" The new class is called derived class or child class and the class from which this
derived class has been inherited is the base class or parent class.
" In inheritance, the child class acquires the properties and can access all the data
members and functions defined in the parent class. A child class can also provide its
specific implementation to the functions of the parent class.
Syntax:
class A:

# properties of class A

class B(A):
# class B inheriting property of class A
# more properties of class B

Page

MAHARASHTRA STATE B0ARD OF TECHNICAL EDUCATION


(Autonomous)
(ISO/TEC - 27001 - 2013 Certified)

Example:
# Base class

class Animal:

def init_ (self, name):


self.name = name

def speak(self):
print("Animal speaks")

# Derived class inheriting from Animal


class Dog(Animal):
def speak(self):
print("Dog barks")

# Derived class inheriting from Animal


class Cat(Animal):
def speak(self):
print("Cat meows")

# Create instances of derived classes

dog = Dog("Buddy")
cat = Cat("Whiskers")

# Call the speak method of the derived classes


dog.speak() # Output: Dog barks
cat.speak() # Output: Cat meows
b) Design a class student with data members : name, roll no., department,
mobile no. Create suitable methods for reading and printing student
information.

Ans class Student:

def_init_(self):
self.name = ""

self.roll no = ""

self.department ="n
self.mobile_no =

def read_student_info(self):
self.name = input("Enter student name: ")

self.roll_no = input("Enter roll number: ")


self.department = input("Enter department: ")
self.mobile_no = input("Enter mobile number: ")

def print_student_info(self):
print("Student Information:")
print("Name:", self.name)
print("RollNumber:", self.roll_no)
print("Department:", self.department)
print("Mobile Number:", self.mnobile_no)

# Create an instance of the Student class

student = Student()

Page

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION


(Autonomous)
(ISOMEC- 27001 - 2013 Certiffed)

# Read and set student information

student.read_student_info()

# Print student information

student.print_student_info)
output:
Enter student name: raj
Enter roll number: 11

Enter department: computer


Enter mobile number: 123456

Student Information:

Name: raj

Roll Number: 11

Department: computer
Mobile Number: 123456
d) Explain method overloading and overriding in python.
Ans Method Overloading: Method overloading is the ability to define the method with the same
name but with a different number of arguments and data types. With this ability one method
can perform different tasks, depending on the number of arguments or the types of the
arguments given. Method overloading is a concept in which a method in a class performs
operations according to the parameters passed to it. Python does not support method
overloading, that is, it is not possible to define more than one method with the same name in
a class in Python. This is because method arguments in python do not have a type. A method
accepting one argument can be called with an integer value, a string or a double as shown in
next example.
class Demo:
def method(self, a):
print(a)
obj= Demo( )
obj.method(50)
obj.method('Meenakshi')
obj.method(100.2)
Output:
50
Meenakshi
100.2
d) Write a program to create class EMPLOYEE with ID and NAME
and display its contents.
class employee:
id=0
name=n

def getdata(self,id, name):


self.id=id
self.name=name
def showdata(self):
print("ID:", self.id)
print("Name :", self. name)

e= employee()
e.getdata(11, "Vijay")
e.showdata()

Output:
ID:11
Name : Vijay
c) Explain use of format() method with example
The format() method formats the specified value(s) and insert
them inside the string's placeholder.
The placeholder is defined using curly brackets: ().
The format() method returns the formatted string.
Syntax
string.format (valuel, value2...)
Example:
#named indexes:
>>>txt1 = ("My name is {fname), I'm {age'".format(fname =
"abc", age =36))
>>>print(txt1)
My name is abc, I'm 36

#numbered indexes:
>>>txt2 =( "My name is {0}, I'm {1}".format("xyz",36))
>>>print(txt2)
My name is xyz, I'm 36

#empty placeholders:
>>>txt3 = ("My name is {}, I'm {".format("pgr",36))
>>>print(txt3)
My name is pgr, I'm 36

You might also like