Python Operators: From Basics to Advanced
Python offers a wide range of operators, allowing you to easily perform various operations during the programming process. In this article, we will explain Python operators and how to use them with example code blocks.
Table of Contents
- Assignment Operators
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
1. Assignment Operators
Assignment operators are used to assign a value to a variable.
# Simple assignment operator
x = 10
print(x) # Output: 10
# Other assignment operators
x += 5 # x = x + 5
print(x) # Output: 15
x -= 3 # x = x - 3
print(x) # Output: 12
x *= 2 # x = x * 2
print(x) # Output: 24
x /= 4 # x = x / 4
print(x) # Output: 6.0
2. Arithmetic Operators
Arithmetic operators are used to perform numerical operations.
= 15
b = 4
print(a + b) # Addition: Output: 19
print(a - b) # Subtraction: Output: 11
print(a * b) # Multiplication: Output: 60
print(a / b) # Division: Output: 3.75
print(a % b) # Modulo: Output: 3
print(a ** b) # Exponentiation: Output: 50625
print(a // b) # Floor division: Output: 3
3. Comparison Operators
Comparison operators are used to compare two values and return boolean (True or False) results.
x = 10
y = 20
print(x == y) # Equal to: Output: False
print(x != y) # Not equal to: Output: True
print(x > y) # Greater than: Output: False
print(x < y) # Less than: Output: True
print(x >= y) # Greater than or equal to: Output: False
print(x <= y) # Less than or equal to: Output: True
4. Logical Operators
Logical operators are used to evaluate boolean expressions.
x = True
y = False
print(x and y) # And: Output: False
print(x or y) # Or: Output: True
print(not x) # Not: Output: False
5. Bitwise Operators
Bitwise operators operate on numbers at the bit level.
a= 10 # 1010 in binary
b = 4 # 0100 in binary
print(a & b) # And: Output: 0 (0000)
print(a | b) # Or: Output: 14 (1110)
print(a ^ b) # Xor: Output: 14 (1110)
print(~a) # Not: Output: -11 (in binary: -1011)
print(a << 2) # Left shift: Output: 40 (101000)
print(a >> 2) # Right shift: Output: 2 (0010)
6. Membership Operators
Membership operators are used to check if a value is present in a sequence (list, tuple, set, etc.).
list = [1, 2, 3, 4, 5]
print(3 in list) # Membership: Output: True
print(6 not in list) # Not membership: Output: True
7. Identity Operators
Identity operators are used to check if two variables point to the same object.
a= 10
b = 10
c = a
print(a is b) # Same object: Output: True
print(a is not c) # Not the same object: Output: False
list1 = [1, 2, 3]
list2 = list1
list3 = list1.copy()
print(list1 is list2) # Same object: Output: True
print(list1 is list3) # Same object: Output: False
print(list1 == list3) # Same values: Output: True
Conclusion
Python operators offer a wide range of tools to perform various types of operations. In this article, we have explained the basic assignment, arithmetic, comparison, logical, bitwise, membership, and identity operators along with example code blocks. This knowledge will help you improve your fundamental understanding of Python programming and assist you in writing more complex programs.