## What is Operators?

**Operators definition**: Operators in python is a symbol which is responsible to perform specific operation.

Python Operators are as follow

- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Special Operators

## Arithmetic Operator

**Python arithmetic operators**: Arithmetic Operators are used to perform any arithmetic calculations.

- The most common arithmetic Operators are + (Addition), – (Subtraction), * (Multiplication), / (Division), % (Modulus).
- In python, There are 2 special Arithmetic Operators: // (Division Operator) and ** (Exponent Operator)

**1. // (Division Operators)**

This Operator is also division operator then what is // (Division) operator?

// Operator is known as Floor Division Operator.

**Floor Division**: It takes nearest integer value

Floor Division can work on both float and int value.

If both values are int, then result will be int

If both values are float, then result will be float.

**Example**: – a, b = 10, 2

a // b => 5

**2. What is difference between / and // Operator**

Consider a=10.5 and b=2 then,

a / b => Division Operator always give floating point value i.e. a/b = 5.25

a // b => In Floor Division Operator, When operand is float and another one is int then result will be float by applying floor i.e. a // b = 5.0 - + Operator applicable for String type also. + Operator performs String concatenation operation.

**Example**: – ‘Technical’ + ‘Seek’ => TechnicalSeek - If you want to apply + operator for String type then both arguments (operands) must be of String type only.

**Example**: – ‘Technical’ + ‘Seek’ => TechnicalSeek

‘Technical’ + 101 => Gives an Error **To concatenate number or other type you can use str() (Type Casting)**

‘Technical’ + str(101) => Technical101

Or print number as string as

‘Technical’ + ‘101’ => Technical101**Similarly we can apply * Operator for String type, which is known as String Repetition Operator or Multiplication Operator.**

Example: – “abc” * 3 => abcabcabc**If you want to apply * operator to str type then second argument must be int type.**

Example:- ‘abc’ * ‘3’ => Gives an Error**For float type we cannot apply * operator.**

Example:- ‘abc’ * ‘3.0’ => Gives an Error**Argument order is not important.**

Example:- ‘abc’ * 3 => abcabca bcor 3 * ‘a’ => abcabcabc

## Relational Operator

**Python Relational operators**: Relational Operators are used to perform any Comparisons.- Relational Operators are < (Less than), <= (Less than equal to), > (Greater than), >=(Greater than equal to), (==)Equality.
**Example**: – a = 10 , b = 20

print(“a > b “,a>b) => False

print(“a > =b “,a>=b) => False

print(“a < b “,a<b) => True

print(“a <= b “,a<=b) => True**We can apply Relational Operator for str type.**

Example: – a= ‘abc’ and b=’xyz’

print(“a > b “,a>b) => False

While applying relation operator on string, comparison is performed based on alphabetical order.

(Ascii code: for a starts with 97 and A starts with 65)

Example: – a=”Tech” , b= ‘tech’

a>b => False**We can apply relational operator to Boolean type, internally it considered as 1 and 0 for Boolean value.**

Example: – a=True and b= False

a > b => True

a < b => False**Both arguments must be of same type.**

Example:- a=’True’ and b=True

a > b => Gives and Error**Chaining of Relation Operators.**

a) Chaining is applicable.

Example: – 10<20<30<40 => True

b) If all the conditions in chaining operation satisfied then, Result is true.

Example: – 10<20<30<40 => True

10 < 20 = True

20 < 30 = True

30 < 40 = True, So we get result as True

c) If at least one condition in chaining not satisfied then, result is False.

Example: – 10<20<30>40 => False

10 < 20 =True

20< 30 = True

30 > 40 = False, one condition is not satisfied so we get result False.**Equality Operators (==, !=)**

a) Equality Operators used to check equality of two operands.

Example: – 10 == 20 => False and 10 != 20 = True

b) == Equality always performs content comparisons.

10 == True => False and True == True => True

c) Equality also used or string comparison.

Example: – ‘Tech’ == ‘Tech’ => True

d) If we use incompatible type then it does not give an error instead it gives False as result.

Example: – ‘Tech == 10 => False

e) Chaining of Equality Operator.

If all the conditions in chaining are satisfied then result is True, Otherwise False.

Example: – 10 == 5+5 == 3+7 => True

f) Character type is not applicable.

Example: – ‘a’ == 97 =>False

g) Complex type conversion is also applicable.

Example: – (10+20j) == (10+20j) => True

h) Type casting done automatically.

Example: – 10 == 10.0 => True

Smaller type converted to large.

i) Both arguments must be of same type.

Example: – 10 == ’10’ => False

## Logical Operator

- Python Logical Operators: Logical Operators are used to perform any Logical Comparisons.
- Logical Operators AND, OR and NOT.
- You can apply logical operators for any type of data.
- For Non Boolean type :

0 means False

Non Zero means True

Empty string always treated as False **AND Operator :**

**and in python**: If both arguments are True, Then only result is True, Otherwise False.

Consider x and y

If x evaluates to False then result is x otherwise returns y.**Example**: – 10 and 20 => 20

0 and 20 => 0

1 and ‘Tech’ => ‘Tech’**OR Operator :**

**or in python**: If at least one argument is True, Then result is True, Otherwise False.

Consider x or y

If x evaluates to True then result is x otherwise returns y.**Example**: – 10 or 20 => 10

0 or 20 => 20

0 or 0 => 0**Not Operator :**Not in python is to make reverse.**Example**: – Not 10 => False

Not ‘ ‘ => True

Not 0 => True

## Bitwise Operator

**Python Bitwise Operators:**Bitwise Operators are used to perform bitwise comparisons.- Bitwise Operators &, |, !, ^, ~, <<, >>.
**& (Bitwise AND Operator) :**

If both bits are 1 then only result is 1 otherwise 0.

