## 01 - 04 Operators¶

Operators in python are the constructs which can manipulate the value of operands. Simply put when operators are used with one or more than one operand, they produce some result. Consider a basic mathematical addition `1`

`+`

`2`

in this case, `1`

and `2`

are operands and ** +** is the operator. Operands can also be variables.
Python supports following types of operators:

`Arithmetic Operators`

`Relational (Comparison) Operators`

`Assignment Operators`

`Logical Operators`

`Bitwise Operators`

`Membership Operators`

`Identity Operators`

### 01 - 04.01 Arithmetic Operators¶

As the name suggests, Arithmetic Operators includes all the operators to perform basic arithmetic functions.

#### .. 01.02 Addition (+)¶

Addition operator adds the value of the operands on its either side.

```
2 + 2
```

#### .. 01.03 Subtraction (-)¶

Subtracts right hand operand with the operand on the left hand.

```
2 - 2
```

#### .. 01.04 Multiplication ( * )¶

Multiplies the operands on its either sides and outputs the product.

```
2 * 2
```

#### .. 01.05 Division ( / )¶

Divides left hand operand with the right hand operand and outputs the quotient of the division.

```
2 / 2
```

#### .. 01.06 Modulus ( % )¶

Divides left hand operand with the right hand operate and outputs the remainder of the division.

```
7 % 2
```

#### .. 01.07 Exponential ( ** )¶

Performs exponential operation on the operands. The left hand operand is 'raised to' the right hand operand.

```
4 ** 4
```

#### .. 01.08 Floor Division ( // )¶

Divides the left hand operand with the right hand operand and outputs the quotient of the division removing the digits after decimal point.

```
3.0 // 2
```

### 01 - 04.02 Relational Operators¶

Relational Operators compare the operands on either side and identifies the relation between them. These are also known as Comparison Operators.

#### .. 02.01 Equal to ( == )¶

If the value of the two operands are equal, the condition becomes true.

```
a, b = 10, 10
```

```
a == b
```

#### .. 02.02 Not Equal to ( != )¶

If the value of two operands are not equal, the condition becomes true.

```
a != b
```

#### .. 02.03 Greater than ( > )¶

If the value of the operand on the left hand side of the operator is greater than the value of the operand on the right hand side, the condition becomes true.

```
a > b
```

#### .. 02.04 Less than ( < )¶

If the value of the operand on the left hand side of the operator is less than the value of the operand on the right hand side, the condition becomes true.

```
a < b
```

#### .. 02.05 Greater than OR Equal to ( >= )¶

If the value of the operand on the left hand side of the operator is greater than or equal to the operand on the right hand side, the condition becomes true.

```
a >= b
```

#### 02.06 Less than OR Equal to ( <= )¶

If the value of the operand on the left hand side of the operator is less than or equal to the operand on the right hand side, the condition becomes true.

```
a <= b
```

### 01 - 04.03 Assignment Operators¶

Assignment operator is responsible for assigning some value to a variable. Example a = 2 .We have been doing this for quite sometime now, but assignment operator can be used in many other ways.

#### .. 03.01 Equals ( = )¶

Assigns the value from right hand side operand to the left hand side operand.

```
a = 10
```

#### .. 03.02 Add AND ( += )¶

It is logically a two step process. In first step, the right hand side operand is added to the left hand side operand. In second step, the output of the first step is assigned to the operand on the left hand side.

```
a += 10 # It is equivalent to a = a + 10
```

#### .. 03.03 Subtract AND ( -= )¶

It is also a two step process where the right operand is subtracted from the left operand and the result is assigned to the left operand.

```
a -= 10 # It is equivalent to a = a - 10
```

#### .. 03.04 Multiply AND ( *= )¶

The right operand is multiplied with the left operand and the result is assigned to the left operand.

```
a *= 10 # It is equivalent to a = a * 10
```

#### .. 03.05 Divide AND ( /= )¶

The left operand is divided by the right operand and the quotient is assigned to the left operand.

```
a /= 10 # It is equivalent to a = a / 10
```

#### .. 03.06 Modulus AND ( %= )¶

It takes the modulus of the two operands and assigns the result to the left operand

```
a %= 10 # It is equivalent to a = a % 10
```

#### .. 03.07 Exponent AND ( **= )¶

It performs the exponential operation on the two operands and assigns the value to the left operand

```
a **= 10 # It is equivalent to a = a ** 10
```

#### .. 03.08 Floor Division AND ( //= )¶

It performs floor division and assigns the quotient to the left operand.

```
a //= 10 # It is equivalent to a = a // 10
```

