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().

# Python works out the value before printing it
print(5 + 13)   # prints 18, not "5 + 13"

The output is:

18

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:

print(5 + 13)   # addition
print(3 * 10)   # multiplication
print(20 - 8)   # subtraction

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:

18
30
12
  • 5 + 13 becomes 18
  • 3 * 10 becomes 30
  • 20 - 8 becomes 12

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.

# Division always produces a floating-point result
print(15 / 4)
print(10 / 2)

Output:

3.75
5.0

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:

# Dividing by zero is not allowed and causes an error.
# Errors are normal; the message tells you what went wrong.
# print(10 / 0)   # try running this, then fix it below

If we remove the # in front of print(10 / 0) and run the program, Python responds with something like:

ZeroDivisionError: division by zero

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!

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal