0
0
Pythonprogramming~15 mins

Accessing and modifying attributes in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Accessing and Modifying Attributes
📖 Scenario: You are creating a simple program to manage a book's information. You will work with a Book class that has attributes for the book's title and author.
🎯 Goal: Build a program that creates a Book object, accesses its attributes, modifies one attribute, and then prints the updated information.
📋 What You'll Learn
Create a Book class with title and author attributes
Create an instance of Book with specific title and author
Access and modify the title attribute of the Book instance
Print the updated title and author of the book
💡 Why This Matters
🌍 Real World
Managing and updating information about objects like books, products, or users is common in software applications.
💼 Career
Understanding how to access and modify object attributes is fundamental for programming jobs involving object-oriented programming.
Progress0 / 4 steps
1
Create the Book class and an instance
Create a class called Book with an __init__ method that takes title and author as parameters and assigns them to attributes. Then create an instance called my_book with the title 'The Great Gatsby' and author 'F. Scott Fitzgerald'.
Python
Need a hint?

Define the Book class with an __init__ method that sets self.title and self.author. Then create my_book using the class.

2
Access the title attribute
Create a variable called current_title and set it to the title attribute of the my_book object.
Python
Need a hint?

Use dot notation to get the title from my_book and assign it to current_title.

3
Modify the title attribute
Change the title attribute of my_book to 'The Great Gatsby - Revised Edition'.
Python
Need a hint?

Use dot notation to assign the new string to my_book.title.

4
Print the updated book information
Print the updated title and author attributes of my_book in the format: Title: [title], Author: [author].
Python
Need a hint?

Use an f-string to print the updated title and author from my_book.