# Operations- Data Manipulation Tools

## Simple Operations

Python has the capability of carrying out calculations. \
Enter a calculation directly into the print statement:\
`print(2 + 2)`         Answer = 4\
`print(5 + 4 - 3)` Answer = 6\
&#x20;The spaces around the plus and minus signs here are **optional** (the code would work without them), but they make it easier to read.

&#x20;Python also carries out multiplication and division, using an **asterisk \*** to indicate multiplication and a **forward slash /** to indicate division.\
\
Use **parentheses** to determine which operations are performed first.\
print (2\*(3+4))       Answer = 14\
print (10/2)            Answer = 5.0

&#x20;Using a single slash to divide numbers produces a decimal (or **float**, as it is called in programming)

![](/files/-MGO0HSlo0SdYt6I3ctd)

The order of their appearance is not accidental. We'll talk more about it once we've gone through them all.

### Exponentiation (\*\*)

A double asterisk `**` sign is an exponentiation operator

`print( 2**5 )`\
`print( 9 ** (1/2) )`

You can chain exponentiations together. In other words, you can rise a number to multiple powers. For example **2\*\*3\*\*2**

### **Quotient (//)**

&#x20;**Floor division** is done using two forward `//` slashes and is used to determine the **quotient** of a division (the quantity produced by the division of two numbers).\
\
**For example:**\
`print( 20 // 6 )` `Ans = 3` \[6 goes into 20 three times] remainder 2 but we check if the remainder is 2, rounded off to the **LESSER** integer, so the answer remain a= 3 \
You can also use floor division on floats.

{% hint style="danger" %}
Lets look at \
`print (6//4)  Ans = 1`\
`print (6.//4) Ans = 1.0`

There are 2 things at play here:

* Remember when we divided an integer by an integer we **ALWAYS** got a float as the result. Here we can see that we got **another integer.**
* The second thing is that the ***real result*** of 6 /4 is  **1.5** But because we are doing floor division (//)  the actual answer is 1!! How did we get 1? The rule with floor quotients is that the result of the integer divison is **ALWAYS ROUNDED OFF** to the **LESSER** integer!! In this case the real answer is 1.5 and normally we would round this of to 2, but we have to go to the lesser integer - so in this case its 1

**NOW!!! Lets try this**:

`print (6 //`**`-4`**`)   Ans =`` `**`-2`**&#x20;

How did we get a 2 here ? Well if we divide this the **real** answer is -1.5 and we are doing floor division so we will be **ROUNDING OFF TO THE LESSER** integer, and so in this case the lesser integer would then be **`-2`** :)
{% endhint %}

### Remainder (%)

&#x20;The **modulo operator** is carried out with a percent symbol (%) and is used to get the **remainder** of a division.\
\
**For example:**

`print(20 % 6)      Ans = 2` \
`20 // 6 = 3` \
`3*6 = 18` \
`20-18=2`

`print(1.25 % 0.5)  Ans = 0.25`\
`1.25 // 0.5 = 2.0`\
`2.0 * 0.5 = 1.0`\
`1.25 -1 = 0.25`

`print(12 % 4.5)     Ans = 3.0`\
`12 // 4.5 = 2.0`\
`2 * 4.5 =9.0`\
`12 - 9.0 = 3.0`

**Remember integer divided by a float is always a float !!**

### **Operators and their Binding**

Don't forget **PEMDAS(Parentheses, Exponents, Multiplication and Division\[left to right], Addition and Subtraction\[left to right])** when it comes to figuring which to do first :)

The binding of the operator determines the order or computations performed by some operators with equal priority, put side by side in one expression.\
Most of Pythons options have **left-sided** binding, which means that the calculation of expressions is handled from **left to right**. For example:\
\&#xNAN;**`print (9 % 6  %  2)`**

![](/files/-MGPvC11kTW_2TEuJBqn)

As can be seen, this occurs by Python using the **left-sided binding -** But there is **one** interesting exception!! - EXPONENTIATION

### Exponentiation - Use Right-Sided Binding

**`print (2 ** 2 ** 3)`**

**`2**3 = 8`**\
**`2**8 = 256`**

### Operators and Parentheses

&#x20;`print((5 * ((25 % 13) + 100) / (2 * 13)) // 2)`\
`print[((5 * ((25 % 13) + 100) / 26]` \
`25 // 13 = 1`\
`1 * 13 = 13`\
`25 - 13 = 12`\
\
`12 + 100 = 112`\
`112 * 5  = 560`\
`---------------`\
`560 / 26 = 21.53`\
`21.53 // 2`\
\
`Answer = 10.0`

### List of Priorities

#### Learnt Lists of Priorities so far: -

![](/files/-MGQQqhngUZ1dGO-DGPx)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://python.microcisco.com/python-inststitute-pcap/python-essentials-part-1/module-2/operators-data-manipulation-tools.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
