Hello.
Welcome back.
In this lesson we should take a look at classes and object and this is a very quick introduction to
object orientation in Python.
So objects you can think of objects as an encapsulation of variables and functions into a single entity
so objects are like verb and functions proved to get and combined into a single entity an object to
get the variables and functions from classes.
Classes are essentially templates to create our object.
So let's see how to do this.
I'll close this and create a new project over here for file new and then I'll press control as to save
and then I'll say this is number seven on the school I call this classes and to create a simple class
we can close this one here to create a simple class we can just use the class keyword class and then
the name of the class.
This is simple class and then we put some cool on here we hit enter and then we can give the class a
simple variable.
This rebel the class contains a variable called Hello.
And we can assign a value to hello hello.
The class contains a simple variable known as greeting and we can assign hello to greeting.
So the class contains this variable.
Also we can add a function to the class so we can hit enter.
Come down here an added function and we can say.
Def function 1 and then into brackets here we put self and then we hit enter and then now we can write
what we want this function to do.
We can say the function we want the function to print
this is a simple class like this.
So this is it.
This this is a very simple class.
So now how do we use this class.
We can create objects from this class.
So we can we can come down here.
Let's test it.
So to test the class we come out of the class just like we came out of the function earlier so we can
create a simple we create a variable here on a simple object and simple object.
Here we can assign our class Singapore class right like this.
So let's run this and see what we get.
So first of for this entire section is not to make an expert in Python.
Like I said we just getting used to some of the concepts and basics and then we go straight to DSP.
All of this.
There is much more to it.
This an entire book can be written on Python object orientation but this is just to get you familiar.
You don't need all of that.
I'm giving you just the foundation you need to do your DSP and you've been machine learning stuff so
you will click over here to run the module and save.
Okay and then over here simple object we have this as an object of the class so to access the class
members or to access the content of the class.
Just a simple on the school object
we could assign let's say we want X X because Mexico's simple on this call object
that variable or dot region creating this court or greeting is the verb with the right word is called
greeting.
As you can see as I said simple object dot the ideal is helping me with what the class contains.
So I could either choose the function or degree tense so I just say greeting her and then I'll hit enter.
So if I say X to print x I get hello in the same way.
We can just we can see why.
Because simple on the score object.
This time we can just see the function and a few and see the function is.
If u n c t front 1 and then we hit enter and then this time we don't need to make it print because the
function automatically print.
The reason why we had to say X us in print x is because this hasn't got a print statement but a function
automatically print this and we can create as many objects as we want actually.
So this is just simple objects.
We can say simple on the score object to call a simple class and we've just created another object and
we can see a simple score object to
front one and it's going to print the same thing.
So this is a very very simple class.
We've demonstrated here.
It doesn't do much to just print stuff but you get the idea.
So let's see on that example this one with much more content so let's say would create another class
called computer.
I'm going to come down here and see a class computer and I'll post some cool on here and I can create
the first variable of the class.
Let's call it name.
And this is going to be left empty like this and then hit enter.
We'll create another variable called kind and we can give this this variable a default value which is
laptop.
How about that.
And then we come down here we say color and we'll leave it empty as well and we'll come down here and
we give it a cost.
Let's see another variable cost space this out to improve readability a bit you can give it a cost and
let's give it a default course value of five hundred and we can add a function.
So we've got four variables in our class.
We can at least one function just to make it more fun so we can see the F this is how we create functions
and can give the function a name.
We call this description like this.
We can pass self here.
You can put up some call on here and what we want to do is we want to create a string once grade this
string and this string.
We wanted to say percentages is a percentage as percentages with percentage to F.
And basically what we're going to do is we're going to say laptop we're going to let us going to let
a user assigned a name of the computer.
So if you put Doe here it's going to say Dell s if you put color red.
So Dell is red.
And then if you put kind laptop or that desktop it's going to be Dell is a red desktop with cost basically.
So this is it.
And this a creative way of putting it all together.
Percentage is is a percentage as percentage as with percentage points to.
And this is this one is useful float.
They pointed to efforts with two decimal places.
So then we come over here just like we did earlier when we're seeing the functions we come over here
and we put a percentage here and then we put all the variables we want to assign and we've got a self-taught
name self the color self kind self or cost.
So these are then these variables here where we assign them we come here and then to be added to the
string automatically.
Very simple.
And once that is done we can return the string so we can see return to s desk SDR here description string
and then we can see control.
So let's go to the let's run and test it out with the other window right ID and just put the test code
here.
So click over here and then click on module and let's see.
We want to create a computer class so we can see a computer one because computer like this.
So we've created the object let's say we want to create a computer object it's as simple as computer
one of course computer.
So no no we've created the object the objects known as Computer.
Let's give our computer a name of say computer computer one object a school computer one dot then we
can pass name and let's say we assign something like Dell Latitude it's one of the popular computers
grew up using put Dell Latitude once that's done we've given our computer objects known as Computer
one to name.
How about we give it a color so I can come here and see a computer one dot collar like this and we can
say block like this.
Once that is done we can give it a cost and a kind word denote a kind.
So this that then we've not given the kind yet.
So by default our class says kindness laptop.
If we've not given a kind that default is going to be used if we do not give it cost the default cost
is going to be use.
But let's change the cost here just to you know add some variation.
So I say computer 1 that cost because 800 like this once they stand we can say print computer one dot
description so we access and they function within the within the class computer 1.0 description and
we don't have to pass any argument cost indicated we already passed the argument that supposed to be
printed we hit enter.
And he says Dell Latitude is a black laptop with five hundred dollars.
What happened.
OK.
So we did an assigned cost.
We have a typo here instead of cost.
It's written.
Oh yes.
That's why it's using the default cost so less assigned cost.
Say computer one does cost u s t a course.
Eight hundred 800 hit enter and no less print print into bracket computer one dot description like this
will hit enter.
OK so we have an error here.
Very very simple error.
Just bear with me.
We passed cost as a screen care.
A mistake cost everything else is a string.
So it's easy to get confused though it's easy to forget.
Cost is not a string.
So we don't need this quotation marks around cost so we can come here and see a computer one that cost
because he 200 like this will hit enter and there.
Now we can print Computer 1 dot description
like this and we'll hit until like this and it says Dell Latitude as a black laptop with eight hundred
dollars.
So this is a very simple very straightforward.
This is how to make use of classes.
Yeah.
We just have one object.
We can make 10 object and we do need to create all of them from scratch.
We can have another.
We can create a new computer computer 2 equals MacBook.
Computer to color.
Course wide computer to prize of course 2000.
And we have it do the same thing MacBook as a white laptop worth two thousand dollars.
Yes.
So this is a very simple example for how classes are used and how they behave.
This order is for this lesson.
If you have any questions at all send me a message or leave it in the questions area.
Have a great day.
By.