Python Tutorial - Introduction to Classes
Key Takeaways
This video tutorial covers the basics of Python classes, including defining classes, creating instances, and using attributes and methods. It also demonstrates how to create reusable and modular code using classes, and how to automate data pipelines and create reports with minimal code.
Full Transcript
let's talk about python classes so in this beginner tutorial I'm going to walk you through some examples really showcasing you step by step how you can get started using classes within python to work towards what's so-called objectoriented programming we are going to cover the very Basics starting with the structure of a class so how do you type it out then we talk about the different elements that are within a class that you need to understand so these are things like the Constructor the attributes and the methods so by the end of this tutorial you will understand all of those Concepts I'm also going to share all of the code and this giup repository with you for reference because I know in the beginning that wrapping your head around this can be pretty confusing while it's it's pretty straightforward I know that when I was learning this I constantly had to refer back to examples and that's why I want to provide this for you so you not only can go through this right now but you can also save and store this as a reference for later projects and then finally once we get some clarity on the basic use cases of working with classes I'm also going to show you an example of how I would actually use this in an actual project in my work as an AI engineer so I'm going to provide you with an example that you can just copy paste and run so everything really starts to click because sometimes what can really be a problem with beginner tutorials is that you understand the elements at the very like basic individual level but then don't see the whole picture of how everything would come together in the bigger picture in the total project so that's really my goal here with this tutorial so let's dive in in order to follow along you only need a python installation and I will be walking you through this within fiest Cod and I'm going to be using the interactive terminal so I'm going to start that up here and if you want to learn more about how I do this then check out a video in the description here called my development workflow where I show you how to do this but first let's talk about why you want to use classes why you want to understand them and in short it's basically to create more reusable and modular codes so in the beginning when you're a beginner developer probably what you do is how you write code is you create one python file and you start at the top and you just continue on creating some variables here some list there Etc you go on and on maybe you already using functions but in the end it's just like one big file where there's maybe a lot of overlap and redundancy and then if you later come back and look into that it might be confusing like what was this variable and how does this work and it can get messy real quickly and this is of course completely fine in the beginning that's how I learned to program as well but once you get a little bit better and a little bit more comfortable what you uh ideally would like to do is try to work in a more structured way and classes can really help with that so really at the core classes allow you to Define blueprints for creating objects and we'll get into that what that means but basically means we can create a little box let's call it where you can store uh you can can store data into that box but you can also store Behavior Uh within that box in the form of functions so we don't necessarily change the output of our code but rather we refactor it for usability and readability so now let's look at the structure of a class and when it comes to the structure there are some elements that we first just have to understand from a syntax perspective so this is just the way how python works and then we can get more into how to actually use this so here's an example of the various elements that make up a class so to create a class in Python you start with simply typing out class lowercase followed by a space and then we type out the class name now it's important to note that the Python language in general in general only uses lowercase uh letters but when it comes to classes we use camel case so we start with a capital letter here the C and then it's if it's two words the second name also starts with a capital letter now this is not something that you necessarily have to do or if you don't do it it doesn't work but this is just standard uh python notation so it's good to adhere to that and work on those best practices now I know that when you're new to classes this all seems very abstract and I'm going to use a very simple example to help you think through all of this but naturally along the way there are going to be some elements where you might feel like okay I follow this I understand how to do this but why am I doing this and what what is the purpose of this and bear with me once we get get through all the building blocks and then actually bring everything together that is where most of the things will most likely start to click for you so we are going to use a simple example of a class About Pets so this class will capture some information so for example a pet can have a name a pet can have a species so it's a cat or a dog for example and then we're also going to introduce a function in there to do something with that information so now let's actually come over to the code editor and start building out our Pat class so if we look at the structure we start by typing out class then we type pet with a capital P and that is the first part and then we get into the second and probably the most confusing part in the beginning this is really that abstract thing where you have to see it a couple of times in order to make a click but that is this init method over here and it's a function although it's a special function uh it is called The Constructor so special method that is called when an instance of the class is created so remember remember we create these classes to later reuse them within our code and do something with it so first we Define the class that is what you see over here and then later when we want to do something with it we create an instance that's what it's called and up on creating an instance this is the function that is called automatically just by using or creating this class so in it I always remember it from initialization so up on creating the instance this will run and there's a special notation to this so it must follow the two underscores in it and then also two underscores and then we get into the part where it's similar to how a regular function Works where we can have parameters and do things with that but there's one uh difference in there and that is also this notion of self and this is also something that is completely unique to working with classes compared to functions and this again relates back to creating those instances where every time we create a new instance this self parameter refers to that particular instance and now like I've said this is probably very abstract right now so let's actually see what that could look like in the pet class to get a little bit more clear on this so we continue and we Define the init now then we continue using the notation of a standard function but we put in the self and then we think about what kind of elements do we want to capture within this class and now I already mentioned that a pet for example can have a name and a pet can also have a species so let's follow the standard notation of a function following by a column and then we actually get into the function so let's now have a look about what we can do over here and we could do basically anything that you can do within a regular function as well but mostly what you will see in classes is that we set attributes and with attributes we we take the parameters that for example the user has provided to us and we really set it to the the self element so that it now becomes part of the class so what that then could look like in our P example is we could say self. name equals the name that the user has provided and similarly we can do self. species and we can set that equal to the species that the user has provided so this now means that when we have this class in order to created it needs a name and a species as input and up on the creation upon creating an instance it will run this function and set the name and the species accordingly so let's now actually see what that looks like so if I store this in memory I can now create an instance so this is the part I was talking about we have first defined the class and now we're going to use it to create an instance so let's say we want to create an instance for a doc so we say Doc equals and then we call our class and as inut parameters we're going to have to provide a name and let's call the dog buddy and species we're going to set that equal to Doc so now we can run this and now we can have a look at this instance that we have just created so if I look at the uh instance of the pet class Doc and just print it for example over here I can just see there's not much here this basically means that it's an object within within memory uh but we don't see anything that's going on over here for example we don't see buddy we don't see dog so now what we can then do in order to actually use the data to use the information of this class created is we can call the attributes using a DOT notation so similarly to how we set the self. name equals to name you should really view self uh you should view that now as the as the doc variable that we have created over here so if I now hit a DOT and I for example take the name and I print that you can see that we now get body and similarly I can call Doc do species and we get dog so now if you come back to the example we have covered the first basic structure of how to define a class then how to put in the parameters how to then set those parameters as attributes how to create an instance of that CL class uh by calling the class and putting in the parameters and then since there are now attributes how we can call them from the uh instance using the dot notation following the names of the attributes so as you can see there are a lot of thms and abstractions over here that that we're using but at the core this is really what you can do with a class and that's also why I said you probably have to get and to have to get back to this come back to this a couple of times see a couple of different different examples before for this really clicks but really this is it at the core and now to give you another example of creating another instance as well we can for example compare dog and cat where the cat is called whiskers and it's a cat and we now have the dog and we have the name and the species but similarly we can also do cat.n name and we have whiskers and we can do cat. species so it's the same class different instances and therefore different data and now you can already probably start to imagine what we can do by defining a class one time defining the structure and then reusing it later throughout our code because if we wanted to do this without using a class we would again have to Define okay cat has a name cat has a species Etc and that would result in a lot of redundant Cod and right now it's very simple we want another instance we want now want a cat okay let's call it and we don't have to change this all right so so far we've looked at creating a simple instance of the class using just some simple parameters and setting them as attributes but classes can also contain functions and when you use a function within a class you call it a method and a method is again not any different really from using a regular function as we can capture all kinds of logic in it we can return stuff but the only difference again here is that we put the self parameter in here in order to make sure that the instance has access to its own information so let's now look at an example where I've created a function called introduce and I have extended the the pet class with it and it's a simple print statement and it says hello my name is self. name and I am self. species so this is a simple print statement to print out the information of the instance of the class and here you can see why we have to use the self because we use the self here to refer to its own attributes again so referring to the dog referring to the cat whenever we uh call this introduced function so calling functions on a class instance is actually very similar to how you call the attributes so we use the dot notation but instead we now also have to use parentheses so let me show you what that looks like if we store this into memory again we create another instance so we have the dog and let's also do the cat so that is now in memory again let me clear that up and now we can do dog. introduce and we run this line and we say hello my name is Buddy and I'm a dog and for the cat hello my name is whiskers and I'm a cat so that is how you use the methods of a class following the dot notation using the parentheses now the final thing that I want to show you here before I show the example of how I use this structure within my projects is how you can update attributes of an instance so if we look at the dog right now if you look at buddy uh buddy is now uh saying that his name is Buddy and he's a dog but we can also overwrite those and next to overwriting we can also introduce completely new attributes all using the same notation so I can for example take the dog instance which is Buddy right now and I can overwrite it and now call it Max and if I now call the introduce um Max will now say hello my name is Max and I'm a doc but we can just as well Define a new attribute by calling doc. color um so that is not in the original structure of the class but we can later add that and when I now come in here and for example say doc. color you can see that it works just the same way all right so that covers this beginner tutorial really on classes and how you can work with them and now again you probably have to few a couple of more examples redo this exercise maybe a couple of times before it really clicks and you start to get the hang of it but what I now really want to do is I want to show you how I use classes and how I structure them within the projects that I work on because I knew that for myself in the beginning this took this took me a very long time to figure out I quite quickly understood how you could structure a class how you could create a class so like this it's if you if you watch this pretty easy to to wrap your head around it but then how you actually use use this in a project to really write really cool code actually that took me a long time to figure out so I'm going to show you this example with a data surface that I have created now if you're new to Python and you look at all of this code you're like what's going on over here don't worry about it I'll make this available as an example I just want to show you what's possible so the way I like to structure my projects is where whenever I create a project I have some code and then I create a separate fold ER called surfaces so this is a particular design pattern that I use this is again not something that alters the functionality but it's just a folder structure and a naming convention that I like to use and then if I have some kind of like complex calculations or methods or connections to an API I want to create separate surfaces for that and in this example I have created a data surface and what this data surface does is there is some simple data within this project repository so it's very simple dummy data it's a product a quantity and a price and there are some some dummy numbers um in there and what this data service does is it takes the data it first uh processes it by adding some calculations then it creates a report and then it also creates uh an image of that report so it creates a plot and it does all of that all from within the data surface so here you can see we have the same structure so there's nothing new is just more of the same where we have the initialization then we have various methods like load data process data export data generate a report uh we have some ways to uh create a chart and we can set some styling and then finally we have a run function which just kicks off the whole Pipeline and now the cool thing is by doing this and importing that data surface so importing that class uh so this is already a little bit more advanced but uh what we can do by defining it in this file the data surface and then importing it in the main file over here we can create the instance within this file without having to look at all of the other code and this is really where the reusability and modular code comes in because now this data surface I can not only use maybe in this file over here but also maybe in another file within this project but also maybe maybe even complete a different project and what I want to show you right now before I give a little bit of details on this is if I come in here and I import this data surface and I create the instance and I now run the data processing pipeline what it will do behind the scenes it will take this raw data it will process it create a processed uh CSV file uh over here sorry first it will put it in the interim over here so it will create calculations put it in here it will create a sales report put it in this folder and it all will also create a figure that figure that you're seeing over here and store it in the figures folder and this is now just uh two lines of code so there there are a lot of comments over here but we do an import and with two lines of code I can do an entire data pipeline to create let's say a weekly report so if you work in sales for example uh and you're an analyst data anal data analyst in sales for example you could create data services like this with various functions where for example every week or every month uh someone puts a new Excel or a new uh CSV file somewhere on I don't know SharePoint or Google drive or whatever and you can create a surface around that where if you structure it correctly once then all you have to do every once per week for example once per month you come in here and you just run this and that's it and you have another report so that's how I wanted to wrap up this video I know if you look at this and you're new to using classes you're probably new to python this is a lot but just remember that it's not much more than what we did over here but we just stacked more functionality uh within the same class and I wanted to show you this because I always like to begin with the end in mind so why should you learn this why do you want to learn this and for me it was not until I figured out that okay wait this is actually pretty cool I can create some really cool code with this come in here and I'll just hit one function and it will kick off the entire pipeline uh it was not until then that I started to change my own way of working so that's also for example when I really switch from mainly using jupyter notebooks to only working in Python files creating modular and reusable code so that whenever the project that I work on or for example the clients that I work with whenever I have to come back to a project it's just very clear okay we have a data surface what's in here okay this is what it does and then okay I can uh run it Etc all right and that's it for this video make sure to start this repository or clone it so you have it at hand uh this repository is called AI fundamentals I will I plan to create a lot of more tutorials in here covering really the fundamentals of uh python to create AI Solutions so you have this a a as a reference uh you can look back to it now if you find this video helpful please leave a like and also consider subscribing like I've said I create more videos on all things artificial intelligence and working with python so if that's something you're interested in then definitely subscribe and then I'll see you in the next one
Original Description
Want to get started with freelancing? Let me help: https://www.datalumina.com/data-freelancer
Need help with a project? Work with me: https://www.datalumina.com/consulting
🔗 Link to Repository
https://github.com/daveebbelaar/ai-fundamentals
👋🏻 About Me
Hey there! I'm Dave, an AI Engineer and the founder of Datalumina, where our mission is to facilitate entrepreneurial and technological proficiency in professionals and businesses. Through my videos here on this channel, my posts on LinkedIn, and courses on Skool, I share practical strategies and tools to navigate the complexities of data, artificial intelligence, and entrepreneurship.
🛠️ My Development Workflow
https://youtu.be/3sIzCFuLgIQ
✔️ My Project Management Tool
https://clickup.pxf.io/datalumina
🎓 Our Training Programs
https://www.skool.com/data-alchemy
https://www.skool.com/data-freelancer
🔗 Let's Connect
https://www.linkedin.com/in/daveebbelaar/
https://www.instagram.com/daveebbelaar/
📥 Datalumina's Newsletter
https://www.datalumina.com/newsletter
📊 How I'm using data to track my health
https://join.whoop.com/datalumina
#python #clases #oop
📌 Video Description
In this video, Dave provides an introduction to object-oriented programming (OOP) in Python. He begins by explaining the fundamental concepts of classes and objects, which form the building blocks of OOP.
Classes allow you to define blueprints for creating objects that encapsulate data and behavior. They provide a way to group related data (attributes) and functions (methods) together into a single unit. By using classes, you can create instances (objects) of that class, each with its own unique set of data.
Dave demonstrates how to define a class using the `class` keyword and how to create instances (objects) of that class. He also covers the `__init__` method, which is a special method used to initialize the attributes of an object when it is created.
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Dave Ebbelaar · Dave Ebbelaar · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
How to Install Homebrew on Mac (Getting Started)
Dave Ebbelaar
How to Install Python on Mac (Homebrew)
Dave Ebbelaar
How to Install Anaconda on Mac (Getting Started)
Dave Ebbelaar
How to Set up VS Code for Data Science & AI
Dave Ebbelaar
How to Use Git in VS Code for Data Science
Dave Ebbelaar
Data Science Desk Setup to Maximize Productivity
Dave Ebbelaar
THIS Is How I Write Clean Data Science Code EVERY TIME
Dave Ebbelaar
Data Science Tutorial - Project Structure
Dave Ebbelaar
Changing rcParams for Better Data Science Plots | Matplotlib Tutorial
Dave Ebbelaar
How to Read Excel Files with Python (Pandas Tutorial)
Dave Ebbelaar
My Data Science Journey (Zero to Freelance)
Dave Ebbelaar
How I Automate Data Visualization in Python
Dave Ebbelaar
16 Apps I Use Daily as a Data Scientist
Dave Ebbelaar
How to Manage Conda Environments for Data Science
Dave Ebbelaar
How to Export Machine Learning Models in Python
Dave Ebbelaar
VS Code Speed Hack for Data Science
Dave Ebbelaar
17 VS Code Tips That Will Change Your Data Science Workflow
Dave Ebbelaar
How to Predict the Future with Python (Forecasting Tutorial)
Dave Ebbelaar
How to Use Python Environment Variables
Dave Ebbelaar
7 Data Science Tips for Beginners in 2023
Dave Ebbelaar
How to Effectively Use the Data Science Lifecycle
Dave Ebbelaar
Full Machine Learning Project — Coding a Fitness Tracker with Python (Part 1)
Dave Ebbelaar
Full Machine Learning Project — Processing Raw Data (Part 2)
Dave Ebbelaar
Full Machine Learning Project — Data Visualization with Matplotlib (Part 3)
Dave Ebbelaar
This Will Change Data Science as We Know It (ChatGPT)
Dave Ebbelaar
Full Machine Learning Project — Detecting Outliers in Sensor Data (Part 4)
Dave Ebbelaar
Full Machine Learning Project — Low-pass Filter & Principal Component Analysis (Part 5a)
Dave Ebbelaar
Full Machine Learning Project — Fourier Transformation & Clustering (Part 5b)
Dave Ebbelaar
Full Machine Learning Project — Predictive Modelling (Part 6)
Dave Ebbelaar
Automate Machine Learning with ChatGPT
Dave Ebbelaar
Scraping Web Datasets for Data Science Projects
Dave Ebbelaar
Full Machine Learning Project — Counting Repetitions (Part 7)
Dave Ebbelaar
How to Use GitHub Copilot for Data Science (Python + VS Code)
Dave Ebbelaar
Every Beginner Data Scientist Should Understand This
Dave Ebbelaar
Revealing My New AI-Powered Data Science Workflow
Dave Ebbelaar
Auto-GPT Tutorial - Create Your Personal AI Assistant 🦾
Dave Ebbelaar
Build Your Own Auto-GPT Apps with LangChain (Python Tutorial)
Dave Ebbelaar
Building Slack AI Assistants with Python & LangChain
Dave Ebbelaar
ChatGPT Code Interpreter - Goodbye Data Analysts?
Dave Ebbelaar
How to Deploy AI Apps to the Cloud with Flask & Azure
Dave Ebbelaar
How to Build an AI Document Chatbot in 10 Minutes
Dave Ebbelaar
Is Falcon LLM the OpenAI Alternative? An Experimental Setup with LangChain
Dave Ebbelaar
GPT Engineer... Generate an entire codebase with one prompt
Dave Ebbelaar
Pandas DataFrame Agent... the future of data analysis?
Dave Ebbelaar
OpenAI Function Calling - Full Beginner Tutorial
Dave Ebbelaar
How to use ChatGPT's new “Code Interpreter” feature
Dave Ebbelaar
LangChain just launched their new "LangSmith" platform
Dave Ebbelaar
How I'd Learn AI (if I could start over)
Dave Ebbelaar
I Used AI To Scrape The Web & Write PDF Reports
Dave Ebbelaar
LangSmith Tutorial - LLM Evaluation for Beginners
Dave Ebbelaar
7 Lessons for New AI Engineers - Beginner’s Guide
Dave Ebbelaar
The Rise of the "New-Age" Machine Learning Engineer
Dave Ebbelaar
OpenAI Assistants Tutorial for Beginners
Dave Ebbelaar
How To Connect OpenAI To WhatsApp (Python Tutorial)
Dave Ebbelaar
How to Build Chatbot Interfaces with Python
Dave Ebbelaar
PostgreSQL as VectorDB - Beginner Tutorial
Dave Ebbelaar
My MacBook Setup (as a coder & business owner)
Dave Ebbelaar
Easiest Way to Connect AI Chatbots to WhatsApp
Dave Ebbelaar
ClickUp Tutorial - What Is ClickUp Brain? 🧠
Dave Ebbelaar
My Development Workflow for Data & AI Projects
Dave Ebbelaar
More on: LLM Foundations
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
The New Geography Of Entrepreneurship—How Founders Are Rethinking Where To Build
Forbes Innovation
Esports Company BLAST Reports Record Growth Following US Expansion
Forbes Innovation
Explorers Get Naming Rights. Infrastructure Builds The Future.
Forbes Innovation
Jerry Soko named Eswatini CEO as MTN doubles down on internal talent
TechCabal
🎓
Tutor Explanation
DeepCamp AI