Learning outcomes
By the end of this lesson you should be able to: get a value from the user with input(); use the arithmetic and string operators correctly; predict the output of an expression using operator precedence; and convert user input into a number so it can be used in calculations.
Prerequisites
Lesson 2 (Variables & types). You need to know that a variable stores one value under a label, and that integers, floats and strings behave differently and cannot be mixed without conversion.
The big idea
This lesson completes the three building blocks you have been assembling. Lesson 1 gave you output (print) and the idea of ordered instructions. Lesson 2 gave you storage (variables) and types. This lesson adds the other direction — input — and the operators that let you actually combine and transform what you have stored. With these three, you can already write programs that ask a question, compute an answer, and tell someone the answer. That is not a toy; it is the skeleton of almost everything you will write.
Why this matters before the syntax
The most important fact here is that input() always gives you a string. Whatever the person types — even if they type digits — arrives as text. This is why the arithmetic you learned does not just work: a variable holding the text "7" is not the number 7. If you try to multiply the text "7" by the number 3, Python will do something surprising rather than erroring, because the multiply operator is overloaded: on numbers it multiplies, on a string and an integer it repeats the string. So "7" * 3 gives "777", not 21. Learning to notice this is the entire point of the lesson.
The second fact is precedence. An expression is not always read left to right; certain operators bind more tightly than others. Multiplication binds tighter than addition, so 2 + 3 * 4 is 2 + 12, not 5 * 4. Parentheses override everything. This is not a rule to memorise a table for; it is enough to remember that the two or three most common mistakes all come from assuming left-to-right.
Worked example 1: a program that listens
The program asks one question, waits for the user to type a name, and greets them. Notice there is a pause — the program does not print the greeting until the person presses Enter.
name = input("What is your name? ")
print("Hello, " + name + "!")What is your name? Ada
Hello, Ada!The line name = input(...) does two jobs in one: it calls input, which collects a line of text from the keyboard, and it stores the result under name. The string concatenation in the second line joins the greeting, the typed name, and the exclamation mark into one string. If the user typed Ada, Python builds the single string Hello, Ada! and prints it.
Read the output block as a terminal transcript: the word Ada sits on the prompt line because the terminal echoes what you type, while the program itself writes only the prompt and the greeting. That distinction matters when you predict output — the program never writes the input back, it only stores it.
Worked example 2: operator precedence
Two expressions use the same numbers and operators but different grouping, and they produce different results. This is precedence doing real work.
print(2 + 3 * 4)
print((2 + 3) * 4)14
20In the first line, * binds tighter than +, so Python multiplies 3 by 4 to get 12, then adds it to 2 to get 14. In the second, the parentheses change the grouping so 2 plus 3 is computed first to give 5, which is then multiplied by 4 to give 20. Same four symbols, same two numbers; only the order of operations differs.
Worked example 3: operators on strings
Some operators change meaning depending on what they are applied to. The plus sign joins two strings but multiplies a string by an integer.
print("Py" + "thon")
print("ha" * 3)Python
hahahaThe plus sign joins Py and thon into Python — string concatenation, distinct from numeric addition. The asterisk repeats the string ha three times, giving hahaha. This overloading is why the turn of phrase "a string times a number" is not an error: it is a deliberate feature. It is also why the classic bug in example 4 happens — the same symbol means something sensible for a string and a number, but not what a beginner expects when the values are the wrong type.
Worked example 4: everything returns bool for comparisons
The comparison operators give back a truth value — True or False — which is a type of its own, the sort of thing the next lesson builds entire branches on.
print(10 > 5)
print(10 < 5)
print(10 == 10)True
False
TrueEach of these is an expression whose value is a boolean. Ten is greater than five is True, ten is less than five is False, and ten equals ten is True. Note the double equals sign for comparison — a single equals is assignment, and confusing the two is one of the most common beginner bugs. You are not storing 10 into 5; you are asking whether two things are equal.
Common mistakes
Watch outForgetting that input() returns a string. If the user types 7, you have the text "7". Multiplying it by 3 gives "777", not 21. Convert first with int() or float().
Watch outUsing a single equals sign for comparison. if x = 5 is assignment, not a question. Comparison is if x == 5. The equals used for comparison is always doubled.
Watch outAdding a string and a number. "Age: " + age fails when age is an integer. Convert with str(age) or use a comma so print places them side by side.
Watch outAssuming left-to-right evaluation. 2 + 3 * 4 is 14 because multiplication binds first. If you want the sum computed before the product, add parentheses: (2 + 3) * 4.
Check your understanding
A user types 7 in response to input(). What is the type of the value stored, and what does "7" * 3 give?
What is the output of print(2 + 3 * 4)?
Why does comparing with a single equals sign fail, and what is the correct symbol?
Multiple choice
What does input() always return, regardless of what the user types?
What is the value of print(3 * 2 + 1)?
What is printed by print("abc" + "def")?
A user enters 10 into a program. Which line correctly stores the number 10 so it can be used in arithmetic?
Which operator pair does comparison and produces a boolean?
What does "ha" * 3 produce?
Why is 10 == 10 True but 10 = 10 invalid in a program?
What is the value of (5 + 3) * 2?
Summary and next
input() reads a line of text as a string; print() writes output. Operators combine values: arithmetic on numbers, concatenation and repetition on strings, and comparison to produce a boolean. Precedence — multiplication before addition, parentheses overriding — decides the grouping. The single most important rule: convert input with int() or float() before arithmetic, because input is always text.
Next: conditions. Lesson 3 ended with comparison operators producing True or False. The next lesson puts those booleans to work, letting your program choose between two different courses of action — the start of real decision-making.