Name : Shayan Muhammad
ID : 25867
Section : BSAI . EF
Lab Assignment
Q1: Explain the basic concepts of OOP such as the following with example in code.
1. Class
2. Object
3. Method
4. Constructor (_init_)
5. self keyword
1. A class is a blueprint for creating object.
2. Object
An object is an instance of a class. When a class is defined, no memory is allocated until an
object is created.
3. Method
A method is a function defined inside a class. It performs actions using the object's data.
4. Constructor (__init__ method)
The constructor is a special method called automatically when an object is created. It's defined
using __init__.
5. self keyword
The **self** keyword is used to refer to the current object.
Q2: Why is self necessary in method definitions? What happens if we remove self? Can the
name self be changed? Explain with an example?
In Python, self refers to the current object (instance) of the class. It is used to:
• Access attributes of the object.
• Call other methods of the same object.
Q3: Create a Python class called Student that demonstrates the use of the self parameter.
The class should have:
1. Two instance variables: name and marks.
2. A constructor method (_init_) to initialize these values.
3. A method called display_info that prints the student's name and marks.
4. A method called update_marks that updates the marks for a student.