Python Operators Tutorial

Introduction

Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand. Python provides a variety of operators, which can be categorized into different types based on their functionality.

Table of Contents

  1. Arithmetic Operators
  2. Comparison Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Membership Operators
  7. Identity Operators
  8. Conclusion

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.

Table of Arithmetic Operators

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
** Exponentiation
// Floor Division

Examples

# Addition
print(5 + 3)  # Output: 8

# Subtraction
print(5 - 3)  # Output: 2

# Multiplication
print(5 * 3)  # Output: 15

# Division
print(5 / 2)  # Output: 2.5

# Modulus
print(5 % 2)  # Output: 1

# Exponentiation
print(5 ** 2)  # Output: 25

# Floor Division
print(5 // 2)  # Output: 2

2. Comparison Operators

Comparison operators are used to compare two values. They return a boolean value, either True or False.

Table of Comparison Operators

Operator Description
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Examples

# Equal to
print(5 == 3)  # Output: False

# Not equal to
print(5 != 3)  # Output: True

# Greater than
print(5 > 3)  # Output: True

# Less than
print(5 < 3)  # Output: False

# Greater than or equal to
print(5 >= 3)  # Output: True

# Less than or equal to
print(5 <= 3)  # Output: False

3. Logical Operators

Logical operators are used to combine conditional statements.

Table of Logical Operators

Operator Description
and Logical AND
or Logical OR
not Logical NOT

Examples

# Logical AND
print(True and False)  # Output: False

# Logical OR
print(True or False)  # Output: True

# Logical NOT
print(not True)  # Output: False

4. Bitwise Operators

Bitwise operators are used to perform bit-level operations on binary numbers.

Table of Bitwise Operators

Operator Description
& AND
| OR
^ XOR
~ NOT
<< Left Shift
>> Right Shift

Examples

# Bitwise AND
print(5 & 3)  # Output: 1

# Bitwise OR
print(5 | 3)  # Output: 7

# Bitwise XOR
print(5 ^ 3)  # Output: 6

# Bitwise NOT
print(~5)  # Output: -6

# Left Shift
print(5 << 1)  # Output: 10

# Right Shift
print(5 >> 1)  # Output: 2

5. Assignment Operators

Assignment operators are used to assign values to variables.

Table of Assignment Operators

Operator Description
= Assign
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign
%= Modulus and assign
**= Exponentiate and assign
//= Floor divide and assign
&= Bitwise AND and assign
|= Bitwise OR and assign
^= Bitwise XOR and assign
<<= Left shift and assign
>>= Right shift and assign

Examples

# Assign
a = 5
print(a)  # Output: 5

# Add and assign
a += 5
print(a)  # Output: 10

# Subtract and assign
a -= 3
print(a)  # Output: 7

# Multiply and assign
a *= 2
print(a)  # Output: 14

# Divide and assign
a /= 2
print(a)  # Output: 7.0

# Modulus and assign
a %= 3
print(a)  # Output: 1.0

# Exponentiate and assign
a **= 3
print(a)  # Output: 1.0

# Floor divide and assign
a = 10
a //= 3
print(a)  # Output: 3

# Bitwise AND and assign
a &= 2
print(a)  # Output: 2

# Bitwise OR and assign
a |= 3
print(a)  # Output: 3

# Bitwise XOR and assign
a ^= 1
print(a)  # Output: 2

# Left shift and assign
a <<= 2
print(a)  # Output: 8

# Right shift and assign
a >>= 1
print(a)  # Output: 4

6. Membership Operators

Membership operators are used to test if a sequence contains a specific item.

Table of Membership Operators

Operator Description
in Returns True if sequence contains the specified item

4, 5]|True | | not in | Returns True if sequence does not contain the specified item |6 not in [1, 2, 3, 4, 5]|True` |

Examples

# in
print(5 in [1, 2, 3, 4, 5])  # Output: True

# not in
print(6 not in [1, 2, 3, 4, 5])  # Output: True

7. Identity Operators

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location.

Table of Identity Operators

Operator Description
is Returns True if both variables are the same object
is not Returns True if both variables are not the same object

Examples

a = [1, 2, 3]
b = a
c = [1, 2, 3]

# is
print(a is b)  # Output: True
print(a is c)  # Output: False

# is not
print(a is not c)  # Output: True

8. Conclusion

Python operators are essential tools that allow you to perform a variety of operations on variables and values. Understanding how to use each type of operator—arithmetic, comparison, logical, bitwise, assignment, membership, and identity—will enable you to write more efficient and effective Python code. This tutorial provided an overview of each operator category along with examples to demonstrate their use.

Comments