Now we know how to make Python show text.
>>> 'Hello!' 'Hello!' >>>
But that includes
''. One way to show text to the user without
is with the print function. In Python, printing doesn't have anything
to do with physical printers, it just means showing text on the screen.
>>> print('Hello!') Hello! >>>
Now we are ready for a classic example, which is also the first program in many tutorials :)
>>> print("Hello World!") Hello World! >>>
But what exactly is print?
What are functions?
Let's see what happens if we type
>>> print <built-in function print> >>>
We could also type
print(print), it would do the same thing. Python
replied to us with some information about print wrapped in
As we can see, print is a function. Functions do something when they are
called by typing their name and parentheses. Inside the
parentheses, we can pass some arguments too. In
Functions are easy to understand, They simply do something when they
are called. Functions run immediately when we call them, so the
text appears on the screen right away when we run
Sometimes people think that doing
thingy = print('hello') means that
Python is going to print hello every time we type
thingy. But this
is not correct!
print('hello') runs print right away, and if we
thingy later it's not going to run
Now we know that
thingy = print('hello') doesn't store the
print('hello') call in a variable. But what does it do then?
>>> thingy = print('hello') hello >>> print(thingy) # thingy is now None None >>>
thingy = print('hello') set
thingy to None.
Here's what happened, explained in more detail:
- When we do
thingy = print('hello'), the right side is processed first.
print('hello')calls the print function with the argument
- The function runs. It shows the word hello.
- The print function returns None. All functions need to return something, and print returns None because there's no need to return anything else.
- Now the right side has been processed.
print('hello')returned None, so we can imagine we have None instead of
print('hello')there, and the assignment now looks like
thingy = None.
thingyis now None.
Now we understand what a return value is. When we call the
function, Python "replaces"
function(arguments) with whatever the
Calling a function without assigning the return value to anything (e.g.
print('hello') instead of
thingy = print('hello')) simply throws away
the return value. The interactive
>>> prompt doesn't echo the return
value back because it's None.
thingy = print('hello') is useless compared to
because the print function always returns None and we can do
thingy = None
without any printing.
Not all functions return None. The input function can be used for getting a string from the user.
>>> stuff = input("Enter something:") Enter something:hello >>> stuff 'hello' >>>
input("Enter something:") showed the text
Enter something: on the
screen and waited for me to type something. I typed hello and pressed
Enter. Then input returned the hello I typed as a string and it was
Usually we want to add a space after the
:, like this:
>>> stuff = input("Enter something: ") # now there's space between : and where i type Enter something: hello >>>
Handy things about print
We can also print an empty line by calling print without any arguments.
>>> print() >>>
\n is a newline character. Printing a string that contains
a newline character also prints a newline:
>>> print('hello\nworld') hello world >>>
If we want to print a real backslash, we need to escape it by typing two backslashes.
>>> print('hello\\nworld') hello\nworld >>>
We can also pass multiple arguments to the print function. We need to separate them with commas and print will add spaces between them.
>>> print("Hello", "World!") Hello World! >>>
+, the arguments don't need to be strings.
>>> print(42, "is an integer, and the value of pi is", 3.14) 42 is an integer, and the value of pi is 3.14 >>>
Variables names and built-in things
In the previous chapter we learned that
if is not a
valid variable name because it's a keyword.
>>> if = 123 File "<stdin>", line 1 if = 123 ^ SyntaxError: invalid syntax >>>
input are not keywords, so can we use them as
>>> print = "hello" >>> print 'hello' >>>
We can, but there's a problem. Now we can't even do our hello world!
>>> print("Hello World!") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object is not callable >>>
The error message complains that strings aren't callable because we just
'hello' and now we're trying to call it like
a function. As you can see, this is not a good idea at all. Most
editors display built-in functions with a special
color, so you don't need to worry about doing this accidentally.
Exit out of Python and start it again, and
should work normally.
function()calls a function without any arguments, and
function(1, 2, 3)calls a function with 1, 2 and 3 as arguments.
- When a function is called, it does something and returns something.
function(arguments)is "replaced" with the return value in the code that called it. For example,
stuff = function()calls a function, and then does
stuff = the_return_valueand the return value ends up in stuff.
- Python comes with
input. They are built-in functions.
- Avoid variable names that conflict with built-in functions.
You may use this tutorial freely at your own risk. See LICENSE.