0% found this document useful (0 votes)
8 views12 pages

Classes and Objects in Python

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
8 views12 pages

Classes and Objects in Python

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

Classes and

Objects in Python

By: Daisy E. Robles


Introduction to Object-Oriented Programming
(OOP)

Definition:

OOP is a programming paradigm based on the concept of “objects”.

Key Concepts:

Classes, Objects, Methods, Attributes

2
What is a Class?

Definition:

A class is a blueprint for creating objects.

Syntax:

class ClassName:

# class attributes and methods

3
What is an Object?

Definition:

An object is an instance of a class.

Example:

obj = ClassName()

4
Attributes and Methods

Attributes: Variables that belong to a class.

Methods: Functions that belong to a class.

Example:

class Dog:

def__init__(self, name, age):

self.name = name

self.age = age

def bark(self):

print("Woof!")

5
The __init__ Method

Purpose: Initializes the object’s attributes.

Example:

class Person:

def __init__(self, name, age):

self.name = name

self.age = age

6
Creating Objects

Example:

p1 = Person("John", 36)

print(p1.name) # Output: John

print(p1.age) # Output: 36

7
Inheritance

Definition: A mechanism for creating a new class using details of an existing class.

Example:

class Student(Person):

def __init__(self, name, age, student_id):

super().__init__(name, age)

self.student_id = student_id

8
Polymorphism

Definition: The ability to use a common interface for multiple forms (data types).

Example:

class Cat:

def sound(self):

return "Meow"

class Dog:

def sound(self):

return "Woof"

9
Encapsulation

Definition: The bundling of data with the methods that operate on that data.

Example:

class Car:

def __init__(self, make, model):

self.__make = make

self.__model = model

def get_make(self):

return self.__make

10
Conclusion

Summary: Recap of key points about classes and objects.

Q&A: Open the floor for questions.

11
References

Sources:

https://www.w3schools.com/

https://www.geeksforgeeks.org/

12

You might also like