(toc)

Operators are special symbols in Python that perform specific operations on variables and values. They are used to manipulate data and carry out computations. Python provides a variety of operators for arithmetic, comparison, logical operations, and more.

we’ll cover:

  • What operators are.
  • Types of Python operators.
  • Examples to demonstrate their use.

What are Operators?

Operators are symbols or keywords that perform operations on one or more operands (values or variables). For example, the + operator adds two numbers, while == checks if two values are equal.


Types of Python Operators

Python supports the following types of operators:

  1. Arithmetic Operators
  2. Comparison (Relational) Operators
  3. Logical Operators
  4. Assignment Operators
  5. Bitwise Operators
  6. Membership Operators
  7. Identity Operators


1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

Operator Description Example
+ Addition 3 + 25
- Subtraction 5 - 32
* Multiplication 4 * 28
/ Division 10 / 25.0
% Modulus (remainder) 7 % 31
** Exponentiation (power) 2 ** 38
// Floor division (quotient) 7 // 23

Example:


a = 10 b = 3 print(a + b) # Output: 13 print(a - b) # Output: 7 print(a * b) # Output: 30 print(a / b) # Output: 3.3333333333333335 print(a % b) # Output: 1 print(a ** b) # Output: 1000 print(a // b) # Output: 3


2. Comparison (Relational) Operators

Comparison operators are used to compare two values. They return a Boolean result (True or False).

Operator Description Example
== Equal to 5 == 5True
!= Not equal to 5 != 3True
> Greater than 5 > 3True
< Less than 3 < 5True
>= Greater than or equal to 5 >= 5True
<= Less than or equal to 3 <= 5True

Example:


x = 10 y = 5 print(x == y) # Output: False print(x > y) # Output: True print(x <= y) # Output: False



3. Logical Operators

Logical operators are used to combine multiple Boolean expressions.

Operator Description Example
and Returns True if both are True True and FalseFalse
or Returns True if one is True True or FalseTrue
not Reverses the Boolean value not TrueFalse

Example:


x = 10 y = 5 print(x > y and x > 0) # Output: True print(x > y or x < 0) # Output: True print(not (x > y)) # Output: False



4. Assignment Operators

Assignment operators are used to assign values to variables, often with arithmetic operations.

Operator Description Example
= Assign x = 10
+= Add and assign x += 5x = x + 5
-= Subtract and assign x -= 5x = x - 5
*= Multiply and assign x *= 2x = x * 2
/= Divide and assign x /= 2x = x / 2
%= Modulus and assign x %= 3x = x % 3
//= Floor division and assign x //= 2x = x // 2
**= Exponentiation and assign x **= 3x = x ** 3

Example:


x = 10 x += 5 # Equivalent to x = x + 5 print(x) # Output: 15



5. Bitwise Operators

Bitwise operators perform operations on binary representations of integers.

Operator Description Example
& AND 5 & 31
` ` OR
^ XOR 5 ^ 36
~ NOT ~5-6
<< Left shift 5 << 110
>> Right shift 5 >> 12

Example:


x = 5 # Binary: 0101 y = 3 # Binary: 0011 print(x & y) # Output: 1 (Binary: 0001) print(x | y) # Output: 7 (Binary: 0111) print(x ^ y) # Output: 6 (Binary: 0110)



6. Membership Operators

Membership operators are used to check if a value is part of a sequence (like a list, tuple, or string).

Operator Description Example
in Returns True if the value is found "a" in "apple"True
not in Returns True if the value is not found "x" not in "apple"True

Example:


fruits = ["apple", "banana", "cherry"] print("apple" in fruits) # Output: True print("grape" not in fruits) # Output: True



7. Identity Operators

Identity operators compare the memory locations of two objects.

Operator Description Example
is Returns True if they are the same object x is y
is not Returns True if they are not the same object x is not y

Example:


x = [1, 2, 3] y = x z = [1, 2, 3] print(x is y) # Output: True (same object) print(x is z) # Output: False (different objects) print(x == z) # Output: True (same content)

Leave a Reply