Lessons · Coding · beginner · freeCodeCamp.org

Object Oriented Programming with Python - Full Course for Beginners

The video “Object Oriented Programming with Python - Full Course for Beginners” by freeCodeCamp.org as 14 lessons, with the idea explained, the code from the screen, steps to try in the practice terminal, practice problems with solutions and a quiz that leads back to the moment.

14lessons
20pieces of code
40exercises
44questions
133min of video
Open the lessons →Lesson 1 is free · Beyond Pass opens all
Object Oriented Programming with Python - Full Course for Beginners, video by freeCodeCamp.org▶ 2:12:34 · freeCodeCamp.org
1234567891011121314
0:0014 lessons across the whole video2:12:34

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. 1

    Why OOP: modeling store items with variables Free

    0:00–3:123 min of video

    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. 2

    Defining a class and creating instances Beyond Pass

    3:12–6:253 min of video

    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. 3

    Adding methods and understanding self Beyond Pass

    6:25–12:256 min of video

    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. 4

    Constructors with __init__ and instance attributes Beyond Pass

    12:25–20:508 min of video

    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. 5

    Validating input with type hints and assert Beyond Pass

    20:50–31:0010 min of video

    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. 6

    Class attributes and per-instance overrides Beyond Pass

    31:00–43:4013 min of video

    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. 7

    Tracking all instances and a readable __repr__ Beyond Pass

    43:40–50:507 min of video

    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. 8

    Reading a CSV file with a class method Beyond Pass

    50:50–1:00:2010 min of video

    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. 9

    Static methods for general helper logic Beyond Pass

    1:00:20–1:13:3013 min of video

    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. 10

    Inheritance with a Phone child class and super() Beyond Pass

    1:13:30–1:30:0017 min of video

    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. 11

    Splitting code into item.py and phone.py modules Beyond Pass

    1:30:00–1:34:405 min of video

    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. 12

    Read-only and writable properties with getters and setters Beyond Pass

    1:34:40–1:50:4016 min of video

    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. 13

    Encapsulation and abstraction with private methods Beyond Pass

    1:50:40–2:03:2013 min of video

    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. 14

    Reusing code and polymorphism across item types Beyond Pass

    2:03:20–2:12:349 min of video

    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.

0:00 to 3:12 in the video

Practice terminal

Type the lesson's commands, break things, and see why.

Playground

Run and change the code, with real Python and JavaScript.

Scratchpad

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

main.py: tracking one item with separate variablespython 1:36 on 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)) # int

Save as main.py and run with: python main.py

Build along in the practice terminal

  1. Create a new project folder and a file called main.py in your editor.
    $ python main.py
    <class 'str'>
    <class 'int'>
    <class 'int'>
    <class 'int'>
    You should see: Four lines, each showing a class: str for the name and int for the three numbers.
  2. 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.
  3. Print the type of each variable and run the file to confirm the classes.

Break it on purpose

Practice terminal
$ python main.py
NameError: name 'item1' is not defined
Why: 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

Calculate Solution checked

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
  1. Price total = price × quantity.
  2. 250 × 3 = 750.

750

Code Solution checked

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
  1. Create item1, item1_price and item1_quantity.
  2. Multiply price by quantity to get item1_price_total.
  3. 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'>

Explain Solution checked

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
  1. Python only sees separate variables with separate names and types.
  2. The shared prefix is a naming convention for people reading the code, so Python does not link them.
  3. 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

  1. What does type(item1) print when item1 = 'Phone'?

    1. <class 'str'>
    2. <class 'int'>
    3. <class 'item'>
    4. <class 'Phone'>
    Show the answer

    A. <class 'str'> · 'Phone' is a string, so its class is str.

    2:17 watch the moment
  2. Why are item1, item1_price and item1_quantity not treated as related by Python?

    1. Python only sees separate variables; the shared prefix is just a naming convention
    2. Python links every variable that starts with the same prefix
    3. Python requires a class before any variable can exist
    4. 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 moment
  3. What does a class let you do that separate variables do not?

    1. Group related data, such as an item's name, price and quantity, into one object
    2. Make programs run faster than functions
    3. Avoid using variables altogether
    4. 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

Open lesson 1, with the video →

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