Introduction to Classes

Learn what classes are, why they're important, and how to use them effectively.

StartPython Illustration
Chevron Left Icon
Introduction to Classes
Lesson 1 of 2
Chevron Right Icon
  1. 1

    Python is an object-oriented programming language, which means it manipulates programming constructs called objects. You can think of an object as a single data structure that contains data as we...

  2. 2

    A basic class consists only of the [...] keyword, the name of the class, and the class from which the new class inherits in parentheses. (We'll get to inheritance soon.) For now, our classes ...

  3. 3

    We'd like our classes to do more than... well, nothing, so we'll have to replace our [...] with something else. You may have noticed in our example back in the first exercise that we started o...

  4. 4

    Excellent! Let's make one more tweak to our class definition, then go ahead and instantiate (create) our first object. So far, [...] only takes one parameter: [...] . This is a Python conve...

  5. 5

    Perfect! Now we're ready to start creating objects. We can access attributes of our objects using dot notation. Here's how it works: [...] 1. First we create a class named [...] with an at...

  6. 6

    Now that you're starting to understand how classes and objects work, it's worth delving a bit more into [...] and [...] . They can be confusing! As mentioned, you can think of [...] as the me...

  7. 7

    Another important aspect of Python classes is scope. The scope of a variable is the context in which it's visible to the program. It may surprise you to learn that not all variables are accessib...

  8. 8

    When a class has its own functions, those functions are called methods. You've already seen one such method: [...] . But you can also define your own methods!

  9. 9

    A class can have any number of member variables. These are variables that are available to all members of a class. [...] 1. In the example above, we create two instances of an [...] . 2. T...

  10. 10

    Classes like [...] and [...] make it easy to understand the concepts of classes and instances, but you probably won't see many zebras or lemons in real-world programs. However, classes and obj...

  11. 11

    Inheritance is a tricky concept, so let's go through it step by step. Inheritance is the process by which one class takes on the attributes and methods of another, and it's used to express an *i...

  12. 12

    In Python, inheritance works like this: [...] where [...] is the new class you're making and [...] is the class from which that new class inherits.

  13. 13

    Sometimes you'll want one class that inherits from another to not only take on the methods and attributes of its parent, but to override one or more of them. [...] Rather than have a separate...

  14. 14

    On the flip side, sometimes you'll be working with a derived class (or subclass) and realize that you've overwritten a method or attribute defined in that class' base class (also called a *parent...

  15. 15

    First things first: let's create a class to work with.

  16. 16

    Great! Now let's add a member variable and a method to our class.

  17. 17

    Let's go ahead and create an instance of our [...] class.

  18. 18

    Finally, let's create an [...] class that inherits from our [...] class. (An equilateral triangle is a triangle whose angles are all 60˚, which also means that its three sides are equal in leng...

  1. 1

    Classes can be very useful for storing complicated objects with their own methods and variables. Defining a class is much like defining a function, but we use the [...] keyword instead. We also u...

  2. 2

    We can use classes to create new objects, which we say are instances of those classes. Creating a new instance of a class is as easy as saying: [...]

  3. 3

    Classes can have member variables that store information about each class object. We call them member variables since they are information that belongs to the class object. Creating member varia...

  4. 4

    Each class object we create has its own set of member variables. Since we've created an object [...] that is an instance of the [...] class, [...] should already have a member variable named ...

  5. 5

    There is a special function named [...] that gets called whenever we create a new instance of a class. It exists by default, even though we don't see it. However, we can define our own [...] fu...

  6. 6

    Calling class member variables works the same whether those values are created within the class (like our car's [...] ) or values are passed into the new object at initialization. We use dot notat...

  7. 7

    Besides member variables, classes can also have their own methods. For example: [...] The [...] class method is identical to defining any other function, except that it is written inside of t...

  8. 8

    We can modify variables that belong to a class the same way that we initialize those member variables. This can be useful when we want to change the value a variable takes on based on something tha...

  9. 9

    One of the benefits of classes is that we can create more complicated classes that inherit variables or methods from their parent classes. This saves us time and helps us build more complicated...

  10. 10

    Since our ElectricCar is a more specialized type of Car, we can give the ElectricCar its own [...] method that has different functionality than the original Car class's.

  11. 11

    Chances are, you won't be designing Car classes in the real world anytime soon. Usually, classes are most useful for holding and accessing abstract collections of data. One useful class method to ...

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo

Introduction to Classes

StartPython Illustration