Python Coding Exercise #01: Predict the Output
Part of the series Python Practice Exercise — part 1
Before you start: Is familiar with variables, print() and input() in Python
The question
What will be the output of the following program if the numbers 2 and 5 are entered when it is executed?
n1 = input('Enter first number: ')
n2 = input('Enter second number: ')
x = n1 + n2 * 3
print(x)
My Solution
If n1 is 2 and n2 is 5, the maths looks simple:
n2 * 3is5 * 3 = 15n1 + 15is2 + 15 = 17
However, this is not what Python will print. Let’s break it down.
1. input() always gives you a string
Whatever the user types, input() returns as text (a string), even when it looks like a number.
Try these line of codes in Python:
n1 = input('Enter first number: ')
n2 = input(Enter second number: ')
These line of codes will ask the user to enter their input.
print(type(n1))
print(type(n2))
These line of codes will print the data type of the input.
print(repr(n1))
print(repr(n2))
Lastly, these line of codes will print the value exactly as Python stores it.
The second and third line of code will print <class 'str'>.
So after the user enters 2 and 5:
| Variable | Value | Type |
|---|---|---|
n1 | '2' | str |
n2 | '5' | str |
The quotes matter. '2' is the character (a string) “2”, not the number (integer) two.
2. The same operators mean different things for strings
For numbers, + adds and * multiplies. For strings:
+joins two strings together (concatenation).*with a number repeats a string.
print('2' + '5') # 25
print('5' * 3) # 555
3. Multiplication happens before addition
Python follows the
P (Parentheses)
E (Exponents)
M (Multiplication)
D (Division)
A (Addition)
S (Subtraction) order of operations,
so x = n1 + n2 * 3 is read as n1 + (n2 * 3).
Here is the program, step by step:
# Step 1: multiplication first — repeat '5' three times
n2 * 3 # '5' * 3 → '555'
# Step 2: addition second — join '2' and '555'
n1 + '555' # '2' + '555' → '2555'
So, x holds the string '2555', and when the program prints x, it outputs 2555.
How to get the program to output a number?
Convert the input to numbers first, using int():
n1 = int(input('Enter first number: '))
n2 = int(input('Enter second number: '))
x = n1 + n2 * 3
print(x)
Now
n1 + n2 * 3 is
2 + (5 * 3),
resulting in
2 + 15 = 17.
Key takeaways
input()always returns a string, even if the user types digits.- Add
int()orfloat()to convert input () from string to integer or floating-point number. See Data Types in Python and Exercise #08 for more. +joins strings and*repeats them, so the same line can behave very differently depending on the data types involved. For more on*with strings, see Exercise #54.- Python always follows the PEMDAS order of operations.
- If an output confuses you, use
print(type(x))andprint(repr(x))to quickly check the data type.
✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦
Practice Source: This exercise is taken from Ultimate Python Programming: Learn Python with 650+ Programs, 900+ Practice Questions, and 5 Projects by Deepali Srivastava, published by BPB Publications. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.