Your first class
Python · Unit 12: Classes and objects
- Full plan
- Python
- 10 exercises
A dictionary let you group the data about something. A class goes further: it creates a new data type of your own, like int or str.
You write it with class and a capitalized name. pass means "nothing for now". To create an object of that type, you call the class with parentheses.
class Pet:
pass
toby = Pet()
print(type(toby) == Pet)
print(type(5) == int)Prints
True True
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Complete the code
Complete it to create the
Petclass.2. Multiple choice
In
luna = Pet(), what is each thing?3. Predict the output
What does this code print?
4. Predict the output
What does this code print?
5. Complete the code
Complete it to print Sofia's name.
6. Predict the output
What does this code print?
7. Find the bug
This program should print Leo's name and grade. Which line has the error?
8. Put the lines in order
Put the lines in order to create a book and print its title.
9. Predict the output
What does this code print?
10. Predict the output
Objects can go in lists too. What does it print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.