Python Day #15: Python OOP Made Easy: Learn Encapsulation Step-by-Step
Learn how to use encapsulation in Python with real-world examples. This beginner’s guide to OOP shows you how to build classes, objects, and cleaner code fast.
Here you are well into The Python Roadmap to Success. We’ve hit on all the fundamentals so far and you have begun to break into Object-Oriented Programming.
Last lesson, you were introduced to the basics of objects and working with properties and methods. This was done through interactive programming as we used the Turtle module to create interactive objects and give them their own unique behavior.
In this lesson, we will hit on the first pillar of OOP. This is what is known as encapsulation and really is the basics for programming and building out your own classes where objects can be used for interaction with them
If you’ve been following along with our Roadmap to Python Series, you’ve already learned so much, so well done! I’ve made sure that this entire series builds to give you the real skills and logical thinking skills you need to form a solid foundation.
This lesson will focus on taking your programming skills to a new level, a level where by the end you should confidently be able to not only build out your own blueprints, which we call classes. But you should be able to pick up many libraries and know how to start working with them.
Every week you’ll be introduced to a new topic in Python, think of this as a mini starter course to get you going and allow you to have a structured roadmap that actually builds to create you a solid foundation in Python. Join us today!
Object-oriented programming is truly used in most applications whether you realize it or not. Most of the things you will actually do in Python will require knowledge of how this works.
This is because even if you don’t build out your own projects to include OOP, the libraries and frameworks you will use in your projects are built around these concepts. This will allow you to truly understand what is happening and how to properly use them in our programs.
If you are new to these concepts or still confused by OOP, then you need to go back to last lesson. Last lesson, I promise will give you a deeper understanding on what all these fundamentals really are. This is the tired and true method to learn OOP here.
Over the last 8+ years I’ve been teaching full-time around the world which has given me the unique opportunity to work with over 1,500+ students. The last four years I’ve been teaching Python full-time. Breaking down complex topics, step by step.
I’ve spent this time curating a structured learning path that ensures my students have the greatest chance of success and don’t just learn but actually master Python as well as their logical thinking and problem solving.
One point I should note is, spend time digesting these when I write them, after six weeks most of my posts are automatically put behind a paywall.
If you haven’t subscribed to my premium content yet, you should definitely check it out. You unlock exclusive access to all of these articles and all the code that comes with them, so you can follow along!
Plus, you’ll get access to so much more, like monthly Python projects, in-depth weekly articles, the '3 Randoms' series, and my complete archive!
I spend a lot of my week on these articles, so if you find it valuable, consider joining premium. It really helps me keep going and lets me know you’re getting something out of my work!
Thank you for allowing me to do work that I find meaningful. This is my full-time job so I hope you will support my work by joining as a premium reader today.
If you’re already a premium reader, thank you from the bottom of my heart! You can leave feedback and recommend topics and projects at the bottom of all my articles.
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
P.S - Drop any questions in the comments or feel free to DM me directly as I’m always happy to help you guys out.
Encapsulation: The First Pillar of OOP
Encapsulation in Object-Oriented Programming (OOP) is the core concept that involves grouping data (attributes/properties) and the methods (functions) that control the data within a single unit, known as a class.
This whole idea aims to restrict direct access to the internal state of an object and expose only a controlled interface for interaction. Really that was a whole lot of jargon.
How I want you to view encapsulation is similar to a folder. In a folder we store certain items that relate to each other for instance. Well in encapsulation we basically make what’s called a class.
This class can store variables which relate to something that uses the class. It can also hold functions that only objects defined from the class can use. This allows us to really expand on our programming and program for a unique scenario that we can reuse.
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
The Essence of Object-Oriented Programming
Before I take you right into coding out your own class, I covered these terms below in last article. But these will be used throughout your career. Not only today, but in documentation and other areas. So it’s important you drill these in and know what I am referring to, as I use them throughout the lesson.
These new terms are going to be: Classes, Objects, Methods, and Properties.
Class: A blueprint for creating objects. Objects are collections of data (properties) and behaviors (functions/methods) that are grouped together. You can think of a class similar to a folder for these.
Object: Is a real, usable version of the blueprint (the class). These objects are what’s known as an “instance” or an “example” of the class and allow us to use the inner components of the class.
Method: A method is just a function that is inside a class. It is the behavior for an object and to use a method (function) they must be linked to an object from the class.
Property: A property is simply a variable in a class. These are the attributes you can give to an object of a certain class.
Everything in Python is essentially built on the terms that I touched on above. But learning how to creating these and use these can sometimes feel overwhelming.
We want to break this down so first you can see it in action. What do I mean by an object? Or how can I assign a specific object certain behavior and personality?
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
Ready to Go From Zero to Confident in 90 Days?
Most people get stuck before they even start…
They waste hours Googling, watching random YouTube videos, and never actually finish a project. But that doesn’t have to be you.
I’ve built The Python Masterclass to take you from “I don’t know where to start” to “I can build real-world Python projects” — in less than 90 days.
👉 I’m giving you my exact system that’s been proven and tested by over 1,500 students over the last 4+ years!
Here’s what you’ll get when you join today:
✅ All Course Videos — Learn Python without wasting time on scattered tutorials
✅ Python Templates & eBooks — Pre-built scripts, guides, and checklists
✅ How to Build a Portfolio - Mini Course — Learn the 5 step process ✅ Bi-monthly Live Q&A Calls (Pro/Elite) — Get answers in real time
✅ Portfolio/CV Reviews (Pro/Elite) — Tailored feedback to help you land work
✅ Personal Onboarding, Fast Support, & Custom Feedback (Elite) — I’ll personally guide to keep you moving
✅ Access The Nerd Nook premium FREE (Pro/Elite) — Stay ahead with insider tips
My masterclass is designed so you see your first win in just 7 days — you’ll build your first working Python scripts in week one and finish your first full project in your first month.
With just 30–45 minutes a day, you’ll be on your way to becoming a confident Python developer.
👉 Ready to get started?
P.S. — The sooner you start, the sooner you’ll have projects you can actually show to employers or clients. Imagine where you’ll be 90 days from now if you start today.
Programming a Blueprint in Python
A class is essentially a blueprint for how an object should look. It is the rough sketch on how an object should look and act. Image the class as like a blueprint to a house, this is the rough idea. It doesn’t look pretty yet but I could use this blueprint to build multiple houses.
Then we we want to use the blueprint (class) we can create an object. The object is where we can give specific arguments to build the exact house we want. The object is essentially the final product after we are done building.
Below here is your first class, I made sure to keep this super basic and will use the housing example I referred to above. Yo are going to notice new Python keywords that are used in OOP.
First the python class
keyword is used to define a class. Usually the first method (function) in a class is __init__
. This is a very special function which is also know as the “constructor” method. This method is used to “build” the class, hence the name.
Generally __init__
is used to store all the properties in our class. It is here that we pass in parameters and define the properties that we can later use in the class. This method is special as it is also automatically ran when we make an object.
Another new word you are seeing is the word self
. This new word is needed as the first parameter in any method we define in a class, as well as the start of any properties that we define.
You can think of self
like a special key. This will allow us to pass properties and methods between each other as long as we are in the class. We only every use self
inside a class, you never use it outside the class.
All the properties we are not hard coding, we are not giving them defined values. This makes the code more reusable as when you define an object this is where you will give the arguments that become the final values for the properties.
Then I made a method (function in a class) called house_info
. This takes no parameters expect self
and just prints the properties for the class. Outside the class is where I made two objects, an object is a variable thats value is a class.
These objects are called dream_house
and apartment
. Here we give the arguments that will become the value to the properties in the class. Once you have made an object you can then use any methods you defined in the class.
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
Important note is that if you want to use a property or method, these need to be linked to an object of that class. So you can’t just call the method like a normal function, you need to link it to the object then call it.
Alright now let me add in one final method that we could use to change the starting value of the house. Here is a method that would allow you to expand the house size in case you wanted to upgrade.
I give two starting parameters, but this time I assigned a default value of 0. If I want to expand my house, I could call this method with my object and give it the number of rooms I want to upgrade.
Then this will update the properties values then call the original house_info
method so we can see the new properties of our house. Today was simple, but it was meant to show you how we create this interaction and build out a class blueprint of your own.
This is what we will use in the coming lessons. We will expand on this topic and learn the next three pillars in OOP to expand your skillsets. I hope that you learned something new today and got value from this lesson!
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
My Best Starter Resources
Here are the best resources I have to offer to get you started with Python no matter your background! Check these out as they’re bound to maximize your growth in the field.
Code with Josh: This is my YouTube channel where I post videos every week designed to help break things down and help you grow.
Zero to Knowing: My Python Masterclass Subscription gives you everything you need to go from zero to building real-world projects — with new lessons, challenges, and support every month. Over 1,500+ students have already used this exact system to learn faster, stay motivated, and actually finish what they start.
My Books: Maybe you’re looking to get a bit more advanced in Python. I’ve written 3 books to help with that, from Data Analytics, to SQL all the way to Machine Learning.
Wrapping Up Lesson Fifteen
You’ve made some real progress in your Python journey. Now that you’ve got a handle on encapsulation, you’ve stepped into a new level of programming—Object-Oriented Programming. This isn’t just theory; it’s the kind of code structure that shows up in the tools, libraries, and apps developers use all the time.
But today wasn’t just about learning some new words or commands. It was about learning how to think like a real programmer. You created your own class, made a few objects, and saw how encapsulation helps you keep your code clean and easy to reuse.
That’s the kind of thinking that moves you from just messing around in Python to actually building things with it.
Up next, we’re going to keep building on what you’ve learned. We’ll get into the next three big ideas in OOP—inheritance, polymorphism, and abstraction. These are tools that will help you write better, smarter code that you can grow and improve over time.
So take a second to recognize how far you’ve come. If you’ve stuck with it this far, you’re not just learning Python—you’re starting to think like a developer. Keep it up nerds!
Let’s keep moving forward with Day #16 next week.
Hope you all have an amazing week nerds ~ Josh (Chief Nerd Officer 🤓)
👉 If you’ve been enjoying these lessons, consider subscribing to the premium version. You’ll get full access to all my past and future articles, all the code examples, extra Python projects, and more.