Bird
Raised Fist0
Pythonprogramming~10 mins

Public attributes in Python - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a public attribute named name in the Person class.

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

p = Person("Alice")
print(p.name)
Drag options to blanks, or click blank then click option'
Aage
Bself
Cname
Dperson
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self' as a parameter name instead of 'name'.
Forgetting to pass the parameter when creating the object.
2fill in blank
medium

Complete the code to access the public attribute color of the Car object.

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

my_car = Car("red")
print(my_car.[1])
Drag options to blanks, or click blank then click option'
Amodel
Bspeed
Cowner
Dcolor
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access an attribute that does not exist.
Using parentheses like a method call instead of attribute access.
3fill in blank
hard

Fix the error in the code by completing the blank to correctly set the public attribute age.

Python
class Animal:
    def __init__(self, age):
        self.[1] = age

cat = Animal(3)
print(cat.age)
Drag options to blanks, or click blank then click option'
Ayears
Bage
Cold
Dage_
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different attribute name than the one accessed.
Misspelling the attribute name.
4fill in blank
hard

Fill both blanks to create a public attribute height and print its value.

Python
class Tree:
    def __init__(self, [1]):
        self.[2] = height

pine = Tree(15)
print(pine.height)
Drag options to blanks, or click blank then click option'
Aheight
Bsize
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for parameter and attribute.
Using a name not matching the print statement.
5fill in blank
hard

Fill all three blanks to create a public attribute width, set it, and print it.

Python
class Box:
    def __init__(self, [1]):
        self.[2] = width

b = Box(10)
print(b.[3])
Drag options to blanks, or click blank then click option'
Awidth
Dheight
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for parameter, attribute, or access.
Using 'height' instead of 'width'.

Practice

(1/5)
1. What is a public attribute in a Python class?
easy
A. An attribute that can be accessed and changed from outside the class
B. An attribute that is hidden and cannot be accessed outside the class
C. A method that runs automatically when an object is created
D. A special function to delete an object

Solution

  1. Step 1: Understand attribute visibility

    Public attributes are designed to be accessed and modified from outside the class.
  2. Step 2: Compare options

    An attribute that can be accessed and changed from outside the class correctly describes public attributes. Options A, B, and D describe other concepts.
  3. Final Answer:

    An attribute that can be accessed and changed from outside the class -> Option A
  4. Quick Check:

    Public attribute = accessible outside [OK]
Hint: Public means anyone can access or change it [OK]
Common Mistakes:
  • Confusing public with private attributes
  • Thinking methods are attributes
  • Mixing up constructors with attributes
2. Which of the following is the correct way to create a public attribute name inside a Python class constructor?
easy
A. self->name = value
B. self.name = value
C. name = self.value
D. def name(self):

Solution

  1. Step 1: Recall syntax for public attributes

    Inside __init__, public attributes are created by assigning to self.attribute_name.
  2. Step 2: Check each option

    self.name = value uses correct syntax: self.name = value. Others are invalid Python syntax or wrong usage.
  3. Final Answer:

    self.name = value -> Option B
  4. Quick Check:

    Use self.attribute = value [OK]
Hint: Use self.attribute = value inside __init__ [OK]
Common Mistakes:
  • Using def instead of assignment
  • Wrong arrow syntax like self->name
  • Assigning attribute to name instead of self.name
3. What will be the output of this code?
class Dog:
    def __init__(self, name):
        self.name = name

my_dog = Dog('Buddy')
print(my_dog.name)
medium
A. Dog
B. Error
C. my_dog
D. Buddy

Solution

  1. Step 1: Understand attribute assignment

    The constructor sets self.name to the value passed, which is 'Buddy'.
  2. Step 2: Print the attribute value

    Printing my_dog.name outputs 'Buddy'.
  3. Final Answer:

    Buddy -> Option D
  4. Quick Check:

    Print attribute value = Buddy [OK]
Hint: Print object.attribute to see stored value [OK]
Common Mistakes:
  • Expecting class name instead of attribute value
  • Confusing object name with attribute
  • Thinking it causes an error
4. Find the error in this code that tries to create a public attribute age:
class Person:
    def __init__(self, age):
        age = age

p = Person(30)
print(p.age)
medium
A. The attribute should be assigned to self.age, not age
B. The print statement should be print(age)
C. The constructor is missing a return statement
D. The class name should be lowercase

Solution

  1. Step 1: Check attribute assignment

    Assigning age = age only creates a local variable, not an attribute of the object.
  2. Step 2: Correct attribute assignment

    It should be self.age = age to create a public attribute accessible outside.
  3. Final Answer:

    The attribute should be assigned to self.age, not age -> Option A
  4. Quick Check:

    Use self.attribute = value to create public attribute [OK]
Hint: Always assign attributes to self.attribute inside __init__ [OK]
Common Mistakes:
  • Assigning to local variable instead of self.attribute
  • Trying to print variable not attached to object
  • Thinking constructor needs return
5. You want to create a class Car that stores the public attributes make and year. Which code correctly creates these attributes and allows access to them?
hard
A. class Car: def __init__(self, make, year): self.make = make year = self.year my_car = Car('Toyota', 2020) print(my_car.make, my_car.year)
B. class Car: def __init__(self, make, year): make = self.make year = self.year my_car = Car('Toyota', 2020) print(my_car.make, my_car.year)
C. class Car: def __init__(self, make, year): self.make = make self.year = year my_car = Car('Toyota', 2020) print(my_car.make, my_car.year)
D. class Car: def __init__(self, make, year): self.make = make self.year = year my_car = Car('Toyota', 2020) print(make, year)

Solution

  1. Step 1: Check attribute assignments

    class Car: def __init__(self, make, year): self.make = make self.year = year my_car = Car('Toyota', 2020) print(my_car.make, my_car.year) correctly assigns self.make and self.year to the passed values.
  2. Step 2: Check attribute access

    class Car: def __init__(self, make, year): self.make = make self.year = year my_car = Car('Toyota', 2020) print(my_car.make, my_car.year) prints my_car.make and my_car.year, which are valid public attributes.
  3. Final Answer:

    class Car: def __init__(self, make, year): self.make = make self.year = year my_car = Car('Toyota', 2020) print(my_car.make, my_car.year) -> Option C
  4. Quick Check:

    Assign and access via self.attribute and object.attribute [OK]
Hint: Assign attributes to self and access via object.attribute [OK]
Common Mistakes:
  • Assigning attributes backwards (make = self.make)
  • Accessing attributes without object prefix
  • Mixing local variables with attributes