python-tutorial/using-functions.md at master · Gwith/python-tutorial

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?

>>> print
<built-in function print>
>>> 

In Python 3, 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 print("hello") the function is print and we give it one argument, which is "hello".

Functions are sometimes thoght of as difficult to understand, but they really are not. They just do something when they are called. But if we do x = print('hello'), what is x?

>>> x = print('hello')
hello
>>> print(x)       # x is now None
None
>>> 

So doing x = print('hello') set x to None. Here's what happened, explained in more detail:

  • In x = print('hello'), the right side is processed first.
  • print('hello') calls the print function with the argument 'hello'.
  • The function runs immediately when it's called. 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 x = None.
  • x is now None.

Calling a function without assigning the return value to anything (e.g. print('hello') instead of x = print('hello')) simply throws away the return value. The interactive >>> prompt also echoes the return value back if it's not None.

Of course, x = print('hello') is useless compared to print('hello') because the print function always returns None and we can do x = None without any printing.

You can also print an empty line by calling print without any arguments:

In Python, \n is a newline character. Printing a string that contains a newline character also prints a newline:

>>> print('hello\nworld')
hello
world
>>> 

If you want to print a backslash, you need to escape it by typing two backslashes:

>>> print('hello\\nworld')
hello\nworld
>>> 

You can also pass multiple arguments to the print function. Separate them with commas, and print will add spaces between them.

>>> print("Hello", "World!")
Hello World!
>>> 

Unlike with +, 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
>>> 

Not all functions return None. The input function can be used for getting a string from the user.

>>> x = input("Enter something:")
Enter something:hello
>>> x
'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 assigned to x.

You may want to add a space after the :, like this:

>>> x = input("Enter something: ")  # now there's space between : and where i type
Enter something: hello
>>> 

Summary

  • function() calls a function without any arguments, and function(1, 2, 3) calls a function with 1, 2 and 3 as arguments. x = function() calls a function, and assigns the return value of the call to y.
  • When a function is called, it does something and returns something.
  • Python comes with print and input. They are built-in functions.

You may use this tutorial freely at your own risk. See LICENSE.

Previous | Next | Back to the list of contents