Computer Science
Grade 7
20 min
Methods and Attributes
Methods and Attributes
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define 'attribute' and 'method' using a real-world analogy.
Use dot notation to access an object's attributes and call its methods.
Differentiate between a method that requires arguments and one that does not.
Correctly use at least three common string methods (e.g., .upper(), .lower(), .find()).
Explain that methods perform actions while attributes store information.
Store the result of a method call in a new variable.
Distinguish between a general function (like print()) and an object-specific method (like .upper()).
How does your video game character know its name and health, and how does it know how to jump or run? 🎮 It's all about its attributes and methods!
In this lesson, we'll explore how objects in Python, l...
2
Key Concepts & Vocabulary
TermDefinitionExample
ObjectAnything in Python that can hold data and perform actions. Think of it as a 'thing' like a noun. A piece of text (a string), a number, or a list are all objects.In `name = "Alex"`, the variable `name` holds a string object.
AttributeA piece of information that describes an object. It's like a characteristic or a fact about the object. You access it, but you don't 'run' it.A 'dog' object might have a `color` attribute, like `dog.color` which could be 'brown'.
MethodAn action that an object can perform. It's like a verb. Methods are functions that belong to a specific object.A 'dog' object might have a `bark()` method, which you would run like this: `dog.bark()`.
Dot NotationThe period (`....
3
Core Syntax & Patterns
Accessing an Attribute
object.attribute_name
Use this pattern when you want to get a piece of information stored in an object. Notice there are no parentheses at the end.
Calling a Method (No Arguments)
object.method_name()
Use this pattern to make an object perform an action that doesn't need any extra information. The parentheses `()` are required to make the action happen.
Calling a Method (With Arguments)
object.method_name(argument1, argument2)
Use this pattern to make an object perform an action that needs more details. You put the extra information (arguments) inside the parentheses.
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A user enters their favorite game as ` skyrim ` with extra spaces. You need to remove the spaces and make it a proper title. Which code block correctly does this?
(Note: `.strip()` is a method that removes leading/trailing whitespace)
A.game = " skyrim "
clean_game = game.strip()
final_title = clean_game.capitalize()
B.game = " skyrim "
game.strip()
game.capitalize()
C.game = " skyrim "
final_title = capitalize(strip(game))
D.game = " skyrim "
final_title = game.strip.capitalize()
Challenging
Analyze the following code. Why will the final output be `start` instead of `FINISH`?
`status = "start"
new_status = status.replace("start", "finish")
status.upper()
print(status)`
A.The `.upper()` method cannot be used after `.replace()`.
B.The result of `status.upper()` was never stored in the `status` variable, so the original value was printed.
C.The `new_status` variable was not used in the print statement.
D.The code has a syntax error on the line `status.upper()`.
Challenging
You are given a sentence: `log_entry = "ERROR: User not found."` You need to find the position of the word 'User' and then create a new message that says `"CRITICAL: User not found."`. Which code is correct?
A.position = log_entry.find("User")
new_message = log_entry.replace("CRITICAL:", "ERROR:")
B.position = find(log_entry, "User")
new_message = replace(log_entry, "ERROR:", "CRITICAL:")
C.position = log_entry.find("ERROR:")
new_message = log_entry.replace("User", "CRITICAL:")
D.position = log_entry.find("User")
new_message = log_entry.replace("ERROR:", "CRITICAL:")
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free