Module 4 · Operators — topic 2 of 16
Addition
The + operator in depth: adding numbers of different types, joining strings and lists, adding many values with sum(), and the errors that appear when types don’t match.
Learning objectives
- Add ints, floats and mixed numbers
- Use + to concatenate sequences
- Use sum() for many values and avoid type errors
Definition
The + operator in depth: adding numbers of different types, joining strings and lists, adding many values with sum(), and the errors that appear when types don’t match.
Introduction
The + operator adds numbers, but in Python it does more: it also joins strings, lists and tuples end to end. What + does depends on the types of its two operands.
Why it matters
Totals, counters and running balances all use addition, and building messages often uses string concatenation. Knowing which combinations are allowed, and which raise errors, saves time when combining data from different sources.
Detailed explanation
Numbers: int + int gives an int; anything with a float gives a float; adding a complex gives a complex.
Sequences: "Py" + "thon" is "Python"; [1, 2] + [3] is [1, 2, 3]; (1,) + (2,) is (1, 2). Both operands must be the same sequence type.
Mixing a string and a number raises TypeError. Convert first with str() or use an f-string.
sum(iterable) adds many numbers at once and is clearer and faster than a loop of + operations. An optional start value can be given: sum(values, 100).
Booleans can be added because they are ints: True + True is 2. This is handy for counting but can be surprising.
Adding a value to itself in a loop is common enough that Python has the shortcut total += value, covered in the assignment operators lesson.
Joining many strings with + inside a loop creates a new string each time, which becomes slow for thousands of pieces. For large amounts of text, collect the pieces in a list and join them once with "".join(pieces), which you will meet in Module 7.
Syntax
- Operands must be compatible types.
- Concatenation creates a new string or list.
- sum() adds every number in an iterable.
Code examples
Adding numbers
375 3.5 2
Joining sequences
Database [1, 2, 3, 4] (5, 6)
More examples
Totals from different sources
All sales: [1200, 850, 430, 990, 1500] Grand total: 4970 Total plus opening balance: 14970 Total: 4970
Step-by-step explanation
- online_sales + shop_sales concatenates the two lists into one new list.
- sum(all_sales) adds every number in it, giving 4970.
- sum(all_sales, 10_000) starts counting from 10,000 instead of 0.
- Joining text with a number fails, so the except branch converts the number with str() first.
Expected output
Running “Totals from different sources” prints exactly:
Practical applications
- Accounting scripts total transactions with sum().
- Report generators concatenate header and body strings.
- Data pipelines merge lists of records from several files with +.
Best practices
- Use sum() for adding many values.
- Convert text to numbers before adding.
- Prefer f-strings over long chains of string +.
Common mistakes
Adding numbers stored as text
Avoid
Prefer
+ on two strings joins them; convert to numbers to add.
Adding a list and a single item
Avoid
Prefer
Both operands must be lists; wrap the item in brackets or use append().
Key takeaways
- + adds numbers and concatenates same-type sequences.
- Mixing str and numbers raises TypeError.
- sum() totals an iterable, optionally from a start value.
Try it yourself
- 1Add the numbers in a list of five prices with sum() and also with a for loop; check both agree.
- 2Join a first name and last name with a space between them using +.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
14 questions cover the “Operators” module. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhat is printed?