## Table of Contents[Hide][Show]

It has been clear to you that there are two types of numbers in programming. i.e. integers and floating-point numbers.

The arithmetic operations in Python are the same as everyday math and they revolve around these two data types.

## Arithmetic Operators

There are seven basic types of arithmetic operators. These are:

**Addition (+) :** Adds two numbers. e.g.

`print(10 + 4)`

**Subtraction (-):** Subtract the second number from the first. e.g.

`print(10 - 4)`

**Multiplication (*):** Multiply two numbers. e.g.

`print(10 * 4)`

**Division (/):** Performs division on two numbers. e.g.

`print(10 / 4)`

**Floor Division (//): **Performs division and rounds off the answer to the nearest integer. e.g.

`print(10 // 4)`

**Modulo Operator (%): **Performs division and returns the remainder. e.g.

`print(10 % 4)`

**Exponent (**): **Takes the power of the integer e.g.

`print(10 ** 4)`

All of these operations are shown below:

Now for all these operators that you learned, we have an augmented assignment operator. Let me show you how it is used.

Let’s say we have a variable called ‘x’. We set it to 10, now we want to increment this by 3, we’ll have to write code like this.

`x = 10`

`x = x + 3`

Python interpreter will add 3 in ‘x’ and store it in ‘x’. Let’s print this:

An augmented assignment operator can be used to replicate the same functionality but more efficiently.

The same code will be written like this.

`x = 10`

`x += 3`

Now, this operator can be used for subtraction and multiplication too. Look at this program.

Here we are first incrementing ‘x’ by 3 and then multiplying it by 3. The output of line 2 should be 13 and the output of line 3 should be 39.

## Operator Precedence

In math, we have a concept called operator precedence, which means the order of execution of operations in an equation. It’s not specific to Python, and all programming languages follow the operator precedence. Let me remind you of the order:

- Parenthesis
- Exponent
- Division or Multiplication
- Addition or Subtraction

Let’s write a program and check this:

`x = 10 + 3 * 2 ** 2 - (9 + 2)`

What should be the answer to the above equation?

If your answer is 11, you don’t need to repeat high school.

## Math Functions

In this section, we’re going to look at a few useful functions for working with numbers.

There are some built-in math functions to assist the math operations. Some of these are ’round’ and ‘abs’ to round off a floating-point number and obtain the absolute value of a number respectively.

They function like this:

In Python, we have a handful of built-in functions for performing mathematical operations. If you want to write a program that involves complex mathematical calculations, you need to import the math module.

A module in Python is a separate file with some reusable code. We use these modules to organize our code into different files.

To understand this, think of a supermarket. When you go to a supermarket you see different sections for fruits and vegetables, cleaning products, junk food and so on. Each section in the supermarket is like a module in Python. Similarly, we have a math module that contains a bunch of reusable functions for performing mathematical calculations.

To use the math module, type:

`import math`

Now you have access to a broader range of dot functions similar to the ones we learned in the last lecture.

Let’s test our new tool:

Let’s try a few of these functions:

The ‘ceil’ function rounds off the number to the upper closest integer, while the ‘floor’ function rounds off to the lower of the closest integer.

The ‘log10’ takes the logarithm of the number with base 10 and the ‘exp’ function calculates the exponential value with the base set to ‘e’.

You can check all the math functions here.

## Wrap Up

That’s all from this lecture. I hope you are having fun while learning Python through this series.

We will learn to run ‘if statements’ and more in the next lecture.

## Leave a Reply