Assignments-Online-Python-Saylani - Assignment-06.ipynb at Master Idreesmath - Assignments-Online-Python-Saylani PDF
Assignments-Online-Python-Saylani - Assignment-06.ipynb at Master Idreesmath - Assignments-Online-Python-Saylani PDF
Email: [email protected]
Question1:
Question2:
Question3:
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:
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.
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_5.full_name()
car_1.read_odometer()
car_3.full_name()
car_3.update_odometer(100)