Inputs and Outputs

In the following section you will learn how to manage the inputs and outputs of a Python command line program. Also, in this part you will write your very first Python program. Please not that this guide is written for Python 2. For this part, there are some differences between Python 2 and Python 3 which will be explained in this guide.

Outputs

Let’s start with the outputs. Why? Because it is easier to start with that. The outputs of a basic Python program will occur through the terminal in form of text. This text will be handled inside Python as a string. With the following code, the string can be printed on screen:

print "This message will appear on screen"

This is a print statement. As its name says, it prints something. This something is the indicated text in double quotes (” “). This text is nothing else than a data string, just like the variable type. Instead of the double quotes, you can also write single quotes (‘ ‘).

It is also possible to print the text that has been stored inside a variable directly:

name = 'Shacky'
print name

The output of these lines would be simply the content of the variable name. A last thing that you might need to know is that you can also combine (concatenate) strings to form a more complex message.

name = 'Shacky'
serial_number = 39271947

print "My name is %s and I my serial number is %d." % (name, serial_number)

As you will see, the %s and the %d will be replaced by the values of the variables specified at the end. the %s is used for strings, the %d for integers and if needed, %f will be used for a float.

An alternative method would be to use the .format option. This is a more modern method for formatting the variables:

name = 'Shacky'
serial_number = 39271947

print "My name is {0} and I my serial number is {1}.".format(name, serial_number)

This method allows you to use the same variable several times by indicating the index of the variable in between curly brackets ({1}).

You could also simply add the elements with a plus sign between the individual elements. This would also work fine, but it is considered bad practice. Here you would need to include the white space before and after the quotes as otherwise the values of the variables would be sticking to the words before and after the inserted variable.

name = 'Shacky'
serial_number = 39271947

print "My name is " + name + " and I my serial number is " str(serial_number)

Now you are able to output text. This can already be used to make very simple programs that would simply display you some text and numbers. This could be used to display the results of some calculations or to display the time. When using the Python language in inter active mode, the print statement might not be very useful for this task though.

Note

The code shown above is valid for Python 2. The print statement has a slightly different syntax for Python 3. Instead of giving the argument in double quotes after the print keyword, you hand it in between parentheses. In this way, the print statement is handled like a function. The double quotes are still mandatory for string messages.

print("Hello World")

Now you are able to understand the hello world program that was already shown in the Programming Languages page.

As you can also write the parentheses for the Python 2 syntax, even though they are not mandatory, there is nothing wrong in writing them. This makes it easier to write Python programs regardless if it is version 2 or 3.

Inputs

The inputs of a basic Python program are given through the terminal. The terminal will prompt you to enter a value, often with a message, and when hitting enter, it will hand the input to the Python program. Sometimes, this is simply used to stop the program at the end so that the user can see the output on the screen before the window closes.

There are two main types for receiving the inputs in Python. There are the input() and the raw_input() functions. These functions return the value that has been entered in the terminal window as a string. The raw_input() function gives you the value as a simple string while the input() function tries to interpret the content as a Python command. Usually, you want the raw_input(). Also, you probably want to store the content of this function inside a variable. Inside the parentheses you can enter e string that will be shown on the screen so that the user knows what input is expected.

name = raw_input("Please enter your name and press ENTER")

The above example will show the message: “Please enter your name and press ENTER”. When entering your name and pressing ENTER on the keyboard, the input will be stored inside the variable called name. The fact that the message mentions the ENTER key is only for information to the user, it does not influence the actual key mapping. The ENTER key is always the one that needs to be pressed for continuing the program.

Note that the value obtained from the raw_input() will always be given to you as a string value. When you want to get an integer or float, you need to convert it first into the correct variable type.

speed = int(raw_input("Enter speed in RPM: "))
voltage = float(raw_input("Enter the voltage (Range -> 0.0 .. 5.0 V): "))

The raw_input() command is very useful to provide a really simplistic user interface. It can tell the user what is expected and combined with the print statement, it can display the output of your program.

Basic I/O Python Program

Now you can combine the previous segments to make a very simplistic program that will ask the user for the name, the age and then returns the number of years until the user turns 100. This simple program might appear silly to you, but it is the first step towards bigger programs.

#!/usr/bin/env python
print("Hello, this program will tell you when you will be 100 years old.")

name = raw_input("Please tell me your first name: ")
age = int(raw_input("Please tell me your age: "))

years = 100 - age

print("Hi {0}, in {1} years, you will be 100 years old.".format(name, years))
raw_input()

The above program will ask the name, the age and will then return the remaining years. The very first line is called the hash-bang, or she-bang and it tells the computer that this is a python program. The very last line will prevent the program from exiting before the user had the possibility to read the message.

That’s it, when you copy this code into a text editor and safe it for example as calculateAge.py you can run it with:

python calculateAge.py

You just made your very first Python program. Congratulations. The next part will explain how you can implement code that verifies if a certain condition is met. Depending on that, you can make your program decide what it should do. These are called conditional statements. If a condition is true, it will do something, if it is false, it will do something else.

Continue learning about conditional statements or go back to revisit arithmetic expressions.