0
0
Pythonprogramming~15 mins

Public attributes in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding Public Attributes in Python Classes
📖 Scenario: Imagine you are creating a simple program to manage a library's books. Each book has a title and an author. You want to store this information in a class and be able to access it easily.
🎯 Goal: You will create a Python class with public attributes for the book's title and author. Then, you will create an object of this class and print its attributes.
📋 What You'll Learn
Create a class named Book with public attributes title and author
Create an object of the Book class with the title 'The Great Gatsby' and author 'F. Scott Fitzgerald'
Access and print the title and author attributes of the object
💡 Why This Matters
🌍 Real World
Public attributes are used to store and access information about objects in many programs, such as managing books in a library or products in a store.
💼 Career
Understanding how to use public attributes is a basic skill for programming jobs that involve object-oriented programming, such as software development and data modeling.
Progress0 / 4 steps
1
Create the Book class with public attributes
Create a class called Book with an __init__ method that takes title and author as parameters and assigns them to public attributes self.title and self.author.
Python
Need a hint?

Remember, public attributes are created by assigning values to self.attribute_name inside the __init__ method.

2
Create a Book object with specific title and author
Create an object called my_book from the Book class with the title 'The Great Gatsby' and author 'F. Scott Fitzgerald'.
Python
Need a hint?

Use the class name Book followed by parentheses with the title and author strings inside to create the object.

3
Access the public attributes of the my_book object
Write two lines of code to assign the title attribute of my_book to a variable called book_title and the author attribute to a variable called book_author.
Python
Need a hint?

Use dot notation like object.attribute to access public attributes.

4
Print the book's title and author
Print the values of book_title and book_author on separate lines.
Python
Need a hint?

Use two print() statements, one for each variable.