Open In Colab

INPUT AND OUTPUT

In order to generate a benefit, a program has to interact with the user. Therefore, we can make use of some built-in functions to output and input data.

Input

It can be useful to assign a certain value the user inputs to a variable. With the input() function we can directly assign the input that is entered by the user into the standard input (i.e. in the Integrated Development Environment: the Console/Shell). We can also add optional text that will be displayed to the user so that he knows what has to be entered. The program will pause with the execution until some data is inputted:

In [ ]:
name = input("Please enter your name: ")
print(name)
Please enter your name: EasyPass
EasyPass

Please note that the data that is inputted through the standard input is always converted to a string. This means even if the user enters a number the variable will have a data type string and not integer. However, we can later convert that value by using the previously mentioned built-in conversion functions (int()).

In [ ]:
age = input("Please enter your age: ")
type(age)
Please enter your age: 21
<class 'str'>
In [ ]:
type(int(age))
Out[ ]:
int

Input from the command line (Not relevant for examination)

When using Python from the Terminal / Command line we have different options to input data into the program: For this we need to import the module sys. Let's see how this is done in the Python script:

In [ ]:
#Import sys
import sys

#Create variables that use values that are passed from the command line 
input_1 = sys.argv[1] 	#sys.argv[1] stands for the first parameter we input from the command line.
input_2 = sys.argv[2] 	#sys.argv[2] stands for the second parameter we input from the command line.

At the same time, we somehow need to pass the parameters from the command line. This is done by just appending the parameters when calling the script from the command line: python3 /Users/user/Desktop/test_script.py input_1 input_2.

Probably, you have noted something unusual in the code above. Normally, we always use zero-based indexing, which means that the first argument should have an index of 0 and not 1. However, the reason is the following: the first argument sys.argv[0] is the script name itself (=test_script.py), the following arguments are technically then the 2nd and the 3rd argument.

Output

Sometimes it can be very useful to visualize an output – for example, a result of a calculation or some other text – to the user. Thus, in Python we usually use of the print() function. When we "print" data, this means that the data outputted to the standard output (i.e. in the Thonny in the Console). Let's look at some examples:

In [ ]:
#Print predefined data (e.g. information to the user):
print("This is a predefined message.")

#Print the result of a calculation:
print(14*14)

#Print the value of a variable:
myName = "Kevin"
print(myName) 

#Combine some predefined text with a value of a variable:
print("My name is", myName)
This is a predefined message.
196
Kevin
My name is Kevin