Mixing Types in One Expression
Concepts covered: Data Types
Every operator looks at the type of the object on each side before it does anything. For +, 2 numbers add and 2 strings join. A number next to a string has no meaning, so Python stops with a TypeError instead of guessing. The table lists what each pairing produces.
| Left | Operator | Right | Result | Example |
|---|---|---|---|---|
| int | + - * ** | int | int | 3 + 4 gives 7 |
| int | + - * ** | float | float | 3 + 4.0 gives 7.0 |
| int | / | int | float | 8 / 2 gives 4.0 |
| int | // | float | float | 7 // 2.0 gives 3.0 |
| bool | + - * | int | int | True + 1 gives 2 |
| str | + | str | str | "3" + "4" gives "34" |
| str | * | int | str | "ab" * 3 gives "ababab" |
| str | + | int | TypeError | "3" + 4 fails |
| str | * | str | TypeError | "3" * "4" fails |
| str | - / | str | TypeError | "10" - "1" fails |
Numbers and booleans mix freely
Among numeric types, the result takes the wider type. An int beside a float produces a float, and / produces a float even when both sides are int and the division is exact. A bool behaves as the integer 1 or 0 inside arithmetic, which is occasionally handy and more often a sign that the wrong value reached the expression.
print(3 + 4.0)
print(8 / 2)
print(7 // 2)
print(7 // 2.0)
print(True + 1)
print(False * 10)
print(1 + 2.5 + True)Output:
7.0 4.0 3 3.0 2 0 4.5
Text has its own arithmetic
+ between 2 strings joins them, and * between a string and an int repeats it. Neither one does any numeric work: "3" + "4" produces the 2-character text "34", because both operands are text. A bool works as the repeat count too, since it is an int underneath.
print("3" + "4")
print("ab" * 3)
print("-" * 12)
print("x" * True)
print("x" * False)
print("done")Output:
34 ababab ------------ x done
The fifth line is empty: a string repeated 0 times is the empty string, and printing it still ends the line.
The combinations that fail
Python raises a TypeError when a string sits beside a number under +. It does the same for - or * between 2 strings, and for any / involving text. The program runs normally up to that line, prints whatever it printed, and stops. The message names both types, which tells you which operand carried the wrong kind of value.
minutes = 48213
print("Building summary")
message = "You listened for " + minutes
print(message)The order of the operands changes the message, because Python asks the left object first. Every outcome in the table is the same stop.
| Expression | Message |
|---|---|
| "3" + 4 | TypeError: can only concatenate str (not "int") to str |
| 4 + "3" | TypeError: unsupported operand type(s) for +: 'int' and 'str' |
| "3" * "4" | TypeError: can't multiply sequence by non-int of type 'str' |
| "10" - "1" | TypeError: unsupported operand type(s) for -: 'str' and 'str' |
| True + "!" | TypeError: unsupported operand type(s) for +: 'bool' and 'str' |
Let print do the joining
print takes any number of values separated by commas, converts each one to text on its own, and writes them with a space between. That is the route for showing a number next to a label without an operator in between. Turning a number into a string yourself is covered in a later lesson on type conversions.
count = 5
print("Rows:", count)
print("Rows: " + count)What does this line print?
print(True + True + "!")- A TypeError, because 2 + "!" has no meaning
- 2!
- TrueTrue!
- 3
The comma route adds exactly 1 space between arguments, on top of any spaces inside the text. A label written for joining with + usually ends in a space, so drop it when you switch, or the output shows 2.
files = 9
print("Files:", files)
print("Files: ", files)Output:
Files: 9 Files: 9
The second output line has 2 spaces between the colon and the 9: one from the label and one added by print. When a + line fails because one side is a number, replace it with a print call that passes the label and the value as separate arguments. Trim the label's trailing space in the same edit.
Each operator needs the pair it accepts. A repeat count stands on the other side of * from the string: "-" * 4 gives ----, while "-" * "4" fails. The same int added to a float gives a float, so 4 + 0.5 is 4.5.
About this lesson
This section is part of the Python Expressions: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.