Locked lesson.
About this lesson
What are classes and what are they used for.
Exercise files
Download this lesson’s exercise file.
Overview of Classes and Object Oriented Programming.docx59.3 KB
Quick reference
Overview of Classes and Object Oriented Programming
Let's start talking about classes!
When to use
Classes are a fundamental part of object oriented programming.
Instructions
A class is like a blueprint that takes in inputs and send output based on that blueprint.
In Python, everything is an object...and you can do objecty things to them.
Objects come from classes.
Hints & tips
- Classes are like blueprints that do something
- Everything in Python is an object
- 00:04 Congratulations, you've made it to the advanced section of the course.
- 00:08 And in this section, we're going to be looking mostly at classes.
- 00:11 And classes kind of confuse a lot of people.
- 00:13 They're kind of hard to wrap your brain around the first time around.
- 00:17 But they're just so very important that you really need to learn all about them.
- 00:20 So classes are relatively new in the world of computer programming.
- 00:24 Back in the old days programming languages weren't using this object
- 00:28 oriented class structure that we have now.
- 00:30 And this new way to do it, Python has had classes and
- 00:33 object oriented programming from the very beginning.
- 00:36 It was built with this sort of baked in and so that's a very good thing.
- 00:40 And you'll see as we learn about these, why it's so very cool.
- 00:44 But it allows us to do things very easily.
- 00:46 And a class basically, think of a class as a blueprint, right?
- 00:50 So let's say you have a factory and the factory creates hats.
- 00:53 So your factory has a blueprint for what a hat is,
- 00:56 the kind of hat that it's going to make.
- 00:59 But within that blueprint, you can have all kinds of different types of hats.
- 01:02 You can have a blue hat or red hat, a green hat, you might have large hats,
- 01:07 medium hats, small hats.
- 01:08 But they're all hats, they're all basically the same thing.
- 01:11 Well, that's the way it class is, it's a blueprint that spits out things that may
- 01:16 be different but fundamentally, they're all the same.
- 01:18 It's all a hat, right?
- 01:20 You can have coffee cups, they're all coffee cups.
- 01:22 It might be a red coffee cup or a blue coffee cup, but it's a coffee cup.
- 01:26 That's what a class does, it allows you to feed in things and
- 01:30 get output based on whatever blueprint you've created.
- 01:33 So that makes them just so very useful for so many different things and
- 01:37 actually pretty easy to create.
- 01:39 And that's we're going to start doing in the next video.
- 01:41 In this video I just want to spend a minute or two talking a little bit more
- 01:45 about what object oriented programming is, what classes are.
- 01:48 So we've seen all kinds of examples of object oriented programming throughout
- 01:53 this course.
- 01:53 Everything is an object, right?
- 01:55 In the last couple of videos we were opening files,
- 01:57 we were creating a file object.
- 01:59 A variable is basically just an object.
- 02:01 And objects allow you to do objecty things.
- 02:05 So if you have a string for instance, you could do stringy things.
- 02:09 So what are stringy things?
- 02:10 Well, we looked at those at the beginning, you could change it to upper case,
- 02:13 change it to lower case.
- 02:14 You could swap the case, you could do all kinds of stringy type things to it.
- 02:18 A number is an object, you can do numbery type things to it.
- 02:22 What are numbery type things?
- 02:23 Well, you can add, subtract, do math of different sorts,
- 02:26 change them into floats or ints, or whatever, numbery things.
- 02:29 And so the class system allows us to do that.
- 02:32 A class is made up of methods, and those methods are the things.
- 02:37 So the stringy things uppercase, lowercase,
- 02:40 those are methods in the string class.
- 02:43 So if you remember, let's just do a real quick example we had a variable
- 02:47 called name and let's say john elder, all right?
- 02:50 So if we want to change this around, we can do stringy type things to it.
- 02:55 We could go print name.title and we put in our little parentheses guys.
- 03:02 So if we save this and run it, we get this John Elder.
- 03:05 We could change it to a upper.
- 03:07 Save this, boom, JOHN ELDER.
- 03:10 We could capitalize, in which case we just get upper case J.
- 03:15 Remember, we could swap case.
- 03:19 So now everything is going to be upper case because before,
- 03:21 it was all lower case.
- 03:22 So all of these dot things, those are methods of our string class.
- 03:28 So this name is the object and
- 03:30 we're doing stringy things by calling different methods of that object's class.
- 03:36 That's all class really is,
- 03:38 it allows you to do objecty type things to whatever your blueprint is.
- 03:42 In this case, the blueprint is a string.
- 03:44 We can output a string but we can, based on inputs,
- 03:47 do different things with that string.
- 03:50 Like a hat factory, we can output a hat but it might be a blue hat,
- 03:53 it might be a medium hat, it might be a small hat, it might be a red hat, and
- 03:56 that is what classes do.
- 03:57 So that's the best way I could sort of describe it,
- 03:59 getting started before we have actually done anything.
- 04:02 In the next video, we are going to jump in and start building our own class.
- 04:05 Python has a bunch of built in classes that we have already been using, but
- 04:08 you can create your own and that is where the real power of this comes into play.
- 04:12 When you can start building your own classes there is not much you
- 04:14 can't do anymore.
- 04:15 It's very exciting, very fun.
- 04:17 In the next video we're going to jump right in and start building these things.
Lesson notes are only available for subscribers.