Skip to content

Input/Output

Learning Objectives

At the end of this sub-unit, students should

  • know how to read user input and print values.
  • be able to move from IDLE to editor.

Preliminary

So far, the execution is no different from not using variables as we always initialize the variable with a fixed value. This is not ideal, but it is a good first step. In the case of numeric operation computing the interest rate, we simply have to change the initialization. Then, the rest of the computations will be different depending on this initial value. Can we make this better by initializing the variable to user input?

At this point, we will also start giving code to be run on editor. You should run the code on editor instead of IDLE.

Input

To read user input, Python has a built-in function called input(). The function waits for user input until a newline (i.e., Enter) is given and returns the user input as a string. We may also provide a prompt input(msg). In this case, the msg will be printed first as a string. We must emphasize that the user input will be read as a string.

The expression input() is called a function invocation. We will be discussing the general syntax for this in the future as you will be making your own function. Simply remember that this is the way you call the input() function in Python.

Now we are ready to make a truly general code. This code will be doing an actually useful computation that depends on the user input. Let us illustrate this with the interest rate example. For simplicity, we assume that the P, n, and t are integers while r is a floating point number.

Interest Rate

1
2
3
4
5
6
7
8
9
# Reading user input, we omit the prompt
# Note the explicit type conversion to the type
P = int(input())
r = float(input())
n = int(input())
t = int(input())

# Actual computation
B = P * ((1 + (r / n)) ** (n * t))

If this is your first encounter with a full code, you should follow the steps below.

  1. Copy the code into editor.
  2. Save the file: you can press Ctrl+S.
  3. Run the file: if you are using Python editor, you can simply press F5.

You may notice that there is no output. Do not fear, this is normal because the input() function halts the execution of the program until the user has provide input. To indicate that the input has finished, the function waits for the user to press Enter.

Once the user has pressed Enter, we have read one input. So that is also not the end of the execution because we have 4 input() functions. That means we need to provide 3 more user inputs.

Try entering the following:

  1. 10000 followed by Enter. This will assign 10000 to P.
  2. 0.05 followed by Enter. This will assign 0.05 to r.
  3. 12 followed by Enter. This will assign 12 to n.
  4. 10 followed by Enter. This will assign 10 to t.

Unfortunately, you will also not see anything printed but you will see that the prompt >>> is shown. This is another difference between IDLE and editor, we will not print anything when running from editor unless we explicitly choose to print. For now, type the following on IDLE.

>>> B
16470.0949769028

You can now try running the code again by pressing F5 from the editor and try different values. You will then find that the output will be different. So now we have managed to write a more general code that represents an actual computation that can work for different values. But that is still not good enough because we have to do more work to see the result. We will solve that next.

Output

To print an output to the monitor (also called as standard output), Python has a built-in function called print(...). The function can take 0 or more values that we will call arguments1. These arguments must be separater by commas. To illustrate the behavior, try running the following code from the editor. The output is also shown below.

print('CS', 1010, "S", "is" + " easy")
CS 1010 S is easy

First, note that we have 4 arguments.

  1. The string 'CS'.
  2. The integer 1010.
  3. The string "S".
  4. The expression "is" + " easy".

Note the following.

  1. We first evaluate all the arguments.
    • In particular, the 4th argument is evaluated into the string "is easy".
  2. The arguments are printed from left-to-right with a whitespace (i.e., ' ') added in between arguments.

More interestingly, we can now enhance the code for interest rate to automatically print the result as follows.

Interest Rate

# Reading user input, we omit the prompt
# Note the explicit type conversion to the type
P = int(input())
r = float(input())
n = int(input())
t = int(input())

# Actual computation
B = P * ((1 + (r / n)) ** (n * t))

# Print
print(B)

  1. We use the simpler terminology of arguments to differentiate it from parameters. You will learn the details of these in the future. Simply note that you may see actual parameters in place of arguments and formal parameters in place of parameters if you read other sources.