Python Operators
Python Operators are special symbols or characters that perform operations on one or more operands (variables or values) and produce a result. Python supports a wide range of operators, which can be classified into several categories:
Arithmetic Operators
Addition: +
Subtraction: –
Multiplication: *
Division: /
Floor Division: // (returns the quotient of the division, discarding the remainder)
Modulo: % (returns the remainder of the division)
Exponentiation: ** (raises a number to a power)
x = 10
y = 3
# Addition
print(x + y) # Output: 13
# Subtraction
print(x - y) # Output: 7
# Multiplication
print(x * y) # Output: 30
# Division
print(x / y) # Output: 3.3333333333333335
# Floor Division
print(x // y) # Output: 3
# Modulo
print(x % y) # Output: 1
# Exponentiation
print(x ** y) # Output: 1000
Comparison Operators
Equal to: ==
Not equal to: !=
Greater than: >
Less than: < Greater than or equal to: >=
Less than or equal to: <=
x = 5
y = 3
# Equal to
print(x == y) # Output: False
# Not equal to
print(x != y) # Output: True
# Greater than
print(x > y) # Output: True
# Less than
print(x < y) # Output: False
# Greater than or equal to
print(x >= y) # Output: True
# Less than or equal to
print(x <= y) # Output: False
Assignment Operators
Assignment: =
Addition assignment: +=
Subtraction assignment: -=
Multiplication assignment: *=
Division assignment: /=
Floor division assignment: //=
Modulo assignment: %=
Exponentiation assignment: **=
x = 10
# Addition assignment
x += 5
print(x) # Output: 15
# Subtraction assignment
x -= 3
print(x) # Output: 12
# Multiplication assignment
x *= 2
print(x) # Output: 24
# Division assignment
x /= 4
print(x) # Output: 6.0
# Modulo assignment
x %= 5
print(x) # Output: 1.0
# Exponentiation assignment
x **= 3
print(x) # Output: 1.0
Logical Operators
Logical AND: and
Logical OR: or
Logical NOT: not
x = 5
y = 3
# Logical AND
print(x > 0 and y > 0) # Output: True
# Logical OR
print(x > 0 or y > 0) # Output: True
# Logical NOT
print(not x > 0) # Output: False
Bitwise Operators
Bitwise AND: &
Bitwise OR: |
Bitwise XOR: ^
Bitwise NOT: ~
Left shift: << Right shift: >>
x = 0b1101 # Binary representation of 13
y = 0b1010 # Binary representation of 10
# Bitwise AND
print(bin(x & y)) # Output: 0b1000 (Binary representation of 8)
# Bitwise OR
print(bin(x | y)) # Output: 0b1111 (Binary representation of 15)
# Bitwise XOR
print(bin(x ^ y)) # Output: 0b0111 (Binary representation of 7)
# Bitwise NOT (One's complement)
print(bin(~x)) # Output: -0b1110 (Binary representation of -14)
# Note: The bitwise NOT operator in Python represents the one's complement of a number. It flips the bits of the number.
# Left Shift
print(bin(x << 2)) # Output: 0b110100 (Binary representation of 52)
# Note: The left shift operator shifts the bits of a number to the left by a specified number of positions. It is equivalent to multiplying the number by 2 raised to the power of the shift count.
# Right Shift
print(bin(x >> 2)) # Output: 0b0011 (Binary representation of 3)
# Note: The right shift operator shifts the bits of a number to the right by a specified number of positions. It is equivalent to dividing the number by 2 raised to the power of the shift count.
Membership Operators:
in: Checks if a value is present in a sequence (e.g., a string, list, tuple, etc.)
not in: Checks if a value is not present in a sequence
my_list = [1, 2, 3, 4, 5]
# in operator
print(3 in my_list) # Output: True
# not in operator
print(6 not in my_list) # Output: True
Identity Operators
is: Checks if two variables refer to the same object
is not: Checks if two variables do not refer to the same object
x = 5
y = 5
z = [1, 2, 3]
w = [1, 2, 3]
# is operator
print(x is y) # Output: True
# is not operator
print(z is not w) # Output: True
Ternary Operator
The ternary operator allows you to write a compact if-else statement in a single line. Its syntax is value_if_true if condition else value_if_false. For example:
x = 10
result = "Even" if x % 2 == 0 else "Odd"
print(result) # Prints "Even"
These are some of the commonly used operators in Python. They enable you to perform various operations on variables and values, manipulate data, make decisions, and control program flow.