0% found this document useful (0 votes)
11 views

Python Lecture Week 11-12

Uploaded by

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

Python Lecture Week 11-12

Uploaded by

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

Week 11-12:

Class and Module


&
Mini Projects
Python Classes and Objects
Classes in Python

• Class: A blueprint for creating objects. Objects are


instances of a class, and each object can have
attributes (characteristics) and methods (functions
associated with the object).

• Object: An instance of a class.


Srep1: Define a Class
Step2: Create an Object
Step 3: Access Attributes
Example
Classes: A class is a blueprint for creating objects. It encapsulates data (attributes) and functions (methods)
that operate on the data. Classes support the concept of encapsulation, inheritance, and polymorphism.
self is a reference to the instance of the class and is used to access its attributes and
methods. The name "self" is a convention; you could technically use any name, but it
is widely adopted in the Python community.
Exercises

1. Create a Python class named Student with the following attributes:


name (string)
age (integer)
grade (float)
Include a method named display_info that prints the student's information.

Create two instances of the Student class and initialize them with the following
information:
Name: "Alice", Age: 20, Grade: 85.5
Name: "Bob", Age: 22, Grade: 78.2
2. Update Object Attributes
Update Alice's age to 21 and Bob's grade to 80.5. Then, display the updated
information for both students.
3. Create a Class Method
Add a class method to the Student class named is_passing that takes a grade as a
parameter and returns True if the grade is greater than or equal to 70, and False
otherwise.
4. Use Class Method

Use the is_passing method to determine if Alice and Bob are passing students. Print the
results.
• the join method is a string method that concatenates elements of an iterable (such
as a list) into a single string. It takes the form of separator.join(iterable) and
returns a string where the elements of the iterable are joined together with the
specified separator.
5. Books, including name, author (of which there may be more than one), genre and ISBN
Python Modul
es
Modules: A module is a file containing Python definitions and statements. It serves as a way to organize code
into separate files, making it more manageable. Modules can contain functions, classes, and variables.
Mini Projects

You might also like