Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
22 views

Python__OOP Lab 14 (1)

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Python__OOP Lab 14 (1)

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Department of Electrical

Engineering
Riphah College of Science & Technology
Faculty of Engineering & Applied Sciences
Riphah International University, Lahore

Program: B.Sc. Electrical engineering Semester: II


Subject: CSL-212 Object Oriented Programming Date: ……………….

Experiment 14: Python Classes and Objects

OBJECTIVES:
(i) To get introduced to the concept of Classes
(ii) To use Objects and Methods of a Class

Name: ….…………………………………………………………

SAP: ……………………………………………………………

No. Lab Evaluation Scheme Obtained


Marks
1 Understanding and Ability to Conduct Experiment. (0-5)
2 Implementation and Results (0-5)
Total
No. Lab Report Scheme Obtained
Marks
1 Report Content (0-5)
2 Code/Output Presentation and Conclusion (0-5)
Total

Remarks (if any): ………………………………….

Name & Signature of faculty: …………………………………


Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering

EEDEPARTMENT

THEORY

Python Classes/Objects

Python is an object oriented programming language. Almost everything in Python is an object,


with its properties and methods. A Class is like an object constructor, or a "blueprint" for
creating objects.

Python Classes

A class is considered a blueprint of objects. We can think of the class as a sketch (prototype)
of a house. It contains all the details about the floors, doors, windows, etc. Based on these
descriptions, we build the house; the house is the object. Since many houses can be made
from the same description, we can create many objects from a class.

Define Python Class

We use the class keyword to create a class in Python. For example,

class Name:
# class definition

Here, we have created a class named Name.

class Bike:
name = ""
gear = 0

Here,

Bike - the name of the class


name/gear - variables inside the class with default values "" and 0 respectively.

Note: The variables inside a class are called attributes.

Object Oriented Programming 2nd Semester-EE RCST Lahore


Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering

EEDEPARTMENT

Python Objects

An object is called an instance of a class. Suppose Bike is a class then we can create objects
like bike1, bike2, etc from the class. Here's the syntax to create an object.

objectName = ClassName()

# create class
class Bike:
name = ""
gear = 0

# create objects of class


bike1 = Bike()

Here, bike1 is the object of the class. Now, we can use this object to access the class attributes.

Access Class Attributes Using Objects

We use the . notation to access the attributes of a class. For example,

# modify the name property


bike1.name = "Mountain Bike"

# access the gear property


bike1.gear

Example 1: Python Class and Objects

# define a class
class Bike:
name = ""
gear = 0

# create object of class


bike1 = Bike()
# access attributes and assign new values
bike1.gear = 11
bike1.name = "Mountain Bike"

print("Name:” ,bike1.name, “Gears”, bike1.gear )


Object Oriented Programming 2nd Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering

EEDEPARTMENT

Create Multiple Objects of Python Class

We can also create multiple objects from a single class. For example,

Example

# define a class
class Employee:
# define a property
employee_id = 0

# create two objects of the Employee class


employee1 = Employee()
employee2 = Employee()

# access property using employee1


employee1.employeeID = 1001
print(f"Employee ID: {employee1.employeeID}")

# access properties using employee2


employee2.employeeID = 1002
print(f"Employee ID: {employee2.employeeID}")

Python Methods

We can also define a function inside a Python class. A Python function defined inside a class
is called a method.

Example 2

# create a class # assign values to all the properties


class Room: study_room.length = 42.5
length = 0.0 study_room.breadth = 30.8
breadth = 0.0
# access method of a class
# method to calculate area study_room.calculate_area()
def calculate_area(self):
print("Area of Room =", self.length *
self.breadth)

# create object of Room class


study_room = Room()

Object Oriented Programming 2nd Semester-EE RCST Lahore


Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering

EEDEPARTMENT

In the above example, we have created a class named Room with:

Attributes: length and breadth

Method: calculate_area()

Here, we have created an object named study_room from the Room class. We then used the
object to assign values to attributes: length and breadth. Notice that we have also used the
object to call the method inside the class,

Python Constructors

Earlier we assigned a default value to a class attribute,

class Bike:
name = ""
...
# create object
bike1 = Bike()

However, we can also initialize values using the constructors. For example,

class Bike:

# constructor function
def __init__(self, name = ""):
self.name = name

bike1 = Bike("Mountain Bike")

Here, __init__() is the constructor function that is called whenever a new object of that class is
instantiated. The constructor above initializes the value of the name attribute. We have used
the self.name to refer to the name attribute of the bike1 object. If we use a constructor to
initialize values inside a class, we need to pass the corresponding value during the object
creation of the class.

Note: Here, "Mountain Bike" is passed to the name parameter of __init__().


Object Oriented Programming 2nd Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering

EEDEPARTMENT

The self Parameter

The self, parameter is a reference to the current instance of the class, and is used to access
variables that belongs to the class. It does not have to be named self, you can call it whatever
you like, but it has to be the first parameter of any function in the class:

Lab Task

Task 1
Make modification to example 1 and 2 to show your understanding.

Task 2
Create a class name semester with two methods of your choice to show semester class can be
used.

Conclusion

Object Oriented Programming 2nd Semester-EE RCST Lahore

You might also like