Your dog's tricks
Rocky knows how to sit. You say: Rocky, sit! In Python: rocky.sit()
07FUNCTIONS ยท METHODS
Some functions belong to a value. You call them with a dot: value.job(). They are called methods.
IN REAL LIFE
You do not teach these tricks again. You say who, then a dot, then the job.
Rocky knows how to sit. You say: Rocky, sit! In Python: rocky.sit()
The volume button belongs to the TV: tv.volume_up()
backpack.open() opens this backpack, not your friend's.
LOOK FIRST
Write the value, a dot, and the method: name.upper()
"hi".upper() gives back "HI". "cat".replace("c", "h") gives back "hat".
toys.append("ball") does a job: it puts ball at the end of the list.
STEP BY STEP
Some methods do a job, like def. Some give something back, like return.
toys = ["car", "doll"]A list box with 2 toys, like in Boxes of all kinds.toys.append("ball")The list's own job: add ball at the end. It gives nothing back.print(toys)Shows ['car', 'doll', 'ball']name = "pybot"A text box.print(name.upper())upper gives back a new text in capitals: PYBOTRUN IT
Guess first. Then run real Python and check.
snacks = ["apple"]
snacks.append("banana")
snacks.append("grapes")
print(snacks)What will Python 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: Add one more append line with your favorite snack.
YOUR TURN
Try each tiny question. A wrong answer becomes something to review.
How would Python say it?
Which symbol joins a value to its method?
print("hi".upper())What does Python show?
print("cat".replace("c", "h"))What does Python show?
pets = ["cat"]
pets.append("dog")
print(pets)What does Python show?
print("banana".count("a"))What does Python show?
age = 9
print(age.upper())What happens?
PyBot called upper like a normal function, but upper belongs to the text. Use the dot. Make it show: HELLO
HELLOOUTPUT
Want a fresh start? Clear only this page.
Who, dot, job. Some methods do a job, some give something back, just like the boxes you made with def.