Assignment # 6 (Solution)
Enrolment No. SMIT/19/PY/B2/02738
Roll No. PY02738
Name: MUHAMMAD IDREES
Email: [email protected]
Question1:
Define Object Oriented Programming Language?
The concept of Object-Oriented Programming is common in almost all popular programming
languages (Python, C++, Java) with common idea but different syntax. Object-Oriented Programming
is just a style/way to write programs and manage the code in an easy way and most effective
approaches to writing software as well as it is very helpful in creating real world projects.
Question2:
List down the Benefits of OOP?
Some Benefits of OOP are
1. The complexity of code can be managed easily.
2. It is very helpful in creating real world projects.
3. Easy to understand the complex code.
4. Reusability of code in a project.
5. Through inheritance, we can eliminate code and extend the use of existing classes.</b>
Question3:
Differentiate between function and method?
A function is a block of code to carry out a specific task, will contain its own scope and is called by
name. While a method in python is somewhat similar to a function, except it is associated with
object/classes.
Question4:
Define the following terms:
1. Class
2. Object
3. Attribute
4. Behavior
1. Class: In object-oriented programming, a class is a blueprint for creating objects (a data structure),
providing initial values for state (member variables or attributes), and implementations of behavior
(member functions or methods). For Example, the list in python is blueprint of creating list objects.
2. Object: In object-oriented programming (OOP), objects are the things you think about first in
designing a program, each object is made into a generic class of object. Each object is an instance of
a class or subclass with the class's own methods or procedures and data variables. For Example,
from list class python we can create objects like l = [1,2,3,4], l = [‘a’, ‘b’, ‘c’, TRUE]
3. Attribute: The attributes are data members (class variables and instance variables) and methods,
accessed via dot notation.
4. Behavior: A class's behavior determines how an instance of that class operates.
Question5:
Write a code in python in which create a class named it Car which have 5 attributes such like (model, color
and name etc.) and 3 methods. And create 5 object instance from that class.
In [26]: class Car():
def __init__(self,model,color,name,year,price):
self.model = model
self.color = color
self.name = name
self.year = year
self.price = price
self.odometer_reading = 0
def full_name(self):
print(str(self.year) + " " + self.name + " " + self.model.upper())
def read_odometer(self):
print("This car has" + " " + str(self.odometer_reading) + " miles
on it")
def update_odometer(self,miles):
self.odometer_reading = miles
print("The car is running " + str(miles) + " miles")
car_1 = Car("VX4", "White", "Suzuki", 2009, 1200000)
car_2 = Car("2-OD", "Gray", "Toyota", 2015, 24000000)
car_3 = Car("Blaze", "Black", "Vitz", 2016, 19000000)
car_4 = Car("Vagon-R", "Blue", "Honda", 2013, 2200000)
car_5 = Car("Royal Speed", "Red", "BMW", 2018, 250000000)
car_5.full_name()
car_1.read_odometer()
car_3.full_name()
car_3.update_odometer(100)
2018 BMW ROYAL SPEED
This car has 0 miles on it
2016 Vitz BLAZE
The car is running 100 miles