Operators are essential components of any programming language, and Python is no exception. They allow you to perform various operations on variables and values, making your code more dynamic and powerful. In this blog, we'll explore different types of operators in Python and provide simple examples to help you understand their usage.
Table of Contents
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Assignment Operators
- Identity Operators
- Membership Operators
- Bitwise Operators
1. Arithmetic Operators
Arithmetic operators are used for mathematical calculations.
- 1. Addition (+): Adds two values.
a = 10
b = 5
result = a + b # 10 + 5 = 15
print(result) # Output: 15
- 2. Subtraction (-): Subtracts the second value from the first.
x = 8
y = 3
result = x - y # 8 - 3 = 5
print(result) # Output: 5
- 3. Multiplication (*): Multiplies two values.
p = 7
q = 4
result = p * q # 7 * 4 = 28
print(result) # Output: 28
- 4. Division (/): Divides the first value by the second.
m = 15
n = 3
result = m / n # 15 / 3 = 5.0
print(result) # Output: 5.0
- 5. Modulus (%): Returns the remainder of division.
numerator = 17
denominator = 5
result = numerator % denominator # 17 % 5 = 2
print(result) # Output: 2
- 6. Exponentiation (**): Raises the first value to the power of the second.
base = 2
exponent = 3
result = base ** exponent # 2^3 = 8
print(result) # Output: 8
- 7. Floor Division (//): Returns the floor value after division.
dividend = 17
divisor = 5
result = dividend // divisor # 17 // 5 = 3
print(result) # Output: 3
2. Comparison Operators
Comparison operators are used to compare values.
- 1. Equal to (==): Checks if two values are equal.
x = 5
y = 5
result = x == y # True
print(result) # Output: True
- 2. Not Equal to (!=): Checks if two values are not equal.
a = 10
b = 7
result = a != b # True
print(result) # Output: True
- 3. Greater than (>): Checks if the first value is greater than the second.
num1 = 8
num2 = 3
result = num1 > num2 # True
print(result) # Output: True
- 4. Less than (<): Checks if the first value is less than the second.
p = 5
q = 9
result = p < q # True
print(result) # Output: True
- 5. Greater than or equal to (>=): Checks if the first value is greater than or equal to the second.
m = 7
n = 7
result = m >= n # True
print(result) # Output: True
- 6. Less than or equal to (<=): Checks if the first value is less than or equal to the second.
alpha = 4
beta = 6
result = alpha <= beta # True
print(result) # Output: True
3. Logical Operators
Logical operators are used to combine and manipulate boolean values.
- 1. AND (and): Returns True if both operands are True.
p = True
q = False
result = p and q # False
print(result) # Output: False
- 2. OR (or): Returns True if at least one operand is True.
x = True
y = False
result = x or y # True
print(result) # Output: True
- 3. NOT (not): Returns the opposite of the operand's value.
value = True
result = not value # False
print(result) # Output: False
4. Assignment Operators
Assignment operators are used to assign values to variables.
- 1. Assignment (=): Assigns a value to a variable.
a = 5
print(a) # Output: 5
- 2. Add and Assign (+=): Adds a value to the variable and assigns the result.
num = 10
num += 3 # num = num + 3
print(num) # Output: 13
- 3. Subtract and Assign (-=): Subtracts a value from the variable and assigns the result.
value = 8
value -= 2 # value = value - 2
print(value) # Output: 6
- 4. Multiply and Assign (*=): Multiplies the variable by a value and assigns the result.
count = 4
count *= 5 # count = count * 5
print(count) # Output: 20
- 5. Divide and Assign (/=): Divides the variable by a value and assigns the result.
total = 15
total /= 3 # total = total / 3
print(total) # Output: 5.0
5. Identity Operators
Identity operators are used to compare the memory locations of two objects. They are often used to determine if two variables refer to the same object in memory.
- 1. is Operator: This operator returns
True
if two variables point to the same object in memory, andFalse
otherwise.
x = [1, 2, 3]
y = x
z = [1, 2, 3]
print(x is y) # Output: True
print(x is z) # Output: False
- 2. is not Operator: This operator returns
True
if two variables do not point to the same object in memory, andFalse
if they do.
a = 5
b = 5
print(a is not b) # Output: False
6. Membership Operators
Membership operators are used to test whether a value or variable is found in a sequence, such as a list, tuple, or string.
- 1. in Operator: This operator returns
True
if a value is found in the sequence, andFalse
otherwise.
fruits = ['apple', 'banana', 'orange']
print('apple' in fruits) # Output: True
print('grape' in fruits) # Output: False
- 2. not in Operator: This operator returns
True
if a value is not found in the sequence, andFalse
if it is.
numbers = (1, 2, 3, 4, 5)
print(6 not in numbers) # Output: True
print(3 not in numbers) # Output: False
7. Bitwise Operators
Bitwise operators are used to perform operations at the bit-level. They are often used in low-level programming and when working with binary representations of data.
- 1. Bitwise AND (&): Performs a bitwise AND operation on each bit of the operands.
a = 10 # Binary: 1010
b = 6 # Binary: 0110
result = a & b # Binary: 0010 (Decimal: 2)
print(result) # Output: 2
- 2. Bitwise OR (|): Performs a bitwise OR operation on each bit of the operands.
x = 10 # Binary: 1010
y = 6 # Binary: 0110
result = x | y # Binary: 1110 (Decimal: 14)
print(result) # Output: 14
- 3. Bitwise XOR (^): Performs a bitwise XOR operation on each bit of the operands.
p = 10 # Binary: 1010
q = 6 # Binary: 0110
result = p ^ q # Binary: 1100 (Decimal: 12)
print(result) # Output: 12
- 4. Bitwise NOT (~): Inverts the bits of the operand, effectively changing 0s to 1s and vice versa.
num = 10 # Binary: 00001010
result = ~num # Binary: 11110101 (Decimal: -11)
print(result) # Output: -11
- 5. Left Shift (<<): Shifts the bits of the left operand by the number of positions specified by the right operand.
value = 5 # Binary: 00000101
shifted = value << 2 # Binary: 00010100 (Decimal: 20)
print(shifted) # Output: 20
- 6. Right Shift (>>): Shifts the bits of the left operand to the right by the number of positions specified by the right operand.
number = 40 # Binary: 00101000
shifted = number >> 3 # Binary: 00000101 (Decimal: 5)
print(shifted) # Output: 5
Top comments (0)