Python Operators

Introduction

Operators in Python are special symbols that perform operations on variables and values. Python supports a variety of operators, categorized as follows:

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

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.

Operator Name Example
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponentiation x ** y
// Floor Division x // y

Examples:

x = 10
y = 3

print(x + y)  # Output: 13
print(x - y)  # Output: 7
print(x * y)  # Output: 30
print(x / y)  # Output: 3.3333333333333335
print(x % y)  # Output: 1
print(x ** y) # Output: 1000
print(x // y) # Output: 3

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operator Example Equivalent to
= x = y x = y
+= x += y x = x + y
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x / y
%= x %= y x = x % y
**= x **= y x = x ** y
//= x //= y x = x // y

Examples:

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

x *= 2  # Equivalent to x = x * 2
print(x)  # Output: 30

3. Comparison Operators

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

Operator Name Example
== Equal to x == y
!= Not equal to x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

Examples:

x = 10
y = 5

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

4. Logical Operators

Logical operators are used to combine conditional statements.

Operator Name Example
and Logical AND x and y
or Logical OR x or y
not Logical NOT not x

Examples:

x = True
y = False

print(x and y)  # Output: False
print(x or y)   # Output: True
print(not x)    # Output: False

5. Bitwise Operators

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

Operator Name Example
& AND x & y
` ` OR
^ XOR x ^ y
~ NOT ~x
<< Left Shift x << y
>> Right Shift x >> y

Examples:

x = 10  # In binary: 1010
y = 4   # In binary: 0100

print(x & y)  # Output: 0 (binary: 0000)
print(x | y)  # Output: 14 (binary: 1110)
print(x ^ y)  # Output: 14 (binary: 1110)
print(~x)     # Output: -11 (binary: ...11110101, inverting all bits)
print(x << 2) # Output: 40 (binary: 101000)
print(x >> 2) # Output: 2 (binary: 0010)

6. Membership Operators

Membership operators are used to test if a sequence (like a string, list, or tuple) contains a specified value.

Operator Name Example
in In x in y
not in Not In x not in y

Examples:

x = "Hello"
y = [1, 2, 3, 4, 5]

print("H" in x)     # Output: True
print("h" in x)     # Output: False
print(3 in y)       # Output: True
print(6 not in y)   # Output: True

7. Identity Operators

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

Operator Name Example
is Is x is y
is not Is Not x is not y

Examples:

x = ["apple", "banana"]
y = ["apple", "banana"]
z = x

print(x is z)        # Output: True (z is the same object as x)
print(x is y)        # Output: False (x and y are different objects with the same content)
print(x == y)        # Output: True (x and y have the same content)
print(x is not y)    # Output: True (x and y are different objects)

Conclusion

Understanding Python operators is fundamental for performing various operations on data. This guide covers the different categories of operators, including arithmetic, assignment, comparison, logical, bitwise, membership, and identity operators. Mastering these operators will enable you to write more complex and efficient Python code.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top