Introduction to OOP
Introduction to OOP
As programs become larger, code can become messy.
You may have many variables, many functions, and repeated code.
Object-Oriented Programming helps organize programs better.
OOP uses classes and objects.
What Is OOP?
OOP stands for Object-Oriented Programming.
It is a way of writing code by grouping related data and actions together.
Real World Example
Think about a car.
A car has:
- data: color, brand, speed
- actions: start, stop, move
In OOP:
- data = attributes
- actions = methods
Why OOP Matters
OOP helps you:
- organize code
- reuse code
- avoid repetition
- model real things
- build bigger programs
Without OOP
car_name = "Toyota"
car_color = "Blue"
def start_car():
print("Car started")
start_car()
This works, but related data is separate.
With OOP Idea
class Car:
pass
A class is a blueprint.
It describes what objects will look like.
What Is an Object?
An object is a real item created from a class.
Example:
- Class = Car blueprint
- Object = one real car
Real Examples
- Class: Student → Object: Tom
- Class: Product → Object: Laptop
- Class: User → Object: admin user
First Simple Class
class Dog:
pass
This creates a class named Dog.
Create an Object
class Dog:
pass
pet = Dog()
print(pet)
Output Example
<__main__.Dog object at ...>
The memory address may be different on your computer.
Why This Is Useful
Now you can create many dogs from one class.
Example
dog1 = Dog()
dog2 = Dog()
print(dog1)
print(dog2)
Each object is separate.
Code Along
Create a class called Book.
Create one object named my_book.
Print it.
Mini Challenge
Create a class called Phone.
Steps:
-
Create the class
-
Create two objects:
phone1phone2
-
Print both objects
Expected output:
<__main__.Phone object at ...>
<__main__.Phone object at ...>
Real World Use Case
Apps use classes for users, products, messages, orders, vehicles, and game characters.
Quiz
- What does OOP stand for?
- What is a class?
- What is an object?
- Why is OOP useful?
Assignment
Create a class called Student and make three student objects.
Summary
You learned that OOP organizes code using classes and objects, making programs cleaner and easier to manage.