Python print() Function: Show Text and Numbers
What Is a Function?
In chapter 1 you wrote print("Hello, World!"). That print is a function. Before we learn all its tricks, let's see what a function is.
A function is a named mini-machine that does one job. You don't need to know how it works inside. You just call it by its name, give it what it needs, and it does the job.
Think of a juice machine. You put fruit in, press the button, and juice comes out. You don't build the machine every time. You just use it.
How to call a function
You call (use) a function by writing its name, then brackets:
print("Hi")
printis the function's name.- The brackets
( )say "run it now". - What goes inside the brackets is what you give the function. Here, the text
"Hi".
print() is a built-in function: it comes with Python, ready to use. Python has many more, and later you'll even write your own.
Why functions are great
- Reuse: you write
print()once in a line, and it works every time. - Less to think about: you don't need to know how Python puts letters on the screen.
- Clean code: one short name does a whole job.
A function is a named mini-machine: call it with its name and brackets.