Learn Python Object Oriented Programming! πŸš—

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody so we have finally made it to Python objectoriented Programming this is a very important topic in Python an object is a bundle of related attributes and methods attributes are similar to variables to describe what the object has so look around you right now you are surrounded by different Real World objects next to me I have a phone a cup and a book each of these objects can have different attributes to represent it for example an attribute of the phone next to me could be version number I could set that to be 13 is on could be another attribute is the phone powered on or not that could be true or false or even a price I have a cup next to me what liquid is within the cup in this case coffee what's the temperature of the cup is the cup empty is empty or even a book What's the title of the book that could be a string how many pages does the book have Pages could be another attribute now objects also have the capability to do things they have methods which are functions that belong to an object people mix up functions and methods all the time they're technically different even when teaching I tend to make that mistake calling a method a function and a function a method usually people know what you're referring to though a method is a function that belongs within an object what are some actions these objects can perform with a phone you can make a call or receive a call turn the phone on or turn the phone off those could all be functions with a cup you could fill the cup drink from the cup or empty the cup with a book you can open the book read the book and close the book an object is a bundle of related attributes and methods they can represent real world items to create many objects we'll need to utilize a Class A Class is a type of blueprint used to design the structure and layout of an object we need to design what our objects have their attributes and what they can do their methods we will create a class of car we will create some car objects class car to construct a car object we need a special type of method called a Constructor it works similarly to a function we will Define a function of double underscore init meaning initialize double underscore again and then follow this with a set of parentheses this is our Constructor method we need this method in order to construct objects it's a Dunder method Dunder meaning double underscore that's a future topic I don't want you to be overloaded with information right now all you need to know is that we need this method in order to create objects this method behaves similar to a function we need to set up the parameters self is already provided to us self means this object we're creating right now this car so what are some attributes that a car should have a model that could be a string like a BMW a year that could be a number a color let's add a Boolean of for sale is the car for sale or not that's true or false to assign these attributes we're going to access self self dot the name of the attribute self. model equals the model we receive these are parameters when we receive the name of a model we will assign it to this object let's do this with year self. year equals year self. color equals color self do for sale equals for sale this is an example of a few attributes that a car might have a model year color and if it's for sale or not represented by a Boolean now to construct a car object we need a unique name for this car let's just say car one car 1 equals take the name of the class add a set of parentheses to invoke The Constructor we're going to do this this almost exactly like a function we have parameter set up we need to send a matching number of arguments self is provided to us behind the scenes automatically we need a model year color and if it's for sale or not so pick a car of you're choosing I'll pick my favorite car the model will be a Mustang for the year I'll go with the recent year of 2024 a color I'll pick red is the car for sale I like this car so no I will set that to be false make sure false is capitalized let's see what happens if I attempt to print our car object of car one what we're given is the memory address of this car object where it's located but I would like one of the attributes located at this memory address instead of printing the object itself we're going to access one of the attributes found within this car we will follow the name of the car with a DOT this dot it's known as the attribute access operator I would like the model of car one that would give me Mustang let's access the year take the name of the car car 1 dot the year 2024 followed by the color car 1. color red car one is it for sale we'll print that that is false now let's create a second car we're going to reuse this class to create a second car we will create Car 2 equals car we'll pass in some different arguments a Corvette the year will be 2025 the color will be blue is this car for sale let's say that is true instead of accessing car 1's attributes let's access car 2's attributes that would give us a Corvette the year is 2025 the color is blue for sale is set to true or even a third car car 3 equals a new car we will pass in a string of charger the year 2026 the color will be yellow is this car for sale let's say that is true as well then I will print car 3's attributes the model is charger the year is 2026 the color is yellow for sale is set to True with classes they can take up a lot of space for better organization you can place them within a new python file so let's cut our class and we will create a new python file within our project folder file new python file the name of this python file is going to be all lowercase car then we will paste the class that we cut originally class car which has a capital c so from our main python file we're going to import our car file our car module mod from the name of the module car import the name of the class car then when I run this program nothing should change we should still have access to all of our car objects you could either keep your classes within your main python file or import them if you would like to organize things let's talk about methods methods are actions that our objects can perform within our class we will Define a method of Drive self is going to be provided to us when we invoke the drive function let's print you drive the car what other things can cars do let's stop we will print you stop the car let's take car one access the drive method you drive the car car 2 also has a drive method you drive the car same thing with car 3 you drive the car let's access the stop method car one. stop car 2. stop and car 3. stop these methods are identical for each car object instead of printing the word car let's insert the model of the car I will convert these print statements to F strings instead of the word car let's add a placeholder let's add self. model self is referring to the object we're currently working with use the attribute AIS operator followed by the name of the attribute let's also do this with the stop method sell do model let's take car one use the drive method you drive the Mustang car one. stop you drive the Mustang you stop the Mustang let's do this with Car 2 you drive the Corvette you stop the Corvette car three you drive the charger you stop the charger now within our F strings let's also insert let's insert the color I'll add a placeholder self. color do this with stop as well self. color you drive the yellow charger you stop the yellow charger car one you drive the red Mustang you stop the red Mustang card two you Drive the blue Corvette you stop the blue Corvette let's add one last method let's create a method to describe our car we'll print the details of the car let's print I'll use an F string add three placeholders let's print self doye followed by self. color then self. model we'll take car 1 use the describe method that we created describe car 1 car 1 is a 2024 red Mustang describe Car 2 Car 2 is a 2025 blue Corvette car 3 is a 2026 yellow charger all right everybody so those are objects in Python an object is a bundle of related attrib attributes attributes are variables that an object has and methods methods are functions that belong to an object they Define what this object can do and well everybody that is a summary of object-oriented programming using python
Info
Channel: Bro Code
Views: 11,338
Rating: undefined out of 5
Keywords: python tutorial, python course, python OOP, python objects, python object oriented programming
Id: 1XE-_s4ZBT8
Channel Id: undefined
Length: 12min 17sec (737 seconds)
Published: Mon May 20 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.