Python Operators are used to perform some operations on variables and values, python has divided the operators into different types depending on the operation as shown below:

- Arithmetic Operator
- Comparison operator
- Equality Operator
- Logical Operator
- Bitwise Operator
- Shift Operator
- Assignment Operator
- Ternary Operator
- Special Operator Identity Operator and Membership Operator
- Operator Precedence
- Mathematical Functions by using Math Module

Arithmetic operation is also called a** basic operation** where we are going to perform this operation with **numerical values.**

**The different type of arithmetic operations are : **

- Addition(+)
- Subtraction(-)
- Multiplication(*)
- Division(/)
- Modulus(%)
- Floor Division Operator(//)
- Exponential Operator(**)

The first five operators are basic arithmetic operators let us understand these operators, consider an example as `a=20`

and `b=10`

```
a=20
b=10
print(a+b)= ?
print(a-b)= ?
print(a*b)= ?
print(a%b)=?
```

**The division operator (/) : **

In python, the division operator always going to return the answer in floating point format only,

Ex: `a=10`

and `b=5`

then if you divide this `a/b`

then the answer is `2.0`

```
a=10
b=5
print(a/b)
```

`a=10`

and `b=3`

then `10/3=3.33333`

```
a=10
b=3
print(a/b)
print(10/3)
```

**The floor division operator (//): **

This operator is meant for both integral and floating point output depending on our arguments, If both the arguments are an integer then it will return an integer and if anyone argument is float then the result will be a float.

Ex: `a=10 `

and `b=2`

then` 10//2=5`

```
a=10
b=2
print(a//b)
print(10//2)
```

` a=10.0`

and `b=2 `

then `10.0//2=5.0`

```
a=10.0
b=2
print(10.0//2)
```

** Let us understand the normal division and floor division with example**

If` a=10`

and `b=3 `

then in case of normal division in python, it will always result in floating point and hence the answer is `3.3333`

```
a=10
b=3
print(10/3)
```

If `a=10.0`

And `b=3`

then in case of normal division in python, it will always result in floating point hence the answer is `3.33333`

```
a=10.0
b=3
print(10.0/3)
```

If `a=10`

and` b=3`

then, in case of** floor division in python** if **both the arguments** are** an integer** then the result will be an **integer** only but here if we divide `10`

by `3`

then the result is `3.3333`

but floor value is the **nearest value to the 3.33** so the actual answer is

`3.0`

but both the arguments are of integer type and hence the `3`

```
a=10
b=3
print(10//3)
```

If** a=10.0** and

`b=3`

then, in case of floor division in python if `3.0`

because the nearest value for `3.3333`

`3.0`

only.```
a=10.0
b=3
print(10.0//3)
```

**Exponential operator : ** This operator is also called a** power operator** which is represented by **two stars ( **).**

Ex: if

`a=10`

and`b=2`

`print (10**2)`

means `10`^{2}

so the answer is `100`

```
a=10
b=2
print(a**B)
print(10**2)
```

If `a=3`

and `b=3`

then `print (3**3) =3`

^{3} =27

```
a=3
b=3
print(3**3)
```

For **addition operation in python, both the arguments should be a string type only, if anyone argument is not string type then python will throw an error**, for more information you can go through our previous article typecasting

ex: If `a=chercher`

and `b=5`

then `print("Chercher"+5)= chercher5`

```
a="chercher"
b=5
print(a+b)
print("chercher"+5)
```

If `a="apple" `

and `b="mango"`

then `print("apple"+"mango")`

```
a="apple"
b="mango"
print("apple"+"mango")
```

If` a=banana`

and` b=10 `

then we can execute this in two ways

`print("banana"+"10")`

and we can convert int type into string datatype directly and then we can print the output as `print("banana"+str(10))`

```
a="banana"
b=10
print("banana"+"10")
or
print("banana"+str(10))
```

In the same way, to apply an `*`

** operator** to the string, one argument should be of int type and another should be of string type if both the arguments are of string type then python will throw an error

```
a=grapes
b=3
print("grapes"*3)
```

```
a="apple"
b="mango"
print("apple"*"mango")
```

Relational operators are comparison operators which are used to **compare the equality between the two entities** and returns the result in the **boolean format depending on the comparison condition.**

**Relational operators : **

- Greater than(>)
- Greater than or equal to(>=)
- Less than(<)
- Less than or Equal to(<=)

Let us understand this with an example: `a=10`

and` b=20`

```
a=10
b=20
print(a>b)
print(a>=b)
print(a<b)
print(a<+b)
```

Even we can apply relational operators between string data types also, but before comparing the string datatypes you should know the ASCI** values** of strings, if you do not know the value of the string, python is having two inbuilt functions **ord and char **to find the ASCI values of the string

Where `ord`

is to find the **Unicode value** of the given character as a `print(ord(a)=97`

and if you give as a `print(ord(A))=65`

```
print(ord('a'))
print(ord('A'))
```

Another one inbuilt function is to know the character of the given Unicode value as a print(char(a))

```
print(chr(66))
print(chr(45))
print(chr(78))
```

The Following example of **comparing two string** by using the relational operators, in the below example I had taken `s1='Cat'`

and `s2='Dog'`

so here python compares the Unicode value of first letters of both the strings and returns the result.

```
s1='Cat'
s2='Dog'
print(s1>s2)
print(s1>=s2)
print(s1<s2)
print(s1<=s2)
```

If `s1='apple`

' and `s2='apple'`

```
s1='apple'
s2='apple'
print(s1>s2)
print(s1>=s2)
print(s1<s2)
print(s1<=s2)
```

If **s1="Mango"** and **s2="mango"**

```
s1="Mango"
s2="mango"
print(s1>s2)
print(s1>=s2)
print(s1<s2)
print(s1<=s2)
```

And if we** compare between the string and the int** then python will throw a **type error **

`print(9<"chercher")`

Let us consider another one example

```
a=10
b=20
if a>b:
print("a is greater than b")
else:
print("a is not greater than b")
```

In the same way, the **relational operators chaining is also possible**, that means when we are having multiple operators, if one operator comparison become False then the result become False and if all the comparison become True then the result will be True

```
print(10<20)
print(10<20<30<40)
print(10<20<30<40>50)
print(20<10<30<20)
```

The Equality operator compares two operands whether the two operands are equal or not and returns the result in** boolean format**.

The Equality Operators are:

- Equal To(==)
- Not Equal To(!=)

Understand these concepts with an example

```
if a=10 and b=20
10==20
10!=20
```

```
1==True
0==True
0==False
10==10.0
"cherchertech"=="cherchertech"
```

In the case of Equality operators, if compare the arguments between **string** and **integer type**, that means **even after comparing the incompatible datatypes also python never give any type error or value error, instead, it just returns the False as the answer.**

```
10==False
10=="10"
```

We know that the relational operator is applicable for chaining, in the same way, equality operators chaining is also possible, if **all the comparison become True then the answer will be True** and if **anyone comparison become False then the result will be False only.**

```
10==20==30==40
2==2==2
2!=3!=6!=7
```

whereas Is operator is used to comparing the reference variables

ex:

```
L1=[10,20,30]
L2=[10,20,30]
print(L1==L2)
print(L1 is L2)
```

in the above example, I created two objects, L1 and dL2 are the reference variables pointing to the objects, but when we** compare between the contents of the two objects by using the equal to operator**, python returns** True because both contents are same**.

And when we compare between the reference variable by using is operator, python returns the False, because the **two objects are pointing to the two different address **

Logical Operators are used to compare the conditional statements.

The Three Logical Operators are:

- And
- Or
- Not

In Boolean datatypes **And operator** returns **True only when both arguments are True**

```
True and True
True and False
False and True
False and False
```

**Or operator** returns true if **at least one argument is True**

```
True or True
True or False
False or True
False or False
```

The following example demonstrates clearly how to use `And`

Operator

```
username=input('Enter user nme :')
password=input('Enter password :')
if username=='chercher' and password=='123' :
print('valid user')
else:
print('invalid user')
```

`Not`

Operator is always going to return the reverse complement

```
Not True
Not False
```

Whenever we apply an **AND Operator** to the **Non-Boolean datatypes **the result always will be **Non-boolean datatype only**.

- In the boolean type,
**when the first argument is True then the result is based on the second argument**, if the**second argument is True then the result will be True or else False**. In the same way, if the first argument is False then obviously the result will be False only.

But in Non-boolean types, the same concept will be going to apply but it will always result in `x`

or `y `

format only.

- If
`x`

evaluates to False then the result is`x`

- If
`x`

evaluates to True then the result is`y`

`x=0 y=10 0 and 10 `

- In the above example,
`0`

evaluates to False and hence the result is`0`

.

```
x=10
y=20
10 and 20
```

So in the above example, the value `10`

evaluates to True then the answer is `20`

And if `x="chercher" `

and `y=" " `

then

```
x="chercher"
y=" "
"chercher" and " "
```

In the above example, the `x`

** evaluates to True** and the `y`

** value** **evaluates to False** and hence the answer is `y`

```
x="apple"
y="mango"
"apple" and "mango"
```

In the above example, the `x`

value evaluates to True and hence the answer is `y`

- whenever we apply
**or operator**to the non-boolean data types, if the**first argument is True then the result is True**and if the**first argument is False then the result is based on y**, that means**if the second value is True then the result is True and if the second value is False then the result is False.**

**That means if x evaluates to True then the result is x **and

`x`

Evaluates to False then the result is` y`

.```
x=10 and y=20
10 or 20
x=0 and y=20
0 or 20
```

Consider another example as shown below

```
[ ] or "chercher"
"chercher" or "tech"
" " or "tech"
```

- Next, when we apply Not operator to the non-boolean data type the result is always boolean only.

`not "orange" not 0 not 10`

In the above, First example, we have taken as `Not Orange`

, where the is a string is not empty, so it will result as True.

In the Second example, I took Empty string as not Empty string and hence it will result as False.

In the third example, `Not 0`

, 0 value in boolean is False, hence I took here as Not False then it will result as True.

In the Fourth example, I have taken as `Not 10`

, where `10 `

is non zero, non-zero means True and hence it will result as False.

Bitwise operators are those which operates on one or more **bits patterns** or **Binary Numerals** at the level of there individual bits.

There are Six Bitwise operators in python are as follow:

- Bitwise And-&
- Bitwise Or-|
- Bitwise exclusive Or-^
- Bitwise complement operator-~
- Bitwise Left-shift Operator-<<
- Bitwise Right-shift Operator->>

**We can apply Bitwise operators only for integers and Boolean Datatypes **

```
16 & 18
False & True
```

And if you try to apply these operators between the** float or string values** then python will** throw an error.**

`1.2 & 1.5`

`"war" & "love"`

__Bitwise And (&) :__

In Bitwise And Operator **if Both the bits are one(1) then the result is 1 otherwise result is Zero(0) **

__Bitwise Or (|): __

In the Bitwise Or Operator **if at least one bit is one(1) then the result is one(1) otherwise Zero(0).**

** Bitwise exclusive Or(^) : **

In the bitwise Exclusive Operator,** If both the bits are different then the result is one(1) else the Result is zero(0).**

```
print(3&5)
print(3|5)
print(3^5)
```

To understand how Bitwise complement operator works in python you should aware of few important things listed below:

- The Most significant bit acts as the sign bit ex:0-->+Positive Number and 1--> Negative Number
- Positive Numbers will be represented directly in memory.
- Negative numbers will be represented in the 2's complement form.
- Changing the Zero's to one and One's to Zero creates 1's complement.
- And adding One(1) to 1's complement will result in 2's complement

Let us consider an example, find the `~4`

`find the complement of 4 or ~4`

Note: How the compliment ~4 is -5

The binary form of 4 is 100. as it is the Positive number and hence the first most significant bit acts as sign bit so, and we have to mention it in 32-bit format, 0000....0100

Next, do the compliment of above binary form, that convert 0's to One and 1's to Zero, 1111...1011

So, the above the first bit is 1 and hence acts as a Negative number

Then convert 0000...0100 to 2's complement, by adding 1 to it then it becomes 0000...0101, that means 5

so, the complement of** 4 is -5**

The binary form of 4 is 100. as it is the Positive number and hence the first most significant bit acts as sign bit so, and we have to mention it in 32-bit format, 0000....0100

Next, do the compliment of above binary form, that convert 0's to One and 1's to Zero, 1111...1011

So, the above the first bit is 1 and hence acts as a Negative number

Then convert 0000...0100 to 2's complement, by adding 1 to it then it becomes 0000...0101, that means 5

so, the complement of

Let us find the complement of negative value,` ~-4`

- As it is the negative number, the most significant number should be 1 and the remaining things will be represented in 2's complement
- First, know the binary value of normal 4, that is 000....0100
- Next, convert the normal four value into1's complement by flipping 0's to 1 and 1's to 0, 111...1011
- Then convert the above value into 2's complement by adding 1 to it, 111....1011+1 then the 2's complement become 111...11100
- So
**in memory, the negative values will be stored in 2's complement form**,`-4=111...11100`

- Now apply the complement to the negative value, that means 0's become 1's and 1's become 0,
`~-4=000...00011`

- The most significant value of ~-4 is 0 and hence the result will be a
**positive number**and the remaining bits**represents the value directly in the memory and convert the 000...00011 to decimal form then the result is 3**`print(~-4)`

The shift operator performs the bit manipulation on data by shifting the bits of its first operand by left or right.

In the bitwise left operator, the left operand value will be shifted to left by the number of bits specified with the right operand.

ex: `x<<2`

, here the `x`

operand bits will be shifted to the left side and the value will be specified by right operand that means by `2`

values. When the bits moved to the left-hand side than on right side vacant cells are formed and these vacant cells are filled with the zeroes.

```
x=10
print(10<<2)
```

In the above example, we have to left shift two bits of 10, so here the binary value of 10 is 0000...01010 now if we shift two bits to the left side, two vacant cells will be formed on right side 00...01010.., now add two zeroes to the right side, it becomes 00..0101000.

If we convert the 00...0101000 to decimal then the value becomes 40 and hence `10<<2=40`

In the bitwise left operator, the left operand **value will be shifted to the right by the number of bits specified with the right operand.**

Let us consider the same example `10>>2`

, here when we shift two bits to the right side, then on the left side two vacant cells will be formed and then the** vacant cells will be filled with sign bits.**

```
print(10>>2)
```

The binary value of 10 is 000....01010, now if shift two bits to the right-hand side then the value becomes 000...010 and on the left-hand side two vacant cells will be formed and these vacant cells will be filled with sign bits.

So here, 10 is already a positive number and significant bit will be 0 and remaining bits are stored directly in the memory and hence the value becomes 00000...010 and when we convert it to the decimal form the value becomes 2, so `10>>2=2`

Working with IDLE Python

Assignment Operators are those which are used to assign the values to the variables in python, Equal(=) is called as an assignment operator.

Ex: `a=5`

, the value 5 is assigned to the variable an on the left-hand side.

There are some compound assignment Operators also there in python which is represented as

ex: `x=10`

and `x+=20`

, that means` x=x+20`

and hence` x=10+20=30`

```
x=10
x+=20
print(x)
```

Every arithmetic operators can be combined with an assignment operator to form the compound operator.

- +=
- -=
- *=
- /=
- %=
- //=
- **=
- &=
- |=
- ^=
- <<=
- >>=

Let us consider an example `x=10`

and `x&=5 print(x)`

```
x=10
x&=5
print(x)
```

Next, consider `x=10`

and `x**=2`

then `print(x)`

```
x=10
x**=2
print(x)
```

Python does not support increment and decrement operators

```
x=10
x++
print(x)
x=10
x--
print(x)
```

Let us consider below examples, in that the `+`

acts just as a sign and not as an increment or decrement numbers.

```
x=10
print(+x)
print(++x)
print(+++x)
print(++++++++++++x)
```

And in the same way, if we use `-`

it will acts as a sign only

```
x=20
print(-20)
print(--20)
print(----20)
```

In the above example `--20`

results as `20`

that means `-(-20)=20`

.

Ternary Operator is nothing but the **conditional operator**, as it **reduces the code size and increases the readability of code**, this is because** it evaluates a statement with a condition being True or False.**

There is no particular keyword for the Ternary Operator and the syntax is as shown below:

**T=first value if condition else second value **

ex: `a=10`

and `b=20`

then** C=30 if a>b else 40**

```
a=10
b-20
c=30 if a>b else 40
print(c)
```

Let us consider another one example

```
#Read 2 int values from the keyboard and print min value
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
min= a if a<b else b
print("The minimum value:", min)
```

Next, we can use one ternary operator inside the other one, this we call it as **Nesting of Ternary Operator.**

**Let us understand this with an example **

```
#Read 3 int values from the keyboard and print the minimum value of three numbers:
a=int(input("Enter the first number:"))
b=int(input("Enter second number:"))
c=int(intput("Enter third number :"))
min=a if a<b and a<c else b if b<c else c
print("The minimum value:", min)
```

```
# Read 3 int values from the keyboard and print max value from the three numbers
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
c=int(input("Enter third number"))
max=a if a>b and a>c else b if b>c else c
print("The maximum value:", max)
```

Let us consider one more example

```
#There are two numbers if two numbers are equal then print
#Both numbers are Equal
#The first number is smaller than the second number
#The first number is greater than the second number
a=int(input("Enter the first number:"))
b=int(input("Enter the second number:"))
result="Equal" if a==b else "Smaller" if a<b else "Bigger"
print(result)
```

There are two types of special operators in python which are :

- Identity Operators
- Membership Operators

Let us understand these operators in detail:

Identity operator compares the address location of the two objects.

There are two Identity operators:

- Is Operator
- Is Not Operator

**R1 is R2** Returns True only when **two reference variables are pointing to the same object**.

And **R1 is not R2** Returns True only when **two reference variables are not pointing to the same object.**

As we know, object reusability concept is applicable in all the Datatypes except Complex Datatypes so let us consider below examples:

Example1: In this example, I am considering int Datatypes, as we know object reusability concept is applicable in int datatypes.

```
a=10
b=10
print(a is b)
print(a is not b)
```

Example2: In this, I am considering string Datatypes

```
a="ash"
b="ash"
print(a is b)
print(a is not b)
```