Python Pioneers: A Beginner's Guide πŸ‡ͺπŸ‡Ή
8.63K subscribers
346 photos
40 videos
24 files
64 links
Welcome to Python Pioneers: Empowering Ethiopian students to embark on their coding journey.

πŸ“’ Channel: @python_pioneers
πŸ‘₯ Group: @python_pioneers_chat
Download Telegram
OOP in Python

Object Oriented Programming (OOP) just means we organize code using objects that have Data and Actions. Data is something we know about the object, and Actions are things we can do to the object or with the object.

For example, when looking at a computer, we may find ourselves asking the brand, model and specs of a computer, these are some of the data(information) we can have about a computer. Also we can do things on a computer; power it On & Off, Open & Close apps, adjust the volume - these are some of the action we can take on a computer.

Note: In the context of programming we usually say Attribute or Property instead of Data and Method instead of Action.
❀3
...continued

To do OOP in python we use classes. Classes define the shape (attributes and methods) of an object. Think of classes like a blueprint for creating objects. Next we use classes to create objects.

It's a simple two step process:-
β€’ Modeling:- defining a class (shape).
β€’ Instantiating:- creating objects (instances) using class.

Syntax: to create a class we use the class keyword.
# Step 1 - Modeling
class ClassName:
attr1 = 0
attr2 = True
attr3 = "string"

def method1(self): pass
def method2(self): pass

# Step 2 - Instantiating
obj1 = ClassName()
obj2 = ClassName()


Note:

β€’ Attributes can be of any data type.
β€’ Methods are just functions attached to the object.
β€’ self is a reference to the object; obj1 or obj2 (we will talk more about them later)
...continued

To access attributes or call methods on an object we use a dot-notation., the variable followed by dot and the attribute or the method name.

Syntax: the dot-notation
# Accessing an attribute
object.attribute

# Calling a method
object.method()


Example: simplified computer object
class Computer:
# __init__ is a special method (a constructor)
def __init__(self, brand, model):
# attributes
self.brand = brand
self.model = model

# methods
def powerOn(self):
print(f"Powering on {self.brand} {self.model}")

def powerOff(self):
print(f"Powering off {self.brand} {self.model}")

c1 = Computer("HP", "OMEN")
c2 = Computer("Dell", "XPS")

# Test code
print(c2.brand + " " + c2.model)
c1.powerOn()


# Output
Dell XPS
Powering on HP OMEN


#oop #PythonProgramming #DotNotation
❀1
In Python's object-oriented programming, what are the official terms used to describe an object's data and its actions?
Anonymous Quiz
46%
Variables and Functions
14%
Fields and Actions
32%
Attributes and Methods
8%
Properties and Routines
Discover a Smarter Way to Learn & Share Knowledge

As a ScholarXiv Ambassador, I’m excited to introduce you to ScholarXiv β€” a platform built for students, researchers, academics, and lifelong learners who want to discover and share scholarly knowledge.

Whether you’re working on an assignment, research project, thesis, or simply exploring a new field, ScholarXiv can help you connect with academic resources and ideas.

πŸ”¬ Research. Learn. Share. Connect.

If you’re serious about your academic journey, take a look at ScholarXiv and see what it has to offer.

πŸ‘‰ Explore: scholarxiv.com
❀2