Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Exercising Your New Knowledge

00:00 As with most things, but especially with classes, you need to exercise your new knowledge to make it stick and to include it—to be able to include it—as part of your tool kit when you go forward.

00:12 Do exercises, write small scripts that make use of classes, and notice whenever you’re using classes as well. The Python standard library relies heavily on object-oriented programming.

00:24 So here are some exercises for you to try. Modify the Doggo class to include another attribute—.coat_color, for example—and add that to the .__init__() constructor.

00:36 Maybe also add that to the .__str__() function.

00:40 Create a new class, a Car class, with a few instances that would be appropriate for cars. Add a .mileage attribute to the Car class, and then add a .drive() instance method to the class, which will add a number of miles to the mileage. So if you instantiate a car—I don’t know, a Ford—and then you say, and you pass in, say, 10 to that method, if you look at the ford.mileage, you’ll see that 10 has been added to the mileage, which might have started out at 0, or maybe the starting mileage is something that you can pass in to the constructor.

01:17 Already here you can see that you can really get creative with classes, and that’s the best thing to do to really get a grip on it. So good luck, and definitely do these exercises.

01:28 It’s one thing just to look through and say, yep, I understand that. Yep, I understand that. Yep, I understand that. But when you actually come to a blank script and have to actually write this stuff and get it working, that’s when you realize, oh, I didn’t understand that part. So definitely give that a shot.

An example solution would be really useful here!

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Nov. 2, 2023

@CB Agreed! Please find an entire collection of exercises accompanying this video course, along with their solutions and detailed discussions, in a separate course: Python Basics Exercises: Object-Oriented Programming. If you have specific questions, then don’t hesitate to ask them in the comments or hop on the Real Python Slack.

Avatar image for Natasha Kaplan

Natasha Kaplan on Dec. 22, 2023

Hi! Can you please post the solutions to these in the discussions? It is not helpful to be digging through the extra videos while trying to practice these!

Avatar image for Martin Breuss

Martin Breuss RP Team on Dec. 22, 2023

@Natasha Kaplan you can get all the solution code by navigating to the exercises course that Bartosz linked above.

You don’t need to watch the course, you can download the code by scrolling to Downloadable Resources in the course overview page, then click on Sample Code (.zip).

It’s a lot of files and a lot of lines of code, so downloading the code from there will probably be easier than copying it from a comment here.

Avatar image for Anonymous

Anonymous on Feb. 18, 2024

The last couple of note you shared, were spot on, once you follow a lecture, and you go on to the next section, and keep following what the instructor is doing, without practicing anything and coming up with your own solution to your own projects, etc., by the end once you are face to face with a blank script, reality hits you in the face, because it’s almost like you forgot everything that you just learned, and it seems that the concepts you learned are slowly fading away, because you didn’t put extra work reiterating through the concepts, and practicing it enough.

Sometimes we lie to ourselves that we actually understood it, because you were copy and pasting what the instructor was saying, but truly, to understand something, you have to be able to explain concepts in your own words, and be able to apply what you learn to your own projects, when to use classes, vs when not to use it, following a tutorial is not the same as working on your own projects, because there is more involved than writing code, as every beginner will soon find out, take away points are, practice enough, and most of all, create projects that you are passionate about, along the way solving problem using the divide and conquer method, most importantly do no wait until you went through enough tutorials to start your own projects.

A little advice to someone reading this is to download the free tool, and sketch your ideas through diagrams, before you start on any projects, that way you reduce your workload by some %, setting aside the thinking and focusing on coding, just wanted to share my point of view on this topic that some beginners are ignoring.

Avatar image for Martin Breuss

Martin Breuss RP Team on Feb. 19, 2024

Thanks for sharing your thoughts @Braele, totally agree :)

Avatar image for Dat Dinh

Dat Dinh on June 4, 2024

My solution (please comment if the solution is not complete, thank you):

class Doggo:
    species = "Canis familiaris"
    def __init__(self, name, age, coat_color): = name   
        self.age = age    
        self.coat_color = coat_color
class Car:
    brand = "X"
    def __init__(self, name, year, color, mileage): = name   
        self.year = year    
        self.color = color 
        self.mileage = mileage 

    def __add__(self, other):
        self.mileage = self.mileage + other.mileage

    def drive(self, other):
        return f"New mileage: {self.mileage}"

    def __str__(self):
        return f"Car: {}, {self.year}, {self.color}, {self.mileage}"

Become a Member to join the conversation.