A dog
Knows: its name and its color. Does: sit, bark, run.
14OBJECTS ยท THINGS
In Python, a text or a list is an object. It keeps data (what it knows) and has methods (what it can do).
IN REAL LIFE
Look around you. Each thing has data (what it knows) and actions (what it does).
Knows: its name and its color. Does: sit, bark, run.
Knows: how much battery is left. Does: call, take a photo.
Knows: what is inside. Does: open, close.
LOOK FIRST
A list knows the things inside it. A text knows its letters.
You already used them: word.upper() and toys.append("ball").
thing.job() asks the thing to do its job. The ( ) means: do it now!
STEP BY STEP
A list and a text. Each one knows things and does things.
toys = ["car", "doll"]An object: a list. Its data is the two toys.toys.append("ball")An action: the list adds a toy to itself.print(toys)The data changed: 3 toys now.word = "robot"Another object: a text. Its data is its letters.print(word.upper())An action that gives back a new text: ROBOTprint(type(word))type() tells what kind of object it is: <class 'str'>. Remember that word class!A secret: In the turtle zone, t = turtle.Turtle() made an object too. t knew its place and its color, and t.forward(50) was its action.
RUN IT
Guess first. Then run real Python and check.
word = "pybot"
print(word.upper())
print(word.count("b"))
print(type(word))What will the first line show?
PyBot's hint
Ready. Press Run Python.
Pyodide 314.0.3 runs CPython in your browser with WebAssembly.
Open-source under the Mozilla Public License 2.0. License
Now change one thing: Change "pybot" to your own name and run it. Then try print(type(9)).
YOUR TURN
Try each tiny question. A wrong answer becomes something to review.
Which one is an action?
What are the name and the age?
pets = ["cat"]
pets.append("fish")
print(pets)What does Python show?
Which one asks the text to DO its job?
print(type("hi"))What does Python show?
a = ["cat"]
b = ["dog"]
a.append("fish")
print(b)What does b show?
PyBot forgot the ( ), so the action never runs. Python shows something strange instead. Make it show: PYBOT
PYBOTOUTPUT
Want a fresh start? Clear only this page.
Its data is what it knows. Its methods are what it can do. Next, you make your own robot mold!