In the last tutorial, you learnt about Python Variables, Lists and Dictionaries. In this tutorial, you'll learn how to save your Python program to a file and run it. You'll also see how to display things and how to take input. Let's get started!
Open the Python console and try this:
>>> name = 'Maria'>>> name'Maria'>>> print(name)Maria
When you just type name, the Python interpreter responds with the string representation of the variable name, which is the letters M-a-r-i-a, surrounded by single quotes, ''. When you say print(name), Python will "print" the contents of the variable to the screen, without the quotes, which is neater.
As we'll see in the next section, print() is required when we want to print things while running code saved in a file.
So far we've been writing all our python code in the interpreter, which limits us to entering one line of code at a time. Normal programs are saved in files and executed by our programming language interpreter or compiler. So far we've been running our programs one line at a time in the Python interpreter.
We're going to need more than one line of code for the next few tasks, so we'll quickly need to:
- Exit the Python interpreter
- Open up our code editor of choice
- Save some code into a new python file
- Run it!
To exit from the Python interpreter that we've been using, simply type the exit() function
This will put you back into the command prompt.
Earlier, we picked out a code editor from the Code Editor section. We'll need to open the editor now and write some code into a new file:
Obviously, you're a pretty seasoned Python developer now, so feel free to write some code that you've learned so far.
Now we need to save the file and give it a descriptive name. Let's call the file hello.py and save it to your desktop. We can name the file anything we want, but the important part here is to make sure the file ends in .py. The .py extension tells our operating system that this is a Python executable file and Python can run it.
Note You should notice one of the coolest thing about code editors: colors! In the Python console, everything was the same color; now you should see that the print function is a different color from the string. This is called "syntax highlighting", and it's a really useful feature when coding. The color of things will give you hints, such as unclosed strings or a typo in a keyword name (like the def in a function, which we'll see below). This is one of the reasons we use a code editor. :)
With the file saved, it's time to run it! Using the skills you've learned in the command line section, use the terminal to change directories to the desktop.
Change directory: OS X
On a Mac, the command will look something like this:
$ cd ~/Desktop
Change directory: Linux
On Linux, it will be like this (the word "Desktop" might be translated to your local language):
$ cd ~/Desktop
Change directory: Windows Command Prompt
On Windows Command Prompt, it will be like this:
> cd %HomePath%\Desktop
Change directory: Windows Powershell
And on Windows Powershell, it will be like this:
> cd $Home\Desktop
Now use Python to execute the code in the file like this:
$ python3 hello.pyHello, Commonlounge!
Note: on Windows 'python3' is not recognized as a command. Instead, use 'python' to execute the file:
> python hello.py
Alright! You just ran your first Python program that was saved to a file. Feel awesome?
So far, you've only be outputting / printing values. It's useful to input values as well. For this, you can use the input function. Replace your code in hello.py with:
name = input("Whats your name? ")print("Hello " + name + "!")
Then, run the file again:
$ python3 hello.pyWhats your name? CommonloungeHello Commonlounge!
Again, on Windows, 'python3' is not recognized as a command. From now on, replace 'python3' with 'python' to execute the file.
> python hello.py
You'll see the following:
Whats your name? KeshavHello Keshav!
Above, the program gave the output Whats your name? , and I typed Keshav. Then, the program gave the output Hello Keshav!.
Remember we said a function is a sequence of instructions that Python has to perform? The following is the sequence of instructions for input
- print the value the function is called with
- Wait for user to type something and press enter
- Return what the user typed
When we did name = input("Whats your name? "), the program outputted Whats your name? and then waited input from the user. Once I typed Keshav and hit enter, the value got stored in variable name. Then, we used the print function to greet the user.
The input function returns a string. If you want use the input function to get an int, don't forget to use the int function to convert the value.
Let's say we want to ask the user to enter two numbers, and output the sum of the numbers. Save the following code to sum.py
a = input("Type a number: ")b = input("Type another number: ")print("The sum of the two numbers is " + str(int(a) + int(b)))
Save it and run it:
You'll see the following output:
Type a number: 42Type another number: 51The sum of the two numbers is 93
(In the above, 42 and 51 was typed by me).
There's a bunch going on above, so let's go step by step. In line 1 and 2, variable a got the value "42" and variable b got the value "51". Notice that the values are strings, not ints. Line 3 evaluates as follows:
print("The sum of the two numbers is " + str(int(a) + int(b)))=> print("The sum of the two numbers is " + str(int("42") + int(b)))=> print("The sum of the two numbers is " + str(int("42") + int("51")))=> print("The sum of the two numbers is " + str(42 + int("51")))=> print("The sum of the two numbers is " + str(42 + 51))=> print("The sum of the two numbers is " + str(93))=> print("The sum of the two numbers is " + "93")=> print("The sum of the two numbers is 93")
If instead we added a and b directly (without converting them to ints), then the result we would get would be "4251" instead of 93, since + operation on strings joins them together.
You've gotten very far! You just wrote a Python program that interacts with the user and does something useful!
In the last few exercises you learned about:
- printing things using the print function
- saving Python program to a file and running it
- asking the user for input using the input function
Time for the next lesson!
Based on content from https://tutorial.djangogirls.org/en/python_introduction/