Skip to content

Latest commit

 

History

History
182 lines (139 loc) · 5.23 KB

File metadata and controls

182 lines (139 loc) · 5.23 KB

3. Using functions and storing code in files

Functions

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. They are used by typing their name [*] and parenthesis. Inside the parenthesis, we can pass some parameters too. In print("hello") the function name is print and we give it one parameter, which is "hello".

[*] Actually, a name of a variable that points to the function. Functions are also variables in Python. This means that print_me_a_thingy = print and then print_me_a_thingy('hello world') works just fine.

Functions are often thoght of as difficult to understand, but they really are not. They just do something with the parameters they're given. 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 parameter 'hello'.
  • The function runs immediately when it's called. It shows the word hello.
  • The print function returns None.
  • 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.

All functions need to return something. So does the print function, and that's why it returns 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.

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:

>>> str(123)
'123'
>>> 

You can also pass multiple parameters separated with commas. The print function will add spaces between them.

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

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

Storing code in files

Now it's time to write some code into a file for the first time. In IDLE, go to File at top left and select New File, or just press Ctrl+N.

New File in IDLE

Type something like this into the window that opens. When your code is in a file, adding x will not show the value of a variable called x to the user. You need to print it instead.

print("Hello.")
word = input("Enter something: ")
print("You entered", word)

Go to File, Save and save the file. Then press F5 to run it. It will run in the window you opened first. Another window like that will be opened if you closed it already. You should see something like this in it:

>>> ================================ RESTART ================================
>>>
Hello.
Enter something: hi
You entered hi
>>>

Awesome, it runs and you can interact with it by typing text into it and pressing Enter! 😃 Again, when the program ran it waited for me to type something. I typed 'hi' and pressed Enter.

After running the program you can check what the word variable contains in the window the program ran in. Again, in the >>> prompt you don't need print to show things like you need in files.

>>> word
'hi'
>>>

Exercises

  1. Write a program into a file that asks the user to write a word and then prints that word 1000 times. For example, if the user enters hi the program would reply hihihihi....
  2. Make it to print spaces between the words. It should be like hi hi hi hi....
  3. Make something that asks the user to enter two words, and prints 1000 of each with spaces in between. For example, if the user enters hello and hi the program would print hello hi hello hi hello hi....

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

Back to the list of contents