# 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)

![](https://3018064759-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LudO0LP3BbNbyBiBB3l%2F-MGNzGQuZoQ57seXQYXB%2F-MGO0HSlo0SdYt6I3ctd%2Fimage.png?alt=media\&token=818c217f-1415-4948-9c4f-875f75d6999c)

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)`**

![](https://3018064759-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LudO0LP3BbNbyBiBB3l%2F-MGPogmZWXyRrNR5PhGi%2F-MGPvC11kTW_2TEuJBqn%2Fimage.png?alt=media\&token=0c9af349-6fb2-4ad3-ba33-6e4299d2118b)

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: -

![](https://3018064759-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LudO0LP3BbNbyBiBB3l%2F-MGQGnE33Zh2JWWlH7EH%2F-MGQQqhngUZ1dGO-DGPx%2Fimage.png?alt=media\&token=38c0d19b-4ad7-4766-ad70-4d77b191c300)
