Doing Simple Arithmetic
Introduction
Welcome back to Writing and Running Your First Python Programs! We have arrived at the fourth and final lesson of this course, so this is our last stop before wrapping things up.
So far, we have learned how to display values with print(), keep our code tidy with comments and spacing, and recognize the three basic value types: int, float, and str. Now, it is time to put those numbers to work.
In this lesson, we will use Python as a calculator. We will introduce the four basic arithmetic operators, notice a small surprise involving division, and even encounter our very first error message on purpose. Errors are a normal part of coding, so let's treat this one as a friendly introduction rather than a scary event.
Python Evaluates Before It Prints
Before we examine the operators one by one, here is the key idea to keep in mind: when Python sees an arithmetic expression inside print(), it does not simply show us what we typed. Instead, it computes the expression first, reduces it to a single value, and then passes that value to print().
The output is:
Notice that the screen shows 18, not the text 5 + 13.
Because there are no quotes around 5 + 13 in our code, Python treats it as a math expression rather than a string. It performs the addition, calculates 18, and only that final number appears on the screen.
Addition, Subtraction, and Multiplication
Python understands the same arithmetic symbols we already know from school, with one small twist: multiplication uses the asterisk * instead of the multiplication sign familiar from math class. Here are three operators in action, one per line:
Each line follows the same recipe: we place an expression inside the parentheses of print(), Python evaluates it, and only the final number appears in the output:
5 + 13becomes183 * 10becomes3020 - 8becomes12
Whether we add, multiply, or subtract, the expression is replaced by its result before anything reaches the screen.
Division and Floating-Point Results
Division uses the forward slash, /. Let's look at two examples: one that does not divide evenly and one that does.
Output:
The first result, 3.75, is exactly what we would expect from 15 divided by 4. But look at the second one: 10 / 2 is clearly 5, yet Python printed 5.0.
That is the small surprise we hinted at earlier: the / operator always produces a floating-point number, even when the division is exact.
Recall from the previous lesson that any number with a decimal point is a float. So, while using +, *, and - with two integers returns an integer, / always returns a float, no matter how clean the division looks.
Division by Zero: Meeting Your First Error
There is one input that division cannot handle: zero. Mathematically, dividing by zero is undefined, and Python agrees. In our code, that risky line is kept as a comment on purpose so the program does not crash:
If we remove the # in front of print(10 / 0) and run the program, Python responds with something like:
Errors like this are not scary; they are simply Python's way of pointing out the problem so we can fix it. The message even names the exact issue: division by zero.
The fix is easy: switch the divisor to any nonzero number, such as 2, and the line runs successfully again, just like the print(10 / 2) we saw a moment ago.
Conclusion and Next Steps
Great job! We now have four arithmetic operators in our toolbox: + for addition, - for subtraction, * for multiplication, and / for division.
We also picked up two important habits along the way: division always produces a float, even when the numbers divide evenly, and dividing by zero triggers a ZeroDivisionError that we can fix by choosing a different divisor.
That wraps up Writing and Running Your First Python Programs! In the next course, we will learn how to name our values by storing them in variables, which will let us reuse and update data across our programs. Before we get there, a short round of hands-on practice is waiting to let us put these operators to work. Let's fire up Python's calculator and try them out!
