0
0
Pythonprogramming~5 mins

Classes and objects in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a class in Python?
A class is a blueprint or template for creating objects. It defines properties (attributes) and behaviors (methods) that the objects created from it will have.
Click to reveal answer
beginner
What is an object in Python?
An object is an instance of a class. It is a specific example created using the class blueprint, holding actual data and able to perform actions defined by the class.
Click to reveal answer
beginner
What is the purpose of the __init__ method in a class?
The __init__ method is a special function called a constructor. It runs automatically when a new object is created and is used to set up initial values for the object's attributes.
Click to reveal answer
beginner
How do you create an object from a class named Car?
You create an object by calling the class name like a function. For example: <code>my_car = Car()</code> creates an object named my_car from the Car class.
Click to reveal answer
intermediate
What is the difference between a class attribute and an instance attribute?
A class attribute is shared by all objects of the class. An instance attribute is unique to each object and usually set inside the __init__ method.
Click to reveal answer
What keyword is used to define a class in Python?
Aobject
Bclass
Cdef
Dfunc
Which method is automatically called when creating a new object?
A__init__
B__str__
C__call__
D__new__
How do you access an object's attribute named 'color'?
Acolor.object
Bobject->color
Cobject[color]
Dobject.color
What is the output of this code? class Dog: def __init__(self, name): self.name = name my_dog = Dog('Buddy') print(my_dog.name)
ABuddy
Bname
CDog
DError
Which of these is true about instance methods?
AThey can only access class attributes.
BThey cannot modify object data.
CThey must have 'self' as the first parameter.
DThey are called without creating an object.
Explain in your own words what a class and an object are, and how they relate.
Think about how a cookie cutter (class) makes cookies (objects).
You got /3 concepts.
    Describe the role of the __init__ method and why it is important when creating objects.
    It's like setting up your new phone with your name and settings right after buying it.
    You got /3 concepts.