โ†Previous page

14OBJECTS ยท THINGS

Things that know and do.

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

Things know stuff and do stuff.

Look around you. Each thing has data (what it knows) and actions (what it does).

A dog

Knows: its name and its color. Does: sit, bark, run.

A phone

Knows: how much battery is left. Does: call, take a photo.

A backpack

Knows: what is inside. Does: open, close.

LOOK FIRST

Data and actions, joined by a dot.

Data: what it knows

A list knows the things inside it. A text knows its letters.

Methods: what it does

You already used them: word.upper() and toys.append("ball").

The dot

thing.job() asks the thing to do its job. The ( ) means: do it now!

STEP BY STEP

Two objects you already know.

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: ROBOT
print(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

Ask a text to do its jobs.

Guess first. Then run real Python and check.

1

Guess the result

word = "pybot" print(word.upper()) print(word.count("b")) print(type(word))

What will the first line show?

objects.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide โ™ฅ

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

Know or do?

Try each tiny question. A wrong answer becomes something to review.

0Completed
8Not tried
0Review
01

A bike

A bike knows its color and how fast it goes. It can ring its bell.

Which one is an action?

02

A cat's notebook

Michi the cat knows its name and its age.

What are the name and the age?

03

Ask the list

pets = ["cat"] pets.append("fish") print(pets)

What does Python show?

04

Do it now

Which one asks the text to DO its job?

05

What kind?

print(type("hi"))

What does Python show?

06

Each one its own

a = ["cat"] b = ["dog"] a.append("fish") print(b)

What does b show?

07

Fix PyBot's code

PyBot forgot the ( ), so the action never runs. Python shows something strange instead. Make it show: PYBOT

Goal output
PYBOT
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

An object is a thing that knows and does.

Its data is what it knows. Its methods are what it can do. Next, you make your own robot mold!