Python Operators Explained with Examples

Operators in Python are used to perform operations on variables and values. They can be categorized into several types based on their functionality.

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
// Floor Division a // b
% Modulus (Remainder) a % b
** Exponentiation a ** b

Example:

a = 10
b = 5

print(a + b)  # 15
print(a - b)  # 5
print(a * b)  # 50
print(a / b)  # 2.0
print(a // b)  # 2
print(a % b)  # 0
print(a ** b)  # 100000

Comparison Operators

Comparison operators are used to compare two values and return a boolean result.

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

Example:

a = 10
b = 5

print(a == b)  # False
print(a != b)  # True
print(a > b)  # True
print(a < b)  # False
print(a >= b)  # True
print(a <= b)  # False

Logical Operators

Logical operators are used to combine boolean expressions.

Operator Description Example
and Logical AND a and b
or Logical OR a or b
not Logical NOT not a

Example:

a = True
b = False

print(a and b)  # False
print(a or b)  # True
print(not a)  # False

Assignment Operators

Assignment operators are used to assign values to variables.

Operator Description Example
= Simple assignment a = b
+= Add and assign a += b
-= Subtract and assign a -= b
*= Multiply and assign a *= b
/= Divide and assign a /= b
//= Floor divide and assign a //= b
%= Modulus and assign a %= b
**= Exponent and assign a **= b

Example:

a = 5
b = 3

a += b  # Equivalent to a = a + b
print(a)  # 8

a -= b  # Equivalent to a = a - b
print(a)  # 5

a *= b  # Equivalent to a = a * b
print(a)  # 15

a /= b  # Equivalent to a = a / b
print(a)  # 5.0

a //= b  # Equivalent to a = a // b
print(a)  # 5

a %= b  # Equivalent to a = a % b
print(a)  # 0

a **= b  # Equivalent to a = a ** b
print(a)  # 1

Bitwise Operators

Bitwise operators are used to perform operations on binary numbers.

Operator Description Example
& Bitwise AND a & b
| Bitwise OR a | b
^ Bitwise XOR a ^ b
~ Bitwise NOT ~a
<< Left shift a << b
>> Right shift a >> b

Example:

a = 5  # Binary: 0101
b = 3  # Binary: 0011

print(a & b)  # 1  # Binary: 0001
print(a | b)  # 7  # Binary: 0111
print(a ^ b)  # 6  # Binary: 0110
print(~a)  # -6  # Two's complement
print(a << b)  # 40  # Binary: 101000
print(a >> b)  # 0  # Binary: 0000

Membership Operators

Membership operators are used to test whether a value or variable is a member of a sequence (such as a list, tuple, or string).

Operator Description Example
in Membership test a in b
not in Non-membership test a not in b

Example:

a = [1, 2, 3, 4, 5]
b = 3

print(b in a)  # True
print(b not in a)  # False

Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operator Description Example
>is Identity test a is b
is not Non-identity test a is not b

Example:

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

print(a is b)  # True
print(a is not b)  # False

Understanding and using these operators effectively can greatly enhance your Python programming skills.