## Table of Contents

Python is used extensively in finance, simulation, data science, statistical analysis, and in many other mathematically-intensive fields of work. The reason being is that Python makes working with numbers very easy and intuitive.

You can't avoid math, but you knew that already, right?

## Arithmetic Operators

Here are the most common **arithmetic operators** that allow you to manipulate numbers in Python.

### Addition

You can add numbers in Python using the `+`

operator.

```
PYTHONx = 2
y = 5
total = x + y
print(total)
```

```
BASH7
```

### Subtraction

You can subtract numbers in Python using the `-`

operator.

```
PYTHONx = 8
y = 3
total = x - y
print(total)
```

```
BASH5
```

### Multiplication

You can multiply numbers in Python using the `*`

operator.

```
PYTHONx = 4
y = 6
total = x * y
print(total)
```

```
BASH24
```

### Division

You can divide numbers in Python using the `/`

operator.

```
PYTHONx = 54
y = 2
total = x / y
print(total)
```

```
BASH27.0
```

### Modulus

You can find the remainder of a division in Python using the `%`

operator.

```
PYTHONx = 64
y = 23
total = x % y
print(total)
```

```
BASH18.0
```

### Exponents

You can find the **power** of a number in Python using the `**`

operator.

```
PYTHONx = 2
y = 4
total = x ** y
print(total)
```

```
BASH16
```

### Order of Operations

**Order of Operations**, also known as **PEMDAS**, is at play here, including with the use of parentheses.

```
PYTHONx = (4 * 5) + 20 / 4
print(x)
```

```
BASH25
```

## Python Number Types

There are three kinds of numbers in Python, and they are as followed:

- int
- float
- complex

```
PYTHONa = 1337 # int
b = 13.37 # float
c = 1337j # complex
print(type(a))
print(type(b))
print(type(c))
```

```
BASH<class 'int'>
<class 'float'>
<class 'complex'>
```

### int

**Integers** are whole numbers like `4`

or `532`

. They can be positive or negative, and they don't contain any decimals.

```
PYTHONa = 423
b = 74
c = 87937595
```

These are all valid integers.

### Float

**Floating point numbers** are numbers containing a decimal, and can also be positive or negative. Here are some examples:

```
PYTHONa = 1.53
b = 3.1
c = -95.23
```

### Complex

**Complex** numbers are Python's representation of imaginary numbers, and they use a `j`

to represent the `i`

.

```
PYTHONa = 4+2j
b = 9j
c = -3j
```

## Number Type Conversion

Python offers built-in methods to convert between these types using the `float()`

, `int()`

, and `complex()`

methods.

```
PYTHONa = 5 # int
b = 3.3 # float
c = 7j # complex
float = float(a)
int = int(b)
complex = complex(a)
print(float)
print(int)
print(complex)
print(type(float))
print(type(int))
print(type(complex))
```

```
BASH5.0
3
(5+0j)
<class 'float'>
<class 'int'>
<class 'complex'>
```

## Math Methods

We mentioned before that Python is used heavily in any field that utilizes math. The built-in methods that Python provides is a large reason why. Here are some of the most common ones:

### Absolute Value

Get the **absolute value** of a number by using the `abs()`

method:

```
PYTHONnumber = -34
print(abs(number))
```

```
BASH34
```

### Floor

Use the `floor()`

method to get the number passed in rounded down to the nearest integer if it is not already an integer.

```
PYTHONimport math
number = 6.21
print(math.min(number))
```

```
BASH6
```

### Ceiling

Use the `ceil()`

method to get the number passed in rounded up to the next highest integer if it is not already an integer.

```
PYTHONimport math
number = 6.21
print(math.ceil(number))
```

```
BASH7
```

### Natural Logarithm

You can get the **natural logarithms** of a number using `log()`

.

```
PYTHONimport math
number = 123
print(math.log(number))
```

```
BASH4.812184355372417
```

### Base-10 Logarithm

Alternatively, you can also get the **base-10 logarithm** of a number using `log10()`

.

```
PYTHONimport math
number = 123
print(math.log10(number))
```

```
BASH2.089905111439398
```

### Maximum

You can get the **maximum** of two or more numbers using the `max()`

method:

```
PYTHONnumber1 = 123
number2 = 456
number3 = 789
print(max(number1, number2, number3))
```

```
BASH789
```

### Minimum

You can get the **minimum** of two or more numbers using the `min()`

method:

```
PYTHONnumber1 = 123
number2 = 456
number3 = 789
print(min(number1, number2, number3))
```

```
BASH123
```

### Power

You can take the **power** of a number using `pow`

.

```
PYTHONbase = 3
exponent = 4
print(pow(base, exponent))
```

```
BASH81
```

### Round

You can **round** off any number to the nearest integer using `round()`

.

```
PYTHONnumber = 3.1459
places = 3
print(round(number, places))
```

```
BASH3.146
```

### Square Root

You can take the **square root** of a number using `sqrt()`

.

```
PYTHONimport math
number = 81
print(math.sqrt(number))
```

```
BASH9
```

### Random

Generating random numbers in Python is easy. There's a module named `random`

that has all we need. Call the `randrange()`

method to get a number in between two other numbers you define:

```
PYTHONimport random
start = 1
end = 10
print(random.randrange(start, end))
```

```
BASH6
```

## Math Constants

One last cool to point out is that Python's `math`

module also comes with some predefined constants for us.

### Euler's Number

One of the constants that the `math`

module comes with is **Euler's Number**:

```
PYTHONimport math
e = math.e
print(e)
```

```
BASH2.718281828459045
```

### Pi

Python's `math`

module also comes with a value for **Pi**, the ratio of the circumference of a circle relative to its diameter.

```
PYTHONimport math
pi = math.pi
print(pi)
```

```
BASH3.141592653589793
```

You've got to love some delicious pi. 🥧🥧🥧

- Getting Started with TypeScript
- Getting Started with Svelte
- Create an RSS Reader in Node
- Git Tutorial: Learn how to use Version Control
- How to Set Up Cron Jobs in Linux
- Best Visual Studio Code Extensions for 2022
- How to deploy a PHP app using Docker
- How to deploy a MySQL Server using Docker
- How to deploy a Node app using Docker
- Learn how to use v-model with a custom Vue component
- Getting Started with Moment.js
- Creating a Twitter bot with Node.js