In this module tutorial, we will learn Python Operators. Itβs a basic idea of Python that's needed for understanding the workflow of a Python program.

## Python Operators

Operators commonly are used to performing operations on both values and variables in Python. These are standard symbols used for the aim of logical and arithmetic operations. In this post, we will look into various types of Python operators.

- Arithmetic operators
- Relational operators
- Logical operators
- Bitwise operators
- Assignment operators
- Membership Operators

### Arithmetic operators

These operators are used to performing mathematical functions such as addition, multiplication, division, subtraction and much more.

Operator | Description | Syntax |

+ | Addition: It adds two operands | x + y |

- | Subtraction: It subtracts two operands | x - y |

* | Multiplication: It multiplies two operands | x * y |

/ | Division (float): It divides the first operand by the second | x / y |

// | Division (floor): It divides the first operand by the second | x // y |

% | Modulus: It returns the remainder when the first operand is divided by the second | x % y |

** | Power: It Returns first raised to power second | x ** y |

```
# Examples of Arithmetic Operator
a = 90
b = 40
# Addition of numbers
add = a + b
# Subtraction of numbers
sub = a - b
# Multiplication of number
mul = a * b
# Division(float) of number
div1 = a / b
# Division(floor) of number
div2 = a // b
# Modulo of both number
mod = a % b
# Power
p = a ** b
# print results
print(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
print(p)
Output:
130
50
3600
2.25
2
1
1.4780E + 78
```

### Relational Operators:

A relational operator compares the values. It either returns True or False based on the conditions.

Operator | Description | Syntax |

> | Greater than: It gives True if the left operand is greater than the right | x > y |

< | Less than: It gives True if the left operand is less than the right | x < y |

== | Equal to: It gives True if both operands are equal | x == y |

!= | Not equal to: It gives True if operands are not equal | x != y |

>= | Greater than or equal to : It gives True if the left operand is greater than or equal to the right | x >= y |

<= | Less than or equal to : It gives True if the left operand is less than or equal to the right | x <= y |

```
# Examples of Relational Operators
a = 1
b = 3
# a > b is False
print(a > b)
# a < b is True
print(a < b)
# a == b is False
print(a == b)
# a != b is True
print(a != b)
# a >= b is False
print(a >= b)
# a <= b is True
print(a <= b)
Output:
False
True
False
True
False
True
```

### Logical operators:

Logical operators perform Logical AND, Logical OR and Logical NOT operations.

Operator | Description | Syntax |

And | Logical AND: It gives True if both the operands are true | x and y |

Or | Logical OR: It gives True if either of the operands is true | x or y |

Not | Logical NOT: It gives True if the operand is false | not x |

```
# Examples of Logical Operator
a = True
b = False
# Print a and b is False
print(a and b)
# Print a or b is True
print(a or b)
# Print, not a is False
print(not a)
Output:
False
True
False
```

### Bitwise operators:

Bitwise operators act on bits and perform bit by bit operations.

Operator | Description | Syntax |

& | Bitwise AND | x & y |

| | Bitwise OR | x | y |

~ | Bitwise NOT | ~x |

^ | Bitwise XOR | x ^ y |

>> | Bitwise right shift | x>> |

<< | Bitwise left shift | x<< |

```
# Examples of Bitwise operators
a = 10
b = 4
# Print bitwise AND operation
print(a & b)
# Print bitwise OR operation
print(a | b)
# Print bitwise NOT operation
print(~a)
# print bitwise XOR operation
print(a ^ b)
# print bitwise right shift operation
print(a >> 2)
# print bitwise left shift operation
print(a << 2)
Output:
0
14
-11
14
2
40
```

### Assignment operators:

Assignment operators are used to assigning values to the variables.

Operator | Description | Syntax |

= | Assign the value of the right side of expression to the left side operand | x = y + z |

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

-= | Subtract AND: Subtract right operand from left operand and then assign to left operand | a-=b a=a-b |

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

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

%= | Modulus AND: Takes modulus using left and right operands and assign the result to left operand | a%=b a=a%b |

//= | Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operand | a//=b a=a//b |

**= | Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operand | a**=b a=a**b |

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

|= | Performs Bitwise OR on operands and assign value to left operand | a|=b a=a|b |

^= | Performs Bitwise XOR on operands and assign value to left operand | a^=b a=a^b |

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

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

#### Learn full tutorial-

I hope this module can be useful for you to understand Operators in Python. You should try to use all the Operators and functionality to get a better understanding. Stay in touch with us to find out more modules that are taught like this. Keep rehearsing it!

Original posted on - Python Operators

## Top comments (0)