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

Classes and Objects in Python

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

Classes and Objects in Python

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy