Python Intermediate Tutorial #1 - Classes and Objects

NeuralNine · Beginner ·🛠️ AI Tools & Apps ·6y ago
We are starting with the new Python tutorial series for intermediates. In this first episode, we are getting into the basics of object-oriented programming. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🖥️ My Coding Setup 🖥️ ⌨️ Keyboard: http://hyperurl.co/neuralkeyboard 🖱️ Mouse: http://hyperurl.co/neuralmouse 🖥️ Monitor: http://hyperurl.co/neuralmonitor 🎙️ Microphone: http://hyperurl.co/neuralmicrophone ✏️ Drawing Tablet: http://hyperurl.co/neuraldraw 🎵 Outro Music From: https://www.bensound.com/

What You'll Learn

This video tutorial covers the basics of object-oriented programming in Python, focusing on classes and objects, as part of an intermediate Python tutorial series by NeuralNine.

Full Transcript

what is going on guys welcome to the Python tutorial series for intermediates we're now finished with the beginners tutorial so we've covered all the basics all the fundamentals of Python and with this series we're getting into the next category into the intermediate concepts now in today's first video we're going to talk about object oriented programming and classes and objects basically so let's get into the code so let's talk about what object-oriented programming actually is now object-oriented programming is a programming paradigm that tries to model the real world into the programming world so we're taking real world objects or real world things and try to make them abstract models or try to make abstract models of them in the programming world so for example we have books in the real world we have computers televisions we have buildings and people and cities and all kinds of things and we want to model these things into the programming world we want to make abstract models of them and to do that we use classes and objects and you can imagine a class to be somewhat like a blueprint of the object and the actual object to be an instance of this class so let's make an example here a class person and the class person this is how we define a class in Python class then the name of the class followed by a colon and of course everything that belongs to that class is indented then and the person would now be this class person would now be the blueprint of what a person looks like in Python so we we define what is a person what attributes has a person what happens when we call the method of a person but we don't really say this is the name of the person we don't really say this is the age of the person we just say that a person has a name a person has an age and so on so it's just a blueprint and then the actual person person 1 person 2 would be the actual object so how do we create objects of a class we do that by calling the so called constructor and the constructor is just a method in our class that we need to call whenever we want to create an object so we now have the class and this constructor we have to call has a specific name in Python and this name is underscore underscore init underscore underscore so this method here calls the constructor or is the constructor and it takes at least one parameter which is self and the self parameter is a parameter that has to be in every method in every function that we define for our class because it refers to the object that we're using right now because this is just a way to generalize what we do with our objects a class as I said we're not going to work with a class we're going to work with objects that are just an instance of the class and whenever we want to access some attribute of that object we have to use the self parameter the self keyword here because the self always refers to the object we're dealing with right now so we say def in itself which means that we're creating a new a new object which is which is now referred to as the self and what I can do here just let let's just print hello world the first time and to now create an object of that class this is now a class it does not have any attributes doesn't have any real methods but we can create an instance of that class which we do by saying a person one for example actually you can call this whatever you want you can also call it act let's just say variable and you can now say are x equals person with a capital p because that's our class name we always provide the class name here we can say x equals person and we just call the constructor like that so instead of saying person dot in it this is not how we do it this is not the way we called function we just call the constructor by saying by calling the whole class by calling the person class and when we do that we execute what is written in our in the constructor so in this case we print hello world because this is the function of the init method and we want to print that now of course this is you not what we doing because usually the constructor is for initializing the person so what we could do is we can say okay the self this object I'm dealing with right now has a certain name and I can say self dot name equals and I can create a basic default name like Mike again and maybe an H or default age 25 I don't know and whenever I create a person right now this person will have the name Mike and the age 25 of course this is not what we want but we can check how this works so person 1 equals person and we just call the constructor and we can then just print person one dot name for example and print person 1 dot H so we can now access the attributes of that person this is how objects work and as you can see it prints Mike and 25 now of course we all we don't want to always have the same values because if I make a person - I want different well values I want different names I want different ages and so on so what we usually do is we pass some more parameters we say ok give me a name give me an age give me a height and just a warning here I'm going to use the metric system because I'm not a fan of feed and inches and pounds and so on because simply for the reason that I don't understand this metrics or I don't know how many feet is normal so I can tell you 180 centimeters is a normal height but I cannot tell you what a foot is for example so I'm going to use the metric system here so let's say self dot height equals height and of course we do that with all the other attributes as well so self that H is just H and self dot name is just name so what we're basically doing here is we're accepting some parameters name agent height and we're then assigning the values of these parameters to the actual attributes of our class of our opt of the self object so I can no longer just call the constructor because when I do it you'll see that I get a type error because it's missing three required positional arguments and this means that when I call the constructor right now I have to pass the values I have to pass a name for example Mike I have to pass at age 30 and I have to pass a height of 180 centimeters and then I can create an object and then of course I can go ahead and print all the height of that object as well as you can see I can print all the attributes because I have to find them I have passed values for them for them and of course when I now define a person - I can pass different values and it will not be always a name Mike I can call the second person Sarah or I don't know like pop or something now of course I can also change the values of the attribute so for example I have printed all the values and now I can go ahead and say I don't know person 1 dot name Mike changes this name so I'm going to call him I don't know well Henry right now and then I can print person one name again and you'll see that I can change its name I can change all his values now let's talk about some other default or some other predefined methods here because in it is not the only special method that we can use because I can also define some simple methods here for example def hello world and I always have to pass the self here but basically I can just define all kinds of methods here that belonged to my class and then I can just call person 1 dot hello world so I can not only define attributes I can also define methods but this is not what I'm trying to tell you here because what you can also do is you can not only define a constructor you can also define he destructor so what to do when the object gets deleted we're not we haven't talked about deleting certain things yet we have not delete bearable we've not deleted objects yet but you can define a method Dell to determine what happens when you delete the object so for example we can just print object delete it and basically what i'm doing here is when i delete the element so i can use the del keyword this is something that we haven't learned yet but it's quite simple when you want to delete something when you do want to delete a variable and object you just call the delete value of the delete keyword del keyword sorry and you can delete the whole object so I can just del person 1 and it prints object delete it so the object is no longer there it doesn't occupy any memory anymore it's gone basically and with a del function the destructor so called I can define what happens when I delete it but we're not going to use that quite often so just so you heard about it but I don't think that we're using it quite often so another very important function that we can define is the STR function what happens when we print our object because it would be quite nice if we could just print the person so print person one two just get all the values to just get all the attributes but when I print person I got just the object I get just main that person object add and then I get the memory address I don't really get any information that is interesting to me what I would like to get is just a summary of all the attributes maybe all the values print it out on to the screen in a nice format and to determine what happens when I print my person object or to define what happens when I converted into a string when I type casted into a string would it which is what happens when I try to print it I can define a STR method with two underscores so again self and what I can do here is I can just say we can now use string formatting that we learned about in the last video off the beginners tutorials so named placeholder H placeholder and height placeholder and then dot format and self dot name self dot H self dot height it's a little bit so and then again close it and when I now print person one what happens is return to Nantes okay sorry I cannot print it I have to return it it's a mistake I always make you don't want to print something because you don't want to take action you just want to say what do I get when I do this drink so what do I get when I print or what do I get when I typecast my object into a string you don't want to print something you want to return something makes kind of sense so when we run it we get this output here name age and height because this is what we want to have when we have the string when we when we treat our object like a string of course I can just return some here as well uh it doesn't matter you just have to think about what you want to get when you print or when you treat your object like a string now the last thing that we're going to talk about art is so-called class variables because up until now we've only defined attributes we mentioned it we can define functions for example let's say you get older by so in so many years and then I can say I don't know self dot H plus equals years and I can make a person grow older and I can make all kinds of attributes I can find all kinds of methods all kinds of functions but we haven't talked about class variables yet in class variables are variables that are not unique for each object but are the same that have the same value for each object so one example for that it could be the amount of people we already already have so let's just say amount equals zero and every time we create a new person we want to have this amount increased by one and of course every time we delete one we want to decrease that by one and I made a mistake here you cannot just write amount but you have to write person dot amount now notice that we're not using self here because self refers to the object to the individual object when we access self we access person one person two and so on when we access person we access the whole class so in this case we say amount equals zero and then we say person dot amount plus equals one and person dot amount minus equals one because we're referring to the whole class we're referring to one actually just to not make sense we're referring to the whole two to one bearable that has one value for all the objects because it's a class variable and what I can do is I can just let's delete this here I can just print the amount by saying print person dot amount and you'll see I have one person right now if I create another person I don't have to do anything with that person just say person equals person Sarah 40 years old and 176 I will now have to the amount of 2 and when I go ahead and delete person - I will have one because I created one and then I delete it one again so this is basically how class variables work if you want to have some values that are of global that are not only for this particular object but for all the objects for example for counting how many objects there are we can use class variables so that was the first episode of the Python tutorial series for intermediates we learn about classes and objects in the next video we're going to talk even more about object oriented programming and the episodes after that we're going into socket programming network programming multi-threading queues and so on so it's getting very very interesting and keep watching the series so thank you very much for watching hit the like button if you like the video subscribe to this channel if you want to see more leave feedback and ask questions in the comment section down below and thank you very much for watching see in the next video bye [Music]
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 12 of 60

1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This tutorial introduces the basics of object-oriented programming in Python, including classes and objects, to help intermediate learners improve their Python skills.

Key Takeaways
  1. Define a class in Python
  2. Create an object from a class
  3. Understand class attributes and methods
  4. Use inheritance in Python classes
💡 Object-oriented programming is a fundamental concept in software development that helps organize and structure code, making it more maintainable and efficient.

Related AI Lessons

I Asked Gemini AI to Preview My Haircut Before My Salon Appointment - Here’s What Happened
Learn how AI can help you preview hairstyles before a salon appointment and make informed decisions
Medium · AI
7 Best AI Tools for Research, Coding, and Development in 2026
Discover the top 7 AI tools for research, coding, and development in 2026, curated from a pool of 47 options, to boost productivity and efficiency
Medium · Data Science
7 Best AI Tools for Research, Coding, and Development in 2026
Discover the top 7 AI tools for research, coding, and development in 2026, curated from a list of 47 tools
Medium · Programming
How to Write a Project Status Report With AI in 15 Minutes
Learn to write a project status report with AI assistance in under 15 minutes, ensuring honesty and accuracy in your reporting
Medium · AI
Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →