## What are the Operators?

**Operators definition**: Operators in python is a symbol which is responsible to perform a 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 performing 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 a division operator then what is // (Division) operator?

// Operator is known as Floor Division Operator.

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

Floor Division can work on both float and int value.

If both values are int, the result will be int

If both values are a float, the result will be a 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 the 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 the relation operator on the string, a comparison is performed based on alphabetical order.

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

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

a>b => False**We can apply the 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 an 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 the 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 performing 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 the only result is True, Otherwise False.

Consider x and y

If x evaluates to False then the 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 the result is True, Otherwise False.

Consider x or y

If x evaluates to True then the 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 a reverse.**Example**: – Not 10 => False

Not ‘ ‘ => True

Not 0 => True

## Bitwise Operator

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

If both bits are 1 then the 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

We get 0000 0000 0000 0101

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

= 0000 0000 0000 0101

Final result = -5**Shift Operators :**

There is 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 the right and empty bit are filled with sign bits ( 0 for a positive number and 1 for negative number)

**Right shift Example: –**

## Assignment Operator

**Python Assignment Operators**are used to assign the value to a variable.**Assignment Operator**Example: – x= 10- In case you have multiple variables with a different value in other languages you have to write multiple lines for a 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 the 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: –**

This Operators used for address purpose.

To know the 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 the same object.

If both reference pointing to the 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 the same so only one object created for both object a and b and both points to the same object.

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

=> We know this by an address of an object. is the operator used to check both references pointing to the same object?

Example:- a is b => Returns True

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

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

The syntax for ‘is’ not Operator:-

First_reference is not second _reference

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

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

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

Example: – a is not b => True

Identity operator used for address comparison, not the 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 a given object is the member of List or not.

Membership Operators are in and not in Operators.

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

If the object is present in the 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 an object is not present in the 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