0
0
Pythonprogramming~10 mins

Creating objects in Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an object of class Car named my_car.

Python
class Car:
    def __init__(self, brand):
        self.brand = brand

my_car = [1]
Drag options to blanks, or click blank then click option'
ACar('Toyota')
BCar = 'Toyota'
CCar.brand('Toyota')
DCar('Toyota')()
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call a method on the class instead of creating an object.
Assigning a string directly to the class name.
Adding extra parentheses after object creation.
2fill in blank
medium

Complete the code to access the color attribute of the car object.

Python
class Car:
    def __init__(self, color):
        self.color = color

car = Car('red')
print(car.[1])
Drag options to blanks, or click blank then click option'
Abrand
Bcolor
CColor
Dpaint
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different attribute name than defined.
Capitalizing the attribute name incorrectly.
Trying to access an attribute that does not exist.
3fill in blank
hard

Fix the error in the code to correctly create an object of class Person.

Python
class Person:
    def __init__(self, name):
        self.name = name

person = Person[1]
Drag options to blanks, or click blank then click option'
A['Alice']
B('Alice',)
C('Alice')
D('Alice')()
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets instead of parentheses.
Adding extra parentheses after the argument list.
Passing a tuple with a comma when not needed.
4fill in blank
hard

Fill both blanks to create a dictionary with keys as names and values as ages from the people list.

Python
people = [('Alice', 30), ('Bob', 25), ('Cara', 27)]
ages = {person[1]: person[2] for person in people}
Drag options to blanks, or click blank then click option'
A[0]
B[1]
C:
D=>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=>' instead of ':' in dictionary comprehension.
Mixing up indices for name and age.
Using parentheses instead of square brackets for tuple access.
5fill in blank
hard

Fill all three blanks to create a list of Person objects from the names list.

Python
class Person:
    def __init__(self, name):
        self.name = name

names = ['Anna', 'Ben', 'Cara']
people = [[1]([2]) for [3] in names]
Drag options to blanks, or click blank then click option'
APerson
Bname
Cn
DPerson()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling Person() without arguments.
Using a different variable name inside the call than in the loop.
Adding parentheses after the class name in the options.