Object-Oriented Programming with Python in 2024 | 7-Hour FREE Course for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're ready to take your python skills up a level this is the video for you over the next 7 hours I'm going to take you on an objectoriented Journey with oop program taking you through these Advanced crucial Concepts in Python and we're really going to focus on two key Concepts encapsulation and inheritance this course Builds on your core foundational skills so far in programming and it's an extension of my python for absolute beginners course that I released at the beginning of the month right it's still absolutely free that's the best part 7 hours of more premium content coming at you for free if you guys enjoy this stuff do me a favor for this course like And subscribe to this I know right but liking helps this course reach more students people who could really use this course and comment your thoughts below if you've been feeling overwhelmed by this topic o right let me break it down for for you I've got you covered by the end of this course you're going to have mastered a crucial concept not only in Python but in programming Concepts as a whole with over 7 years in the education space I've created interactive lessons in materials as well as live coding exercises that's right all these challenges to further your understanding l p if you're ready I've been ready let's Dive In [Music] welcome to a code with Josh special don't skip this part guys all right the next few minutes are the most important few minutes of this course because I'm going to break down who this is for is it even for you all right and who this is going to work best for how to go through this course all right so just give me a few minutes here right this is my extension to really break down a key crucial Concept in programming right o or objectoriented program all right now this is a topic that I did Cover in my course python for absolute beginners I'll put that here all right that was still free for you right but it's a rather crucial and advanced concept this course is dedicated to furthering your understanding in this crucial concept of oop through encapsulation and inheritance all right now if you're new to programming this is not the course for you okay you should understand python as a whole already right control structures functions modules all right you should maybe have worked with classes before if you haven't that's okay right because I'm not going to teach program the basics of that I've done that in my previous course all right now here is going into o we're going to further that and I want to emphasize too this is not a Project based course all right I do have projects in this but I want you to think of these projects as more like challenges or scenarios as we work through and introduce you to more advanced oop Concepts now guys also this is still 100% free right I'm not getting paid anything for this but I'm hosting this course as well as my other course for you guys on my own platform Zer towing.com I'm going to put that as the first link in the description all right head on over there and you get a boatload of free resources for you right my handcrafted python guide you get an Interactive Learning Community to chat with others and learn that's key all right if you're going to hold yourself responsible and you want to progress and learn don't sit here and watch a YouTube video it's not going to happen all right by learning engaging asking questions answering questions that is how we learn right so check out the platform it's absolutely free it's the link in the description all right um and that's just going to bring you guys a smoother viewing experience right it's still free and uh so use it all right now this course guys I have timestamps use the timestamps if there are topics you know Skip ahead to that topic indulge in it comment ask questions all right and code along the way all right I'm really excited for this right any questions let me know all the resources I have for you guys are in the description all right I think I think that's it over to the course [Music] enjoy hi and welcome to the zero to knowing object orientated programming and python course my name is Josh with over five years in the education field for which two of the last have been specifically with python I've taught teens all the way to young adults I've taught hundreds of students and I'm here to share my knowledge with you let's jump into a general course overview of what you can expect throughout this class throughout this class you are going to build a strong foundation in the fundamental concept of objectoriented programming you're going to use your existing python skills as we grasp New Concepts and you learn new ways to implement your logic the knowledge you gain in this course is going to be instrumental in your programming Journey from here on out and as we go through our lessons we are going to build on each previous lesson and this is going to give you a good understanding of O and the general concepts that come with it I've seen many students struggle with this topic or try to speed past this topic topic of objectoriented programming this course is designed to give you a strong foundation in those important fundamentals that we will use in any o language many online courses have too much and some of the fundamental concepts get brushed over because of too much material all-in-one courses don't spend enough time strengthening your foundation skills I've broken this course down to a level that's easy for you to understand this course is structured in the best possible way so as you progress through it's gradually going to get harder building on the skills that you're learning from the previous lessons throughout this course you will get an overall idea of what is objectoriented programming you will spend time working with different objects spend time defining your own classes and objects we together will grasp the concept of class inheritance as well as the concept of multiple class inheritance and finally to end this course you are going to learn about Dunder methods or the special methods that come with objectoriented programming in Python who is this course designed for this course was created for beginners to intermediate students in mind and will not teach you the fundamentals of python but rather the fundamentals of oop students who join this class should already have a basic python understanding such as what are conditional statements Loops functions and the data structures we use in Python this is not a project-based course but rather a challenge-based course to really hone in your understanding of o key Concepts in the overall logic we need to solve these problems it's designed for those of you who want programing broken down to a different level of understanding it's designed for the visual Learners as well as challenge based Learners throughout this course you will have slides for our new topics live coding exercises as well as quizzes I can't wait for us to get started and to take a closer look at object or orientated programming I'll see you guys in [Music] class before we get started with our course this video is for those of you who do not have python installed on your computer and do not have an IDE like vs code if you have both of those you can skip this video and head into the first lesson if you don't this is the right video to help you get set up first up let's get Python and install that on your local environment your system on Google I would like you to just Google Python and it should be the first list link or it's going to be the first unsponsored link I'm going to click welcome to python it takes you to the python page now here is where you can find any documentation or anything to help you with python what I'm interested in is I want to scroll down and we see all these subtitles I have get started download docs today we're interested in download and I'm going to click the latest version python Pyon 3.11 now do not worry by the time you watch this video this version of python may have changed and that's okay because currently what I'm on is python 3.10 as you can see this is 3.11 so it's it's fine at the very bottom you are going to see these files now it's very important here if you are in a Mac you are going to click the Mac universal installer if you're on a Windows you can click the windows installer 64bit I'm going to click Mac CU I'm on a Mac and then in the top it is going to download python once python is done downloading you need to go through the steps for installation so open it run it and go through those steps I'm not going to do so only because I already have this on my system great over on Google we need to get a an IDE now you have plenty of options for this you could use Sublime atom or what I personally use is VSS code so I'm going to go into Google and I'm going to search for VSS code there it is it is the first link visual studio.com visual studio is great and it's wildly popular amongst developers around the world if we go in here you can just click the first link it takes you to the homepage you're GNA see a big blue button here now depending on your operating system this will change I am on a Mac so it tells me to download Mac universal if you're in a Windows it's going to tell you to download the windows Universal you can click here and then just like python it's going to download in the top right once VSS code is done downloading you can open it and go through the steps for installation I I highly recommend that you get a shortcut and you save this to your desktop or your icon bar at the bottom of your window as we're going to be using vs code a lot it'll be handy to have a shortcut I'll see you guys in the next setup [Music] video [Music] hi and welcome to the zero to knowing object orientated programming course this is lesson one before we jump in and start coding or anything like that we need to start at the very beginning and we need to discuss what is is object orientated programming or simply put O Let's jump in to further discuss and break down what exactly oop is I have made a list of bullet points it's a way to organize our code this makes it easier to understand manage and reuse all objects that they are like blocks of code and they each have their own properties attributes in their own functions methods simply put if I could explain it like this is objects are like building blocks or like Legos and they can be used to create more complex problems or if you're building with Legos more complex designs in O there's a big word and it's called encapsulation and this is hiding all the internal details of one specific object from the outside by hiding this it makes it easier for us to manage and for us to understand the code then the Holy Grail of oop we have inheritance this makes programming with objects amazing inheritance allows for the creation of classes now going forward I want you to think of a class as a blueprint and this can be used to create multiple objects with shared properties and methods this is going to be a powerful tool as we go forward let's take a look at what an object really is so on the screen each object right so I have object one object two object three they each have their own attributes so in this example an attribute could be considered color they each look different they have their own color and they have their own unique actions but at the end of the day all of these are objects or all of these are squares per se all right so just remember an object has its own properties its own attributes as well as its own actions what can this object do here's a fun example I've put together and going forward in the coming lessons I'm going to use examples like this here is an example of objects now they are all birds we can see that they're all birds and they share some of the same properties but then they all have unique properties of their own so some common characteristics of all of these objects they all have two legs and they all can fly those are shared properties some unique properties for example is I have a type of bird hummingbird this hummingbird is only found in the Americas these are unique properties that only this bird has so you can see we have four different objects and an object I want you to remember this word instance I prefer to use the word object but in other programming documentation you will probably see this word instance more than once an instance is an object it's an example from a class so this object is from this class now a class is like a family it's a group of similar objects Under One Roof all of these are unique Birds but at the end of the day they are all birds if I asked you what animal is that you would tell me it's a bird if I asked you to be very specific what type of bird is that well that bird is a parrot and that bird is an object this is how object orientated programming is broken down let's move on a little more I'm going to give you guys a brainstorm Challenge and I want you to take the next five minutes I would like you to think of three different classes remember family and for each class I would like you to think of three objects as an example if I go back I have one family Birds then three objects would be owl hummingbird Canary that's one example I would like you guys to think of three more classes each class with three objects pause the video here write down down what you come up with pause the video here and write down what you come up with before moving on great some examples that I've come up with I have put in the source code for this lesson and I've also put it here these could be three examples that I came up with I have a class A family of vehicles right each of these are different object at the end of the day they're all vehicles but when you get specific one object is a car one is a truck one is a van and one is a luxury vehicle they're all different types I have a class for furniture but Furniture is very broad what are some objects that come from this class chair sofa table bed finally I had a class of countries and then we have us Thailand Vietnam and France they're all different countries at the end of the day they are all a country they come from a class countries great I would love to hear what you guys came up with so be sure to put it in the Q&A of this lesson before moving on to working with objects in lesson two at this point in your python Journey you should all already have a decent understanding of the following variables functions Loops conditions as well as some basic python lingo you can see on the screen I've made some categories in the first one I have functions now when it comes to functions you should understand both of these how to define a function and if I use the word parameter or argument you should understand what that is too with loops you should know how to set up W loops and for loops and why we use both of these you should understand our conditions and conditional statements which means creating your own and all the logical operators that come with that you should know some basic data structures this could be your dictionaries as well as lists and tles all of these were acquired over the fundamentals of python which you should have learned going forward with object orientated programming this is more of an intermediate to an advanced topic I will be using function Loops conditions and data structures throughout this program if you're unfamiliar with any of these please look some of them up before moving on I will see you guys in lesson two working with [Music] objects welcome back to lesson two in the last lesson we introduced what exactly is objectoriented programming in this lesson we are going to begin working with objects in Python this will strengthen your understanding of what an object is and how we use this to work with objects I am going to introduce a special python module to work with object objects I am going to introduce a special python module this module is called turtle and turtle is a module in Python which allows us to draw and create art but you're probably asking Josh I signed up for a course on object-orientated programming why are we doing this module although this seems like something for kids Turtle module is a Hidden Gem for learning oop throughout my years of teaching I have seen Turtle strengthen the fundamentals of objects in my students that is why I'm using it in this course and this module is going to give you a strong understanding of how objects work in programming and how we can use them throughout our code in the picture on the left we have nine objects all of which are turtles each turtle though has its own properties so its own color and it very well may have its own speed and they all have different actions that can do different tasks now two new key words that I introduced in the last lesson what are methods and properties let's dive in and let's take a look a method is a function all right so simply put if we boil everything down a method is a function do not forget that specifically it is a function in a class a property is a variable specifically a variable in a class if you have to say that a few times that's okay a method is a function in a class A Proper is a variable in a class I want you to think about how do you use variables how do you define variables good got it how do you make a function how do you use a function you should know those and you should start to think about those on the left I have three different properties I have a color a speed and a width if we think of these like a variable which you should I have my variable and then the value to the variable is here so our color its value is maroon parrot speed its value is five Canary width its value is two now a property and a method must be linked to an object in order to work so color is linked to the object owl speed is linked to the object parrot and width is linked to the object Canary if you guys remember from the last lesson I showed us an illustration we had a class or a family which I called birds in the bird family we had four objects we had an owl hummingbird Canary and parrot and those are the objects that I am linking to my properties very good on the right I have an example of three methods it looks the same as a normal function it has a unique function name and it's followed by a set of parentheses just like a property a method must be linked to an object in order to run or work just what I had mention right as you can see all my properties and all my methods are linked to a specific object so let's take a look at how to set up our code we are going to be heading over to VSS code or an IDE of your choosing very shortly now the code on the left here is is actually outputting the image that you see here on the right you should have a basic understanding of how modules work at the top we are importing this module Turtle I am then creating two objects okay so an object is just like a variable but the value of the object is a class so so T1 is an object turtle one the value of T1 is a class my class is Turtle it's capitalized and it's followed by a set of parentheses once I've created my objects I can then set the properties of each object so T1 is going to have the color of orange T2 has the color purple I am then linking two methods to each object I am giving them actions I'm giving them instructions of what to do next when I run this code we can see the output on the right but let me break that down even further here we have step by step of what's happening and this is how I like to use turtle and create object we're importing everything at at the top of our code once we have our Imports I can then create any objects I will use I could create one I could create five as well we are then assigning the color property to each of our objects and then I am linking our two methods to the object in order to get them to move the final function you see here on the screen is done this is a special function that comes with Turtle it allows the drawing to stay on the screen once it is completed great I've shown you some basic Turtle code I'm actually going to show you some of the functions the methods and the variables or the properties we will use in this lesson let's check them out all righty so on the left I have my column of some properties and methods you will use and what they do along the way okay please note that all of these must be linked to an object because they are properties and methods we have the color property and this assigns a color to the object now it's important to note the color inside is a string the second method is width and this is the width of the line you're drawing automatically the width of the line is one if you would like to increase that you can put an integer inside which will change your width the turtle is just the name for the object and we can actually change the shape of all the objects you see on the screen you can change it to Circle Arrow Square Turtle as well as a triangle you can use the speed property which speeds up the drawing itself the maximum speed you can do is 10 in order to give your turtle motion or action we have the forward method you can use the word forward or you can use FD inside we put the number of pixels you want to move in that direction and finally on this page we have our directions we have left and right both of these methods take a degree how many degrees do you want to turn left 90° would make you a square if you did that four times very good take a moment if you need to pause the video here take a screenshot I'm going to move on to a few more properties and methods here are a few more so we have a method called up now if you imagine that your object your turtle is just like this pen I have on my screen I'm drawing right now if I want to move to a different location I need to up I need to pick my pen up and move it to a different location I can use the up method to do that I can use go to to justify which coordinates I would like my object to move to now this does take an X and Y position once we've moved to a new location I can then put my pen back down and start drawing again great the steps in order to move your object up go to a new position put it back down you can then draw again in one of the challenges I'm going to ask that you make a circle now if you're up for a good challenge you can try drawing a circle with just your two methods forward and left if you want to make it easier Turtle has a circle method inside you put the radius how big do you want the circle if I said T1 Circle T1 is going to draw me a circle with the radius of 10 n good the final few methods there's so many that's why I encourage you to go back through these slides read the documentation online and see what you can do with your objects if you want to fill in your drawing which I'm going to show you shortly but you want to have for example your Square to be a solid color once you're done drawing you need to use the begin fill method we use this before you start drawing and once you finish drawing you can use the end fill method these two working together will fill in your object with a solid color the very last line of code you should be using is done this allows your drawing to stay on the screen once it's completed very nice that's a lot of methods I've gone through 12 actually all right so using turtle with the basics of python in lesson one at the end of that lesson I said that you should already have a good understanding of how to make functions Loops conditional statements all of that basic knowledge you can use right here in Turtle to hone in your skills while now we are working with objects so take a look at my code right on the left we see normally I am importing Turtle I then create one object T1 remember an object has the value of a class I assign my object to properties color and width great I am now ready to start doing something with my orange object I want to fill in the shape I draw then I'm using a for Loop you should know how to use a for Loop I'm saying for I in range five that means my Loop is going to repeat five times every time two things are happening my object is moving forward 150 pixels before it turns left 144° once it's completed it's going to fill in my illustration which produces an output like this an orange star you can actually see my object here on the left of the screen that is what your turtle will look like if you do not give it a starting shape very good let's take a look at that example a bit more right so just by using that Loop that 4 I in range five that's producing the orange star you see on the screen if you did not use a loop it would be very repetitive because you would have to write this four more times I saving you eight lines of Code by using a for Loop right so experiment use the the programming knowledge you already have and tie that together with working with objects in Turtle great taking a look at a more complex example don't worry I will break this down for you even a bit more at the top we start with importing Turtle okay I'm going to work left to right you always start with your Imports at the top that shouldn't be anything new then I have defined two functions of my own I made the star function and the circle function for this purpose I am just going to work with the star function my star function takes four parameters it takes an object I'm going to give it a width I'm going to give it a size and a color then just like outside of a function t1. color right I'm taking my object T and I'm assigning it a property what is the color going to be well the color is going to be whatever I give to my parameters the same thing applies to width I'm giving my object a width that is going to be done when I create or call my function this code is the same exact code as you saw in the previous slide if I go back right here you see all of this except in this example I Define the object as T1 specifically in the following example I have defined one object T but T could be anything over in our code then right you can see that I have made T So T equals Turtle I'm going to give this object to my function our program runs we begin with asking for an input this is where I can enter a shape so if my input is equal to Star I'm going to collect a width I'm going to collect a color and I'm going to collect the size this will be my ARG Arguments for my star function I'll break it down a little bit more but I encourage you to pause the video here and really just read through the code you should understand 80 to 90% of this all right so you can see I've colorcoded that those three green variables width call and size they are being used used as the arguments to my star function those arguments are being passed up to the star function and being used as the values within my function the same thing is happening with circle if I enter circle in my input then I am asked for a radius and a color which I am using as the arguments to my circle function they're passed and they're used to create a circle pause here give it a good look right I'm about to give you guys your first challenges in vs code here we are I've put together five challenges for you guys number one I want you to draw a hollow Pentagon each side of a a pentagon has a different color number two draw four solid squares right so these squares should be filled in with a solid color all different colors but all in different locations how do we change a location you might need to go back a few slides number three I want you to create three functions each function will draw something different when it's called this is very similar to the previous example in the last slide go back and take a look but come up with your own shapes don't use a star make something else number four I want you to draw 10 circles each at a random location with a random radius so this might be new to some of you but if you hear the word random in Python you should be thinking about the random module now this one's a good challenge because it's going to require you to import the random module and use the randant function if you don't know how to do this do a little Googling and finally your last challenge I would like you to create three different objects these objects will draw three triangles in a column each triangle is a different color okay now as help you can use the slides to help you please do I encourage you to do that but at this point in your programming Journey you should also be comfortable doing your own research or learning how you can check out the turtle Graphics module documentation just Google that it's going to be one of the first links you can also use these slides to help you now I'm going to head over into vs code and we are going to code this out I encourage you to try these challenges on your own I'll see you guys over in vs [Music] code [Music] great welcome guys to VSS code in this lesson we've introduced working with objects in python as we're now in vs code we are going to put some of that to use and get on coding now before I start coding reminder there are so many way ways to solve one specific problem any way you're able to do it well done amazing you should not have to do it the way I do it if you do it like I do great if you find a different way that's you being creative that's fine too all righty you had a chance to read through the tasks of this challenge before watching this video I highly encourage you to try these challenges on your own go back through the slides of this lesson read the documentation for turtle do your best to overcome these challenges if you get stuck watch the video or if you want a guide to follow along with watch the video too I'm going to jump in with our first challenge in the first exercise I asked you to create a hexagon with six sides each side with a different color there are many ways to do this I'm going to try and do this the more efficient way there is a longer way there are other ways choose your path first thing we want to do is we want to import remember all the Imports are at the top of our code so from Turtle I would like to import everything that's what the star means from the turtle module import absolutely everything I am then going to make an object so for the purpose of this let's pretend you're drawing on a piece of paper so I'll say pencil what is pencil well an object is always equal to a class my class is Turtle great I've made an object now I know that each side of this hexagon needs to be different so you could do this a few ways I'm going to make a list list how many sides are in a hexagon well there's six if you didn't know so I'm just going to make some colors here uh I'll say red uh purple orange what other colors do we have I think lime Green's a color in Turtle I'm going to put that in there uh I'll say light blue I believe that's a color what am I at 1 two 34 uh okay cool let's just say green dark green and then yellow all right we have 1 two three four five oh is that seven I have seven colors let's take away green okay sweet six colors now I want to repeat a task remembering the basics of python how can we repeat something a loop I can say 4 I in range I want to repeat this well six times right that is how many sides are on a hexagon so six times I'm going to take my object my pencil and I want to change the color of my pencil I want it to be one of the colors in my list specifically I want it to start with red then it'll go to purple and work its way through the list so to do this I'm going to take my list colors and I need to give it an index a position I'm going to say I so I is the first index the first time this Loop runs right the next time this Loop runs I will be the second element change my color uh let's just take this let's say the width of our pencil let's just put that at five my pencil can now move forward how far are let's say 100 pixels and then our pencil can move right 90° remember the last line of code I'm going to put here is done this will ensure that the drawing stays on the screen at the end great let's run our code I'm going to head up there run the code terminal appears I should see a window there we go the window is quite big right so I'm seeing an issue right that's drawing a square I don't want that who caught on why is it drawing a square let's go back to my code because I put 90 de that's for a square I should be putting 60° 60 * 6 that is equal to 360 that's the total number of degrees in a circle let's try that again boom there we go go there's our hexagon you can see that my turtle Graphics screen in vs code is quite large that's okay you're able to see what I'm drawing yours is going to be large too so we have a hexagon different color sides with a width of five one object pretty cool all righty I'm going to trash my terminal I'm going to turn all of this off except my import because I want to continue to used that for my other challenges in our next challenge we wanted to draw four solid colored squares each with different colors each in a different location this is weird this is really weird so in order to get this going we need to think about the word random all right now if you haven't used it before which you should have with the basics of python playing with modules there is a module in pyth python called random I want that uh I'm going to say from random import let's say random because that's all I want I want the randant function I already have Turtle so I can create my object let's say pencil again pencil is my object what do I want to do well I'm going to have four squares and they're all going to have to generate a different color a color can be RGB colors so red green blue with the maximum of 255 let's create a random color for each one red green in blue so I can say for I in range how many squares will we have four I'm going to have red green not Global green and blue let's start with red R equals random I can say 0 to 255 that way it's going to choose a random number between 0 to 255 each of those is going to do that because this is going to generate us a color now that I have a color I can just insert in here and I can take my red I'm going to divide that by two 255 we can take our green and our blue that's going to get us a nice unique color that I can use here in Turtle remember there are other ways to do this not just one now that I have a color we want to Define and create some location our locations are always going to be X and Y so a random location the left of my screen is going to be -200 the right can be positive 200 you should already have a good idea of how coordinates work and this should not be a new topic right but that's generally going to keep it on my screen I can go left 200 or right 200 with 0 0 being the exact middle every time I draw a square remember how do we move the pencil around well I need to take my pencil and I need to lift it up once the pencil's up I can take my pencil and I can go to a new position where do we want to go I want to go to my random X and Y that we just created here once I've gone to that new position I want to put my pencil back down on the paper great pencil's in a new position we are ready to draw the square so to draw our Square pencil let's change the color to the color that we just made our color argument right here is this one that's a random color we can then begin fill remember if we want to have a solid shape we use begin fill at the start and we will use end fill at the end great now that I'm in my Loop we can go forward 100 this time I can go write RT right you can also spell right and I'm going to say 90 much better once we've drawn that shape I would like to end my fill the last thing we're going to do is say done right let's run that code we have one object this object oh there we go okay so I have been given an error it's really coming from line 35 and all of this is being triggered so what I'm going to do let's color one equals color equals cool let me give that Oro see what I'm working with here there we go boom one square okay two three all decent locations it looks pretty cool I have a lightish blue one a mint green raspberry color and a gray Square that's the job that's what we were trying to do random squares in a different location with different colors if you really wanted to take this challenge up a notch you could have done random sizes for the squares too think about that how could you implement that in your code try and find out all right I'm going to close this trash my terminal and I'm going to turn off this code here all of it okay let's go down here we are now going to move on to the next challenge of working with objects you should start to begin to feel comfortable with what an object is how do we use it remember pencil is the object object the value of an object is a class and the class has all these special functions and properties that come with it pretty cool number three we wanted to create three functions that each will draw a different shape when they're called functions you should know how to make a function in Python that was the fundamentals of python if not this will be a quick refresh now remember I already have uh I'm just going to comment it out from Turtle import Star I already have that at the top of my code so I'm going to skip that part I'm going to come right back down and we can create our object pencil now I'm going to make some functions so what shapes do we want to draw why don't I draw a square a circle and a pentagon those are all good shapes so I'll make a function called square square is going to take really three parameters it's going to take an object that can be my turtle my pencil it's going to take a size of the square and it's going to take a color now that I'm in my function I can do exactly what I have been doing so I'm going to take my object and I'm going to set the color of this object so my object is t t color the color that I want it to be is whatever my color parameter is let me change this I'm going to say P for pencil that it's going to make it easier to understand so I now have the color that this square is going to be and I'm going to call begin fill on my object as well I can now go through the normal steps for I in range of four remember I'm drawing a square and I want my pencil to go forward how far 100 well you could say that but we have our parameter size whatever I enter as an argument for my function that is going to be the size of my square if I enter 50 cool my square is going to have a size of 50 all right then I'm going to say WR I'm going to start to use these other words now as I don't want to get too shorthand for you that's going to get confusing so object pencil is going forward the size amount the object is going to go right for a square 90° great um at the end what's the last thing we need to do here well I began the fill I now want to end my fill cool um just to kind of show you guys what's happening I'll put that in here uh I'm going to run the code real quick I'm not done by any means but let's just watch what happens I call my function I give it my object pencil I give it a size and I give give it a color let's run it on my screen we are going to see that red square appear right there and it's filled now you noticed it cut out real quick cuz we should have done as the last function here let me run it again cool great I know it's working that was a quick test let's close that trashing our terminal I'm going to turn off that function I called I'm going to make my circle function so circle is also going to take an object it'll take a not even a size it's not going to take that it'll take a radius and it'll take our color so I can say color is our color right very good keeping that the same convention I'm going to call the turtle Circle function and I will give it my radius now even before that that's just going to draw a plain Circle so let's say begin fill here and we can end with our Nill cool that is our Circle while I'm here I'm just going to make our Pentagon as well Pentagon objects size and the color the same things as the other two functions I can start with my color I'm going to go to my begin fill then because it's a pentagon I'm going to make a for Loop what's the range five it's G to be five inside that there we go okay so every time my object is going to go forward 100 my object can go left you can say right right cuz up here I said right left right up to you um let's say left for that is actually going to be 72 great a part of the challenge I want a way that I can call these functions more uniquely than this so I'm going to make like a loop and my Loop I'm going to be able to enter as an input what shape I want to draw pretty cool so let's say start is going to be an input and in here I can say a few things I can say say Square I can say uh Circle or I can say Pentagon and I'm going to make sure that they're all lowercase linking do lower while my input is not equal to oh I don't know let's say stop while input is not equal to stop if my input is equal to square so if I've entered Square I want to collect some information here I want to collect a size and I want to collect a color so I'm going to say size is equal to an integer enter a size color because colors in Turtle are a string I can just say enter a color as a string there then we will call our function I'm going to give it an object what is my object pencil what is my size well I'm going to use my input size and my color can be color nice one done I'm going to add an also if Circle in there um if circle is called I don't need a size I need a radius so I can say radius is our input right uh I will just say enter radius I'm going to copy color cuz that's not really going to change we need a color for Circle then we will call our Circle function let's give it our object let's give it our radius and let's give it our color nice one okay that brings us to our last shape so let's make one more elith and say also if our Pentagon was called what I can do is cuz I'm going to use a size and color I'm going to take what we said here from our Square the only difference is I'm going to call the Pentagon function give it a size give it a color too no good conditions completed without an else so I haven't put any warnings in here I don't have any exceptions so let's just say else anything else I'm just going to say uh error shape not found good do you guys remember what's the last thing we need at the end of a loop well a loop needs a way to break to escape a while loop can go on forever we don't want that that's a problem so you need to be thinking about ways to break that Loop the one way to break it is I can just take my input copy that and I'm going to put that at the end of my Loop that way every time my Loop runs it's going to ask this question again and it will check to see my input at the start of my Loop nice we've done a lot go back read through the code most of it should be familiar if there's something that's not familiar with you really break that down if you need to watch that part of this video again do so run your code experiment with it and let's see what happens I'm going to run this code I'm going to make my terminal a bit smaller I don't really need that cool so as you can see here I need to put this over here Turtle still on the screen at the bottom of here I'm seeing Square Circle Pentagon so I'm actually GNA enter and I'm gonna say Circle enter radius let's say 25 color blue enter I'm going to go back here there we go I have a blue circle on my screen nice one okay let's go back I'm going to do Pentagon Pentagon enter size 150 color orange going back oh there we go ah all right so I did not begin and N Fill with the Pentagon it's not doing anything with that H let me fix that but let's do the square first Square enter 100 uh purple oh what did I do wrong shape not found Square okay H spelled Square wrong so I fix that before I move on let's just go to Pentagon let's just say p. Nill trash my terminal reopen our code to get it working there we go uh let's go into our code let's do Pentagon this time Pentagon 200 purple there we go great now it's filled right so I can now type whatever I want and whatever I type is going to be drawn onto our screen oh matched perfectly that's cool I'm going to type stop that is going to break my Loop right so then our code is done now very very nice I'm going to turn off all this new code right so what I'm doing here is I'm touching on all the topics that were introduced in the fundamentals of python now you might not be a master at these topics but by going through them now you're learning two things one how we work with objects and two how we can use our fundamentals of python to build while working with objects great guys I'm going to head over to the next video we are not done with these challenges I just want to give you guys a chance to review what we've learned in the next next video I'll be back here in vs code and we will finish up those final two object challenges I'll see you guys in the next [Music] video great here we are in the last video for working with objects where I'm going to finish up the final two challenges that I've given you guys in the next challenge we need to draw 10 different circles all at a random position and a random size I love random all right so I have Turtle let's just go up here from random anytime you hear that word random think the random module I'm going to import everything great let's make our pencil so our pencil that is our object the value is our class what I'm going to do here is I only want it to go when um I enter something into input that way it's just not going to it's going to wait for me to start so let's say like start you don't have to do this part you can just do a for Loop so I'll start with that and then I'll go back to what I wanted to do how many times will we repeat this 10 the challenge is to draw 10 circles every Circle needs something it needs a radius if you want to do a random color that would be a really good challenge I'm not going to put that in here though so so radius is random literally pick anything I could say five to One n to 100 let's say 5 to 100 I have a random radius I need a random X we will keep with the statistics of2 200 and 200 uh and I need a y like so okay we have everything we need to start drawing what I'm going to do is I am going going to take my objects so pencil pen up or up right either of those two work I'm going to keep with up I want to pick my pencil up I want my pencil to go to a new position using the X and Y that I've created here right this is a random value this random value is being used for my X position our pencil can then go back down to begin drawing all we need to do to draw a circle quite handy quite useful we saw this in the previous exercise take our pencil call the circle method and I am just going to call Radius radius that is the input that we collected up top here it's going to be a random radius awesome let's run it that's all we needed to do to get this working when Turtle loads it is slowly going to go through and you can see they're not being filled in I didn't ask you to fill them in I did ask you to put them as a random size all in random positions and by saying -200 and positive 200 it's keeping them all in that general area nice really cool guys I'm going to say done so I used one object from a class and I used that object to draw 10 different things or do 10 different things that's great okay uh if you wanted to right I could say like start and I could say input and we could say while start is not equal to off then all of this is going to go and we could take our input put that at the end the only difference this is going to do right is it's going to do that I need to enter something anything right so I could say apple it's going to go back it's going to start drawing by using that while loop with input it's just waiting for the user to tell python to do something before starting additional things from the fundamentals we can use with objects all right that brings us to our last challenge our last challenge I love this one we want to create four different objects each object is going to draw a triangle but these four triangles need to be in a column each triangle is a different color ooh what can we do here for this one so first thing I want to do is I want to create four different objects so far we've just been working with one but I can create multiple objects from the same class and that's what I want to do here so instead of saying pencil I'm going to go shorter I'm going to say T1 I'm working with Turtle so turtle one Turtle class Turtle 2 Turtle I'll fix that and uh T4 equals Turtle nice so this is going to parallel going into our next lesson I have four different objects remember each object has its own data and can do its own thing what I'm going to do in order to do this now you could have made one object right um the thing is they won't all go at the same time you will need to wait for one object to draw and then go to a new position I know I'm going to draw a triangle so let's create a function for triangle my triangle is going to take one of those objects one of those Turtles as an argument it will also take a color right so what I can do here is I'm going to take my object that I'm going to call color give it a color right I can say begin fill because these triangles are going to be a solid color and then we will do our for Loop triangle has three sides our range is three every time our turtle is going to go forward let's say 100 and let's say uh left ooh what is our left it's not 90 be 120 three sides in a triangle 360 / 3 is 120 T and fill very nice our function is done which is great I want a way now that I can iterate through and I can draw these triangles at the same time you could be thinking of different things and you may have already solved this problem what I like to do is I love loops and I love to iterate through things so I'm actually going to make a list called T right and I'm going to put all my objects in this list and for the purpose of this let's call this Turtles can I do that Turtle that's better let's say for every turtle in my list Turtle I want to do something with the turtle I'm on SO the current Turtle That's T I want T I want that Turtle to go up I want that Turtle to go to a new position now I want them to be in a column so the X X that's not going to change but the Y is going to change I need a way to change my y hm how can I do that well we could create a counter variable right let's say y initially is 100 that means the first turtle is going to go to x0 Y 100 100 my turtle can go back down on the paper and then it would do something right but now the next Turtle needs to go to a different position let's change the value of that y variable and let's say y - 100 so every object T1 T2 T3 T4 is going to go to to a slightly different y position are you with me pause it read through see what's happening now the final things we need to do I really just need to call my triangle function and I'm going to say T1 let's say blue as the first color I'm going to say triangle let's say T2 let's say red let's say triangle let's say t three give me a color let's do purple again uh and then triangle T4 excuse me and we will say uh blue did I do blue I've already done blue green there we go who remembers the very last line of code done wow you've done it let's run it let's see what's happening now boom you see four different objects on the screen too that's amazing they're all in a perfect column nicely done if you got any of these challenges on your own you're off to a good start and you're going to do well in this course if you found some of them quite challenging that's okay just know that this course might be a bit harder than you initially expected try to spend some time reviewing the fundamentals as you go through this course such as functions loops and conditions I'll see you guys over in the next lesson in the next lesson we are going to start to Define your very own classes and objects I'll see you guys there [Music] hi and welcome back to lesson three of the zero to knowing course in the last lesson I introduced the topic of objects and how to work with objects in Python in this lesson we are finally ready to Define our own classes and objects if you remember from lesson one I said to think of a class as a blueprint this still applies now a especially going forward a class is a blueprint let's jump in to this lesson if I were to break it down so as I mentioned a class is like a blueprint or it's a template for creating many objects so on the left I have a sketch or a picture of a house and this sketch is our class it contains a general overview of this type of object or it contains the general information you need in order to make an object on the right we have a completed object and this is what we created from the class using a description an object of a class now I want you to kind of remember from the last lesson do you remember this T1 equals Turtle I want you to say that 10 times if you need to get really annoyed by it T1 equals Turtle T1 equals Turtle T1 is an object the value of an object object is a class turtle is a class so here I have my instance or my object the value of an instance is my class now remember a class is always capitalized it is one of the very few things in Python you capitalize a class is followed by a set of parentheses capitalized parentheses let's take a look at a few more objects so here we go you remember from the first lesson I introduced a class of birds well let's take a look at a few more examples as you can see below these are all objects or they're all animals right at the end of the day it it doesn't matter in English all of these are animals that is our class they all have additional Properties or actions of their own right so the owl is the outcast object it is an object it's an animal it has its own name has its own abilities has its own properties okay next up we have three land animals let's call them and they share two of the same properties each of these objects have four legs and a tail but they each have their own properties such as the type of animal and location as well how many objects do we have four how many classes do we have one our class is animals our objects are owls raccoon lion and giraffe let's take a look at another so here they are all phones sharing some of the same properties they all have some of the same properties and actions of their own but then they all have their own unique as well creating a class before we create any objects like a raccoon a lion we need to create a family we need to create a class in this class this is where we Define all the unique properties and all the unique methods that our objects will have but first we need to look at an important function this function is called a Constructor function but really it's called our Constructor method right what is a method a method is a function in a class and this special method is automatically ran when an object is created it creates an instance it creates an object of a specific class this word Constructor what does it look like Constructor looks like construction what is construction build we use the Constructor method to build something to to build our class this method is called in it okay now you can already see and it looks weird you've never seen this before it has two underscores in the beginning and after in it that's very important okay we must use this as our Constructor this is always the first method in our class in just means to initialize initialize just means to start right so every time we start our class or we create an object we automatically use the init method creating our class we're finally here right we're starting to see some actual code come together I'm going to break this down for us if you need to rewatch any of this please do because this is a very important topic object orientated programming so on the left we have all of our code and you can see that I'm starting at the top with my class to define a class we use the class keyword and then we give it a unique name just like a function but the first letter is capitalized then all the code inside is just like code outside you would make a function the same way but this is a method why is it a method because it's a function in a class our first method is our Constructor in it in it is taking one two parameters just like a function takes parameters these parameters are the value to my properties at the end of our class I make an object car the value of an object is a class my class in this example right remember T1 equals Turtle well my T1 is car my turtle is my class I am giving my class two arguments because in it has two parameters that's very important I can then use my drive method and I can link it to my car object if you're a little confused don't worry I'm going to break this down even more but before I do you see a new word on the screen what's that new word boom we see the word self this is very important we use the word self in a class now self it's basically a key okay and it unlocks the class so we can use all the properties and the methods anywhere we want throughout the class self is a key you must have it as the first parameter in any method okay but it's important to note that you never use the word self outside of a class all right let's do some color coordination now okay here we are pause the video okay if you need to just use the color coordination to help you all right so at the top of the screen the first thing I do is we Define we create a basic class I named it my class okay up next the first method in our class is our Constructor method this is where you will Define all the variables all the properties you will use throughout your class okay each parameter is the value to one of the properties next up we Define a method named drive this method this function uses the values from our two properties and gets the sum right so I have a variable X and the value of x are our two properties added together I can use my properties because I am using the word self remember self unlocks our variables and functions to use them throughout the class finally at the end of our code I create an object named car and then I give my two arguments to my class 50 and 60 50 is parameter 1 60 is parameter 2 I then link my drive method to my object and this is how we call a method because a method must be linked to an object right here we go so when I call my drive method the output in our terminal it would say the speed is 110 kilm per hour okay as you can see I added on a little extra there right but because my two arguments are being passed up here they are the value to my parameters I can then use my properties which which I'm adding together here and then I'm converting that integer to a string which is being printed out inside our terminal pretty cool huh everything's starting to link together this is good all right let's take a look at another class okay so I want you guys to pause the video read through the class okay I have two questions on the screen at the bottom try to answer those before I do all right I have a class called app you can see that's created at the top of my screen here is here's app the first method is our Constructor I am creating three parameters users storage and username for each parameter I am creating a property and remembering the value of my property is my parameter that's all in it is doing it is a place to keep create and store our variables we will use in the class I then have two methods my first method is login this is just a normal condition if username if my property self. username is equal to owner and self. users is greater than one then I I can print off welcome username your storage is the amount of storage right so right here what is it going to print off think about that then I have a method called increase capacity increase capacity is its own method in my class it takes one of its own parameters I can use my property self. storage and I can add that parameter to it before I return self. storage so were you able to answer my questions what is the name of my class and my objects well let's take a look okay our class is app our objects we have two product one product two remember the value of an object is a class go find that now my next question for you guys at the bottom of the screen what are the value of my properties pause it answer it there we go the value of my properties are my parameters users storage and username self. storage is equal to storage next question bottom of the screen where are the arguments that will be given to my class right there these are the arguments remember in order right it's very structured users storage username users storage username right in that order these are being given to my class wonderful okay so here's a breakdown I took that same class right and all we're doing is we're going top to bottom because that's how we design our blueprint remember our class is like a blueprint we design the blueprint before we create an object before we create the finished product we create a class called app our method login has a condition which checks the properties against a set value that set value is owner and the integer of one we create a method called increase capacity and all this does is increases the current value of our property self. storage then finally once we've made our blueprint our class we can create an object called Product one or product two and we can call the methods we want okay so here's even a bit more right I've kept a few of the hints from the last slide but I've added a bit more in the new ones I've added we set up our Constructor with the properties we will use throughout this class we have a method that increases the value right and then we have our product to what is this going to Output well let's take a look if you haven't guessed it yet which your mine should be ticking your wheels should be spinning what will this be outputting well let's start with our first object our first object was product one right and product one is taking the arguments 35 256 an owner when I call the login method method those are the first two lines it's going to say welcome owner owner and it's going to say your storage is 256 then I also called the increase capacity method and I gave it the argument of 50 which is being used as my parameter it's going to take my original storage which was 256 and it's going to add a number to it that updates our storage to 306 if I take my second object and I call the login method it's going to show login denied why well my username is Josh if username is equal to owner do this otherwise login denied so my name was not owner login was denied great a lot of things happening right so go back through these read over these see what's really happening once we get to vs code it is going to start easy but I'm going to give you a few challenges and it's going to get harder rather quickly it's meant to challenge you and it's meant to help build you that strong object orientated foundation in Python before vs code let's take a quick flashback to what you were learning before objects here we are a class is a big group of methods and each method contains various elements that you've already learned in Python like conditions Loops lists diction Aries and more right so everything you see on my screen you should feel fairly confident with if you don't I think you should stop now you can go back to Turtle because going back to Turtle you will reinforce your understanding of objects but you can also reinforce and practice your understanding of the basic Python Programming Concepts like conditions loops and functions a quick recap a condition is really just if something is true do this otherwise do something else that's what a condition is if this is true print hello also if name is Josh Print it's me Josh anything else print goodbye that's how a condition is set up a while loop literally translates to while something is true keep repeating if it's not true stop right so in my Loop while count while input is not equal to zero keep repeating every time I repeat I'm going to ask for a new input and I'm going to check if my input is zero it's going to keep repeating right our for Loop if I literally translated a for Loop which I do do that for every item inside something I want to do something with that current item right so I have a list ages one two 3 four five six if I take my Loop for every age in my list ages if that current age is less than or equal to 17 I'm going to remove it all right so my Loop is going to go 33 and it's going to take 33 if 33 is less than 17 nope my Loop is going to keep going and it's going to check every number oh it gets to 15 if 15 is less than 17 that's true it's going to do something right you should feel very comfortable with these all right let's jump in and check out some challenges do not go back okay this is our first challenge in VSS code where we are building classes this is a point where we should try to avoid going back from and I want you guys to try to recreate the code from the previous slides and use it as an example to build your own first class in vs code now remember that this class has one base class and I called it app inside app we had three methods in it login and increase capacity login checked if the username was equal to owner if it was it printed off welcome as well as the value of your storage increased capacity allows us to add a number to the current storage finally create two objects and both of these objects call both methods head over to vs code give this challenge a try and I will see you guys there for the next few challenges I'll see you in vs code [Music] all right great guys here we are in vs code in this lesson we have introduced the topic of defin your own classes and your own objects and that's what we're about to do here in VSS code is to strengthen those foundations and build on what we have been learning in this first challenge I asked you not to go back and I wanted you to try to create the app class from the lesson here in VSS code in this video that's exactly what we're going to do so coming up top here we can use the class keyword to define a new class I'm going to call this class app now remember our class is capitalized everything after is inside the class what's the first method we need to make who remembers what's that special Constructor method well it's in it remember in it means initialize or start and the first parameter inside any method is the word self self acts as a key and it unlocks everything for us to use around our class what were the other three parameters we had users we had storage if I can spell correctly and then we had username for each of these I am going to make a property so self. users equals users self. storage equals storage and self. username equals username like so very good our init is defined the first official method we had was login so I can make login remember give it the key so we can unlock it later this condition check to see if the username was owner and to make sure we had at least one user in the system so I can make a condition and I will say if username but if self. username right because a property it's just like a variable it's the same thing but because we're inside our class in order to pass this variable around we need to use the word self so our variable is called self. username if self username is equal to owner and our self. users is greater than or equal to one we would like to print off a few things the first thing we can print off is is welcome then we want to print off the username so I can say welcome username then I would also like to print off how much storage they have available your storage is let's just say and we can say self. storage like so very nice a condition doesn't need to have an else but most of the time I like to put one what would you like to happen otherwise and we can say you are not a user nice that method's done now remember a method is not the same as a function if I call a function like this and I run my code I'm going to get an error because this function is a method it's a function in a class it tells me login is not defined it can't find login anywhere I want you to think about why as I finish out this class great our next method was to increase our storage capacity so capacity there we go first word is self but then we're going to give it an additional number that I can do stuff with I want to increase my existing capacity so I'll say Self Storage plus equals that new number great I can then just print something like uh updated storage and we can just now do self. storage that's our new number very nice before I create that bonus method I'm going to just create our objects so let's say product one is equal to app um let's say their users they have 35 users 256 GB of storage and their name can be just like the video let's say owner we can then call our methods so the reason Logan didn't work before is because a method must be linked to an object in order to work work so product One login and I'm going to say product two increase capacity let's say uh 5044 that should give me an even 300 right um I'll make one more now I'm going to print off an empty space first then we can do product two is equal to app as well uh product two can have 12 users uh they can have 128 GB of storage and their name can be my name Josh for each of these I'm going to say product two do login what's going to happen there and I can say Product 2 dot I don't want to upgrade it let's just leave it for that let's run the code great we're going to see a few things happen here so awesome the first part so one two three that's really being printed right here right welcome owner my name is owner so if username owner and users is greater than one it's printing off these two then updated storage remember I'm increasing my capacity it so I've changed my storage from 256 to 300 very nice in our last one it just tells me I am not a user because I do not meet these two conditions nice one let's jump into that bonus real quick I asked you to have a bonus right that would check your upgrade so I'm going to call this check upgrade and check upgrade does not take any parameters it does take self right if our users so let's say if users is greater than or equal to our storage then I need to upgrade my storage so I'll say upgrade amount so I can collect an input of how much do I want to upgrade it at I'm running out of space for the amount of users I have so let's just say here upgrade amount like so all I want to do then is I can say self. storage plus equals upgrade amount like so cool very nice that will upgrade it and then um what I want to do then is else so if I do not need to upgrade my storage if I still have enough space I want to print how much storage do I have remaining how can you solve that how much storage I have remaining well let's print off and let's I'm going to say something like you still have then let's do here I need to do a math problem right so I need to do self. storage minus my users but I want to make sure that this is a string so I'm going to convert that to a string then just close off your string and say I don't know spaces open like so nice one I'm going to come down here for product two and let's say product 2. check check upgrade I want to see if it's time to upgrade run our code nice so it says you still have 116 spaces open why is that well 128 minus 12 is 116 that's exactly what we did here 128 minus 12 incredible guys I hope you have followed along this far let's head over into the next video and start with challenge two I'll see you guys [Music] there here we are in challenge 2 now I'm not going to read all this as I'm hoping you guys already have and you've broken it down to what we need to do but I am going to read the overall description of what we are going to be building out in this class the code we make it's going to represent an online course management system we are doing an online course this will help us keep track of online courses students and their grades and it allows us to do things like add new students see the course details and it's going to allow us to calculate the average grades from our students this is a lot okay so I want you guys to try to work through this one by one don't do everything at once do small tasks to work into this big problem what's the first thing we need to do create a class what's the first method make your Constructor method right step by step all right heading into task two I'm going to start out with us coding this I will build a class called let's call it online course that's what it is we will build our Constructor method giving it our key of self what information do we need here here well initially all we need is I need a course like the name of a course and then I need like who is teaching it so an instructor so in this course it' be me in the course name it would be Zer to knowing object orientated programming these two parameters I am going to create our property for remember every parameter we have we would like to to create a property for okay instructor close enough nice in the last one I'm not going to give my init this method but remember that init is a place we keep all the variables the properties we will use in this class so I can make a variable here too I'm going to say self. students now this is going to be a list that I can use throughout my program that's all it is right now great your first method that we needed to make was a method to add students to so the first method we needed to make was a method to enroll students let's just call it enroll students now it's going to need who who do we want to enroll well that's going to take a name right it's going to take a student's name and what I want to do is any student who is enrolled I want to add them to my list right remember the list we just made here self students append who do we want to append the student name very nice once we have enrolled a student I want to print off their information so to do this I'm going to use in Python you might know this you might not you might have seen this in other languages in Python it's called an F string I'm going to use an F string here because I just want to keep everything nice and easy and keep it all together so I don't have to use commas and pluses to add different types of data together this just makes it more convenient I want to print off the name of the student so Bobby has been enrolled in the what course self. course Bobby has been enrolled in the science course right the name of the course would be printed there nice that's all we're doing for enroll we add a student to the list and we just print off and say hey this student is now a part of this course all right right our next method is let's say we want course details I want the information about a course so it's not going to accept anything it's literally just going to print off detailed information about our course so here we can print I'm going to use f strings for all of these so the course name and then our course we can just say self doour we are going to print off here and in this print we can just print off the instructor name let's say instructor name and we can tap into our property self. instructor and then in the last one what I'm going to try to do is I want to print off all the enrolled students in the course now you could just print off the list honestly um I could just literally go here and just say self. students and that's going to work there's nothing wrong with that right um if you wanted to get a bit more creative I could do that and let's let's add in here and let's say I'm going to put like a comma right because each name is going to be separated by a comma and then I want to connect I want to join right and we haven't quite Ted touched on this yet right I'm going to use the join method what do I want to join together I want to join together my students like so all right so I'm given an error uh okay let's do this let's change the double quotes let's put single quotes as our outside ones are already double quotes there we go okay so now it's going to join together in one line now there are going to be new things that I drop in this course here and there that I didn't talk about in the lesson but that's okay because as you're now in this part of your programming Journey you should understand that certain things you might need to Google or research or find somewhere else and if you're able to do that that makes you a stronger developer I've made a method for course details well let's call that done okay nice um I'm going to make a method for completion or just call it completed course so like who completed the course when I say who it's going to need a name again right so if the name I'm checking for I want to see if it's in my list because if it is I want to remove that student from my list they are not part of my course anymore so if that name is in my list I can use a condition with my in operator then I want to remove that student from my list very nice um I can just print off and you could say something like uh course completed if they completed the course let's say name has completed the course that's cool okay else uh let's just print off and we kind of need to like warn the user and we basically need to say that the name you're looking for is not even a part of the course is not enrolled in this course nice all right we've made three methods right we're building on everything we've been learning that brings us to our last method guys let's do one for uh average grade I'm it's going to take self this needs to take multiple grades what could that be well that's going to be a list I've actually given you a list too and you should know how to find the average of something as well right you don't need to be amazing at math for programming honestly you don't right the computer can do that for you but you should have some basic understanding of math and this could be just algebra in general so how we find the average of something so to find this average I'm going to create a variable called total and I just want to add together my list so I can use Python's built-in sum function to do that and I'm just adding everything together now that I have the total we want to create an average and the average is going to be the total divided by the length of our list the total divided by the length of our list okay now that we have our average we can just go through you can print off anything you want to say the average grade is let's just say that let's just put here and I'm going to say average awesome uh in the beginning let's do a few things here let's make this more unique let's say course input let's say name and let's say student right because this is how a real program would run unless we're coming from like a database so let's say input let's say enter a course uh I'm going to make sure everything's lowercase okay that's just good practice uh let's say enter uh teacher or instructor right instructor that's what we want there lower and then the last one you can say enter a name because the last one is for our student so we can just say enter a name lower nice one okay we've been done with our class class these three inputs will be used as the arguments for our class so I can come down here um I can say create an object called course and this is equal to our class our class needs three well actually our class just needs two arguments it needs our course name which is course input and it will need our instructor which is name the grades I've already given you and this is going to be a list so 90 85 92 78 and 80 it's our list of grades we can use this now we can go through and I want you guys to call all four methods that we've just made so take our object course I'm going to link that to average grade and I'm going to give that my list grades I'm going to take my object and I want to enroll a student who do I want to enroll well I can enroll this into here is going to be enrolled in my course I can take my object and I can get my course information the details right let's just run this for now actually enter our course python instructor Josh name of a student Billy nice one the average grade is 85 that's the average from the list cool Billy has been enrolled in the python course oh nice all right that's going up top here uh course python instructor name Josh students Billy it's printing off the basic course details nice uh I'm going to call the last method here I'm just going to use my name actually so I'm going to say course uh complete completed course uh I need need to give it a name so I'm actually going to say name hardcode this because I don't have this in the system I'm going to say just Josh for completed course so we'll say python uh Bob instructor Billy there we go so I'm using my name hardcoded and it says Josh is not enrolled in this course right because well I'm not enrolled in the course if name if Josh in list remove else R I'm not removed nicely done guys that was your first challenge without anything from the lesson that we tried to implement on our own very nicely done I will see you guys in the next video for our next [Music] challenge [Music] all right wonderful welcome back to our next challenge if you haven't noticed this is actually the code from the previous challenge which in the text file for this one I included as a hint to bring in that code as we're going to use it what we want to do in this task is we want to redo an extend parts of this class I want to be able to have multiple students and each student is going to have three grades in order for us to calculate them now we can go to the bottom of our class for now because until we continue I don't want to add anything else you can see that I've saved my inputs and my object course from the last Challenge and from here is where I want to extend onward when my program runs after I'm asked for course input and my name I want to be asked for let's say numb students I would like to see how many students are going to be enrolled in this class so enter number of students very nice I'm then going to create a loop and I'm just going to say for this uh for I no I don't even need to say I for in range I'm not going to use the index for anything I just want a loop that's going to repeat for however many students we collected through our input right there every time this Loop runs we're doing two things we're asking for a name so who is the next student we are adding to the class roster enter a student name uh for good practice let's make sure that's lowercase then we're going to ask for grades but before I ask for them I want to create where will these grades be stored currently it's going to go into a list now that I know that we can Loop through how many grades do you want I initially said three you can change this for this purpose I'm going to keep three so in range of three every time this Loops we are asking for a grade which is going to be an integer so in input we can say enter a grade like so then we can take our grade and we can append it add it to our list list of grades great so imagine the loop runs one time the First Time The Loop runs it's collecting all this information as a matter of fact every time our Loop runs it's collecting this information a name of the student and for that current student it is going to collect three grades and add those to a list that's linked to that student nice okay where do I want to keep this information I have two items but for a list I can only add one element at a time and that's a problem if I have 10 students each with a name and a set of grades I need to keep them together somehow in order to add them to our list to do this we will create a class to store this information I will say class student now this class is nothing except like a database it's going to hold a name of my student and it's going to hold a list of their grades for each of those I just need to create a property for where they are going to be held like so I have my one student why don't I take the information I collected and create an object called student the value of my object is my class and I'm going to give my input student name as the name and my list of grades as the grades nice I can now use this student object and I can use it as an argument in the methods from our initial online course class I am just going to say here I'm going to call course that's our initial object right so I'm back up to here and for course let's enroll students who do I want to enroll I'm just going to say student right that's my object right there uh I can call my course object and why don't we say average grade I can say that and we'll give it a student again then at the end let's get uh our course details so all the info about the course great you can see in those two methods here at the bottom that I've passed in the student object which we created here as the argument nice before I run my code I'm going to need to go up and make some adjustments to the methods that we have built how do I use a method well let's it's a serious question a method must be linked to an object in order to work we know this from the lesson here is my method it's linked to my object course how do I use a property well a properties the same way I need to have the property linked to an object which I need to do I have an object student student has two properties name in grades when I want to access these I need to link them to my object student so let's go up to enroll students and I'm going to start right here so let's change my argument to my object all right so I'm going to be giving it an object student that object student is what I'm going to now append to my list it's the same thing as before when I print off who has been enrolled name it needs to find name who remember that name is a property in my student class so I'm going to say student object. name that's going to unlock the name all right nice let's come down here to course details right here it's joining together every name in my list but we've changed our list from names to objects so I'm going to I'm going to refactor this I'm going to change this I'm going to say print enrolled students then let's make a loop and let's say for every student in my list self. students I want to print the current student their property name that'll print every student we have enrolled good okay uh coming down completed course let's go in here and do some things so our completed course we give it a name let's Loop through our list and let's say for every student in our list of objects students all right I'm going to do a few things here if my name so if the current Student's name is equal to the name that we gave as a parameter then I want to remove that current student right so I give this method a name Josh for example for every student in my list if the current student in my list has the name Josh then remove that student from the list that's what's happening there I can then print off my name good and I don't actually need this else anymore I'm just going to bring this print right there so it's going to print as soon as the loop ends if no students in that list that prints nice all right then refactoring our last method our average grade we are going to do a few things here so all we really need to do is we need to access these grades remember that that's a property found in our class right here um our average total same thing student. grades and then our average that's it that's done good so we've used what we built in the last challenge we brought it in here and this was really useful as it taught us a few things it taught us how we can use another class as like a database it taught us how we can access these properties from another class or outside of this class and it also showed us how you can refactor your code to either extend it add more functionality or just refactoring in general really nicely done let's run this I'll go through this briefly uh course we can continue with python Josh number of students two students let's say Bobby has a grade of five five and a three cool so it has Bobby has been enrolled in the python course average grade asks me for another student let's say cool asks me for another student let's say Emily Emily has a grade of uh 551 there we go gave me her information and then at the end it printed off the enrolled students Bobby and Emily the one thing I don't like is all these decimal numbers so in our average grade function our method let's put in the python built round function and let's just round it down to one decimal great nicely done I will see you in our next chat [Music] chenge Welcome to our next class challenge before I get started encoding I'm just going to go through and I'm going to read our description I'm assuming at this point you have already read The Challenge and gone through it and you're ready to jump in and get started for this challenge we are going to be provided code that represents travel planning in a cost calculation system it allows you to input the country you want to travel to the month of travel the type of travel so is it Leisure or business the system provides information about the trip and calculates the total cost based on flight expenses and any additional costs you may have great you know me I love travel and I love making code around travel so I'm going to head over into our code here we are so let's get started the first thing I want to do is I want to make my class so using our class keyword I can say travel and inside here our class travel must have our Constructor method in it what properties do we need well I need a country we are going to have a month and then we're going to have like a type of trip right so what type of trip Leisure business right for these three let's create a property for each of these okay so country we have month and month is actually going to be an integer right uh the type so let's I can call it type this equal to type that's fine there we go and the last one I'm going to make a variable here or I should say property for a price initially the price is zero but once we start adding flights and other details that's going to change very nice our Constructor is all set set up I'm going to create a quick method for all the info relating to my trip so what info should we know for this well a few things I'm just going to check and determine when the user is going if they're going on a holiday during the winter months I want to alert them and say hey they're going on a trip to this country in the winter months so how could we do that well well if our month is greater than October equal to October and our month is let's say less than or equal to March which is three that's why I converted my property to an integer if that's the case let's do a few things so I'll put an F string I'll say you are going to Country in the winter this is a style trip so inside here you are going to self. Country and then inside here we can say self. type like so cool okay also if let's say month is greater than March and our month is oh let's say less than October cuz that's where we're at I can just copy my print statement I'm going to put it here and instead of winter let's just say summer just in case there's an issue I'll say an else and I will say oh I don't know I don't know let's say invalid input there we go invalid input that method is done it's just providing us the information about our trip you should have gotten that one if you did did nicely done let's form a new method and this method is going to be responsible for calculating the cost of our trip we do need something though so I'm going to give it a cost as well all right and this cost will be added on to our current price property right so if I give it 10 I'm going to add 10 to our price property and our costs all of them will be held in a list we want this method to repeat as long as cost is not zero as long as you are entering a number it should be repeating and it should be increasing our property self. price right so every cost we're adding it on to that every cost I am going to also keep track of in my list so I can add it to my list and when the loop finishes we should be collecting a new input right to test against our expression again so I will say enter another cost like so cool right that's all that one is doing currently all right very nice now before we can complete that method we should be doing a few things here when the loop breaks okay I am going to call another method which I'm going to get to later so I'm going to put a comment here and I'm going to say uh call let's just say call next method for now right um and then I'm going to want to call O check method I'm going to make a method to like check in order to do that I will need to return two things let's just say two things for now let's call that method done okay I'll come back to that I'm going to create a method called advice now based on how much money I have is going to recommend me the type of trip if I have less than 500 I should probably print off and do something like uh let's just say low budget cool uh let's say if the number that I give this okay if that's over 500 and that is less than let's say 1,500 then I'm going to print off here and we could say for this that's a median budget trip right so take a flight to anywhere dot dot dot great and then anything else which means you're over 1500 essentially uh let's just say luxury trip nice um I have advice that's going to give me my advice now my last method is interesting so I can't exactly budget how much I need for the trip right because anytime you go on a trip you know the overall cost right flight accommodation but the one thing you don't exactly know is how much you're going to be spending per day on food you could go to one place and get coffee for € you could go somewhere else and get dinner for eight right and it changes depending on where you go my next method I want to basically kind of create a way to like average and get a good estimate of how much I'm going to be spending on this trip so I want to inspect my list that I made up here okay so I'm going to give this method that list and really let's say less than 10 any number that's in my list that's less than 10 um I'm going to do something with that so to begin I'm just going to say for I in costs for every cost in costs if I if the current cost is greater than or equal to 10 then I'm going to take my counter and I'm going to increase it by one great um anything else right so once that Loop runs basically okay if a cost is less than 10 I'm adding one to this right and every time you go out to eat right coffee $2 uh a burger $8 a drink $3 all of those are less than 10 that's three items I want to add those three to this counter variable less than 10 my Loop breaks I can now say if that counter variable is less than or equal to 10 okay I'm just going to take my price and I'm just going to add 100 to it right that's giving me 100 for my food budget uh and I'm just going to go in here I'll put an F string and I can just say updated price and let's just print off our new price right it's better to have extra money than not enough money on a trip um all righty these two new methods we just created I want to use them inside CC cost right CU these two are going to be called based on how much money I have for this trip so let's do a few things I'm going to create a variable called advice this is going to basically call my advice method remember to access a method you can do so just use self. method name because we're inside of a class um great advice is going to take a price so it's going to recommend you advice based on the price of your trip nice um let's say inspect and let's do the same thing let's do list inspect method and here I can give it my list costs brilliant the last thing you need to do is you need to return advice and inspect so we can see those results all right wow nice work our class is done that's it okay pretty cool outside of our class now we need a location where are you traveling to so enter a country cool uh I actually want to capitalize this let's get it looking all nice let's say capitalized okay uh let's say trip type I can also enter an input here and we can say uh leion if I can spell leisure correctly or business we can also capitalize to make it look nicer all right so I have location that what am I missing month so remember that we don't need to do int input because month is going to be converted inside our init function enter a month I have month now all we need to do is we need to Jet Set so I need to create an object I need to go travel right um so let's just say uh test okay that's my object and that's going to be our travel class what are we giving to our class as our arguments location trip type that should actually be month right so I can say month just like so great as a test let's take our object and let's call our trip info method let's call that and see what happens I have two commas here enter a country Vietnam uh business month five ooh nice there we go you were going to Vietnam in the summer this is this is a business trip all right looking good let's now do some flight cost calculation so a flight cost is going to be let's say an integer input you could do float right so enter flight cost nice one and then we're going to take our object and we are going to So Cal cost change text to say test there we go and inside here we can put our flight costs all right let's run it one final time to our country I will do that again I will say Vietnam for a business month the same I know a flight cost uh return ticket to the US for me is probably about 24 million so ,000 enter another cost okay I have now entered My Method that's collecting the costs right so I have a flight cost th000 now I can enter all my other costs so for example let's say accommodation 500 eating uh five 4 3 1 2 8 10 done zero there we go so it's triggered two things advice it tells me luxury trip right and then for our list inspect it gives me an updated price right and it adds an extra 100 onto our initial price right so we basically just created a mini system to help us plan a trip and organize the cost great guys feel free to move on to the next lesson if you're feeling confident we have just completed working with classes in this lesson with four challenges but if you're up for it I've prepared a bonus challenge for you head over to the next video if you want to check that out I'll see you guys [Music] there [Music] welcome to the bonus challenge if you're here you must be ready for a good challenge I hope you are as we are going to touch on a few new things and this one is going to be the most challenging of the prior four now I'm going to read our description here and then we are going to jump into our code this code we're trying trying to create it's going to be a guest management system with a loyalty program so I want you to think about hotels or Airlines it allows you to input information about guests like their names their ranks and their ages their rank is like how high are they in the Loyalty program are they a gold member a diamond a platinum something like that the system provides various function fun alties to retrieve guest information track loyalty points and calculate average guest age nice this one's going to be fun let's head over to our code so we know we want a class and we know that we want uh name rank and age so I'm going to start with that and build that out let's say guest so I'm going to go here we will go to init self uh uh I'm going to just say uh last first Rank and age for each of those let's create our properties so I'll actually call it last name last uh first name that'll be first and you can see that last I missed my T here we go my ranks for the purpose of what we're trying to build here I'd like them to be integers so I will convert that and age we can do the same thing with if we want to find the average I'm going to need that to be an integer form all right very good our first method let's create one which is going to give us all the basic information about a guest so like who they are um their age and this we are actually going to give it a list of guests if I I have 10 guests then I will have 10 elements in my list and I want to print off the basic information about each guest so let's just say my all my guests are going to be given here and I can say for every guest in my list we're going to need a list for this let's make one uh I'm just going to call it all guests there we go so for every guest in my list all guests I would like to print off a few things here I'm just going to say self. first self. last name I should be saying and then forour age I'm going to do age but let's just make it look kind of nice let's say age and let's say self. AG so forever guests print off all their information very good okay going forward we want a loyalty program so I want to keep track of like who are my gold guests that's important right um and this will also take my list of guests like so so when I call this method I'm giving it a list of guests and I want to see who has a higher rank and if their rank is high enough I want to add them to a gold members program so to start things off let's just create a local list so like gold members this is our list so anyone who meets a certain condition is going to be added to this list and let's say a gold member must have greater than or equal to 10 loyalty points if they do they will be added to this list great so I want to basically take each last name of my guest and I want to add them to this so I'm going to take self. last name right that's the last name of my guest and for every guest in my list all guests they are being added to this So currently my list Gold members is a list of every guest's last name but I don't want that I only want them to be in this if they have at least 10 points what can I do for this well at the end of my list I'm going to say if my rank is greater than or equal to 10 they will be added to this list I'm going to type that out for you guys because these list concatenations they are quite challenging so breaking them down is actually easier so the first thing I do is I create a list for any member any guest who has more than 10 points okay so any guest who has more than 10 points or who let's change this again to meets certain conditions there we go uh for number two so the second thing we do is for every guest in my list add their last name to the list okay and then the last thing we do is so this condition must be met there we go in order to be added to this list okay so that breaks it down for us all right we have this new list so let's clean that up so if gold members is true what do I want to do well the first thing I want to do is just naturally I'll print off a header that says Gold members nice so my header then for every member that makes sense in my list my new list Gold members I will print off their name now let's do something a little fancy let's say I want to T their name in it's going to say guest then it's going to say the guest's name their last name great all right that was a lot if you stuck with that great job rewatch that last five minutes of what we just did to break down how this list was generated which is a faster and more efficient way to make lists going on to our final method we are really just getting the guests average age so yeah taking self and we're going to give it our list of guests again okay now I'm going to create a counter variable initially the total age is zero every age in our list we're going to add on to this which I can then use to divide to calculate my average let's make our Loop for every guest in my list of all guests TS my total age counter variable I want to add the self. I want to add the age to my counter variable once I've done that then I want to get my average age so let's just say average age is equal to now I need to calculate that so taking our average and I'm going to divide it by the length of my list all guests that's us finding the average then we can print off something nice like your average grade so average uh customer age not grade excuse me customer age there we go then we will just print in our average age our class is done it's just got those three methods in there at the end outside of our class we're going to do a few things here now so we will create an input for let's say number of guests and this is our integer input we can use this for the range so enter a number of guests there we go so for I in range we can enter our number of guests input this is going to be interesting here so every time my range Loops I want to ask one input and through one input I want to collect um a name I can do it like this I want to collect a first name a last name I want to collect a rank and I want to collect an age these will be entered as one and they will be separated by a slash from this single input I want to make a list and that list is going to have four elements first name last name rank and age how do we make a list from an input well we can use the split method and I want to split everywhere I have a slash in order to make my list okay now that we have a list I want to add this to our other list of all guests so all guests is like our parent list one element in that is going to be a child list of data so all guests like so I want to append what do I want to append well I want to append an object I want to append an object guest right so I could do do the same thing right and if I make this longer which let's do the same thing and say guest is equal to guest so guest is our object it's created every time our for Loop runs and the value is our class guest our class if we go up and look it needs four arguments last name first name rank and age the last name is the first element in our our new list called Data so I'm going to say data index0 that's the last name data index one I'm going to say integer data index 2 and I can say int data 3 right so I'm taking the elements from my list if I were to make a very quick list let's say a first name okay uh that is going to be John comma do comma uh rank 8 comma uh age let's say 45 so data one is targeting John so data zero that's the first element of my list is targeting John data one is targeting you see how we're creating that from our data list that we made here right so this is the same thing very good now I do have first last uh rank in age uh let me change this to match that first last Rank and age okay very nice now that we have that we are done with our loop on the outside I want to create a a starting point so just where is my Loop going to officially start so I'm creating a variable and the value is the first element in my list all guests now that I have an object right remember that we are going to append our guest and guest is really an object I can take my object and I can link my methods so loyalty points program is what we call it and I'm going to give it my list all guests I can take my object and I can get the guest info we will give it our list and then I will get the average guest age which we did call guest average that was it giving it our list very nice okay let's run our program see what we have Happening Here enter number of guests I will say three guests to show us first name last name rank age okay so John SL do rank five age 45 enter our second person let's say Jane do she has a rank of 10 she's a loyal customer she's 33 our final one let's say is Josh wner score of I get a 15 I'm really I'm a loyal customer and let's say 25 enter look at what we have going on there I have a few things we want to look at so not a whole lot's happening actually it just printed off John Doe 45 average customer age 45 now that's because we have our object our class guest we need to Target the properties which are in guest remember our properties are up here so because we're passing a list this list just has objects so for each object we need to access the properties for we need to go through our class and instead of like self. name which was our initial property okay we need to go through and I need to link my object guest right because we're passing the list as the parameter we need to access each element and each element is an object each object has these four properties great uh in our Gold members I'm going to do the same thing okay we are going to say guest last name uh instead of self. rank we can say guest. rank uh there we go and going down to our uh Age We anywhere we have the word self we're really just going to say guest uh all guests that's looks good let me try and run this again I'm just going to do two people this time guests to John 8 uh 45 Jane do she has a 12 she is 33 there we are now we have a finished product we have gold members and it prints off John Doe his last name or EXC excuse me it's probably Jane do right because Jane has 12 they have the same last name it prints off the guest information and then it prints off the average customer age great you just made a reservation system right like a loyalty program very nice that was our bonus challenge we have introduced classes how we work with classes in this overall lesson as we head into the next lesson we are going to talk about a new topic of class inheritance I'll see you guys in the next [Music] video hi guys guys and welcome back to the next lesson in the last lesson we looked at defining and creating your very own classes now in this lesson we are going to continue on that path and we are going to look at class inheritance but first let me ask you what do you think of when you hear the word inheritance well chances are it's probably the same for python too let's stop dive in and take a look at some things we already know so a few things we already know right we have on the screen three classes computers animals and games within these three classes we have currently three objects right so MacBook windows and Linux they all come from the same class they are all computers or for games there are many types of games but at the end of the day they're all games right so in our example here computers is a class or a family while the three others are objects now I could create a class for each type of computer as well and this would take key data for from the parent that I could give to the child that is the topic of this lesson so here we are we have our class car you can see four objects each object has three properties so in our first example we have this red car I believe the type of car from a long time ago is called a Road Runner that's the type and then the other two are color and wheels now you can see each object shares two of the same properties color and the number of Wheels but the type of car for each is different we have four objects all coming from a single class but what if I said we could change that what if we could make a child class and give it the attributes of this car class well we can in this example I have a class called vintage let me move me up just a little right there a new class is named vintage and this inherits the car class this is our example of class inheritance so as you can see here I have a new class vintage vintage inside the parentheses is taking an argument this argument is our other class car so it's really inheriting everything we created in the original car class all the properties and the methods I'm going to go back very quickly as I do I want you to keep an eye on the the Road Runner right we saw that in the last class if I go back you can see the properties of the Road Runner are the same I have type color and wheels that's a vintage car I'm going into our new child class and you can see that I brought Road Runner with all the original properties now I have three addition objects all of these objects are a part of the Vintage class but they also have properties from the original car class in this vintage class we have one new additional property you can see here at the bottom price this is only applied in this child class let's break this down even a little more so we've learned a new class we have our four objects and we have one new property in this child class vintage that new property is a price we are inheriting our main class now you're going to hear me start to say some new terms anytime we inherit the class that we do inherit we can call that the super class the super class is being given to other classes pretty cool huh all right we are going to talk about two paths two routes to inheritance super classes and derived classes now you're going to hear me say two things I like to call a super class the parent class and a derived Class A Child class because a child inherits the properties of their parents the same thing works for classes I've put a few examples on the screen so I have all Audi's are cars all dogs are animals all sofas are furniture and all jeans are pants all of those are true right now if I went to my animal I could make a very special class for dog and I could have dog inheriting some normal attributes from animals that most animals share such as Legs hair right there are many attributes that all animals share that would come from our super class animals for dogs what can a dog do well a dog can bark right that is only a dog we have two routes to inheritance Route One the class is given only new methods so it's only given new methods we do not create any new properties for this class which makes inheritance extremely easy because we do not need to create our init method that Constructor that we learned learned in the last lesson if we do not have any new properties we do not need to create an init for our child class we can use the super class's Constructor function that's root one now in root two the class is given both new properties as well as new methods anytime you create new properties we need to make a new Constructor in that child class and we must also activate the super class as well and I'm going to show us how we can do both of these in this lesson so going back to our first slide of this lesson we saw something similar to this right but now I've redone it computers that's going to be our super class and our derived classes all the children will be Windows MacBook and Linux they are all a class which inherits main properties and methods from the super class games is also the same thing it could be considered our super class we have an RPG a racing and a single player they are all styles of games they could be a child class inheriting everything they need from that super class pretty cool all right I'm going to head over to the next video where I'm going to break down the first path to inheritance I'll see you guys over in the next video [Music] all right amazing so far in this lesson I introduced the general idea and concept of class inheritance now we are going to take a look at the first path to class inheritance in route one is inheriting everything from our super class let's take a look at some code creating a child class that only needs new methods that's a key takeaway from this it only needs new methods we are not giving it any new properties when we make an object an instance of a child class our super class that in it is a automatically called and used for our child class on the left I have my super class main you can see I have an init function as well as one other method then I am creating my child class my child class is inheriting my super class you can see there is no new properties only new methods pretty cool right let's use some color coordination now you can see that main is being inherited from that super class and then our properties so for example self. property one I can now use that anywhere I want in my child class because we are inheriting those from that super class I don't need to make those all over over again I can use them as I would all right you can see now I've gotten a bit more complex I've taken our example and I've now put in some Live code we have our super class Main and we have one method in that class user info our child class is user score it's inheriting that super class as well as creating two of its own methods the first method Cal score is using the property self age from our super class and our second method check age is also using that same property I'm creating a child class called underscore and this will be a class which uses all the super classes properties some color coordination right so everything in green right that's coming from the super class I don't have to rebuild we can continue to reuse great amazing all right great this was Route One to class inheritance before we do any coding recap what we just did and we're going to head over the next video and we are going to check out the rout two the path to to class inheritance I'll see you guys in the next [Music] video so far in this lesson I have introduced the concept of class inheritance as well as the first root to class inheritance in the first rot we did not create any new properties but often times we want to have new properties associated with a child class this is the second route to class inheritance in this route we are going to look at something called the super function let's dive in here when creating a child class we are still passing in the super class as a argument and old and new properties are used in this child class as well as new methods so I have my child class user score we are inheriting main The Constructor in this child class we have is right here and you can see some new things are happening specifically right here what is going on here what is this well super is a powerful function super allows us to inherit not just all the methods but it also allows us to inherit all the properties from our super class but we use the super function when we also want to introduce new properties that are only a part of our child class such as score this was created right here in this child class let's take a look break this down even more this is a bigger concept so everything in GR green that's coming from our super class everything in blue has been created and it's only linked and found inside of this child class remember that super is really allowing us to inherit both the properties and the methods but it's also allowing us to create new properties I've put all this in green so inside our child Constructor the first three three parameters are actually the parameters we are inheriting from our super class we can then follow that by making any new properties that this child class is going to have pause the video screenshot the video look at what's really happening and if you need to try and take notes of your own to show this to encapsulate this what is the topic that I just introduced why are we using super and how can we use super right so here we go when we create an object of the child class remember that the super class Constructor in it is automatically called because we are using super this is basically like saying super Act activate if I create a child object it calls my child init Constructor which then calls my super class Constructor as well here is our example so I took some of the example from root one our super class is Maine Maine has three parameters what are those parameters name age location for each of those I have created three properties which you can see I'm using throughout the method in my super class heading over to our child class user score we inherit that Nifty super class and then we are also inheriting our properties I'm going to move me down for this slide in our Constructor of the child we first pass in the three parameters which are found in our super class we are basically activating those we then are free to create any new properties that we want that are only found in this child class I can use these new Properties or the old properties just like I would any variable okay so I've colorcoded this broken this down take a look I love the colors they are all linked to each other right so our child class inherits main we also need to tell Python and activate the original properties before we create any new properties you can see in my method check average that I am accepting a list as a parameter I can then use that list within that method's code finally at the end of our code I have a list called test list I have an object called user and I am calling my check average method with that test list in the output of our terminal we would result in five amazing all right let's head over into VSS code I've prepared some good challenges for us and we are going to build on our class inheritance I'll see you guys in VSS [Music] code great to kick things off through our first challenge this is going to be route one for class inheritance so as we looked at in our description I'm going to have a class animal and this is going to hold all the info about our animals that we want to know so it's going to have a region an animal type right like our species uh we could have a color and then I am going to want to know is the animal dangerous or not so I'm creating a property here soon for lethal for each of these go through and create a property so we have our four properties set up and the only thing we really want this method to do is I want to just get a basic animal bio so information about the animal and let's just kind of call this like our animal passport so it's like the information about the animal for each animal I want to go through and let's print off all four of those properties with some basic information so an animal is going to be found in we can put self. region right how would you like to structure our self. animal type let's say species self thought animal type let's jump in there for the animal color let's just say color and we can say self. color and then our last one let's say dangerous and this is going to be a Boolean value so it's going to return either uh true or false so self. lethal great our super class is completed right so now I'm going to create a child class a derived class for this child class let's call this like I don't know a clinic and it's going to be given all the animals details and we are able to search through those details and find a specific species or region something like that so I'm going to inherit animal now this because it's the first path the class inheritance is not going to have any properties of its own therefore we do not need a Constructor in this class our first method can just be called animal let's call this one animal info keep them different all animal info is going to do is it can say this is a and we'll do self. species from uh region so self. type no not type excuse me self. region and then over here let's do this is a so animal type like so very nice then our final method is I'm going to kind of call this uh search let's call this search and what I want to do is I'm going to give this method an animal so a type of animal and more specifically I'm going to be giving it a list of animals I want to go through this list and I want to find all the animals that are in a specific region of the world so when this method is called I'm going to be asked for a region so let's say here input and I could just say uh enter a region let's make sure that's lowercase to prevent any of our errors and I'm going to go through and I'm going to say for every animal in my list animals if the animal the current animal. region is equal to region animal is my object and I'm linking the property region which is created up here in our super class animal so if the current object's region is equal to the region that I have just input I want to do something with that and I'm just going to print off and I'm just going to say uh species because I want to know which species are found in that uh location and I can say animal type nice okay my classes are done now on the outside here I am going to create a new list and this is the list that's going to be given to my search m method right and I want to be able to continuously add animals so if I say hey there's five animals I want to add five animals as objects to this list now what we can do is I'm going to create a variable called amount of animals and that is going to be an INT input so how many animal in number of animals like so nice for I well this we don't want in it in for I in range of the amount of animals we have every time this repeats I need to collect the basic information about an animal so I need a region I need a species I need a color and I need lethal which I'll do a bit differently here then I will create an object object so let's say animal is equal to our animal class okay so I'm creating an animal for that great so region let's just take this one enter a region it's going to be the same uh species let's say enter a species color we can do the same enter a color now lethal strange so lethal is going to be a Boolean value and it's only going to be created well right here initially so I'm not going to say enter this I'm going to say let's say is it dangerous like so so that's a yes or no question so lethal is going to be true if the input is equal to yes leth will be true great creating our object how can I structure this out well let's just pass all of our inputs in so region animal type which I call species here uh we have color and then we have a Boolean of lethal there we go finally I can take my animals list and let's append our animal and and let's just change my spelling around here uh there we are looking good all right so our Loop's done if I add five animals they will be in my list animals great on the outside let's create our object of uh Clinic which should be fine that's equal to our class clinic now for this I will need to give it a few attributes as well so let's just start off with region animal type color who does the clinic currently have let's just say Asia let's say tiger and let's say color of orange and let's say lethal yeah we can say true for that very good I am now going to call all the methods we created so I'll take Clinic my first method is animal bio so I'm going to call animal bio great so I've called animal bio let's call our other two so Clinic dot we have animal info and then we have our clinic and then we have our search which I can call and search will take my list of animals all right looking good let's go through and add some animals in let's say there's two animals we want to add to our list the first animal I'll enter the region I will say America let's say a bear let's say a brown bear and let's say yes it's dangerous our second animal let's say Asia let's say snake let's say green and it is also dangerous awesome what is happening here well when I made my object Clinic I gave it Asia tiger Orin true that's being printed out as my animal passport which is the method I made in my super class then we are calling our animal info method this is a tiger from Asia finally we called the search method giving it a list of animals and then being asked for an input what's happening in the search method well for every animal in my list animals if that current animal's region is equal to the input above I would like to print off that animal species and let's change this I'm not going to do self. animal type anymore I want to take my object animal and do animal type great I'm going to run this Asia now it's still saying tiger because we need to rerun the application so trash can I'm going to go up and run this again great let's do two animals very quickly I'll go through America let's say bear Brown yes uh region Asia species snake green yes uh enter region now I'll say Asia there we go our species was snake because that was what I was looking for that was found in that region amazing nicely done great way to kick things off while working with class inheritance I'll see you guys in the next challenge video [Music] welcome to the first challenge of class inheritance in the this kickoff challenge I've decided not to make it too easy but to make it a bit more fun when we create these two classes revolving around superheroes we need an easy way to kick things off with class inheritance and I believe that this is it this code is all about superheroes and their incredible abilities we're going to dive in to this exciting world and explain this in simpler terms I'm hoping you've already had a read through this challenge description let's head over into our code okay here we are in our code now I need to create a super class before I create my children classes and in this challenge we're going to have one super class and one child class we know that our super class is going to be called super hero we know that the first method in a class is in it what does the super class in it need what does every superhero have in common well they're going to have a name and a power what can they do for each of these we can create our property our variable for this class self. name equals name self. power equals power in the super class we're going to have a few methods now the first method can be like uh use power right and this is just going to display a little information about our player this can say something like uh player's name is using something power right and inside here we can put our name and we will put here our power to use them both all right so now anytime we call that method it'll use the player's name and the power that they have okay great um we could call one called All right we could make one called intro hero or just intro honestly um and this one could kind of do like the same thing something like that for our hero okay cool let's make uh two more right so just getting in the habit of using our properties using F strings creating methods here right all these methods are very straightforward into the point so print what do you want to print here I could just say self. name has saved the day right what do each of these methods do well all they're doing is printing something I'm using the properties that we defined inside our Constructor in different methods around my class let's create a final one and let's call this like power level now in here what we can do is I want to know how much power they have and specifically like what is their power for example if I come out here and I say fly or I say strength I want to know how powerful they are or specifically how long they are so fly is three strength is eight that's the length of the word so I kind of want to figure that out out here because power level self. power is going to be a word so in here I could just say length and we could get the length of our power word right and then the level of power we have we could just say okay whatever their power length is that's going to be their level but then you can multiply it by something like 10 10 then we can give back our level we can return our level so our super class is done uh for the purpose let's kind of create one let's say Batman before we create a child's class let's create our object just using the super class okay so let's say Batman honestly Batman does not have a power but I am going to put flight just cuz his name is Batman very nice let's run this code now nothing's going to happen I should be airfree great I'm airfree now after this I could take my object Batman and let's call like intro hero and let's call one more and let's say batman. power level there we go now let's run these and this this I'm going to print off because I'm returning I won't be able to see anything there we go I am Batman and I have the power flight and the power is going to be 60 all right because flight is six letters 6 * 10 is 60 our super class is working okay now I'd like to be more specific and I want to create that child class so let's create a subass and let's call this flying so this child class will be given to all the superheroes that can fly and they are still superheroes so they can inherit the original superhero class inside here I need to create one new property what do we need to do if we create new properties for a child class well new properties mean in it we need to have in it inside in it I'm going to put the first two parameters of my initial super class name and power so name power then I can create any new properties I may have so let's just say speed like flying speed how fast can they fly before I create a property for Speed I need to activate my super class we can do this by using the super function linked to the init method just like so inside in it what do I want to initialize well I want to initialize my two parameters name and power then feel free you can create any new properties that this class is going to hold nice our class superhero Constructor is completed and ready to be built out reading through the challenge what two methods do we need here well I would like to have a method called use power use power the only thing this is going to do is I'm going to combine a few things here I am going to print off and we can print off our name right so I'm going to say like Batman is flying at the speed of that's cool then I'll put my speed uh miles per hour there we go so in here I can use my self. name and I'm going to use self. speed now remember that this property is being inherited and initialized through my super class because we're inheriting it and we're using the word self that is unlocking everything in our super class pretty cool that is the purpose of class inheritance okay um our last method I'm going to say like calc distance right because I want to Cal calculate the distance the superhero can fly and I'm going to give it a flight time how long were they flying for and the distance is going to be a local variable a variable that's only created and used inside this method and the value is going to be our speed right and I'm going to multiply that by our flight time I can then return my distance variable all righty our classes are fully built that's it I'm going to head down here to our objects now I'm going to create two objects here I'm going to keep Batman actually um and I am going to create another one called Superman Superman is going to be an object of the flying class that is my child class now here I need to give it a name he will have flight and then because he's from the child class I need to give him three arguments Batman is from the superhero class which only requires two arguments so for Superman I can say what is our last one speed I will say 250 for Speed let's change Batman's to strength like so great on the second object all right I am going to let's call Superman I am going to [Music] use this method with Superman so I can use intro hero again nice I can use Superman let's use use power that's coming from our child class right here then let's call oo I'm going to call a attack I'm going to create a variable and this can be superman. Cal distance all right then just down here at the bottom let's just print out some information so I kind of want to say like my superhero can fly a distance of um they can fly a distance of whatever this method returns so I can use attack because that will be the value so superhero can fly a distance of however long miles in what is their flight distance flight distance hours for name I could say Superman right um but I actually want to Target the properties so remember we work with this why not say superman. name superman. name okay let's give this a worldl we're gonna see a whole bunch of things happening here okay nice um I could even put a print here so we can even read this more clearly the Batman one runs like normal and then we have our Superman I am Clark Kent I have the power flight right that's happening right there use power it says Clark Kent is flying at a speed of 200 miles hour okay and then our last one is Clark Kent can fly a distance of 7,000 500 miles in 30 hours wow very nice guys that was our first real introduction to class inheritance we have a super class called superhero this is being given to our child class flying the child class inherits everything from the super class and also has one new property of its own I made two objects our Batman object is taking the class superhero the super class and Superman is using the child class flying very nicely done let's head over into our next challenge I will see you guys [Music] there [Music] here we are in our Second Challenge I hope the first one really kicked things off for you guys and Spark some interest and understanding of class inheritance this is our Second Challenge in this one our code we are making is going to be a library management system this system is going to allow you to manage a collection of books and our system is going to provide functionalities for adding books displaying all books searching for books by author and searching for books within a specific year range wow so we are actually creating here a library management system we are going to use super classes and we will have children classes as well don't stress out although we are going to have many different classes a lot of what we're going to be doing is using our properties to Output strings with print but this is really going to build on the purpose of showing you that you can work with multiple classes all right let's jump into our library management system I'll head over to our code in our code to kick things off we need our super class we are in a library what should this be called book logic okay our first method always our Constructor what information do we need to know about a book title author two good things and year when did they publish the book for each of those just go through remember every parameter that we give in it needs to have some type of property we can use throughout our program so I'm creating one S.E equals year nice that's it now the only thing this super class is going to do is display information about a book how can we do that well let's just create and let's just say book info remember all your properties and functions and methods you should be using really specific names to help you understand but help other developers understand too so book info awesome what do we want to print out here well I'm going to print off a title I'm going to print off an author and I'm going to print off a Year oops I should have done an F string there you can say year so in here self. year there we go uh in here here self. author and our final one will be our title property cool that's it okay so this is completed uh and to kick things right off I will create an object called Harry Potter that we can use to change as we create uh I will call this Harry Potter I haven't read a Harry Potter book in a really long time uh J K rolling I know that um and then I don't recall when the books came out like the Sorcerer of stone I'm going to put 200 2005 that's probably not right let's take our our new object Harry Potter and I am just going to link the book info method right what do you expect to be output these three lines to run a quick test let's just make sure the initial super class is working and we can see there in our terminal that it is and it looks great all right so our object is completed it's always a good idea to keep our classes together that's why I put all this space right here now what two types of books do we have we have non-fiction and fiction books for each of these let's create a child class so I can create a class called fiction book I know this is a a child so it will inherit our super class now this child class I would like it to have a genre so if I want to create a genre what do we need in it okay before I continue just remember those two takeaway points most of these challenges I'm trying to get you comfortable with new key terms and new aspects like the super function and things like that if you do not have a new property do not make the Constructor function in your child class you could just start making new methods once you inherit just like from our lesson all right this in it is going to take a few things uh I can just go up here and I'm going to copy these three parameters use them here and then our last one I can create a genre to activate all of those we can say super in it and then put those inside there don't forget to make a new property for the final parameter nice all righty so fiction book I'm going to create a method to let's say show info about this book now I want to basically pick up where I left off so when I call this show info method I want this to basically automatically run then I also want to say like genre and I'm going to have myself. genre but that seems a bit counterintuitive that's just repeating a lot these were already taken from from our super class so let's use the book info method and let's say well let's keep the same names so I can say book info now the problem becomes which one is going to work well if I create an object from my child class it's going to use the child method when it hits and runs the child method the first thing I want it to do is I want it to call the book info method from my super class then it can print off my genre so let's give that a run so we can see what's happening here I'm going to change my class to fiction book uh genre I don't know is Harry Potter adventure adventure okay you can can see here I'm now calling book info which is actually this method now because I am using my child class run our code there we are so instead of just printing all over again I just activated my book info method that came from the super class okay now going forward changed both of these to book info right and I did that because I'm going to be working with many classes here but if I only had these two classes we don't need Super if we had our own name in the super class so info I don't really need to say super doino because it has its own unique name to use it I could use it just like a property and I could say self . info that corresponds to our info method from the super class and I'm inheriting it because we're inheriting our super class this would do the same thing right so if I only have two classes this is perfectly fine because I have more than two I'm about to make I'm going to keep with using super and I will we'll keep with the naming Convention of info for now all right our next class the opposite of fiction is non-fiction so let's say non-fiction book but it's still a book so it still inherits our super class and our in it is still going to take those properties now a non-fiction book is going to have a topic what's the book of about so that's pretty much the same thing as genre but let's use topic we will do our super init right get all those working again get our parameters in there and then create a property for our new parameter topic okay pretty cool now I want to do the same thing I'm going to copy this method I'm going to take it I'm going to put it in here they are all called info the order the Precedence that they take is the object that you make so when I create my objects I'm going to create three objects one object from each class so we can really see what's happening whatever object is using that class that's is what's going to take precedence now instead of genre I'm going to change this to topic and instead of our property topic great I hope we see what's happening I hope the gears are turning all righty going down so everything is pretty much completed okay um we do need our last class but let's get some things running first so Harry Potter um I I don't know a good non-fiction book so National Geographic that can be nonfiction book okay Nat goo let's do our info um I don't have book info anymore right I've changed that and then let's create one more as like the main book so to show us that I can create from the super class so main is just our super class book so I can say test um author I will say me and year let's say 2023 uh then I will call Main a.info which is the info method in the super class so you see they are all using the info method but remember their info methods are all slightly different okay uh for the purpose of readability let's put some prints here okay let's run brilliant there we go let's make this even bigger this is from our super class test Josh year our fiction book has the three of the same properties and then we have a genre and then our nonfiction book has the same three properties but it has a topic right very very cool all right let's close this I'm now going to make my last class which will be the most useful one actually what we've done so far is kind of like create a database of what type of book is it and what genre it has but now I need to create a library to store everything not just the general descriptions so let's create a class Library this will not inherit anything which is a good thing for us uh this Library class is going to do a few different things and all the interesting stuff so I will create an init here but in it is not actually going to really do anything it is going to serve as a home for all of our books so let's create a list where all of our books will be held we can create a method to add add a book I should say one book we're not adding two at a time so self and then I'm going to give it one book I want to append this book to my list books all right just like so it's going to go in there okay nice um let's uh display books I want to show all the books that are in my list pretty much so how could we display all the books think about that I have a list I want to go through it and I want to print all the titles of the books how how can we do that a for Loop for every book in my list books cool I can take book and he guesses what we want to do here info I want to call the info method because I am using the word self after I print off the info of every book I am going to put a space we will see this in action in a little bit but if I have 10 books each book has a name an author and a year I want to see that 10 times so all the information about the books all right um we are able to search by author which is pretty cool so I want the ability that you have a favorite author you can look for that author in the library that sounds useful so in order to do this what I'm going to do is I want to Loop through all the books we have and every book I go through if it has the author I'm looking for I want to take that book and I want to add it to a special list just for that author once I've gone through the entire Library I can print off that new list of the books that were written by the author I'm searching for so the first thing we can do in this new method is let's just say uh found books this is like a local list that any book that I'm looking for written by that author is going to be added for for every book in our list books if the current book author is equal to our parameter the author I'm looking for object property I'm accessing my objects property every time our Loop runs it's checking for the author and it's checking to make sure the value of this property is what we entered into our parameter or what we gave to our method so if that's the case I'm going to take my new list and I want to append that current book I'm on great once the Loop's done if the new list contains data so if that's true if there's books inside that list great I'm I want to print off and I can kind of print off some info about this author so I can say like books by uh and then let's get our author name in here well I can't really say uh self. author right because this is a class that's not inheriting anything so I can say author that's the info we give here for every book in my new list found books what do we want to do here well book book I want to info I want to call my info method on that book just like we did in the previous one great um I might as well throw an else in here uh just to alert the user that uh no books found by this author you should be really seeing what's happening we are building on gradually the foundations of python which you should have entered this course with and we are building on object orientated programming so taking those fundamentals and the foundations you already have and turning that into object orientated programming nice all righty uh very nice that brings us to our final method um I did say that we wanted the ability to search by year right so I want the ability for that so I'm going to give it a self right in a year but oo let's also say start year and an end year right that's what we should be doing there okay so once again I pretty much want to do the same general idea as the search author I want a child list okay and this child list so for every book in our list books okay great if my start year so that's my parameter if my start year is we can say less than or equal to the current books Year and that book year is less than or equal to what our end year okay so I'm accessing the current book's year and I'm saying if the book's year is greater than this number and less than this number I want to do something um I'm noticing a problem that I could have let's um do a few things here uh well the problem can be fixed on the outside right just be careful these are using the child class uh these do need to be integers when they're passed into this class as we are doing some logical Expressions so if this is the case what do we want to do just append it to your list right so what do you want to append I want to append that current book great when that's done if we do have books in the library that have that then let's print off just some basic information and you could say like books uh between okay uh I'm going to say start year so books between the year 2000 and the year 2010 I can say that uh nice then for every book in our new list found books what do we want to do drum roll please take the book call the info method on that book to get off our info um um I'm just going to say else print let's just say error okay errors it's fine wow look at everything we did take a moment pause me scroll up look at what you've done nice very nicely done all right so getting back down here to the bottom okay so I actually already have some books I'm going to use use as my examples okay so I'll space these out for you be creative here okay so I create an object called Library that's being right there in this Library I have four books I still have Harry Potter I have the Great Gatsby two fiction books they are objects from the child class fiction book I then have two nonfiction books Childs from the non-fiction class now I can actually go through and I can use all of these so library is like our main object and I want to add these books to my library that I can use later how can I do that how can I add these four books to my library well you have have an add book method and this takes a book or an object I can append an object to a list just like we did in the last lesson this object I now have the ability to access their properties because we added an object to our list so library. add book book one I'm going to go through and we're going to add each book to our list okay so now each book is added right it's like a database now I can take my library and I can do some pretty useful things here let's say library let's call display books method that's going to print all the info about the books run it wow look at that that's cool nice so we have each book's information just like a normal Library database system very useful very nice let's call library search author who do I want to look for let's go up and let's take F Scott Fitzgerald Let's search for him okay uh and then while I'm here let's say library . search year uh now it takes a start year and an end year so where am I at here let's specifically look for this book so I'm going to say the year 2000 until the year 2020 that's two decades let's turn off display books there we go so I searched for f Scotts Fitzgerald so it says books by fcot Scotts Fitzgerald it prints off the books by him books between 2000 and 2020 prints off that book if you're still here you stuck with us amazing guys if you struggled at all with this that's fine slow down okay go back to the previous task take the task and I want you to change elements in it and make it your own make it about a hobby you're passionate about or something you enjoy break down class inheritance all these new terms I'm using properties methods parameters arguments you should know these now this far into the course if there's anything you want to review go back and review take a few of the quizzes you're doing great I'll see you in the next challenge [Music] here we are in the final challenge of this lesson for class inheritance if anything I've called this one the fourth challenge but this might be a bonus this one is going to be quite challenging and use things we haven't learned yet in this course but through the foundation and fundamentals of python you should have already learned and experimented with we will use dictionaries in this challenge if you are not confident with dictionaries or you've only learned that briefly I suggest you go and you spend a few minutes when I say a few minutes I mean 10 or 20 to refresh on what a dictionary is and how we add and access the values of a dictionary I'll give you a tip here in a few minutes on how to remember how to access elements from a dictionary what we want to do here is we are creating a country information system and it's going to allow users to retrieve information about different countries our system is built using class inheritance to categorize countries into two types developed countries and developing countries now I'm hoping you read my description for this challenge a dictionary very briefly right here I'm going to type it out I am going to say let's say we have a dictionary called country okay to access a value dictionary key is equal to a value okay I'm going to type this out so and I'm going to say how I tell my other students is I say dictionary let's use an arrow so dictionary key unlocks a value okay if you have to say that 20 times do it it annoy yourself go to sleep thinking about that dictionary key unlocks a value dictionary key unlocks a value anytime you want the value of a dictionary you need to insert the dictionary key the key unlocks a door the same thing for a dictionary let's go into our code all right so our super class is going to be country because we have developed and developing countries so let's make a class called country our Constructor should have a few details what do you think this Constructor should have I'm going to say a name a capital city and a population that's a good starting point for a country because a country well a country needs all three of those it needs a name it needs a capital city and a country well hope hopefully needs a population I'm going to say self. pop actually and we still need to use population okay nice now I'm going to get the info about this country but instead of just printing line by line I want to create a dictionary that I can use in other parts of my code because the ultimate goal is I want to return the object's value in the final lines of code which you'll see here briefly so I'm going to return I'm going to return a dictionary now in a dictionary remember we have key pairs so the first is our key then the value is what's next so self. name together these are a key pair I have my key I have my value every key must have a value if I delete the key I automatically delete the value I have a name uh capital and finally population okay brilliant that's all that this is doing it's returning a dictionary for me that I can add things to and use as I wish all righty our next class let's call this developed country so countries who are already developed they will inherit the initial Foundation of our super class and let's create an init okay so self we can take our three parameters from the super class right there in this class because it's a developed country let's give it a GDP okay so super I am free to activate everything from that and we can create a GDP gross domestic product for this child class nice okay now I only want one real method here so similar to what we have been doing guys I can create my info method get info I have that in my super class but this is going to be slightly different I need to access my dictionary but my dictionary is held in my super class any guesses or thoughts on how we can do this I'll give you bonus points if you actually put in the comments of this lesson what you thought of before I said anything how do we access this dictionary well let's create a dictionary I haven't kind of created one yet so let's say I don't know my dictionary is called info the value of info let's initialize and let's get our dictionary get info okay so now I have a dictionary to work with my dictionary is called info I want to add my gross domestic product to my dictionary so how do we access that again remember dictionary key value say say that three times dictionary key value so my dictionary is called info insert the key what is my key well it's going to be GDP what is the value self. GDP cool I can now return my new dictionary look at what we've done compare this with working with dictionaries and other things you know okay nice this is a data structure we haven't used in this course yet dictionaries are our friends they are like Advanced lists dictionary key value or a dictionary key unlocks a value very nice Okay um then let's go down here and let's make a class for developing country we can still give that our super class country we will still create our Constructor uh let's give it those three following then I'm going to give it uh HDI there we go that's for developing countries cuz they can use that uh in it cool paste in those nice and we're pretty much going to do the same thing here guys so I'll create my property for HDI and then uh down here I can also create a method for getting info which initially is going to be past the same thing so I'm creating a dictionary from this right and then I need to add a new key pair to the dictionary so I'm going to access my dictionary the new key it's going to be HDI and the value is going to be our property HDI then we can return this dictionary info what do you guys think right our super classes country we have two children classes now we can create a class that is going to use all three of these so let's say class world right all the countries around the world how can we add objects to this class well if you said hey Josh why don't you use a list well you're on the right path let's say countries empty list cool what do you think we need let's add a country right we've been working with this so country uh let's add in a country let's take our list countries let's append that single country we've just added okay let's give some more space then our final one I've said that we need to create is let's just say get country uh info keep with that naming convention so I'm going to be giving it a name okay whoops for every country in my list countries if that current country name is equal to the name that I'm searching for we can return there we go return the current country and I can call the method we made inside get info right so whatever country if I'm searching for a developing country and I say country get get info it's going to give me this if I am searching for a developed country it would give me this they are both called get info but remember that this method is linked to the object itself if the object is a developed country then this method is linked if it's a developing country this method is linked all right I have returned the info um anything else as like a safety net let's just return none cool wow objects need to be built now our classes are done so our first object let's just say world world class I'm going to use that as like the parent the main object now I've already come up with some numbers okay for three different countries so let's just put those in here okay you can read over those be creative guys um do some research on your home countries right so find the information use your country as one of the objects and then find two places you would love to travel to that's always good if you're stuck and you don't know what to put okay so I have my three countries uh us India and China right and I'm going to use these so I got my objects I need to add my objects to my database pretty much right so my database is at world class that's what's being used all right so we can add in our actual objects right we are appending those to the class world so I can deconstruct them in the world class and I can use their properties okay at this stage in our code it does nothing right cuz ADD country it does nothing you just have a list that's all it's doing right so okay great let's go down now remember we're working with dictionaries so as like a test let's just start off and I'm just going to create a variable but the value is going to be a method so world. get country info cool as you can see get country info takes a name so we need to search for something so let's say here Vietnam I can create a condition I can say if the country is true okay so if it's returning something right because it's either going to return this or return this so if it's returning something then let's say like I don't know country info let's print off the basic information and I'm going to Loop through now remember our data is held in a dictionary so I want to Loop through and I want to print off the key pairs I want to print off the key and the value to every key pair in my dictionary how do we do that with a for Loop this is slow slightly more advanced with our Loops but let's break down the idea so for every key and value in my country info it's my dictionary I want to get their items so I want to if I were to translate this for every key and value in my dictionary I want to get their items okay I can print let's print off my dictionary [Music] key and my dictionary value cool and then else I'm just going to put here as like prevention I'll say country not found wow what do you think is going to happen when I run this program okay let's run it country not found what's going on I said Vietnam oh I don't have Vietnam as an object right I'm hoping you guys caught that okay let's take this let's now say India country not found that's also an issue why why is that if I cap capitalize India now there we go there we are okay so before I get on this information I'm going to talk about that initially I said India right you are not putting the object right so for example if I put this object India let's watch what happens country not found we are not putting an object you are putting the country name okay because get country info it takes a name for every country in my list if the country name is equal to the name I entered return country info right so let me go back now let's do USA Country not found okay well because the proper name is United States okay prints off country info let me go down here so we can see it says name United States right name is a key the value is United States capital Washington Capital is a key Washington is the value right do you see how we're unlocking our entire dictionary and using those key pairs very well well done you guys I'm so proud of you for how far we've come over the last three lessons you have learned so much about object orientated programming you got an intro to working with objects we learned how to define and create your own classes and objects and in this lesson we talked about class inheritance which has prepared you for our next lesson let's head over and let's introduce the next Concept in in objectoriented programming I'll see you guys in the next [Music] lesson all right Welcome to our next lesson in the last lesson we introduced the idea of class inheritance and how we can create a super class and give that class to other derived classes or child classes in this lesson I'm going to introduce to you the concept of multiple class inheritance now when it comes to this I want you to think about just normal inheritance but instead of accepting just one class we can accept multiple classes great let's jump in as our example here has we have the animal class and I want you to view the animal class as the Base Class everything is derived everything comes from this animal class and it provides the basic for all the animals each animal can then do something special so we have two sub classes we have can swim and can fly and then finally we have a child class we have a class called bird and bird is inheriting not one but two classes right we have our Base Class we have our classes that Define behavior yes I did not make a l Land class right so animals for land but you could have that right so every animal could have a few things we have our species the type of animal you could have a region of the world this animal is found is it dangerous things like that right and then other special classes for specific animals like a fish a shark a dolphin or can fly right you would apply that to a bird so everything there is being passed down okay we do this the same way we inherit a single class but we do this with two classes so in the example we can do that by entering two classes in in those classes we just need to separate by a comma therefore we can inherit any other classes we want more than just one so take a look at this code this is a very very basic setup of multiple class inheritance but it still does a good job at expressing what's really happening behind the scenes we have a class for our animal this is the base class this class also holds our Constructor method in it with only one property we also have one method eat and this just prints off the name of the animal is eating great the animal class is complete I then have a subass and this class is called can fly and this will be applied to any object that is an animal and can also fly so my bird finally we set up our child class this is the class that's inheriting our two other let's call those parent classes we create a Constructor and we initialize the name from the super class animal I then Justified one additional property for color for the color of the bird right and then I have one method here and this is going to print off the color bird is sleeping so the red bird is sleeping right the blue bird is sleeping at the very bottom then I'm creating an object from my bird class then I'm linking my object my bird to the three different methods we built each method is a part of a different class but we are inheriting all of our classes in one okay let's space out our alignment here's the setup for that in order broken down right python reading top to bottom it's good that you have a good structure for your code now if you haven't quite caught it I'm going to get an error why is my program going to run an error think about it pause it try and find the air great because initially I did not have the color but because my object is from the bird class I have name in color what color is the bird when I create my object I'm giving the name or the species and then I'm giving the color therefore calling all three methods because we're using the super function here in the child class we can pass the name to the parent class and to the other classes which need it the most right they're all kind of hopping around and by using that super function we can therefore take what we want to initialize and pass that around to the other classes that we are inheriting or our super classes right I have my super function and because I'm inheriting these two super classes I can initialize the name to use in my two super classes great let's take a look at the first challenge that I want to kick things off with here we are so don't go back okay don't go back you can try this one on your own before you jump into VSS code and you watch a few of the challenges that I have prepared this may or may not be one of the challenges that you'll see a video of I want you to try to recreate the code from the previous slide use it as an example to build your own I chose the topic animal with can fly and can walk but you should be choosing a topic that interests you where do your passions lie create one Base Class which is my animal super class and then two Behavior or style classes which mind work can fly and can walk finally create a final class for one child class and you just need to use print to interact with all the classes nothing too complicated just to Showcase your understanding of multiple class inheritance remember to use the super function to pass the arguments around to our different classes I'll see you guys over in vs code for our first [Music] challenge [Music] all right here we are in the first challenge I'm excited for this one guys let's go through and check out the description quickly which I hope you're all brushed up with and Dive Right In so in the world of transportation we are evolving we have Smart Cars we have electric cars hybrids we still have gasoline it's changing we have a super class called vehicle and this represents the generic vehicle with all the generic attributes mag model year but we also have two specialized classes and this is going to be your electric vehicle and your gasoline vehicle the electric vehicle is going to focus on any vehicle that's powered by battery and it's going to include Properties or features like battery capacity and the ability to charge our gasoline vehicle class is going to deal with vehicles that are obviously powered by gasoline and this will have its own properties like fuel capacity and ability to refuel both of these classes have a method called get range which is going to help us calculate the range of a vehicle based on their respective energy sources our job is to make a derived Class A Child Called High hybrid hybrid is going to inherit everything let's jump in heading over to our code all right to kick things off let's make our base class right everything is going to have vehicle so I'll say vehicle I will Define my Constructor in it with our key self we then have a make a model and a year for each of those those you can Define your properties then I am not going to do anything else for this class this class is like a mini database if I have 10 different car objects each object is going to have these three I can hold in one object three values that's all this is going to do so think of this class like a mini database for each object awesome continuing on let's make our class for electric car okay um this will actually not inherit anything either so I can create a Constructor and for this I'm creating its own property so remember let's just create battery capacity that's a good one and I'm going to say create one so let's say self. uh let's say battery cap is equal to battery capacity great then I will create a method for charging all of our electric cars can charge self inside we can just return the battery capacity that's all we want to do for that finally what is an important thing to know for an electric car well our range now I cannot say range because range is already a special python function so let's call this get range that's its own unique name right that's a much better name our range we can just return our let's say our battery capacity and let's say the range on the battery capacity is multiplied by five looking great all righty and our last let's call Super class or parent class is the last type of C car we have and I'm just going to say gas car the gas car has its own Constructor and then its own property is going to be fuel capacity and we can make one called fuel cap and that's going to be equal to our fuel capacity parameter inside here we can call one and say refuel or get gas right and all we're going to do is pretty much the same thing we did for the electric car one so we can say self. battery well not battery in this example it'll be fuel cap and then we will have self. getet range and for this range we can return something like let's say you got 20 miles to the gallon right so if you got 20 miles to the gallons we can multiply that by 20 great so we have three parent classes or or super classes here now I want to drop these in to a child class a derived class and this derived class is now a very specific example of a type of car we see out on the road let's make a final class called hybrid it's a hybrid car so hybrid hybrid is going to inherit actually everything right it's going to get the info from the vehicle class it's going to get the electric car info cuz a hybrid's half electric and half gas so look what's happening we can take any other class and we can pass those in I'm giving three classes to a derived class now our hybrid we are going to do a lot here well not actually a whole lot because it's really just in it doing all the work for us what do I need to put inside in it well I need to put all all the parameters we initially made so all the parameters from vehicle I'm going to go and I'm going to put the parameter from electric car because we're going to need that uh and then we are going to put in the parameter from gas car which was fuel capacity what do you think I need to do now inside in it I don't have any new properties I'm inheriting them all I need to activate them each of them so I'm going to say super. init don't forget your underscores so for this init I'm going to take these first three because that's coming from my first super class great let's just copy that row we need two more so I can have my next row for super in it and inside here we just need to activate our battery capacity from the electric car super class our final one we just need to activate our fuel capacity right so now everything is stored locally in this hybrid class which I can call all three of my other classes to use methods and get information ah this is great let's finish out this challenge what I'm going to do here is I'm going to create an object let's just call this car and my car is going to be a hybrid type of car what arguments do we need to give here well we need to give it a lot actually so I'm going to start things off with a uh make of car because I have here make model year in order so my make let's say a Toyota I think you know the model of car I'm going to do I'm going to do a Prius uh let's say a year uh year let's do 20 23 uh for battery capacity let's just give it a round number let's say battery capacity is five and then finally fuel capacity let's say it can hold 40 great on my car object I want to call a few things but notice right we are returning when we're returning from a function or a method the value is going to be used as the value to a variable so instead of taking my object right now I'm going to create a variable and let's say I want to know my battery capacity what is the value to this variable going to be well the value to the variable is going to be whatever this returns if this returns 20 okay then this should be 20 to get that I need to call My Method so I'm going to take car and I am going to call charge just to run through very quickly if I print off my variable battery capacity it should print off five all right so okay I'm actually given an error let's go through and address this quickly so missing two required ah all right inside of our init let's just go in because I'm using three of these uh it's bouncing around let's just go inside here and let's say self for each of these okay I'm using three supers let's just put in self trash my terminal let's try it again I should see the number five and in matter of fact we do not see five okay let's take these let's not use super three times in a row let's link our actual class so in the first one vehicle in our second one electric car and our final one let's say gas car great so I've linked the classes within it there we are I'm giving back my number five right because battery capacity is holding the value of whatever this method returns that's five looking good good tests now now going forward let's just print off basic information about our car such as battery capacity fuel capacity battery range fuel range okay so we have battery capacity I have my fuel capacity and we have our battery I have two batteries those no battery range when we've run our code we are going to see a few outputs here there we go back battery capacity of 5 Kow per hour fuel capacity of 40 gallons we have our battery range of 25 miles and our fuel range of also 25 miles looking great except these last two they're both saying 25 miles I'm going to throw a really easy fix in there let's just honestly say get fuel range for this one uh split things up a little bit so let's say fuel range run it again as our final test and we should see there we go looking much better great I hope this was a very easy way to introduce to you guys the basics and the concepts of multiple class inheritance look back through these videos especially in this video and before going into the next one leave a few comments and a few notes in your code use VSS code as a big notebook I'll see you guys in our next [Music] challenge all right great let's kick off our next challenge there is a bank that offers multiple types of accounts to its customers just like any bank we have a type called savings account and our savings account is a special type of account that allows customers to save their money and earn interest although it's not that high we still get interest this is what we want to build out we are also going to have a few other classes like our main account class our interest account and then all of our transaction history needs to be held somewhere too this challenge is going to be a bit harder than our other ones and we are going to be doing some mathematical operations as well think Banking and think how this can provide a real value to a customer this is a real world scenario problem jumping over into our code let's kick things off and let's make our account super class this is strictly going to hold our account number our account balance and then any deposits or withdrawals we make to this account will also be applied here as well so all the basics our Constructor method is going to take our account number and our balance so let's say account num and uh let's say our balance like so every user needs to have their account and every user is going to have a balance with that bank as well so balance we need a method for all our deposits now this doesn't actually have to do a whole lot every time this method is called all that we want to do is we want to take our current balance and we want to add an amount to this so this amount can be declared as a parameter when we call this method we want to give this method an integer or a float to be added to the existing account balance nice pause the video try to make the withdraw method yourself what could go wrong in this method pause the video here all right our withdraw method let's say withdrawal um withdrawal is also going to need an amount how much do you want to take from the account before we withdraw we need to make sure that the balance well we need to make sure we have enough in the account how can you do that using a conditional statement if your self. balance is greater than um or equal to your amount you can do that right then you're going to take your current balance and you're going to subtract your amount what could go wrong well you have no money so let's print off and let's say uh not enough funds like so okay um that's it great the last method I asked for us to make is going to be a get balance method that is just going to return how much money is in the user's account so I can just say get balance the balance of your account and all we want to do is return and we can return get balance or I should say return the current balance like so very good so our main account is done right a user opens an account at the bank and these are their options with that bank they can deposit and withdraw or get their balance that's it but now a user wants to go forth and open a savings account so this is going to be an interest type of account let's create a class for this let's say interest account this new type of account is going to inherit the current account the base account in this interest account we are going to have its own properties so let's initialize very quickly let's say self uh let's go up here I'm going to take account number and balance pop that in here and then the methods of its own we need a rate right their interest rate before I create their interest rate let's activate our super methods so let's pop those in there uh let's pop in self and let's create our interest rate property n there we go very nice what do we want in here well the only real method of this is I would like to create like a calc interest I want to calculate the interest so I can return what do I want to return let's say self dobal balance I want to take my current balance and I want to multiply it by the interest rate which is going to be our new property that we just created here right that would calculate our interest rate how much are we going to receive good our interest account class is completed before we make our actual savings account we want to create a book like for a bookkeeper or a mini database in a way and I need a place that's going to store all my transactions for me and this will allow us to store them in a list so let's create a class called transactions this is not going to inherit anything actually this is a new class that I can append things to so let's say in it and in it doesn't need any parameters we are just going to create a property for a list okay that I can append things to and remove if you wanted to as well uh every we do so if I deposit I withdraw technically that's a transaction so I would like to add something to my list let's say that and let's say transaction cool uh let's call our list and let's append what do you want to append I want to append the singular transaction brilliant finally let's get our transaction history so let's just say history uh and for this I'm going to say for every transaction in my list transactions I would like to print off the transaction like so looking great okay so look back through what's happening so far I'm about to jump in and I'm about to make the final class our savings account because that's what we're trying to build right now we're building a whole system I built the account we built the interest rate account and then we have like a notebook or a checkbook it prints off all of our transactions and keeps those in check great our final class let's create us a savings account I am going to inherit now here I actually only need to inherit the interest account and our transactions but why why is that going to give me access to everything I need in my account that doesn't make sense because interest rate is actually inheriting a account for us so I don't need to pass in three classes like I did in the last challenge because I'm giving interest account the account super class this is like storing two classes in one so I'm only needing to give my savings account class two classes instead of three great in savings account let's create our in it and we sort of need a lot of things here we need an account number you need your balance and you need your interest rate right which is what we defined everywhere else so let's pop those in here then I am going to call my two classes so interest account do init right this is like our super. init what am I initializing from interest account well I'm actually initializing account number in balance and interest rate in reality right everything then finally I want to use my transactions class and I don't need to put inside in it I don't need to put anything because we don't have anything inside the init of our transactions class so the only thing we can put in here here is self right our key that unlocks everything all of our classes are completed we can now begin to work with our objects so going down here let's go forth and let's create an object called savings and this is going to be our savings account what do we need here well I need an account number so be creative right if it's a savings account I could do savings account double one I'm the first savings account at that bank right bank balance let's say I have 5,000 in the account and I am earning an interest rate of 0.08% all right I can take my savings object and I could deposit and withdraw from this right so let's hold off on the deposits and withdrawals until we kind of see everything else is what's happening right right let's get and let's go forth let's say print transactions well I can't print transactions yet I have get balance so let's return the balance currently I'm going to come down here let's say savings do uh nope let's just create a variable called uh balance and let's take our object now and let's say get balance remember I'm returning a value which can be used us as the value to my variable balance so I'm going to say balance and then let's say here our balance variable that's it okay to fix this error let's just go up uh super. in it but three were given let's try two things really quickly the first one let's change super here let's say account let's see if that does our trick for us it does right so now it's printing off the balance of 5,000 and that's what I wanted to haveen right great okay we have our variable the value is 5,000 okay now I can do a few new things let's do savings and let's call the deposit method let's say cool I'm going to give $2500 so at this point right I should have 7500 but I am also going to make a withdraw and I'm going to say I owe rent so I'm going to withdraw 6 50 okay great I've done my withdrawals so let's calculate our New Balance great New Balance this is how much I have left all righty let's continue on so now going forth let's create one called interest that's going to be a variable and I actually already have a balance so this is going to work out good so interest I want to get a returned value so let's say savings and let's say our Cal interest method right now that I have those we can add in our transactions so let's add two transactions what transactions do we have well we did this one and we did this one so currently for adding transactions you could add the numbers um um for this I'm just going to keep it neat and let's just make a string let's say deposit let's just say 500 okay and let's say withdraw uh let's say uh withdraw and let's not even do that let's say how much did I put in I deposited 2500 into the initial balance and I withdrew 650 from that balance I have my balance and then let's get off and let's print off our uh interest interest and then we can get our variable interests like so looking great I am going to run our program look at that there we have our balance right we then have our interest of 548 now the one thing I'm noticing I didn't display my transactions here right I should print those off so let's say saving let's say h what do we call that I called that history so I want to call the history method all right let's get that bank info I approve of that looking good we have a deposit of this much withdrawal of this much I have my current balance now and I have the interest that I'm earning on the account looking great I hope the gears are turning guys remember before you watch these videos these Live code videos pause the videos try these problems on your own try and find those Solutions because I've given you the text files which breaks a lot of it down so work through a part of it watch the video on maybe how I did it pause me again and then work through the next video that's how we challenge ourselves and get better remember errors are your friends use them to your advantage to help you solve a problem I'll see you guys in our next [Music] video great here we are in our next challenge with multiple class inheritance in this one we're trying to create a customer loyalty system I want you guys to think back on how we did the last Challenge and use the knowledge gained from that in order to implement the correct logic and code to solve this one we are going to create a loyalty system which allows the tracking and managing of loyalty points points earned by customers so start to think about hotels or like Airline points every time you fly you earn points to rank up your status within an airline or an organization we will use class inheritance to organize customers and store their transaction history great I'm hoping you guys have taken the time to read through what we need to do here let's head over and let's implement our logic and start coding all righty to kick things off we want to create a single object so I'm going to create a single class to hold the information for our customer so let's create a Constructor and the only real information is we are going to take a customer ID so like so and then a customer name every name is linked to an ID so let's create our our properties for these and this is the only information our customer needs at least for the time being so when I create a customer object the value will be this class customer and that single object is going to hold our ID and our name nice let's proceed and let's make a class called loyalty points this class will be responsible for earning points redeeming points and checking our balance our Constructor is not going to require the properties we made in the customer class and it's not going to require any information given to it but we do need one property with the initial value of zero soft do points we can create create a method which will allow us to earn points and earn points will take the number of points so I'll I'll say amount how many have we earned and all we can do is take our property points and we're going to add in our amount great what if you want to spend those points you found a great deal on a flight well you should be able to redeem those points how many do you want to redeem that is our amount now we first need to check if you even have enough points so if the points you have are greater than or equal to the amount your requesting to take out then you are permitted to do so and we can subtract the amount requested in our redeem method else let's just say here yeah you you don't have enough points uh so I'm going to say not enough points like so great and then our final one that responsibility is to basically show Point balance and all we can do here is we can return self. points all righty our two classes are done right we have one specifically for for our customer we have one specifically for our points that's going to handle the earning of points and the redeeming of points let's create a new class that's going to inherit both of these who Could That Be Well a special customer a VIP customer what if we make a class called VIP customer and this will get our customer and it will also get our loyalty points inside here not a whole lot's happening but I want to be able to create one object that inherits the functionality of the two super classes customer and loyalty points so I won't make any new methods here think of this as a mini database I will create init though and init is just going to initialize our customer class and our loyalty points class so all we need to do here is I'm going to take our parameters from the init of customer paste those in like so and then let's just initialize both of our classes so customer. init say self paste in those take out that extra comma okay so I've initialized that one and then I'm going to come down here and I'm going to initialize our loyalty points class and just give it all righty VIP is done we have three classes all of these correspond in work together and they are held in one class created right here now moving on we will need two more classes actually right this is a great example of O Let's create our two classes for our transactions my first class is going to be called transaction it's not doing a whole lot it'll be like a database just like our customer class to store relevant information about each transaction so making my Constructor the job of this is to hold three things a transaction ID number a customer and an amount so these will be given to to an object three values and they will be stored inside one object instead of three objects great now that I have that class done I can use this within another class so transaction will be each transaction will be one object each object will hold three parameters or three values a transaction ID number a customer ID or a customer name I should say in the amount now I have the ability to create a class called transaction history inside here we want a place to store all of our transactions so I can create a list called transactions we will create a method which will allow us to add our transactions so it's going to take a single transaction we can take our list we can take our list and we want to append that singular transaction like so let's fix this that shouldn't be a semicolon that should be a colon brilliant in our last one I basically just want to show all the transactions it's a way to check our account so let's say show transactions and I want to Loop through the list that we created here so for every transaction in my list transactions I would like to print what do I want to display about each transaction well I want those ideally so I'm going to say transaction Let's uh link our trans transaction ID for now let's link our customer well let's link our current transaction customer name and then finally we can link our transaction amount great let's create our objects and let's watch what's really happening here all right we have just made five classes this is the most we've made yet these two are working together within our VIP customer and then we have two different classes here one is responsible for our transaction information then one is responsible for our transaction history go back through the code and see what's happening there all righty our objects let's just make one object and let's say I have a VIP customer and he he or she is an object from our class VIP customer I need to give them a customer ID so be creative uh vip1 all right and then a name let's say I am the VIP customer great let's my account let's add a few points to it so VIP earn points how many do I want to earn let's say I've just signed up for a new Airline credit card where they they have an award bonus of 40,000 miles within the first 3 months and then I would like to spend 12,500 points on a quick flight so that's what's happening there all right now for each of these I should have a transaction right so let's make one called transaction one and the value of this can be our class transaction I have two transactions which we see above so let's make two objects the first argument will be the transaction name or ID number so let's say uh T1 it's the first transaction on my account what do we need here I need who well who is my object VIP and finally the amount well the first amount we said 40,000 so I'm going to put that there in our second transaction we have our same object and then this time I spent 12,500 and just because I'm reading this out let's make this a string so then I could actually say plus just for the purpose of our list and seeing and viewing the transaction history here great great all righty so we have three objects on the screen and we are calling two methods it's looking nice it's coming together nice now that we have those let's make one called transaction history this is going to be an object from our transaction history class now we don't need to give it any arguments here but we can use those methods so let's say add transactions what do you want to add well I want to add transaction one I also want to add transaction two remember these are being added to our list up in this class that we built like so there we are so we're adding those transactions that we created here now that I have transactions added what could we do I want to show all the transactions that we've had happen so take our transaction history history and we are going to link the method show transactions great so it's going to show all of those that are occurring now the last things we need to do is just get our customer ID our customer name and the number of points in their account so because we are returning the number of points in the account I need to make a variable to store that value so let's say uh balance now balance is our variable the value is equal to our get Point balance so show Point balance there we go then we just need to print off a few things so let's print off I'm going to say here let's say customer ID and let's just drop in their uh customer ID number let's print off here and let's say our customer name let's say our vip. name looking good and our final one let's just say uh loyalty Point balance and here we can just use our variable balance this looks great we took what we learned from last lesson we implemented key factors here in this Challenge and we even built on it more let's run our program we have our transactions being listed so transaction one Josh uh I added 40,000 points then we subtracted that and then you can see I'm getting my ID my name and then the balance I have in my account after all those loyalty Point transactions let's head over into our final challenge for multiple class inheritance I'll see you there [Music] here we are in our final challenge for multiple class inheritance in this one we are trying to bring in the fundamentals of what you could need to build an eCommerce store or the logic that's happening in the back end so we're trying to create an online shopping system which is going to allow the interaction between customers and sellers the system is designed using class inheritance to tell the difference between customers and sellers each with their specific functionalities read through this challenge I've put this as the the final one for this lesson but do we need to use multiple class inheritance or just normal class inheritance you tell me I want you to give this one a shot before watching this video how do you solve this challenge this challenge is also going to require us to use the open function from python we haven't looked at this now this is something you may have to research on your own Maybe you've seen it before how do we use the open function what operators do we use with it I'm going to break that down as I go through those parts of this challenge let's head into our code to kick things off up top we want a class for user now this we are going to create a single object from and it's going to store a username and the email of the buyer or the person who is signing into the account right if you log on to Amazon this is you for each let's create the properties we can use as we are building this out we will say self. username and self. email as those are the parameters great I'm going to mark this class like so now we can create a class for customer customer will inherit user we need to access the username and the email inside our customer let's build our Constructor our Constructor because we are inheriting will take our username our email I will drop down here and let's just initialize it so with our init and remember to pass in our username and our email like so oops I will make one new property here this property is going to be our shopping cart so all the items you add to your cart will be appended to a list inside customer we would like to have one that's called add to cart what will this do well this is going to add a product that you want to the card we can append that we can append that we have ad cart uh what if you don't want the item then you need to be able to remove that item from the cart so what item do you want to remove I want to remove this product if that product we're searching for is in my shopping cart then we now have the ability to remove it so we can say cart use the remove method remove is the opposite of a pend when working with lists right and then anything else we can print off and just say uh item not in your cart let's view our cart so what do we currently have in that cart how can we do this let's go through and let's just say items in cart then we need to go through so for every product in my shopping cart we would like to print off the name of that product so take our product link the property name are we still here I certainly hope so this brings us to the last method now I want to have the ability to save my cart as a file specifically a text file so all text files they end with our extension txt so for example filename.txt I want to save all this information into a separate file to do this we will use the open function and I need to give it a file name what we can say is with the open function I want to open the file name and I want to write new contents inside this file I'm going to call this file file so with the open function I want to open this file and I am now going to refer to this file using the name file this is like a nickname or a variable we are creating that is linked to this file for every product in my shopping cart I want to write in my file what would you like to write in the file well I can add an FST string and I want to write in the file I want to write the product name and I would also like to write the product price looking great once we've appended all of those to a separate text file that's going to be held somewhere else then we can just print off and I could say something like cart saved to file name it's done right once we run this code we will see the implementation and the output of it but that is how I could use the open function very basically to work with a text file and append to a text file the two real new key phrases here if you've never worked with this before um I'm hoping you did research before watching this challenge is we have the with operator and we have the open function with those we can use the right method to write contents inside our file all of our customer information is completed we now want to build the opposite and we want to build the opposite for the seller so I can create a class for seller we don't need a class for the sell's attributes because they can actually inherit user if you are a seller on Etsy you're still a user if you're a buyer on Etsy see you're still a user both of those have a username and an email linked to those accounts which is why I'm using user for both of those going forth with our seller class we will initialize and inside here we will say username and we will say email again I can take what we did here so my super init I'll take cart I'm going to change that but let's take it I initialized our super class and let's change we do need a list but the seller is going to have a list of products instead not a shopping cart a list of products they can do the same thing as we did in the above so without looking I'm going to scroll down can you do the same methods just in the seller class so for for example we have ADD product we have remove product then our last two we would have view your products which brings us to our final one you could have save product can you complete those without scrolling up the same Concepts we're just changing a few small details within add product we need our keyword self and then we are going to be giving it a list or specifically individual products to be appended to a list so self products append product we can do the same for here we're going to give it s we are going to give it one product before removing we need to check if that product is in my list of products then I have permission I can remove that from my list what do I want to remove the product that I give as the parameter anything else we can alert the user and say uh products not found great for our view products we are going to print off all the products that I have in stock so list of products we can then Loop through and say for every product in my list of products I would like to print off specifically the product name so I'm going to go here we don't need that string I'm going to take my product link the name property good if you're new to the open function what do we do here how do I save the products to a text file well we need to give it a file name to be saved somewhere I am going to say with my open function file name I want to write the contents inside as a file for every product in my list products I would like to take my file and I would like to write contents inside of it what am I trying to write here well I'm trying to write the product name and I am trying to write the price of the product how much am I selling it for once we're done with that you can just print off and you can say uh products saved to your file name where are they going to be saved amazing we are here for our final class now I do need to create a class right cuz because a product is an object and this object is going to store information about the product itself such as a name and a price which we've been working with for each of these let's create that so name is equal to name your price is equal to price that you are giving the product we then have the basic info so basic info about the product we are just going to go forth and I could say print PR what do I want to print here the name of the product so selfname now that we're in this class and I would like to also print the price of the product so self. price great classes are done it is time to create our objects which are going to use these methods that we have now built on the outside let's create two people let's create create a customer and the customer will be from the customer class and let's create a seller it's a simple transaction between a buyer and a seller on an eCommerce platform I need a username so Billy Bob two3 that's the first username uh an email is going to be Billy at Hulu that sounds like a bad that sounds like a hot mail right our seller is going to be Sarah Jane 88 and her email is going to be Jane at Yahoo because who uses Yahoo anymore all right so we have our two objects customer and seller now the seller is going to have a bunch of products so like product one and we could pretty much add all these so I'm going to copy this and let's say she has three products in her store the first product can be a shoes pair of shoes so I'll say shoes she sells her shoes for $65 yeah that's cool 65 even okay her next product is a ring and she sells the ring for 125 something special about it then lastly she sells a t-shirt this t-shirt can be what 28 bucks we have our three products which are going to correspond to the seller and the seller is going to sell them to the customer going forth all we need to do is let's take our customer and let's add those products to the cart right so so what do we want to add to the cart well I want to add here I can say product one product two and product three to our cart now they are in our customer's cart using the add to cart method that we made we can take our customer and let's say view cart we can view the contents of the C so if I were to run this let's watch what happens there we go I have items and cart I have all three of those items that the seller has so shoes ring and t-shirt looking good so far we're airfree I can continue on and let's now take our customer and I want to save my cart I need to give it a file name so what do I want the file to be called where I save this so let's just say customer cart giving it the text extension for the text files then we can have what methods haven't we used remove from cart haven't used that yet so let's remove an item let's say ooh the ring is too much we don't want the ring anymore so here I can take my customer and I can say remove I should say remove from cart uh product two I don't want product two anymore let's now view the customers update cart I'm going to run that great there we go so cart saved this text extension I will view that here in a bit let's just complete and let's go with our seller so I'm going to say seller add product what do we want to add well all of those products we've created so the seller is going to have all three of these let's go product two and product three like so very good then I'm going to take my seller I would like to view the products the seller has and finally what do I want to do I want to save the products where do I want to save it let's say seller products. txt all right I'm going to run this great look at that items and cart we have our three here we have our text file extension this is our updated cart and then here we have the list of products from the seller now where are the text files that's the question they're going to be over here let's open our text files up top here we have our text file customer card text with the contents we have our seller product text with the contents if we wanted to get these on one line all we would have to do let's go up here to our open function let's just put those on a new line and we can do the same for our other one amazing well done guys that was our final challenge for multiple class inheritance I will see you guys in the next video spend some time to review what we've done here before going into our next lesson I'll see you there [Music] we've come so far this is our last lesson of of this course for objectoriented programming and this is a great way to end the course as we are going to talk about special class methods only found in Python and only used within a class these are essentially Python's version of like Special Forces or like the Navy Seals let's check out what I'm talking about what are these special methods so special methods are automatically ran when an object is created and this is the same thing that happens with in it right because in it means initialize it means start and in it is automatically used and called when we create an object we don't even think about this it just automatically happens these are double underscore methods in it has two before and two after and these double underscore methods make operator overloading possible now I can say special methods but I could also say Dunder method Dunder means double underscore method it's a Dunder method and these give extended meaning beyond their predefined operational meaning so you already know one special method in it we've been using this this whole course so you have a general idea of what in it is and how in it works but with python we have so many other special methods that we could use we are only going to take a look at a handful in this course once you get a general idea of what they are how they work you'll be able to implement your own logic in use new ones outside of this course let's check out a few of these now here is a list of some special methods now the first one I've put as in it just to warm things off because you already know that one is down right we use in it to build variables for the object we have a Dunder method called Dell and Dell has the ability to delete an object I want you to think about now how do you delete an object from a dictionary well the same kind of applies for this Dell method we have the dunder method string and this Returns the object representation in a string format so it takes the object and it turns it into a string we have the Len or the length method and this Returns the length of an object we have the equal method which allows us to compare the Val values of two objects using the equal sign operator that's really useful we then have the addition method and this allows us to add two objects you can start to think if there's an add method is there a subtract method yeah there is one and we can take a look at that there's endless special methods where're only looking at a few here and we're not going to cover all of these in the next few slides but you know that they have them and in the challenges I might give you one where you use a method that's not in here think about how can we do that where could you learn about that let's take a look at the first Dunder method I have which is going to be a string this method creates a string representation of our object it's called by the builtin string function we have this this this is a normal built-in function that comes with python and it's commonly used to define human readable strings if you want to have information a person can read we would convert it to a string you can see I have a class person with in it and then under in it I have my string Dunder method it is returning the name and the age of my object which is John 25 when I print my object it's automatically going to return this to me as a string so I can construct the string using the name and the age properties from our in it and it'll return a string for us we create an instance of person if you try to print the object without our string method check out out what happens it's not necessarily an error but it's just printing off literally the word object here is how that would look so everything done correctly with our Dunder string method here we would get a nice output like this if we try to print an object without the string method we would get this so this is a useful tactic right now why do we use this this string method is helpful for a lot of things debugging and displaying object info that's in very specifically human readable format you will see this in code examples as you broaden and strengthen your programming career let's take a look at the next example of a Dunder method next up we have the delete method and this method is called when an object is about to be destroyed destroyed it's used to perform any final actions before the object is removed from the program's memory so think about any cleanup tasks or actions you may have any connections we need to close anything that needs to be handled before we delete an object this is why we would use the delete method now it's not actually very commonly used but it is very useful when you're trying to achieve that goal and it can be helpful in situations where manual cleanup of your code is necessary we have our class file within it and then we have our Dunder method delete currently all this is doing is printing it's not doing anything very useful right but it is being called now to use it we create our object and we use the python Dell operator Dell object name this Dell operator is the same operator we use when working with dictionaries in deleting objects from dictionaries so you should have seen this operator before that can be used with our Dell Dunder method great here is how that would look by calling Dell on the object it would call the Dell method and in this example it would say deleting file data. text moving on to another new method our add method now this is quite useful especially when working with integers or trying to do anything like that this allows objects of a class to be added together now really look what's Happening Here we have in it with two properties an X and A Y then we have that special ad method and this special ad method is taking one parameter other you will commonly see this parameter other it's like a special keyword or a special parameter used by developers it's just like with a for Loop for I in range I we just use I we like it right so other is the same and that represents an other object we then enter that and we're checking if other is also an object of the same class so if that instance is the same as my class Vector that's what that line translates to if our parameter other is the same object or from the same class as we State here then we can do something so if yes we want to add the corresponding X and Y components of the two vectors together new X New Y we're taking the current x value and we're adding it to the other object's x value we can then return a new object with the new values anything else you can raise a type air for the unsupported type of our addition great in result this creates a new object So currently before the program ran we had Vector one vector 2 result is actually going to be Vector 3 right we are adding them together and getting a result this would be the output our new object has the values of six and eight why well remember we added together the values of our objects so six is 2 + 4 8 3 + 5 that is giving us our output of what we're looking at we are then taking the object and linking the properties X and Y to get our values of six and 8 this is a lot okay if you need to pause pause the video go back to the previous slide as I find that one incredibly helpful this one broken down how I've broken this down read that that's translated to plain English that helps me a lot when learning and I certainly hope that that helps you if we have the addition method we must have a subtraction method we do our subtraction method works the exact same as the add method well not the exact same it works the exact opposite as that method you can see here I have the same codee the only difference is inside here I am subtracting those values and then at the bottom I am also subtracting those values this does the opposite right so my new object is going to be 2 -4 because 7 - 5 is 2 4 - 8 is -4 that's the value of my new object if I were to print off Vector 1 that gives me 7 and four and if I were to print off my result or res that would give me two and -4 all of these are working the same that's great this brings us to the final method here and this will be our equal method and I included this one as this is actually quite useful and it allows us to check if two objects are equal imagine if you have 10 different objects from a class just like in our last lesson's final challenge we created this eCommerce platform store and we had three products what if a store had a hundred products you can see how this method could be used to our advantage we have a class called rectangle and then we have our equal method inside there now equal also takes the other parameter just like with add and subtract and we can check if the objects are from the same class and if this returns true so if the width of my object is equal to the width of the other object and the height of my object is equal to the height of the other object then true it's equal those objects those rectangles have the same width and the same height it's going to return true because they're the same object now check out how I can use that I can print and I can take my object and I can use the equal operator with my second object R1 is equal to R2 that's true R1 is equal to R3 no that is false python will call this method on the object to its left so this would be called on the R1 cuz R1 is to the left of the equal operator we check if other is also the same object of the same class if yes check if the objects have the same width same height and if that's true we can return true which results in a true output amazing that wraps up this lesson now before you jump into my videos for vs code you have a wealth of knowledge now as in we started this course objectoriented programming with the introductions of objects the introduction to how to define a class we looked at class inheritance and then multiple class inheritance that brought us all the way to this final lesson of special methods in Python take the next hour take the next 2 hours spend the rest of the day and try to build classes using all the knowledge you've gained from this course and try and include a few of these special methods we've learned I'll see you guys in vs [Music] code [Music] great here we are in the first challenge for the introduction of Dunder methods or the special methods for which we use when working with classes diving in I have worded these challenges slightly different in the explanation to make it hopefully EAS easier or just a different strategy for going through the problem if we go through these one by one ideally we should be creating some pretty cool classes with our objects and our Solutions working correctly in this first one we want to create a system for checking the different roles within a company so I'm about to jump in here and I'm just going to Define my Base Class employee with our init and this is going to initialize the name property and employee ID property let's jump over into our code up top here let's create our class and I will call this employee employee is tasked with creating the properties of our name and employee ID for each of these we can create those properties then we're going to use our string method as in what we want to do is when I print off my object I want it to print off the employees name and their ID so I can do that by using the special string method and we can return what do I want to return well I'm going to return a string like employee then right here let's do some interpolation let's say our name so employee name very nice all right so that is going to return us the employee name and that employee ID whenever we print off the object itself that's the advantage to using this string method is we can print an object and it will automatically return or print something back to us great um moving on let's create a method for to check if something is equal to something else so we know that we're going to have other and other is that just that special parameter word you could say anything else but you will popularly see the word other in here as a parameter what we want to do is we want to see if two employees share the same employee ID because that's not possible and speaking of employee ID let me add in my parameter there my value and to do this we are going to say if I'm going to create a condition and we are going to use the is instance function inside here we are going to say is the other person is the other parameter is that also an object from my employee class that's what we need to check first to make sure I have a object from the same class if that is then we can check if they have the same employee ID if they do I want to return the value true so how can I do that in one line of code I want to return something if it's true what we can say is if the employee ID is equal to the other object's employee ID that's going to be true else if it's not true we can return false like so so now I can use this to check first is the other object from the same class using the is instance function and then we are going to return true if they share the same employee ID which is impossible that's not allowed our last method that I want in this uh super class or this class is I want the ad method the ad method can also take an other and well in this case actually I'm going to create this ad method but it's going to kind of raise an error actually so I'm going to say raise and let's say uh value errror what do I want to say what do I want to alert the user to EV value err and I just want to say can not add employees right that's impossible I can't add to employees because they're not numbers they're objects and those objects those properties one of them is a string we can't do that so I want to raise an error if we try to add those two together and that can be done by using raise the python keyword and then the type of error carrying on I have my employee class now let's make a class for uh manager and manager is going to inherit and I want you to think about while I'm making in it here what is it going to inherit well the manager they are still an employee that's not changing right the manager is just the number one employee in that location so the manager is going to have their their own name and their own employee ID as well as which department are they the manager of that's important let's activate that super class quickly here and put like that perfect and let's create our property of department and say that is the value of our parameter great now that we have that all running we are going to do a few things now each method in this challenge they're all going to be special methods and they're to show you what we can really do with those and how we can use these special methods to your advantage right so look at what's happening and think about in your own projects or your own Creations how could you implement these special methods the first special method here I'm going to do is let's do another string and when this is ran we want to return a few things so I'm going to give back here like a manager name and a manager ID and their department so when this is called let's return and let's say name so the name of the manager and then here we can say like that manager has an ID and that manager has a department let's just say DPT great there we go so I'm returning the manager's name ID and their Department next up to bat let's do the same thing so I'm just going to bring these down actually okay we have the ability to check if the same employee ID is shared and check if a manager was added together okay and then finally our last one right we have a staff and the staff can really inherit everything so I'm going to I'm going to take this I'm going to take the manager class and we can just bring that down and put this here I'm going to ch change manager to staff but it's still going to inherit employee right so each class is ideally doing the same thing but wait until we create our objects to see the interaction between them so a staff is employee now the staff will not really get a department their only difference is going to be a role so what role in the company do they have so let's change that to role uh uh let's say Ru here great looking good cannot add let's say staff members all righty now comes the creation of the objects and what we really want to do with these so first up let's create some employees let's say employee one and I'm I'm going to copy this cuz for each let's create two objects so let's say I have two employees I have two managers and then I have staff and so we can really see the interaction between these objects so employee they are going to be an object of the employee class and we can just give them let's say John do let's say h what is the other employee ID he can be one employee 2 let's carry on with that and let's say janeo she is going to be employee two great our managers we can give them the manager class and they also get a name let's say uh uh Bruce Bruce okay uh they are employee ID let's say nine and their department is going to be marketing great uh manager two they can be Sarah Jane her employee ID is eight and she's going to be ahead of sales our two staff let's say Mark Jones let's say Mark Jones is 12 and let's say his role is going to be he's a mark marketing Ops Dev and then let's say our other staff member this is going to be Emily Davis and she can be 14 her job is going to be software engineer now let's use the classes we've built and see how we can use these special methods to our advantage so let's just go through and I'm just going to go through and I'm going to print off a few things so let's print off employee let's print off manager one let's print off uh staff one right so these are just objects but because we're doing string representation with our special string method we should actually get back some information while I run my code oops okay so let's go up and fix this line 20 where is line 20 super let's change this to employee while I'm up here let's change this to employee all righty trash our terminal run it again great there we are so the three objects I'm printing employee I have the name and the ID number uh the manager is Bruce Bruce id9 Department marketing and then the staff is Mark Jones id2 position marketing op step oh that's so cool so we are using that new method to print off our objects and now we can actually return that value great let's carry on so now that I've done that let's check if some of our objects are equal so I know we have employee one and I want to call the equal method on the object to its left which is employee one and I'm going to check if it's equal equal to employee 2 I can do one more um ideally both of these should return false false very nice okay they are not equal right they are different staff one you can see these three different Arguments for each object they're different they cannot be equal great um and then finally let's try and add the managers together right because I did include the add special function and I want to raise an error let's go down let's say manager 1 plus manager 2 let's watch the error get risen raise cannot add well okay if I'm using the ad I should be giving it a parameter even though we won't do anything with the value of that parameter let's create it now that we have all three of those let me trash let's Replay that code great there we are so I'm still given an error right but then we're given our value air right here right our value air cannot add a manager and that is raising our value air it was risen great that was the interaction we had between three classes we created one super class employee with two sub classes manager and staff each of these classes use the same Three Special methods and we could see the interaction between those as we created and use our objects at the bottom very nicely done I will see you over in our next challenge [Music] Welcome to our next challenge I decided to put a challenge together for you Sports lovers as I don't tend to do too many sports challenges they tend to be about Finance or travel something about that so here's a sports challenge using our special methods now we will have three classes each class will be using the same special methods kind of like in the last challenge although instead of using the addition method you will be using the subtraction method we will be creating sports teams in and using two data types as well you'll be using lists and sets to complete this challenge let's jump into our code and start going on our super class the first class we want to put together is going to be a generic class for sport right every sport starts with something similar in this case it's just going to be a name and we can build off that name so let's start at the Foundation at the basics and let's just create a place for us to store a name of a sport here so I can create my init and I will create one property called self. name we can return a string if we print a sport object and really all we can return here is just say this is and then I guess the name of the sport self. name looking good let's actually just create one final special method and let's do the ADD but I'm going to make it not possible to add one of the objects from the sport class I don't want that so similar to the last one we can raise a type air and say not possible and then I know in the other ones I'm going to have the subtraction one so let's just raise an err in this one as I also don't want this to be possible using our super class sport right so all of these are very Bland very basic right now now we can start to build the other two classes which are going to start to do a few things we are going to have a class called football football is a sport we can inherit that and football has a few of its own details so besides just a name which I need to inherit um it's also going to have a team right so who is that team and let's just take our sport class let's in it and let's say self name and then create a team property like so when we print an object so I'm going to create a string representation here here we want to return the name of the team and the teams they match with or the teams that they have so how this would look is I could create an FST string and I could say this is we can put ourself name here then what we can say is all righty we can check if they are equal so I'm going to say self we are going to say other do you remember how to check if they're equal what do we need to check first we need to check if they're an instance of who other what are we checking if they're an instance of the football class if that's true then we can check to see if the values are equal so we can take the name and we can say if the current object's name is equal equal to the other object's name and my object's team is equal to the other objects team that's going to be true else oh I don't even need an else let's just return false great then the last one we'll do for this one is I included in the task for you to create a subtraction method so let's use the sub special method and I am going to have the ability to do something here so this is going to take a parameter of other and before we can do anything we need to make sure it's an instance from the same class so let's take our other and let's put in football now when this is called we want to create a list and this list is going to have team names but specifically it's going to be a set in a list and remember a set is one of the four data types or data structures in Python the other three being list Tes and dictionaries a set is used to store multiple items within a single variable so seeing that a set is unordered you can't change it and you can't have duplicate values that makes it useful because we can't have duplicate teams so let's create a list here in a way and let's say uh let's call this shared teams that seems like it'll work let's say it's a list and then inside the list we're going to have a set right now the set we're going to have our self. team and our other set is going to be other team like so I can then check and I can say okay so if I have elements if I have something in my list shared teams then I can do something then I want to create a new object from the football class using the list values I've just created so I can return a new football object and the team name I'm going to put a is an F string and I'm going to use the team name as myself. name like so okay that's working then we can say for our other one let's just say uh our list of shared teams and to separate I'll put like two dots great so I'm returning a new object else if that isn't case let's return uh the there's just no common teams no common teams between them all righty and then lastly if that was not true right then we can just uh return let's say not possible kind reminder there all right looking great I am going to do the same thing here for the basketball class so let's take this what do we need to change with this class now well let's change everywhere it says football we are going to give it a new class okay and this can be our basketball class it's going to do the same thing as the football class it inherits sport it has a team we are checking if the values are equal and we have the ability to add teams as well using our sets all right now that we've gotten through all that the last things we should do is let's play around let's see what these values are going to return so let's create let's just create a very basic one so this is the most basic class this is our super class right this is super basic all right there we are um if I go through right and if I print and I print basic what do you expect to see well we expect to see this is this is super basic all right that makes no sense but if I go up to my super class that's exactly what I'm returning okay all right so we know that works this is I'm just going to say super basic that'll just look better we have that let's create um objects from the other two so let's say football one football 2 they are both objects of the football class um the name let's call them well what sport is that the name yeah what sport is it so football okay and then team I'm actually going to make a list here of teams I'm going to hard code this for a second and I'm going to say well let's take some football teams here let's say the Giants and let's say the Dolphins guys I haven't watched American football in years so don't judge me on choosing those teams uh okay let's say uh football let's say now the teams let's say the Steelers and let's say the Eagles there we are so I have the name of the sport and then a list which contains two teams this list is what's going to be passed through here and subtracted against we can do the same thing for basketball so let's say basketball one basketball 2 they are both from that basketball class okay we have a name again so as a string I'm giving the sport of basketball okay and then for your teams you can give a list again uh I'm going to say the Jazz and I am going to to say here the Kings okay uh and then oops our other teams let's say Golden State and let's say the Rockets brilliant so we have all of those now that we have let's go through each one and let's just make sure that they're giving us some type of information that we're looking for so printing off our objects to call our string method here we are this is super basic this is football team matched with teams Giants and dolphins seems like it's working great trash my terminal now let's try a few of the other special methods so let's go through and let's say oh I don't know let's print off let's check if some teams are equal so let's check if football one equals football 2 let's check if basketball ball is equal to football what do we think is going to happen here okay let's give it a check great so it is working both of these are false how could I check if one of them is true let's think about that right I want to turn one true what would I need to change if we wanted to do that we change change this and we say like team a in both of these and then the other team we can say is going to be Team B now when I run this the equal operator is going to evaluate those and check because we're passing those in and you can see that it's true now it's true because when I go up we are checking if the objects are both from the the same class and we're checking if the name and the teams are the same so it's returning true great that brings us to our last ones let's try our subtraction method so let's print off and let's say basketball one minus basketball 2 what can we do here what is the output going to be let's run it okay so we're getting no common teams between them right that's because each of these teams is completely different what if I change Golden State and I now put Kings there we go this is basketball team matched with teams Kings right that I'm subtracting and it found that it's in both of these great very nicely done on this challenge let's move on to our next one and I hope the gear are spinning everything we've been working with here is slowly coming together you're doing great we have two more challenges left and I will see you guys in the next [Music] video [Music] welcome back to our next challenge give a read through now in this challenge we've done Library as a topic for multiple class inheritance and that really just focused on one product they had books now imagine a company with multiple products we don't just have books but if you go go to a library at least when I was a kid I could get books or I could even rent movies and DVDs we want to create a few things on how we can handle this together now let's head over into our code and start on our Base Class our super class item starting in the code let's create our class item now everything in the system is going to be an item a book or a movie doesn't matter they're both items so that's why I'm creating this super class which will be given to both of those items now we need to give it a title and an author or director Creator so selfname great now that we have those let's do a very basic return using the string method and let's just say here what they're getting so just return and say self. title self. name is by the director or the author great there we are so we have an item now an item is going to have two attributes a title that name and it'll also have an author great continuing on let's create our first class which will inherit so we have a book and this book is an item this book is going to have something of its own so it's not just a title and an author it's also going to have the number of pages right just like a movie might have the length of the film so slightly different now let's initialize and let's say item in it we are going to say self we are going to say title and we are going to say author then let's say here let's create a property for the number of pages Pages great now maybe we want to return the length of that book this is a bit strange but let's return the length of the book based on the string representation of the length let's just use Len we haven't used this one yet and let's say self and let's say we want to return the length of self. pages right cuz Len you can't really return the length of a number but let's watch what happens with this all right so our book is complete let's create a movie and let's say a movie is also an item right or technically a movie is not an item so a DVD is let's say that uh this is going to inherit some stuff now we are going to inherit title that's going to come from the item class but then a DVD doesn't have an author it has a director so let's say director there and let's say length or duration how long is the film so we have that which means I can take item we can initialize item very quickly and the only thing we're actually initializing here is we're going to initialize our title but then I'm also going to initialize director because director and author are kind of like the same thing although it's not the same name that's fine I'm still going to take that as the second parameter from my super class so let's just for now initialize director then the real new property is our duration let's say that is equal to our duration okay uh what is the length of this movie going to be well let's use Len let's say return let's say self. duration right now that we have that let me clean this up okay looking Grand continuing downward now I'm going to make one final class now really Watch What Happens here so I'm going to create a class and I'm going to say library item Library item is going to inherit it's going to inherit book and DVD so let's say that library book is this X book and DVD are those X's so I have a tree I just made a tree okay now if you look at that everything started with item so this new class library book has access to the class item even though it does not directly inherit Library item only inherits book and DVD but both of those classes inherit item so I'm passing the item class through other classes for which I can use in this class that's a new level of inheritance all right so in this final class let's initialize well everything I want access to pretty much everything I want access to my title my author my pages my director my duration uh and then let's make a new one how many how many copies does the library have available let's say copies so I'm going to go through and I'm going to say book in it okay now everything coming from book initialize so title author and Pages everything coming from DVD initialize so what's coming from DVD we have a title as well we have a director this time and then we have a duration great um and then our new one I'm going to say copies is equal to copy iies wow this is a bigger class we're putting together here so pause the video just take two or three minutes to look through this new class we just made and see how we're initializing everything and how it's being passed down through the classes this is a really important fundamental concept here okay this last class all it's going to have this is going to have a string method and the addition method which we'll get to here in a second let's just start with string and let's say here for string let's return an F string okay let's try that so if I return my library item object I should get the title is by this author the book is this long and my DVD is going to be this long or do something else creative how could you use those properties to return something something in the string method finally I want to create and use the ad method now imagine I have a copy of a book like Harry Potter 1 and then I get more copies of another Harry Potter one I want to be able to add those copies together right because it's the same book and I'm adding them to my library so I'm going to use the ad method to do so so we have other what do we need here so if we need to check is an instance of the same class so if other is from library item okay and my current title of the book is equal to the other title of the book it means they're a match they're the same object and they have the same title so for example I'm inheriting book and DD okay so this is making sure that either the titles of two books match or the titles of two DVDs match I cannot add together a book and a DVD they are different right and I can do that because we're inheriting two different classes all right what I want to do is let's say combo again and I want to have a combination I want to add together my current copies with the other copies available that's how I can do that we can then return a new library item object I can return here the title of the object which was the existing title really okay um the author is the existing author the pages it's still the same number of pages it's still the same director all the information is still the same the only information that is not the same is the final attribute the number of copies this is increasing to match the variable we've just added combo I'm updating the number of copies by creating this new object finally I'm going to throw an else in here and I'm going to rate a type err and I'm just going to say cannot add two different objects together to alert them just like a friendly reminder hey you can't do that amazing our classes are completed we are ready to move on to create our objects I'm going to go forth here and I'm going to create an object of each I'm going to say book I'm going to say DVD and I'm going to say a library item so I have one from each we can play with so let's say book for our book let's just say Harry Potter okay our author we can say JK rolling um and then what else do we need to know here I'm going to say Pages oh I don't know let's say 500 so DVD is going to be an object of our DVD class let's give it the movie Inception that was a good film let's say the director was Christopher Nolan okay and then the duration of that let's say in minutes uh let's say it was an hour and 30 minutes okay great and then our final item is going to be a library item so I'm going to use my library item class now I'm going to do really two things here I need to include so much information so let's give it a title let's say python course let's say I am the author or director let's say how many pages let's say 500 it can be a number um I need a director now because I'm creating an object from the library item which is a bit strange okay I'm going to go through real quick and I'm going to print off off each object let's give a quick test to that okay so I have been given an error ah it's because I'm using name not title okay so I'm going to say self. name inherit that that makes more sense so I use that in other places should be good okay there we go so I have Harry Potter is by JK rolling Inception is by Christopher Nolan all in all everything is working generally how we want it to work let's test a few new things out now so let's print the length of an object now if we did not have the Len function inside our classes like up in here then [Music] great here we are in our next challenge now I've done Financial challenges I've done things about travel I haven't done one about about crypto so I had to include that one here what we're going to be trying to build is a cryptocurrency management system and we're going to make a main Base Class of crypto and then two child classes of Bitcoin and ethereum to represent those two most popular cryptocurrencies and their properties our code is going to allow users to create new cryptocurrencies compare them add them set prices and calculate the value of their portfolio at the end of this you're going to be creating a mini portfolio and using a dictionary to create objects based on that let's head over into our code which I can call here and I can just say crypto it'll be very generic and this is going to be a very generic class I can have in it and in it is just going to take the name of the crypto okay okay so let's make our property for that we can return a string representation and all we can return for this cuz we don't have a whole lot of information just yet we can just say this is okay um we can check if the objects are equal to cuz we can't have the same names right Bitcoin there's only one Bitcoin there's only one ethereum so self other and and here we can check if is an instance if what if other is an instance of the crypto class then we can check if the self. name is equal to the others self. name that would return to us true right else we can return false all right we can expand on this in the coming classes I I going to create an add special method here and I want to have the ability to add two crypto names together so keep in mind they're just strings so not too many errors should happen and I should be allowed to do this but I do need to check if they an instance of the same class still so I can say other I can say what's my class crypto now now let's just create a variable called combo and this is going to be the combination of my strings that will be the value so what do I want to do I want to add together my self. name uh I can say plus uh let's say a space let's give a space and then we can say other dot name okay looking good then let's create a new crypto from this combo name so I can return a new object the name will be combo so I can use that to create a new crypto if only if it actually worked like that um else we can't perform addition so let me just raise and I'm going to raise a value eror and I'm going to say uh canot preform addition between them okay next let's create a method to set the price so this can take a parameter price and I'm going to create a property right here because I don't need to define the value when I create my object only when I use this method set price so I can Define it locally inside this method but because I'm using self it's a key it unlocks it for which I can use throughout my class let's say I can add my parameter price onto that now let's say Def and let's get the price I have a method to add price or set the price now I want to get the price so the first thing I need to do here is before I get the price I need to check if this object has the attribute price so I'm going to first put in here what do I want to check well I want to check myself my object and I want to see if my object has the price property so I'm going to say price if that's the case let's just return the price else let's just say print print off uh price not set looking good and oh how many methods do we have 1 two 3 four five six including in it let's finish up the super class with one final method and down here let's create a method and let's just say uh Cal value I want to calculate the total value of everything and I'm going to be giving it a quantity so how how much of something do I have so I'll say Quantity and I can say if we have the attribute if I myself has the attribute of a price then what I would like to do is I would like to return the current price times the quantity that I have like so else once again I can just print off and say price not set great so our super class is done that's built now before we create any objects let's create just two child classes so let's create one for Bitcoin and we can give that crypto cuz that is uh for now let me just type pass and I will create one for ethereum and let's call that also crypto let's inherit crypto I should say great now the two classes aren't going to be doing a whole lot so once I do one I'm just going to copy its components but just to give it a bit more functionality for class inheritance using our special methods let's give in it and oops I have an extra underscore so inside in it it's not really going to do anything right but we do want to activate crypto and let's initialize it and let's just give it a name because I don't have the property let's say Bitcoin now when I create a new Bitcoin when I mint a Bitcoin let's say string and let's return what can we possibly return you can be creative here put something you'd like just say uh Bitcoin is decentralized okay then let's create a method called mine and create return statement that just returns something like oops let's say mining the next block great I'm going to copy this paste this in ethereum just change a few of your attributes right so instead of Bitcoin we can say ethereum ethereum what do we want to say here ethereum uses smart contracts we can say that and and then instead of saying that let's say minting tokens all right we have our three class is done the really interesting stuff is happening in our super class and then the two children classes just Bitcoin and ethereum a few things here are happening now let's start playing around with these classes we've made so let's create some objects let's create uh some crypto objects so let's say crypto 1 equals class crypto crypto 2 equals crypto class we can say Bitcoin and we will say ether all right so within each of these right uh Bitcoin and ether do not require any arguments because we hardcoded the name inside the init Constructor so our other cryptos we do need to put something here so I will put Solana as a name and then I will put let's say cardano Okay um there we have two others so Bitcoin eth soul and adaa now you could print off your objects right if I go through and I print off crypto one that's going to return a string representation of salana right and we can also check and we can check a few things so all of the other ones work the same I can say Bitcoin is Bitcoin equal to crypto 2 actually let's say crypto 3 let's put in crypto 3 here and I'll do two of these so I'm going to copy that so initially let's say Bitcoin is equal to crypto 3 and then we are going to say Bitcoin is equal to Ether uh crypto 3 is also Bitcoin so running this is going to result in the same so let's change this here it's missing one required let's give it uh self okay and let's go up to here and we can also give it great there we are so firstly I'm returning a string repres presentation of this because we are printing off crypto one which is salana and then I'm giving true right so Bitcoin equals crypto 3 that's true because both of those share the same name Bitcoin all righty looking great let's run a few more tests so let's just try to add real quick let's say ether plus crypto2 right I can try and run that I'm going to turn these off for [Music] now all right so you have adding you could have the subtraction special method then we also added some other methods of our own right we made a get price a set price and a cal value why don't I take my ether object and I say set price so how much is the price of eth at uh I'm going to Ballpark I think it's at like 1750 so let's say 17 uh 50 then we can print off right now if I were to print off my ether object and I say get price I should be given this let's check we're also adding together ether and crypto too which those that's not going to work let's just create a hardcoded property up here and let's say self. price initially is equal to zero great running that resolve that right so it is giving me 1750 this is ethereum cardano a cryptocurrency so that is US adding together the cryptos right because actually I'm adding together those strings right there all right so everything there is working I'm not going to call every method that we made you guys can play around with that but at the end here let's try something different let me create a portfolio this portfolio can be a list right but each element in my list is going to be a little dictionary so let me just enter this and let's say each item each key pair so my first key pair is going to be crypto and it's going to be let's say Bitcoin right and then and my second key pair is going to be quantity and I'm going to say an integer so how many do you have let's say five um and then let's call the other one let's pretty much do the same but this time let's say ether and let's say Quantity let's say let's say 32 if you know you know let's let's just add one more in here let's say uh our last one can be crypto and for this one I think I have crypto one which is salana let's say Quantity okay and let's say 25 okay what could [Music] I congratulations and well done in your endeavors in this course I'm so proud of you guys you've made it to the end throughout this entire course we have introduced instrumental Concepts in the fundamentals of objectoriented programming in Python we will use these Concepts throughout our entire programming journey and as we begin to introduce more Frameworks and libraries to work together in Python the understandings and the concepts you've gained in this course will be key in determining how you do with that thank you so much and I just want to take a moment to say thank you for your purchase as I know it's a big deal when choosing an online class stay tuned for any future courses I may have cuz they are coming well done and congratulations I'll see you in our next class together
Info
Channel: Code with Josh
Views: 43,692
Rating: undefined out of 5
Keywords: Object Oriented Programming, Python 2024, Free Python Course, Beginners, Encapsulation, Inheritance, Polymorphism, Abstraction, OOP Python, Learn Python OOP, Python Programming, Software Development, Computer Science, Python Basics, Python Projects, Programming Languages, Python Development, Online Education, Coding for Beginners, Python Syntax, Python Web Development, Python Data Science, Python Machine Learning, Python Algorithms, Python Tutorials, Python Examples, python, tech
Id: -YhddS9_uus
Channel Id: undefined
Length: 405min 36sec (24336 seconds)
Published: Thu Feb 22 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.