### 01 - 04.04 Bitwise Operator¶

Bitwise operator works on bits and performs operations bit by bit. Before we jump into the operator, lets revise the concept of Bits. At the smallest scale in computers, the information is stored in bits. Consider bit as a smallest unit of storage, just like an atom. A bit can only store binary values i.e 0's or 1's (but not both). n bits can store 2 to the power of n values (n 0's or 1's). Practically a bit is very small for storage purposes, thus we deal with bytes which is equal to 8 bits. Then comes KiloBytes and MegaBytes and so on.. To understand the working of bitwise operators, we need to convert the operands to bits.

To understand the conversion between decimal and binary numbers, watch this video:

For examples below, we will be using a, b = 6, 10. In python binary equivalent can be obtained by using a built-in function bin() which basically converts the integer to binary representation. If you followed the tutorial video above you must be writing full 8-bit representation for even a small integer. Python's bin() however only prints the bits that are necessary for representing the integer. For example,

```
bin(6) # To print the binary equivalent of integer 6
```

It did not print leading zeros (0b00000110)

The leading 0b is for python to understand that the string representation means a binary number and not a regular string. Lets dive into the bitwise operators.

Let's get going with our examples.

```
a,b = 6, 10
```

#### .. 04.01 Binary AND ( & )¶

Operator uses two operands comparing them bit by bit. It outputs 1 if and only if both the operands have 1 at same bit location

```
bin(a & b)
```

#### .. 04.02 Binary OR ( | )¶

(The key above Return/ Enter key)

Operator uses two operands comparing them bit by bit. It outputs 1 if both the operands do not have 0 at same bit location.

```
bin(a | b)
```

#### .. 04.03 Binary XOR ( ^ )¶

Operator uses two operands comparing them bit by bit. It outputs 1 if and only if both the operands do not have same bit value at same location.

```
bin(a ^ b)
```

#### .. 04.04 Binary One's Complement ( ~ )¶

Operator uses single operand and toggles the bit value at every location.

```
bin(~ a)
```

#### .. 04.05 Binary Left Shift ( << )¶

Operator shifts the bit location of the left operand towards left by the number of bits specified by the right operand.

```
bin(a << 2)
```

#### .. 04.06 Binary Right Shift ( >> )¶

Operator shifts the bit location of the left operand towards right by the number of bits specified by the right operand.

```
bin(a >> 2)
```

### 01 - 04.05 Logical Operators¶

Python supports three logical operators viz AND, OR and NOT.

#### 05.01 AND ( and )¶

If both the operands are true, the condition becomes true.

#### 05.02 OR ( or )¶

If any of the two operands are true, the condition becomes true.

#### 05.03 NOT (not)¶

Reverses the logical state of the operand.

### 01 - 04.06 Membership Operator¶

This operator basically tests if the two operands are pointing at the same object or not. There are two types of membership operators:

#### .. 06.01 is¶

It evaluates to true if the operands on both the sides of the operator point to the same object.

```
a = 10
b = 10
```

```
a == b
```

```
a is b
```

**So, does this mean that == is same as is operator? **

```
id(a)
```

```
id(b)
```

Python keeps an array of integer objects for all integers between -5 and 256, when you create an `int`

in that range you actually just get back a reference to the existing object!

This means that if you check the `id()`

for any integer between -5 and 256 (both included), they will turn out to be the same everytime.

```
id(100)
```

```
id(100)
```

Now if I try to find the `id()`

for any integer *except* between -5 and 256, I will get different id's everytime

*(try running the following two cells multiple times and see the different id's being returned everytime)*

```
id(2500)
```

```
id(2500)
```

For more information you can refer:

- Stackoverflow question as pointed out by Ben Miller (
*Student*NYU CUSP 2017 Cohort). - Python C-API

#### .. 06.02 Is Not¶

It evaluates to true if both the operands do not point to the same object.

```
a is not b
```

### 01 - 04.07 Identity Operator¶

It is same as the python's Membership operator.

## Exercise:¶

What will be the output of 3 / 2 ? Is the output of 3.0/2 and 3 / 2 the same? Why or Why not?

Is 'Six' == 'six' ?If not, why?

Try assigning

`a`

to the power of`2`

, to the power of`2`

, to the power of`2`

. (i.e. $a = {{a^2}^2}^2 $)

- Implement your own decimal to 8-bit binary converter. Convert decimal number 88 and see if the output is 01011000 .

```
a = 10
b = a
```

```
a == b
```

```
a is b
```

What is the difference between ' == ' and ' is ' operator? Are they the same?