Hi, everybody, and welcome, I'm very happy to provide you with this lecture, and today we will see
an additional example in life cording off object oriented programming in Python.
We're going to create a class, the init method, some methods that are off the object and also to create
the instance of an object.
And all of this is going to be in this lecture life.
All right.
So once you were in your pichon, just create a new file and let's begin.
So today we're going to discuss Rentoul program of attendance.
And this is what you were going to do.
All right.
In this lecture, I really recommend you just to listen.
And I'm also going to provide you the Python file that you can later on import into your pie chart or
even copy paste it into your own pie chart.
And that way you could just do experiments on it.
But this is very important for you to listen and to understand all the terms and all the logics that
I'm going to explain basically is going to be live coding example, same as we did in the previous lecture,
but with a different example.
All right.
So let's begin.
So as we were discussing a rental program, let's create a new class and you can call it building.
All right.
I just want you to notice something.
If I put a small letter B here.
All right.
So you can see here that if I'm hovering, you can see class names should use Kemakeza Convention.
So even pie chart tells us, please, dear developer, keep the conventions right.
OK, so we need to put capital letter.
We discussed it in the first lecture or the second of OLP in discourse.
So after we created the class, let's start off and build our object.
All right.
So first one would be inside the class would be the Init method.
OK, you need to put underscores here and then you have here are the variables that you're going to
pass through the object.
By the way, if you put one underscore here, it's no good to put double R discourse in each side and
then it changes its color.
And afterwards you have to add the self key, key word right here.
So the variables that I'm going to use today is use a season in year apartment.
No, an apartment size.
All right.
These are the three variables that I'm going to use.
And inside the Æneas method, we're going to use the properties of the object using the South barometer.
So we put myself at the moment, I'm just going to copy paste it.
All right.
By the way, it's.
Like this and.
Like this, sometimes I'm typing very fast and don't know this, all right, so I'm just going to copy
paste it with my keyboard.
All right.
Just to save time, because I don't want to waste your time on not important things.
I want you to focus on the important things, which is the logic here as well.
S self that then the second variable.
OK, and then the third one self.
That apartment size equals apartment size.
So our init method is ready.
All right.
And I mentioned that this would be a lecture and an object regarding a rental.
All right.
So we're going to create the methods of an object right here.
Two of them.
First one is rental calculation.
All right.
And by the way, once I type it out calculation, I put the first bracket boom, the self parameter
is automatically completed.
I'm going to leave it empty.
For what?
For now.
And the second method would be monthly.
Maintenance
bay, all right.
I didn't put them here, so there was no outdoor comp..
Yeah, like this and then self over here.
All right.
So now we have two methods here.
These are methods of an object and we have air here because we didn't put the bass.
Yeah.
Now, just put in putting the bass here for now.
We're going to fill it with some manipulations on these variables in a bit.
All right.
And the additional thing that I want to do right now is just basically before I'm going to do a lot
of mess here and do some calculations.
And you say these methods are just going to create two instances of an object, and I will call them
lease contract one.
And then we're going to place the name of the class, as always.
And inside the brackets, I'm going to put a values for these three variables, which will become these
three parameters of an instance.
All right.
So first one is season of the year.
I'm going to put some are here, OK?
Summer is a great season.
And second one would be apartment number four, for example, and apartment size.
One hundred and thirty five meters.
And let's create an additional one.
Going to copy it and place it here.
At least contract you.
Then we have spring.
OK, and then apartment number six and apartment size would be one hundred meters.
OK, so we had these three instances of an object and right now basically these three variables are
accepted in these three up and here inside the unit method.
And then these three are our best into these three parameters.
OK, and then we have the self key world inside each of these methods.
So basically we can use any of these parameters, any of these properties inside these methods.
OK, we can do manipulations on them.
We can print them whatever we want.
All right.
So this is this self worth is a connector that basically says all the all the parameters that you have
in here, you can use them inside this method.
This is what the self key word means.
All right.
So, first of all, let's discuss we're going to focus in the next few minutes only in this method.
And I'm going to put some calculations here depending on the variables.
All right.
So we're going to do the following thing and we're going to create two variables.
First one is base price per month equals two thousand.
And we're going to do season price buffer, which the default value would be zero for now.
OK, and then I'm going to put three ifs actually for ifs if self OK, self season of the season in
the year.
OK, so once I put myself that season in the year, whatever is best from here and accept it here and
then went up in here, ok, we can use it inside this method.
OK, and once we have created two instances.
OK, so for each of them the values are going to be different.
For the first one the summer would be accepted.
OK, and for the second one would be spring.
OK, so if season in, in the year would be summer, I want the season parameter
season price by far.
It's not a barometer.
This is simple.
Variable would be one point five.
OK, first of all, I'm referring to this variable and the reason that I don't have self here, OK,
this is the reason I'm not doing this thing.
OK, instance through season, define outside.
In it you see what's happening here.
Once I however this instance attribute season price for define outside in it.
OK, it says to us that we don't have the season price buffer here and that's because we haven't have
him on the inside the method.
So it's not connected.
We don't need to put self here, it's not connected to the self and the reason is because we don't pass
it through here.
It's an internal variable.
OK.
And basically what I want to do is I want to according to the season I'm sorry, according to the season,
I want to change the price, the base price per month value.
Meaning in the summer I'm going to multiply two thousand by one point five because the summer is a very
popular season for.
Car rental, and at the winter, it's less popular, so I'm going to multiply, for example, by oh,
by one point one.
So the price, the total price would be less.
OK, you know what I'm saying?
This is what I'm heading to season winter, OK?
At the end, we're going to add a calculation season, but four is one point one.
And then, by the way, it's not supposed to be if it's ayliffe.
Right.
The last one would be else.
And we have an additional Ayliffe sales season and then we have spring.
Yeah.
And spring is a bit popular.
More popular than the winter, but less than the summer, for example, one point four and else season
price four would be one point three.
OK, so we have in total four seasons in the year.
So I've set up three of them here and the fourth one.
OK, so we have summer, winter, spring and autumn.
OK, so the autumn obviously going to go here.
So if none of these were found and in total in human life, we have only four seasons.
So if none of these were accepted, so the difficult one will be autumn or even more.
Even a better idea would be to put autumn here up.
Just going to remove the redundant space.
Yeah.
And put Autumn here.
Yeah.
And then what we're going to do is put like this, yeah, and with indention and then else meaning if
we got something else, we're going to do return or maybe season.
But for none.
OK, so in cases other of these four items, it was received through the instance, we're going to insert
Nunn into the variable.
OK, this is the more correct way to do it.
OK.
And basically what I'm going to do is I'm going to do an additional thing.
Also, I want four apartments that are very large to to enlarge this buffer even more.
OK, meaning if it's summer and if apartment size is above one hundred thirty meters.
So I want the Susan buffer to be plus zero one up.
OK, so let's have a quick example, Summers received went up and here goes to the summersault, the
buffer is one point five going down and then coming to here.
If we find out that the apartment size is above one hundred thirty meters, I'm saying to the program,
you know what, it's one point five, but make it one point six.
OK, because the apartment is large.
So Grothe, the apartment, the apartment before the lease bought for four zero point one some more.
OK, and then I want to do the last calculation here, which will be I'm going to create a new variable.
By the way, this thing is should be less.
You see that I set up the F here as if it was part of the Ellis'.
OK, so you see the indention here and here is the same.
So basically this thing would be executed only if the program would go inside the house.
This is not what I want.
I want this part to be indented, same as the if else, OK?
I don't want it to be executed only if the program goes through.
Also, I don't want it.
So I'm going to do shift Darb.
OK, and now we are OK.
So for example, we're going into the summer, OK, and then we are ignoring all of this going down
and coming through here.
OK, so this if these else ifs are not connected at all.
OK, so now we have our buffer prepared and I want to do a calculation, so I'm going to create a new
variable.
Total rent price.
Yeah, I'm going to take the best price and multiply by season price buffer like this.
OK, so I have here the total price and I want you to I want to teach you something new.
OK, so usually what we're doing is the total the total price is and we're doing plus SDR and total
price like this.
But I want you to learn in more advanced way to print out values and in order you would not need this.
The casting.
This is like beginners way.
I want you to scale up your prints.
So let's do the following thing.
This thing is, is subtopic inside objects.
I'm going to teach you string formatting.
OK, so for a few seconds, we're putting aside all of this object and methods and we are going to teach
this small topic string formatting because we can do the sprint, this print in a more advanced way.
And this is the following way.
I'm going to use print that up.
So let's try to print it in a more advanced way, as I just said, the total price is and the thing
I'm going to do now is to put percentage.
OK, this is a saved keyword once they appear together and only ones that appear together in a print
percentage.
Yes.
OK, the total price is and then you can see here after the closing quotation mark, I'm going to do
percentage and total rent price.
OK, so what you see here is basically string formatting.
OK, this is how I called and this is a more convenient way to print values.
You can see that I put here percentages and after closing quotation marks, I put higher percentage
and I'm putting the value that I want to be inserted in here.
All right.
It's not very difficult to understand.
And also, I can do the following thing.
I can pull if I want to put more than one value, I'm going to put in brackets here and a comma, OK?
And then I can put an additional variable here and additional percentages here.
OK, and now what's going to happen?
The first percentage.
Yes.
Will receive this value.
The first one and the second percentage will receive this value.
OK, so from now on we'll dry.
We're starting to use string formatting insert instead of this method.
OK, so I'm just going to delete it.
OK, she's going to delete this unwanted variable, and I think we are OK.
All right.
So also I want to bring the buffer, OK, so I'm going to do print the buffer years and then four percent
the.
Yes, and I'm percentage.
Buffer, what's the name of the variable it is session price buffer.
Yeah.
All right.
So there you have it.
All right.
There you have it.
We have the string formatting here and we're printing these two values, OK?
And just need to put spaces here.
Yeah, like that, and there you have it, so we have the printout of the buffer.
OK, it starts off zero is the default value, then it gets a value according to the season.
And then if the apartment is more than one 30 meters, then he gets an additional zero point one.
Just a quick reminder, this statement equals the following statement equals if I would write this.
OK, so these two lines, thirty and thirty one, these are the same way I'm doing here is I'm taking
the existing value and I'm saying add additional zero point one to the existing value.
And right here, also inside this variable, I'm saying take the existing and add zero point one.
We already covered it.
But just a quick reminder to what it is.
OK.
OK, so I think we are done here.
What this method will do is it will calculate the total return price.
OK, and then at the end, what I want is to return total rental price outside of the method.
OK, so once you apply some method and at the end you're putting a return.
OK, so this value needs to go somewhere.
And let's try first to execute this method using the instance of an object that we have created and
then we'll see what we're going to do about this value and how can we extract it.
OK, so this is the creation, OK, creation of an instance of an object one.
OK, this is the second creation of the instance of an object.
Let's focus on one of them and then we'll do the other.
It's the same thing.
So we have created the instance and now we're going to use this instance.
OK, so what happens now is this instance would save would contain all of these properties inside it.
OK, so once I want to execute a certain method, for example, rent calculation, I'm going to take
the name of the instance and put that and then ran calculation.
And what will happen here is this method would be executed by this instance.
OK, but this instance contains these three properties.
So all of these properties would be applied here.
OK, these three properties are saved only for that instance in these three properties.
And then each time a method, an object method is executed using a certain using a certain instance.
OK, this instance.
So automatically all of these three variables are applied inside the methods.
If they are called in our case, we're calling out here for a season in the year.
We're calling out for apartment size.
OK, so these two are executed because they are call-out.
And once we try to execute this method, we are using this instance to execute the method and this instance
saved and stores all these values inside it.
So they are applied inside here.
All right.
So let's try to run and see what happens.
I press the wrong button.
Let's run it.
OK, so there you have it.
OK, so first of all, let's go and do some flow check.
All right.
So first of all, best summer four and one thirty five.
And let's see if it's OK.
OK, so we're going through here, OK.
And we getting to this method right here and we're getting to summer.
OK, so if we got to summer, our season in a year is one point five.
OK, then we're going down and our apartment size is one thirty five.
OK, so once we get here, this is a true statement, meaning we're taking the existing buffer which
is one point five and we're adding zero point one to it.
And then we're going down here and we coming to this print, OK, in in this print we can see one point
six.
So this is correct.
This is one point one plus zero point one.
OK, and then we're coming to here and we having this calculation right here and we can see total ramp
price.
OK, so the default rent is two thousand.
This is the default one.
This is an internal variable.
But then we are multiplying it by one point six.
OK.
And then we have three thousand two hundred dollars, OK, up until this point, everything is OK,
so this method is covered.
OK, we know how to use it and right now, let's try to execute the same method.
But for this instance.
OK, so this contract to that rent calculation like this and then in here between I'm going to do print
slash in.
You already know what it is.
It just breaks up with a redundant line here between the printouts.
OK, let's try it.
All right.
So here we have spring spring coming to this method.
Here is one point for OK, and the apartment size is one hundred.
So once we are checking this thing, it's a false statement.
OK, so we're not adding zero point one.
And then once we're printing the buffer, it should be the same as spring, which is one point for everything
school.
And right here below this is the total prize.
Once 2000 was multiplied by zero point four.
You can check it up with a calculator.
This is a correct answer.
All right.
So we have covered this method.
The only thing is left for us is to understand once we are returning a value.
OK, I want to take this value basically, OK, total rent price and put it in here and said this method,
OK, I want to use one the method value inside an additional method.
OK, so in here I'm going to put total rent or maybe rent price.
OK, I'm just giving any name I want.
He does not have to be the same as this name.
OK, as long as one variable would be passed into this method, it's all right.
It doesn't have to be the same name that is inserted and the one that is received.
OK, so once we want to parse this variable inside the method, they do not have to have the same name.
It's all about the numbers.
Like we're pressing one variable and one variable is received, then we are OK.
The question is how do we proceed from here to here?
OK, so this is like this is not the execution.
OK, this is like what we're using.
This is our tools.
The execution is done from this part.
Right.
So first of all, we know how to use methods.
We're just writing the name of the instance, then DOT and then the name of the method.
So let's do it again for the second method, Lee's account, then the name of it is monthly maintenance.
All right.
And then we need to put some variable in here so that both are rent rent price.
All right.
So we have set up rent pricing here, but what is rent price, OK?
It is like stuck inside this method.
It's returned, but how can we pass it from this method to an additional method?
OK, so listen closely.
We have executed the rent calculation method from this line.
OK, so we have executed this thing and then at the end we are returning this value.
So in order to receive this value out of the method, we must put here rent.
Price equals OK, and then after this method would be executed, the return from this, what I marked
here, will go straight into rent price.
OK, so total total rent price is returned.
OK, it's returned from here.
Sorry, it returned from here from this execution.
And because its return is going to go straight into the variable that I've put here and then I don't
have any problem because the total rent price is stored in here.
And then from here I'm going to put it inside monthly maintenance bay execution.
I'm just going to put it here, OK, this is this this means I'm passing a variable into the method,
right?
I'm placing it variable into the method this way.
OK, so then the rent price would be received inside a monthly maintenance break.
OK, now we have the rent price passed into it and now we can do some calculations.
Let's create a new variable.
Maintenance equals zero.
And here, if our rent price is bigger, then let's say three thousand, then maintenance equals one
hundred.
Oh like this and üç.
Maintenance equals maintenance equals 50, OK?
Basically, what that means is that if we're paying a lot for the rent, meaning it's a popular time
of the year or our apartment is very big, so the maintenance will be big as well.
And if we are renting a smaller apartment and it's not a popular time of the year, so we're going to
pay less for maintenance.
OK.
And of course, how can we verify that everything was fine here?
Let's use the rent.
The maintenance is then presented.
Yes.
As we know already.
And then percentage maintenance.
Yea, buddy.
OK, we are good.
All right, so let's try to execute.
OK, we're going to have the maintenance from here and let's do the same thing from here.
Same thing.
OK, so we're going to put rent price.
I'm going to put here number one and then put rent price number one here.
And the same thing goes here, rent price, too.
And then we're going to do lease contract to that.
Monthly maintenance and rent price, too.
OK, I think this is more understandable.
This is this is method of execution.
Methods, execution.
And here it is, methods, execution.
This is creation of an instance, this is method of execution and let's print it.
And we are expecting to see the maintenance printed out as well.
All right, so as you can see here, we have first rent prices, three thousand two hundred, then we're
just going to discuss the monthly maintenance.
All right.
So we're going inside here and we're checking is the rent price above three thousand?
In first case, it is.
It is three thousand two hundred.
So the maintenance is one hundred then printed.
So it's the correct answer.
In the second condition we're seeing here, the rent price, 2900.
So once we're checking it, it's false.
So you go straight to the Yael's.
So 50 is the correct way.
So there you have it.
We have created an additional instance of an object classers, self barometer's methods of an object
and we use the whole shebang here.
And basically we have covered the object oriented programming the second time now.
So I guess you should be good.
Now I'm also going to remind you that the attachment file decoding file would be attached to this lecture
and you can use it and you can copy paste it into your project and then do some manipulations here,
because like changing the letters or changing the variables always helps for me to understand deeper
what's going on here.
OK, you can just remove some returns and see if it fails for you or not.
I really recommend to do it because experiments really helps in learning.
So this is this is the final the final thing that we're going to learn regarding object oriented programming.
We have created two objects in this lecture and in the previous lecture as well.
I think we covered it pretty nicely and we discussed the whole details of ILP programming in Python
as well.
Don't forget the string formatting that we have studied.
So from now on, we're going to print things with the percentage assigned and the percentage of the
name of the variable outside of the quotation marks.
And this is the more professional way to do it.
In the next lecture, we're going to head on to the practice part and the walk work home assignments
regarding in Python.
Don't worry about it.
It shouldn't be too much difficult if you listen carefully to all of my explanations regarding object
oriented programming.
And I will see you in the next video.
Bye bye.