The same symbols, different meanings
Everything typed into a program from outside, whether a web form, a chat box, or a terminal prompt, arrives as text, even when it looks like a number. Mixing up "text that looks like a number" and an actual number is one of the most common real-world bugs, producing ages that will not add and prices that glue together instead of totaling, and this lesson is how you avoid it.
Here is a classic beginner surprise. What + does depends on what it is given:
print("5" + "5") print(5 + 5)
The first line joins two strings from lesson 2-1 and outputs 55. The second adds two numbers from lesson 2-2 and outputs 10. Same operator, completely different results.
This is why Python cares so much about the difference between "5" and 5. The kind of value something is, whether string, integer, or float, is called its type, and the type is what decides which meaning of + you get.
One operator, two behaviors
The quotes are the only difference between these two lines.
print("5" + "5") print(5 + 5)
Output
55 10
Two characters of punctuation changed the answer from 10 to 55. Nothing else about the line moved.
That is the reason type errors are so slippery. Neither line is wrong, neither produces an error message, and a program that concatenates when it meant to add just reports a confidently wrong total. The only defense is knowing what type each value is before it reaches an operator.
input() always gives you a string
Remember input() from lesson 1-3? Whatever the user types arrives as a string, even if they typed digits. If you want to do math with it, convert it to an integer first with int(...):
print(int(input()) + 5)
Read inside-out, like in lesson 1-3:
input()reads the typed line, say"10"(a string)int("10")converts it to the number1010 + 5is15, andprintshows it
There is also float(...) for decimal input, and str(...) to go the other way, turning a number into a string.
Why two typed numbers concatenate
The output is 34 instead of 7 because input() returns strings, and + joins strings together instead of adding them.
Both calls hand back text, "3" and "4", so + takes its string meaning and produces "34".
Wrapping each call in int(...) converts them to numbers first, and then + adds them to make 7.
This bug has a recognizable signature worth memorizing. When two numbers come out glued end to end instead of summed, the values were strings, every time. The digits in the wrong answer are literally the digits of both inputs in order, which is the giveaway.
Doubling a typed number
Reading, converting, and doubling, all in one line.
print(int(input()) * 2)
Input
21Output
42Reading the pieces
- The inside-out order is
input(), thenint(...), then* 2, thenprint. Each step hands its result to the one wrapped around it. - Without
int(...)the output would be2121, the string repeated twice, because*between a string and a number means repetition as lesson 2-1 showed. - That failure is the same shape as the concatenation bug above. Both operators have a string meaning and a number meaning, and the type of the value silently picks one.