How to Create Classes and Objects in Python! Beginner Lesson

LeMaster Tech
182 followers
6 Video Views·Apr 23, 2025
This is the best place to learn and practice python skills for beginners looking to get started with Python! Learn how to create classes and share functionality across several object instances of those classes!!
This is episode 8 in an introduction to python series coming from LeMaster Tech.
Be sure to give us a like, drop a comment, or subscribe to the channel for more content like this!
The previous video:
https://www.youtube.com/watch?v=nyrbv7AuWIk&t=273s
python download:
https://www.python.org/downloads/
pycharm download for windows:
https://www.jetbrains.com/pycharm/dow...
hello and welcome back to the channel
today's video will be part 8 in our
introduction to python tutorial series
and today's video will focus on classes
and object instances of those classes
so python has a functionality to group
together objects with shared features
into something called a class and the
way you create a class in python is you
just type the word class
and then the most common syntax for
python classes is you would use a
capital first letter so a class i like
to use
for example is sandwich because you
obviously can have a lot of different
sandwiches but then you think about the
things that share them and it's that
they all share and it's like well they
all have bread and they all have some
filling and then in general
they're really only two types of
sandwiches there's hot sandwiches and
cold sandwiches and so
you can create functionality inside of
this
that lets you use
certain functions for all the different
objects just by passing in a few shared
parameters
so
this is a little bit of python specific
syntax but after you create a class
you use this def underscore underscore
init underscore underscore function
and then you start defining parameters
that get shared by all of them and the
first one is always going to be self
just referring to
this class as the first parameter saying
when you when you use self you're
referring to variables inside the class
so self and then we'll say every
sandwich is going to have a name
we'll define a bread
we'll
say what the filling is and then we'll
use a boolean variable to say whether or
not it's hot
and then the next thing about classes
you need to do is you need to take those
variables that you just passed in so
name and you need to assign them to
variables inside the class so self.name
is equal to name
self.bread is equal to the bread
parameter we're going to pass in
self dot filling will be equal to
filling
and self dot
hot we will call hot and actually like
this self dot name you're actually just
creating parameters to be used within
the class
they don't need to be the exact same
parameter names as the names you passed
in so like instead of this being hot
i'll call it hot or not
that way you can see like it this is
just so that we can use it inside of our
our class functionality
so let's go ahead and do an example of
how you would actually
use um these parameters to do something
inside of a class
so we'll create a function called is hot
and it's just the only parameter it
actually needs is self because it exists
inside of this class
and all we're going to say is if self
dot hot or not right so we're checking
if it's hot or if it's not and we'll say
that
um because we kind of get to define this
we'll say that if that uh boolean is
true then it is a hot sandwich so we'll
say if
hot or not then what we're going to do
is we're going to print
self dot
name
which will come through as a string
and then we'll add
is a
hot sandwich and uh if this seems silly
uh just wait until we actually get to
the demonstration of what this is doing
and then you'll kind of see the value in
this so else
print
and we'll say print self.name is
not a hot sandwich
and so we'll add some more functionality
this in a minute but let me go ahead and
make a
a couple instances of this class so this
is how you actually define objects so
think of two sandwiches real quick you
have a peanut butter and jelly we'll
call it pb and j
and to create an object that's an
instance of a class you say equals to
and then the class name and then you
actually give it specific parameters for
all of the inputs so for pb and j we're
going to say
pbj for name and then we'll say
white for bread we'll
peanut
butter and
jelly
for filling
and then we'll say false that's not a
hot sandwich and just so we have a
couple instances of this let's also do a
blt
and that's gonna also be a sandwich but
then this one is gonna have a name blt
it's we're gonna put it on wheat bread
the filling will be
bacon
lettuce and
tomato
there's our
bl and tea
and then this let's say it is a hot
sandwich because you fried the bacon
so
what the way you actually use this is
hot function is you would type the
instance of the class and you would say
pbj dot
is hot and just by being an instance of
this class we have access to all the
functions inside of that class
so this is going to tell us if we run it
pbj is not a hot sandwich okay that's
pretty cool but then let's say blt dot
is
hot as well
and in running this blt is a hot
sandwich so you can see just by having
two different classes with different
parameter definitions we run the same
code twice and get different outputs and
so that's pretty cool
but we can do some cooler things with it
let's do another that's def and we're
actually going to say we want to build
the sandwich
and again it's just inside the function
and we already have the bread and
filling so all we need for parameters is
just self
and let's print
to build
a
and let's use the name here
so let's say to build a plus
self dot
sandwich
and you can see inside these functions
you actually oh it's self.name
you actually use self dot to use the
variable inside of the function not just
the
name itself.name or self.bread or
filling or hot or not
you always use self dot before you use
something inside of a class
so to build a plus self.name
plus
sandwich
and let's say let's copy a few more
print statements
so to build a blank and then this we
want to say start with
plus
self dot
bread
and then
bread
and then we'll say
then put
self dot
filling
in it and then we will just say
enjoy your sandwich
and that's a
wholesome
sandwich building program
but the cool thing is with what we just
did we should be able to run
pbj dot
build
we gotta spell it right
pbj dot
build sandwich it's right there and blt
dot
build sandwich
and when we run this now
we get pbj is not a hot sandwich blt is
a hot sandwich to build a pbj sandwich
start with white bread then put peanut
butter and jelly in it enjoy your
sandwich to build a blt start with wheat
put bacon lettuce and tomato in it enjoy
your sandwich so i mean this is a maybe
a silly example with sandwiches but it
should show right away the kind of
powerful cool functionality that you can
do with classes and class objects inside
of python you write code once
that gets shared across a number of
different objects and you get to use it
however you want so this is just a food
based example but classes can be used
for
multiple
enemies or multiple players in game
formats you can use classes for things
like buttons on apps and and
look up tables and things that
operate the same way over and over and
over again and you have
many of the different instances of them
but they all kind of have shared
features
so this has just been a brief
introduction to classes and objects but
hopefully you found it helpful if you
have any questions about anything that i
covered today or you feel i missed
something important go ahead and drop a
comment and let me know about it i'll
try to get back to you as soon as i can
and uh if you found any of this content
or anything else on the channel useful
feel free to drop a like and subscribe i
really appreciate it
and
as always thanks for checking out the
channel and good luck with your code
thanks bye
