We are going to cover Python Operators with Examples , python Arithmetic Operators, python Logical operators, python Comparison operators, python Assignment operators, python Bitwise operators, python Identity operators, python Membership operators

**What is Operator in Python ?**

Operators are special symbols used to perform operations. The value that the operator operates on is called the operand.

For Example, 2+5=7 here, 2 and 5 are called **operands** and + is an operator that performs addition.

**Python Operators with Examples**

**#1:** **Python Arithmetic Operators:**

Arithmetic Operators are used to perform mathematical calculations on numeric data. And perform operations like addition, subtraction, multiplication and division.

Operator | Description | Syntax |

+ | Addition : add two operands | a+b |

– | Subtraction : subtracts two operands | a-b |

* | Multiplication: multiplies two operands | a*b |

/ | Division: divides the first operand by the second | a/b |

// | Floor division: divides the first operand by the second | a//b |

% | Modulus : divides first operand by second operand and returns remainder | a%b |

** | Exponent(power): returns first raised to power second | a**b |

**Examples of Arithmetic Operators**

```
a = 7
b = 2
#addition
add = a + b
#subtraction
sub = a – b
#multiplication
mul = a * b
#division
div = a / b
#Floor division
div_floor = a // b
#Modulus
mod = a % b
# Exponent(power)
power = a ** b
#print
print(add)
print(sub)
print(mul)
print(div)
print(div_floor)
print(mod)
print(power)
```

**#2: Python Logical Operators:**

Logical operators allow a program to make a decision based on multiple conditions. Each operand is considered a condition that can be evaluated to a true or false value. Then the value of the conditions is used to determine the overall value of the operand1 operator operand2. Logical operators perform **Logical AND, Logical OR **and** Logical NOT **operations**.**

Operator | Description | Syntax |

and | Logical AND: If both the operands are true then result become true. If anyone operand is false then result become false | a and b |

or | Logical OR: If one operand true or another is false then result become true | a or b |

not | Logical NOT: If both operands are true then the result becomes true. otherwise anyone operand is false then the result becomes false. | not a |

**Examples of python Logical Operators**

```
a = True
b = False
# and
print(a and b)
# or
print(a or b)
# not
print(not a)
output:
False
True
False
```

**#3: Python Comparison Operators:**

In python Comparison Operators are also called as relational operators in python. Comparison operators compare the value.

It returns True or False according to the condition.

In python there are different types of comparison operators like less than, greater than, equal to and not equal to.

Operator | Description | Syntax |

> | Greater than:Result becomes True when left operand is greater than the right. | a > b |

< | Less than: If the left operand is less than right then the result becomes True. | a < b |

== | Equal to: If both operands are equal then the result becomes True. | a == b |

>= | Greater than or equal to: If left operands is greater than or equal to the right then the result becomes True. | a >= b |

<= | Less than or equal to: If left operands is less than or equal to the right then the result becomes True. | a <= b |

!= | Not equal to: If both operands are not equal then result become True | x !=y |

**Examples of python comparison operators**

```
a = 12
b = 10
# it returns True because 12 is greater than 10
print(a > b)
# it returns False because 12 is not less than 10
print(a < b)
# it returns False because 12 is not equal to 10
print(a == b)
# it returns True because 12 is not equal to 10
print(a != b)
# it returns True because 12 is greater than or equal to 10
print(a >= b)
# it returns False because 12 is neither greater than or equal to 10
print(a <= b)
```

**#4: Python Assignment Operators:**

In python, assignment operators are used to assign value to variables.

Operator | Description | Syntax |

= | Assign value on the left side of the expression. | a = b |

+= | Add AND: Add right operand with left operand and assign Result to left side operand | a + = ba = a + b |

-= | Subtract AND :Subtract right operand from left operand and assign result to the left operand | a – = ba = a – b |

*= | Multiply AND: Multiply right operand with left operand and then assign result to the left operand | a * = ba = a * b |

/= | Divide AND: Divide left operand with right operand and then assign result to the left operand | a / = ba = a / b |

//= | Divide floor AND: Divide left operand with right operand and then assign result to the left operand | a // = ba = a //b |

%= | Modulus AND: Using left and right operands and assign result to left operand | a % = ba = a % b |

**= | Exponent AND: Calculate exponent value using operands and assign result to left operand | a ** = ba = a ** b |

>>= | Performs Bitwise right shift on operands and assign result to left operand | a >> = ba = a >> b |

<<= | Performs Bitwise left shift on operands and assign result to left operand | a << = ba = a <<b |

&= | Performs Bitwise AND on operands and assign value to left operand | a & = ba = a & b |

**Examples of python assignment operators**

```
a = 7
print(a)
#Add AND
a + = 7
print(a)
#Sub AND
a - = 7
print(a)
#Mul AND
a * = 7
print(a)
#Div AND
a / = 7
print(a)
#Div Floor AND
a // = 7
print(a)
#Mod AND
a % = 7
print(a)
#Exponent AND
a ** = 7
print(a)
#Bit Right
a >> = 7
print(a)
#Bit Left
a << =7
print(a)
#Bit AND
a & = 7
print(a)
```

**#5: Python Bitwise Operators:**

In Bitwise operator operations are done in bit-level. In this operators are used to compare numbers

operator | Description | Syntax |

& | Bitwise AND: Bitwise AND result is 1 if the bits of two operands is 1. If either bit of operand is 0 then result become 0 | a & y |

| | Bitwise OR: If anyone bit operand is one the resilt become 1 | a | b |

^ | Bitwise XOR: If bit operands are opposite then result become 1. | a ^ b |

~ | Bitwise NOT: It changes 1 to 0 and 0 to 1 | ~a |

>> | Bitwise right shift: Towards right | a >> |

<< | Bitwise left shift: Towards left | a << |

**Examples of python Bitwise Operators**

```
a = 12
b = 25
print(a & b)
print(a|b)
print(a ^ b)
print(~ a)
print(a>>4)
print(a<<2)
```

**#6: Python Identity Operators:**

Identity Operator is used to compare the object memory location. It checks if two values are located in the same part of memory.

If two values are the same that does not mean they are identical.

Operator | Description | Syntax |

is | If both operands are the same object then result become True | a is b |

Is not | If both operands are not the same object then result become True | a is not b |

**Examples of Python Identity Operators**

```
x = 'FOSSTechNix'
y = 'FOSSTechNix'
x1 = ["a","b","c"]
y1 = ["a","b","c"]
# True because y is same as x
print(x is y)
# False because y and x same
print(x is not y)
# True because x and y is equal
print(x == y)
# False because x and y is equal
print(x != y)
```

**#7: Python Membership Operators**:

Membership Operators are used to check value is in sequence

Operator | Description | Syntax |

in | If the value is in sequence the result become true | a in y |

not in | If the value is not in sequence the result become true | a not in b |

**Examples of Python Membership Operators**

```
x = ["a","b","c"]
y = "FOSSTechNix"
# in
print("FOSS" in y)
print("FOSS" in x)
# not in
print("d" not in x)
print("a" not in x)
```

We have covered Python operators with Examples.

**Conclusion:**

We have covered Python operators with Examples , python Arithmetic Operators, python Logical operators, python Comparison operators, python Assignment operators, python Bitwise operators, python Identity operators, python Membership operators

**Related Articles:**

Python Introduction for Programmers [Part 1]

Python Conditional Statements with Examples

Functions in Python with Examples

classes and objects in python with examples

**Reference:**

Nice info dear

thanks.

Useful & Nice Information

Very good

Interesting

thanks

Good jobs dear

Good jobs dear