You will be able to
- Define Python classes and create instances with a constructor
- Write methods that use self and class, static, and instance attributes
- Validate data with type hints and assert statements
- Read CSV data and build objects from it with class methods
- Use inheritance and polymorphism to reuse and extend classes
- Protect attributes with properties and separate code into modules
Good to know first
- Basic Python variables, data types, and print statements
- Writing if statements and for loops
- Basic familiarity with running a Python file in an editor or terminal
Syllabus
14 lessons and a final project
- 1
Why OOP: modeling store items with variables Free
You will see how a store item can be tracked with several separate variables that share a naming prefix, and why that is only a convention Python does not understand. You will also learn that every value in Python is an object created from a class, which sets up the idea of making your own class.
1 code3 build steps3 exercises3 questions - 2
Defining a class and creating instances Beyond Pass
You will learn how to define a class with the class keyword, create instances (objects) from it, and attach attributes to each instance using the dot sign. You will also see how the type of a class instance differs from ordinary variables.
1 code5 build steps2 exercises3 questions - 3
Adding methods and understanding self Beyond Pass
You will learn how to write methods inside a class so every instance can use them, and why every method needs a first parameter called self. You will build a calculate_total_price method and call it on two different items.
1 code4 build steps3 exercises3 questions - 4
Constructors with __init__ and instance attributes Beyond Pass
You will learn how the special __init__ method (the constructor) runs automatically each time an object is created, and how to use it to receive values and store them as instance attributes. You will see how to replace repeated hard-coded attribute lines with one clean setup step.
1 code2 build steps3 exercises3 questions - 5
Validating input with type hints and assert Beyond Pass
You will learn how to declare the expected data types of constructor parameters with type hints, and how to reject bad values such as negative numbers with the assert statement and a clear error message. You will also see how attributes can be added to one instance without changing the others.
1 code4 build steps3 exercises3 questions - 6
Class attributes and per-instance overrides Beyond Pass
You will learn how a class attribute is shared by every instance, how Python looks up an attribute from the instance first and then the class, and how to override a value for one specific instance. You will also build an apply_discount method that uses the shared pay rate.
2 code5 build steps4 exercises4 questions - 7
Tracking all instances and a readable __repr__ Beyond Pass
You will make a class keep a list of every instance it creates, by appending self inside __init__. You will also give the class a __repr__ method so printed objects show up as readable text you could paste back into Python.
1 code4 build steps3 exercises3 questions - 8
Reading a CSV file with a class method Beyond Pass
You will store item data in a CSV file instead of inside the code, and read it with a class method that builds the objects for you. You will learn what @classmethod does, why its first parameter is cls, and how csv.DictReader turns each row into a dictionary.
2 code5 build steps3 exercises3 questions - 9
Static methods for general helper logic Beyond Pass
A static method is a helper function that lives inside a class because it relates to the class, but it does not need the instance or the class itself. You learn how to write one with the @staticmethod decorator and how to choose between a static method and a class method.
1 code3 build steps3 exercises4 questions - 10
Inheritance with a Phone child class and super() Beyond Pass
You will learn how a child class (Phone) inherits from a parent class (Item) to get its attributes and methods, and how super() lets the child constructor reuse the parent's constructor. You will also see how a class attribute list is shared through inheritance.
2 code3 build steps2 exercises3 questions - 11
Splitting code into item.py and phone.py modules Beyond Pass
You will split a growing project into separate files, one per class, so each file has one job. You will then import those classes into main.py and see why a read-only attribute protects critical data like a name.
2 code4 build steps3 exercises3 questions - 12
Read-only and writable properties with getters and setters Beyond Pass
You will learn how the @property decorator makes an attribute read-only, how to hide the stored value with a leading underscore or double underscore, and how a @name.setter lets you control when and how the value can be changed. The lesson follows the item.py class built in the video.
1 code4 build steps2 exercises3 questions - 13
Encapsulation and abstraction with private methods Beyond Pass
This lesson shows how encapsulation protects an object's attributes by making them private and changing them only through methods. It then shows how abstraction hides the internal steps of a process behind private methods, so users of a class only see what they need.
2 code3 build steps3 exercises3 questions - 14
Reusing code and polymorphism across item types Beyond Pass
Inheritance lets child classes such as Phone and Keyboard reuse the methods and attributes of the Item parent class. Polymorphism means one entity, such as a method or the built-in len() function, works correctly across different kinds of objects.
2 code4 build steps3 exercises3 questions
Final project
Build a Modular Store Inventory System
Build a small store inventory program that follows the video's Item project from start to finish. It reads products from a CSV file, models them with classes, and checks and protects the data. You will use every idea in the course: classes, validation, class methods, inheritance, properties and modules.
Its 7 tasks open with Beyond Pass.
Free preview · Lesson 1 in full
Why OOP: modeling store items with variables
You will see how a store item can be tracked with several separate variables that share a naming prefix, and why that is only a convention Python does not understand. You will also learn that every value in Python is an object created from a class, which sets up the idea of making your own class.
Type the lesson's commands, break things, and see why.
Run and change the code, with real Python and JavaScript.
Draw and write next to the video.
The idea
Imagine you run a small store and want to track an item. A simple first attempt is to create several variables for one item: item1 holds the name, item1_price holds the price, item1_quantity holds how many you have, and item1_price_total holds the price multiplied by the quantity. The shared prefix item1 is a naming convention that tells a human reader these four values belong together.
Python does not know that. To Python, these are four independent variables. If you print the type of each one, you get str for the name and int for the price, quantity and total. Nothing in the code links them together, so the relationship lives only in the names you chose.
The important detail is what the type output reveals. Each type is printed as a class, such as <class 'str'> or <class 'int'>. This means that the value 'Phone' is an instance (an object) created from the str class, and 100 is an instance of the int class. In Python, almost everything is an object built from some class.
This leads to the central question of object oriented programming. Wouldn't it be nicer if we could tell Python that an item is a data type of its own, with a name, a price and a quantity all stored together? That is exactly what a class lets you do. In the next parts of the course, you will define such a class and then create items from it.
So the chapter's lesson is: separate variables with matching names work, but they are fragile. Grouping related data under one object is cleaner, and that is the reason to write object oriented programs.
Key points
- Several variables with the same prefix (item1, item1_price, item1_quantity) only look related to a human; Python sees independent variables. 2:05
- type() shows each value's class: 'Phone' is a str, and 100, 5 and 500 are int. 2:31
- In Python every value is an object instantiated from a class. 2:35
- A class lets you define your own data type, so one item can hold its name, price and quantity together. 3:00
- Grouping related data into one object is the main reason to write object oriented code. 3:15
Words to know
- variable
- A name that points to a value, like item1_price = 100.
- naming convention
- A habit in how we name things so people can read the code. Python does not check it or act on it.
- class
- A blueprint that defines a kind of data, like str or int. Later you will make your own.
- object (instance)
- A value created from a class. 'Phone' is an object of the str class.
- type()
- A built-in function that tells you which class a value belongs to.
Code from the screen
item1 = 'Phone'
item1_price = 100
item1_quantity = 5
item1_price_total = item1_price * item1_quantity
print(type(item1)) # str
print(type(item1_price)) # int
print(type(item1_quantity)) # int
print(type(item1_price_total)) # intSave as main.py and run with: python main.py
Build along in the practice terminal
- Create a new project folder and a file called main.py in your editor. You should see: Four lines, each showing a class: str for the name and int for the three numbers.
$ python main.py <class 'str'> <class 'int'> <class 'int'> <class 'int'>
- Type the four variables for one item, each with the same prefix item1. You should see: Your file has item1, item1_price, item1_quantity and item1_price_total.
- Print the type of each variable and run the file to confirm the classes.
Break it on purpose
$ python main.py NameError: name 'item1' is not definedWhy: The variables were not created before they were used, or the names do not match exactly. Python reads the file from top to bottom, so define each variable before you print it.
Watch out
- Matching prefixes do not make Python treat variables as a group. The link exists only in your head and in the naming.
- A type printed as <class 'int'> means the value is an object of the int class, not a plain number with no type.
Practice
Using the same idea, an item named Laptop costs 250 and you have 3 of them. Calculate the price total that item1_price_total would hold, as a plain number.
Hint
Multiply the price by the quantity.
Solution
- Price total = price × quantity.
- 250 × 3 = 750.
750
Write the four variables for an item named 'Laptop' with price 250 and quantity 3. Use the prefix item1 as in the lesson. Compute item1_price_total, then print the type of each variable.
item1 = 'Laptop'
# add the other three variables here
Hint
item1_price_total is item1_price * item1_quantity. Call print(type(...)) once per variable.
Solution
- Create item1, item1_price and item1_quantity.
- Multiply price by quantity to get item1_price_total.
- Print type() of each variable. The name is a str and the three numbers are int.
item1 = 'Laptop'
item1_price = 250
item1_quantity = 3
item1_price_total = item1_price * item1_quantity
print(type(item1))
print(type(item1_price))
print(type(item1_quantity))
print(type(item1_price_total))<class 'str'> <class 'int'> <class 'int'> <class 'int'>
In your own words, explain why the variables item1, item1_price and item1_quantity are not automatically grouped together in Python.
Hint
Think about who the shared prefix helps.
Solution
- Python only sees separate variables with separate names and types.
- The shared prefix is a naming convention for people reading the code, so Python does not link them.
- A class would let us group these values into one object.
Model answer: The prefix only helps humans read the names. Python treats each variable as independent, so the grouping is not enforced. A class can bundle the name, price and quantity into one object.
On your scratchpad
Draw a box labelled Item with three smaller labels inside it: name, price, quantity. Next to it, draw the four separate variables as four loose boxes, and note how the group is only implied by the names.
Quiz
What does type(item1) print when item1 = 'Phone'?
- <class 'str'>
- <class 'int'>
- <class 'item'>
- <class 'Phone'>
Why are item1, item1_price and item1_quantity not treated as related by Python?
- Python only sees separate variables; the shared prefix is just a naming convention
- Python links every variable that starts with the same prefix
- Python requires a class before any variable can exist
- Python stores all variables with the same prefix in one list
Show the answer
A. Python only sees separate variables; the shared prefix is just a naming convention · The prefix is a convention for humans. Python sees independent variables.
2:00 watch the momentWhat does a class let you do that separate variables do not?
- Group related data, such as an item's name, price and quantity, into one object
- Make programs run faster than functions
- Avoid using variables altogether
- Remove the need for types in Python
Show the answer
A. Group related data, such as an item's name, price and quantity, into one object · A class lets you define your own data type that holds related data together.
3:00 watch the moment
Lessons 2–14 and the final project
They open with Beyond Pass: every lesson of every course, in full. One payment: $14.99 for 90 days or $29 for a year. No subscription.
Made by Beyond from the video “Object Oriented Programming with Python - Full Course for Beginners” by freeCodeCamp.org. The video plays in YouTube's own player. Watch it on YouTube · All lessons
