# Basic Math in Python

Tyler (291)
Total time: 10 minutes

The goal for this guide is to demonstrate the most commonly used arithmetic operators. You should try to become familiar with each of these as they will be used in all kinds of Python applications.

Posted in these interests:
h/python67 guides
h/code68 guides

## Getting set up

Unlike the previous guide, we're going to use a Python shell to run the commands. A shell is simply an interface for accessing some service, in this case Python.

Python ships with a shell by default. To open the Python shell, use the following:

``python3``

You should shell a Python prompt that looks something like this:

``````Python 3.6.5 (default, Mar 30 2018, 06:41:53)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
>>>``````

## An overview of common arithmetic operators

Here are the operators we'll cover in this guide:

Operator Name Description Example
- Subtraction Subtract a number from another. 2 - 1
* Multiplication Multiply two numbers. 2 * 2
/ Division Divide a number by another. 2 / 2
% Modulus Get the remainer of the left hand operand divided by the right hand operand. 10 % 3
** Exponent Performs exponential power operations. 2 ** 3
// Floor Division Division with the decimal point removed. 3 // 2

Each of these operators will be covered in the following steps. To gain familiarity with the Python shell as well as arithmetic operators, open a Python shell and practice using each operator.

Chances are, you've encountered this one before. In case you haven't, it's the process of finding the sum of two or more numbers.

``````>>> 2 + 2
4``````

## Subtraction -

``````>>> 100 - 30
70``````

## Multiplication *

``````>>> 10 * 3
30``````

## Division /

``````>>> 40 / 10
4.0``````

Notice here that the result is not `4`. It's `4.0`. Python 3 uses true division (as opposed to Python 2, which uses floor division). This means the result will always be a floating point number representing the fractional result. See the different:

### Python 2

``````>>> 1 / 2
0``````

### Python 3

``````>>> 1 / 2
0.5``````

## Modulus %

Related to the division operator, this operator gives us the remainder of a division operation.

``````>>> 10 % 3
1``````

## Exponent **

``````>>> 2 ** 8
256``````

## Floor Division //

This operator gives us what we expect from the division operator in Python 2. It performs the division operation and returns an integer, chopping off any trailing decimals.

``````>>> 10 // 3
3``````
Series: Python Basics
Show all in the Python Basics series
Tyler
Joined in 2015
Software Engineer and creator of howchoo.
Related to this guide:
Get the latest edition of Python in just minutes.
In these interests: codewindowspython
Run Python scripts in command prompt without typing the whole path.
In these interests: windowscodepython
You can run any Python script in a command-line interface.
In these interests: python
Got a Python question? We've probably answered it here.
Not sure what version of Python you’re running? Time to find out!
Specify a parallel filesystem cache for compiled bytecode
AKA the Walrus Operator
Learn how to use formatted string literals in Python
Posted in these interests:
h/python 67 guides
Python is howchoo's favorite programming language. We believe python promotes the most organized and performant codebase possible. We also love Django so, naturally, we love Python.
h/code 68 guides
Code is poetry -- one line at a time.
Discuss this guide:
We're hiring!
Are you a passionate writer? We want to hear from you!

Follow @howchoo and learn cool things:

Like what we do?

Want to support Howchoo? When you buy a tool or material through one of our Amazon links, we earn a small commission as an Amazon Associate.