Welcome back. You now know that variables hold values with types such as int, float, str, and bool. This lesson is about using those values: calculating numerical results, constructing text, and converting a value when its current type does not match the operation you need.
By the end, you should be able to look at a desired output—such as a total, a remainder, or a readable message—and deliberately choose the operators and type conversions that produce it.
Arithmetic: make Python calculate
Python can act as a calculator inside any Colab cell. The most common arithmetic operators are below.
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | addition | 8 + 3 | 11 |
- | subtraction | 8 - 3 | 5 |
* | multiplication | 8 * 3 | 24 |
/ | division | 8 / 3 | 2.6666666666666665 |
// | floor division | 8 // 3 | 2 |
% | remainder (modulo) | 8 % 3 | 2 |
** | exponentiation | 8 ** 3 | 512 |
Run this cell:
total_items = 17
items_per_box = 3
print(total_items + 4)
print(total_items - 2)
print(items_per_box * 5)
print(total_items / items_per_box)
print(total_items // items_per_box)
print(total_items % items_per_box)
print(2 ** 4)
The division result deserves attention:
print(17 / 3)
Python gives:
5.666666666666667
The / operator always produces a float, even when the answer happens to be a whole number:
print(8 / 2)
print(type(8 / 2))
Output:
4.0
<class 'float'>
Floor division and modulo are useful when you are dividing things into complete groups. With 17 items and boxes that hold 3:
total_items = 17
items_per_box = 3
full_boxes = total_items // items_per_box
leftover_items = total_items % items_per_box
print(full_boxes)
print(leftover_items)
The result is 5 complete boxes and 2 items left over. The two operations describe different parts of the same division.
Order of operations
Python evaluates multiplication, division, floor division, modulo, and powers before addition and subtraction. Parentheses let you make the intended grouping explicit.
print(2 + 3 * 4)
print((2 + 3) * 4)
Output:
14
20
The first calculation multiplies 3 by 4 before adding 2. The parentheses in the second calculation cause the addition to happen first. When a formula could be misread, parentheses make your code safer and easier to inspect.
This short segment from Bro Code, Math in Python is easy!, demonstrates the main arithmetic operators and the useful idea of a remainder.
Watch Math in Python is easy! by Bro Code to see arithmetic operators evaluated in a live Python session, including a practical explanation of modulo.
Watch core operators. Focus especially on the distinction between ordinary division and modulo: modulo reports what remains after forming complete groups. The brief +=, -=, and *= examples are shorthand forms you may recognize in later code; the ordinary forms remain the foundation.
The same symbols can mean different things for strings
An operator’s behavior depends on the types on either side of it. For numbers, + means addition. For strings, it means concatenation: joining text together.
first_name = "Ada"
last_name = "Lovelace"
full_name = first_name + " " + last_name
print(full_name)
Output:
Ada Lovelace
Notice the " " between the variables. Python does not insert spaces automatically when you concatenate strings. Without that explicit space, the output would be AdaLovelace.
The * operator repeats a string when the other value is an integer:
cheer = "Go! "
print(cheer * 3)
divider = "-" * 20
print(divider)
Output:
Go! Go! Go!
--------------------
This is repetition, not numerical multiplication. Compare these two expressions:
print(2 * 3)
print("2" * 3)
Output:
6
222
The first uses two integers. The second repeats the text character "2" three times.
You can also use len() to count the characters in a string, including spaces and punctuation:
project_name = "Data Lab"
print(len(project_name))
print(type(len(project_name)))
Output:
8
<class 'int'>
len() returns an integer, because a character count is a whole number.
A useful way to transform text is with a string method such as .upper():
course_name = "python foundations"
display_name = course_name.upper()
print(course_name)
print(display_name)
Output:
python foundations
PYTHON FOUNDATIONS
The original course_name is unchanged. .upper() produces a new string, which this code stores in display_name.

The important boundary is this: a string that looks numerical is still text while it has quotation marks.
number_text = "25"
number_value = 25
print(type(number_text))
print(type(number_value))
Output:
<class 'str'>
<class 'int'>
For a focused explanation of how Python treats + and * with strings, then how to convert between text and numbers, study this Real Python lesson transcript.
Working With Strings and Numbers (Video) – Real Python
Read Working With Strings and Numbers from Real Python. It reinforces the central rule of this lesson: Python chooses an operation based on the types it receives, so conversion is necessary when text needs to participate in a numerical calculation.
In the transcript passages around 1:40 to 3:17, read string behavior to see why string addition and repetition differ from numeric operations. Then, in the passages around 3:30 to 8:04, read numeric conversion. Focus on why int() works for whole-number text and why float() is needed for decimal text. Finally, read the passage around 8:11 to 10:23 on string conversion. Notice that conversion is also needed when a numeric result must be joined to surrounding text.
Type conversion: choose a representation for the job
Type conversion creates a value of a different type. The three conversions you will use most often are:
| Function | Purpose | Example | Resulting type |
|---|---|---|---|
int() | Convert to a whole number | int("12") | int |
float() | Convert to a decimal number | float("12.5") | float |
str() | Convert to text | str(12) | str |
Suppose a quantity comes from somewhere as text:
quantity_text = "12"
extra_quantity = 3
This will fail:
# quantity_text + extra_quantity
Python raises a TypeError because it cannot add a string and an integer. First decide what result you want. If "12" represents a count, convert it to an integer and calculate:
quantity_text = "12"
extra_quantity = 3
quantity = int(quantity_text)
new_quantity = quantity + extra_quantity
print(new_quantity)
print(type(new_quantity))
Output:
15
<class 'int'>
If the text may contain a decimal, use float():
distance_text = "2.5"
extra_distance = 1.2
distance = float(distance_text)
total_distance = distance + extra_distance
print(total_distance)
Output:
3.7
Choose int() only when the text is written as a whole number:
print(int("12"))
But this fails:
# int("12.5")
"12.5" is valid numerical text, but it is not valid integer text. Use float("12.5") instead.
Conversion also works in the other direction. If you want to join a numeric result to text using +, convert the number with str():
pages_read = 42
message = "Pages read: " + str(pages_read)
print(message)
Output:
Pages read: 42
Without str(pages_read), Python would raise a TypeError. It sees text on one side of + and a number on the other, and it cannot infer whether you meant addition or text construction.
For readable messages, an f-string is often cleaner than several + operators:
pages_read = 42
message = f"Pages read: {pages_read}"
print(message)
The f before the opening quotation mark tells Python to evaluate the expression inside {} and place its displayed value into the string. The whole result is still a string:
print(type(message))
Output:
<class 'str'>
A complete example: convert, calculate, then report
A useful programming pattern is to separate a task into three stages:
- Convert data into types that support the calculation.
- Perform the calculation with numerical variables.
- Build a readable string for the output.
Here is a small notebook calculation for the cost of notebooks. Imagine the price and quantity initially arrive as text, as they often do when read from a file or entered by a user.
price_text = "19.99"
quantity_text = "3"
tax_rate = 0.08
price = float(price_text)
quantity = int(quantity_text)
subtotal = price * quantity
tax = subtotal * tax_rate
total = subtotal + tax
receipt = f"Total for {quantity} notebooks: {total:.2f}"
print(receipt)
Output:
Total for 3 notebooks: 64.77
Trace the types through the code:
| Variable | Value | Type | Why that type fits |
|---|---|---|---|
price_text | "19.99" | str | It begins as text |
price | 19.99 | float | Prices can include decimals |
quantity_text | "3" | str | It begins as text |
quantity | 3 | int | Item counts are whole numbers |
subtotal | 59.97 | float | It comes from multiplying a float |
receipt | text sentence | str | It is intended for display |
The :.2f inside the f-string asks Python to display total with exactly two decimal places. This is a display choice: total remains a numerical float, so you can still calculate with it afterward.
When results surprise you, inspect each intermediate variable instead of guessing:
print(price, type(price))
print(quantity, type(quantity))
print(subtotal, type(subtotal))
print(total, type(total))
print(receipt, type(receipt))
This habit is particularly valuable when you later load data from CSV files. Values that appear to be numbers may initially be strings, and a calculation that fails—or produces repeated text such as "88"—often points to the type rather than the arithmetic itself.
Key takeaways
Arithmetic operators let you calculate with numeric values: use / for regular division, // for complete groups, % for leftovers, and ** for powers. Parentheses make the intended order of calculation clear.
Strings use some familiar symbols differently: + joins text and * repeats text. len() counts characters, while methods such as .upper() create transformed versions of a string.
Finally, type conversion is purposeful:
- Use
int()when whole-number text must become a number. - Use
float()when decimal text must become a number. - Use
str()when a number must be joined to ordinary text. - Use
type()whenever you need to verify what Python is actually handling.
Next, you will use comparisons and Boolean operators to create conditions, allowing a program to choose between different actions based on the values it has calculated.
Can't find a good explanation? Sign up and we'll make it for you
Sign up