# 01-04 Operators

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

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

In [1]:
2 + 2

Out[1]:
4

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

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

In [2]:
2 - 2

Out[2]:
0

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

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

In [3]:
2 * 2

Out[3]:
4

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

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

In [4]:
2 / 2

Out[4]:
1.0

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

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

In [5]:
7 % 2

Out[5]:
1

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

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

In [6]:
4 ** 4

Out[6]:
256

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

In [7]:
3.0 // 2

Out[7]:
1.0

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

In [8]:
a, b = 10, 10

In [9]:
a == b

Out[9]:
True

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

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

In [10]:
a != b

Out[10]:
False

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

In [11]:
a > b

Out[11]:
False

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

In [12]:
a < b

Out[12]:
False

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

In [13]:
a >= b

Out[13]:
True

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

In [14]:
a <= b

Out[14]:
True

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

In [15]:
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.

In [16]:
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.

In [17]:
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.

In [18]:
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.

In [19]:
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

In [20]:
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

In [21]:
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.

In [22]:
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,

In [23]:
bin(6)   # To print the binary equivalent of integer 6

Out[23]:
'0b110'

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.

In [24]:
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

In [25]:
bin(a & b)

Out[25]:
'0b10'

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

In [26]:
bin(a | b)

Out[26]:
'0b1110'

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

In [27]:
bin(a ^ b)

Out[27]:
'0b1100'

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

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

In [28]:
bin(~ a)

Out[28]:
'-0b111'

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

In [29]:
bin(a << 2)

Out[29]:
'0b11000'

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

In [30]:
bin(a >> 2)

Out[30]:
'0b1'

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

In [31]:
a = 10
b = 10

In [32]:
a == b

Out[32]:
True
In [33]:
a is b

Out[33]:
True

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

In [34]:
id(a)

Out[34]:
4370377680
In [35]:
id(b)

Out[35]:
4370377680

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.

In [36]:
id(100)

Out[36]:
4370380560
In [37]:
id(100)

Out[37]:
4370380560

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)

In [38]:
id(2500)

Out[38]:
4401733840
In [39]:
id(2500)

Out[39]:
4401734032

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

#### .. 06.02 Is Not¶

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

In [40]:
a is not b

Out[40]:
False

### 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 .
In [41]:
a = 10
b = a

In [42]:
a == b

Out[42]:
True
In [43]:
a is b

Out[43]:
True

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