**Example**: – 4 & 5 => 4

Every bit is checked as 100 & 101 = 100 => 4**| (Bitwise OR Operator) :**

If at least one bit is 1 then result is 1 otherwise 0.

Example: – 4 | 5 => 5

Every bit is checked as 100 & 101 = 101 => 5**^ (Bitwise X-OR Operator) :**

If both arguments are different then result is 1 otherwise 0.

Example: – 4 ^ 5 => 1

Every bit is checked as 100 & 101 = 001 => 1**~ (Bitwise Complement) :**

It performs inverse. It calculates 2’s complement.

1 => 0 and 0 => 1

Example: – ~4 => -5

**How to find bitwise complement?**

=> ~4

=> Binary number for 4 = 100

=> 16 bit for 4 (Because in memory it is stored in 16 bit) = 0000 0000 0000 0100

=> Find 1’s Complement i.e. ~4 = 1111 1111 1111 1011

=> Find 2’s Complement i.e.

~4 + 1 = ~ (1111 1111 1111 1011 ) + 1

= (0000 0000 0000 0100) + 1

= 0000 0000 0000 0101

=> For positive and negative sign check first bit of 16 bit number ( if sign bit is 0 then positive number else negative number)

= 0000 0000 0000 0101

= -5**Shift Operators :**

There are two Type of shift operators in python left shift and right shift operator.

**a) << (Left shift operator) :**

This operator shifts bits to left and fills right empty bits with 0.

**Left shift Example: –****b) >> (Right shift Operator) :**

This operator shifts the bits to right and empty bit are filled with sign bits ( 0 for positive number and 1 for negative number)

**Right shift Example: –**

## Assignment Operator

**Python Assignment Operators**are used to assign value to variable.**Assignment Operator**Example: – x= 10- In case you have multiple variable with different value in other languages you have to write multiple line for variable, but in python you can write in one line as follows

Example: – a, b, c, d = 10, 20, 20, 30

In this case python assigns a=10, b =20, c=20, d=30 - No default value assignment in python.

Example: – a, b, c= 10, 20 => Gives an Error **Compound Assignment Operator**: – Assignment operator combined with some other operator then it is compound assignment operator.

Example: – x += 10 => x= x + 10 => 110**Various compound assignment are as follows:**

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

**Example**: – a=4

a += 4 => 8

## Special Operator

- There are two special operators in python which are as follows:

Identity Operators and Membership Operator **Identity Operators: –**

Identity Operators used for address purpose.

To know address of the object we use the identity operators.

Identity Operators are ‘is’ and ‘is not’ Operator.

**1. ‘is’ Operator:**– This operator used to check object references pointing to same object.

If both reference pointing to same object then it returns True else False.

**Syntax for ‘is’ Operator:-**

First_reference is second _reference

Example:-We can see that a and b contents are same so only one object created for both object a and b and both points to same object.

**How to know both references pointing to same object?**

=> We know this by address of object. is operator used to check both reference pointing to same object.

Example:- a is b => Returns True

**2. ‘is not’ Operator:**– To know two references pointing to different object we use is not operator.

If both reference pointing to different object then it returns True else False.

Syntax for ‘is’ not Operator:-

First_reference is not second _reference

Example: –In this case, two different object are created for a and b.

**How to know both references pointing to different objects?**

=> We know this by address of objects. is not operator used to check references pointing to different object.

Example: – a is not b => True

Identity operator used for address comparison not content comparison.

Example: –

List1 = [10, 20, 30]

List2 = [10, 20, 30]

Lists are mutable that’s why two different objects created for List1 and List2.

So, List1 is List2 => False

For Content comparison we use == Operator.**Membership Operator:-**

To know Membership that is to know whether given object is member of List or not.

Membership Operators are in and not in Operators.

**1. ‘in’ Operator:**– To know object is member of List.

If object is present in list then it returns True else False.

Syntax: – object in List

Example : –

List1 = [10, 20, 30]

10 in List1 => True

40 in List1 => False

**2. ‘not in’ Operator:**-To know object is not a member of List.

If object is not present in list then it returns True else False.

Syntax: – object not in List

Example : –

List1 = [10, 20, 30]

10 not in List1 => False

40 not in List1 => True

You can apply Membership Operator anywhere.

Example:-

s = “hi Welcome to Python”

P in s => True

## Operator Precedence

**What is Operator Precedence? **

**Python operator precedence :** Which Operator is evaluated is decided by Operator Precedence.

**Example**: –

10+2*3 => 16

(10+2)*3 => 36

**Operator Precedence in python :**

1. Parenthesis ()

2. Unary Operators: **, ~, –

3. Binary Operators: *, /, %, //, + , -, <<, >> , &, ^, |, < , >=, <, <=, ==, !=, =, +=, -=, *=, is, is not, in, not in, and , or, not.

4. Ternary Operator.

## Ternary Operator

**Python ternary operator :**Ternary Operator has 3 arguments that is why it is called as Ternary Operator.**Ternary Operator syntax in python :-**

Var= (First value) if Condition else second value

If condition is true then first value is assigned to var else second value**Python ternary operator Example:-**

x = (30) if (10 < 20 ) else 40

print(x) => 30**Nested ternary condition:-**

Syntax :-

Var= (First value) if Condition1 else second value if condition2 else third value

Example:-

x = 10 if 20 < 30 else 40 if 50 < 60 else 70

print(x) => 10

## Increment and Decrement Operator

- In Other language we have unary increment and decrement operator as ++x or x++.
- But in python, Increment and decrement operators are not available.
**Example**:- x = 10

print(++x) => 10

In python , this statement does not shows any error because it treated as sign operator as

++x => +(+x) = +10

In case x++ python gives error as we cannot write sign at end.

## Leave a Reply