# Basic Math in Python

Tyler (275)
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:

python
PRIMARY
62 guides
code
65 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``````
Tyler
Joined in 2015
Software Engineer and creator of howchoo.
Related to this guide:
Python is a very popular programming language for data visualization.
Nick
In these interests: python
In Python, comprehensions are a useful construct that allows us to create new sequences in a very concise way.
Tyler
In these interests: codepython
Slack has become one of the most important tools for communication in many companies, mine included.
Tyler
In these interests: slackpython
If you're familiar with Python's keyword-only arguments, then you've probably wondered why the same constraint doesn't exist for positional arguments. This changes with Python 3.
Specify a parallel filesystem cache for compiled bytecode
AKA the Walrus Operator
Learn how to use formatted string literals in Python
While this requirement may seem rare, learning how to nest defaultdicts properly can be extremely powerful and save you from bloated and confusing initialization code.
In Python, we often run into scenarios where we need to merge two or more lists. This is also called list concatenation, and Python provides multiple methods for accomplishing this.
Find and fix slow running Python unit tests
Posted in these interests:
python
PRIMARY
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.
Code is poetry -- one line at a time.
Discuss this guide:
We're hiring!
Are you a passionate writer or editor? 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.