Python Operators
Python Operators
What Are Operators?
In Python, operators are symbols that perform operations on values (also known as operands) and produce a result. There are different types of operators in Python, including:
- Arithmetic operators: perform arithmetic operations on numerical values (e.g.,
+,-,*,/,%,**). - Comparison operators: compare two values and return a boolean value (
TrueorFalse) indicating the result of the comparison (e.g.,==,!=,<,>,<=,>=). - Assignment operators: assign a value to a variable (e.g.,
=,+=,-=,*=,/=,%=,**=). - Logical operators: perform logical operations on boolean values (e.g.,
and,or,not). - Identity operators: compare the identity of two objects and return a boolean value indicating whether the objects are the same (e.g.,
is,is not). - Membership operators: test whether a value is a member of a sequence (e.g.,
in,not in). - Bitwise operators: perform bit-level operations on integers (e.g.,
&,|,^,~,<<,>>).
Here's an example that demonstrates the use of some of the operators in Python:
a = 5
b = 3
c = a + b
d = a - b
e = a * b
f = a / b
g = a % b
h = a ** b
print("a + b =", c)
print("a - b =", d)
print("a * b =", e)
print("a / b =", f)
print("a % b =", g)
print("a ** b =", h)
In this example, the arithmetic operators +, -, *, /, %, and ** are used to perform various operations on the values a and b. The results of the operations are stored in variables c, d, e, f, g, and h, respectively, and are printed to the standard output.
Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus | a % b |
| ** | Exponentiation | a ** b |
| // | Floor division | a // b |
% Operator
% modulus operator return the remainder.
print("a/b = ", 20/6) # modulus 2
// Operator
// floor Modulus operator return the quotient
print("a//b = ", 10//20) # floor modulus 0
print("a//b = ", 20//6) # floor modulus 3
Other examples of Arithmetic operators
a, b = 10, 20 # variable a and b
print("a+b = ", a+b) # add 30
print("a-b = ", a-b) # sub -10
print("a*b = ", a*b) # multiply 200
print("a/b = ", a/b) # divide 0.5
Assignment Operators
| Operator | Example | Equivalent |
|---|---|---|
| = | a = 5 | a = 5 |
| += | a += 5 | a = a + 5 |
| -= | a -= 5 | a = a - 5 |
| *= | a *= 5 | a = a * 5 |
| /= | a /= 5 | a = a / 5 |
| %= | a %= 5 | a = a % 5 |
| //= | a //= 5 | a = a // 5 |
| **= | a **= 5 | a = a ** 5 |
| &= | a &= 5 | a = a & 5 |
| |= | a |= 5 | a = a | 5 |
| ^= | a ^= 5 | a = a ^ 5 |
| >>= | a >>= 5 | a = a >> 5 |
| <<= | a <<= 5 | a = a << 5 |
+= , -= , /= , *= Operator
Some examples of assignment operators:
a = 5
a += 2 # first add and assign the value in variable
print("The value of a is",a) # output 7
a = 5
a -= 2 # first subtract and assign the value in variable
print("The value of a is",a) # output 3
a = 5
a *= 2 # first multiply and assign the value in variable
print("The value of a is",a) # output 10
a = 5
a /= 2 # first divide and assign the value in variable
print("The value of a is",a) # output 2.5
a = 5
a //= 2 # first divide and assign the value in variable (return quotient)
print("The value of a is",a) # output 2
a = 5
a **= 2 # first multiply and assign the value in variable (return exponentiation)
print("The value of a is",a) # output 25
Comparison Operators
Comparison operators are used to compare two or more operands .
| Operator | Description | Example |
|---|---|---|
| == | Equal | a == b |
| != | Not equal | a != b |
| > | Greater than | a > b |
| < | Less than | a < b |
| >= | Greater than or equal to | a >= b |
| <= | Less than or equal to | a <= b |
Examples
Comparison Operators return True or False :
a = 5
b = 10
print(a == b) # output False
print(a != b) # output True
print(a > b) # output False
print(a < b) # output True
print(a >= b) # output False
print(a <= b) # output True
Logical Operators
| Operator | Description | Example |
|---|---|---|
| and | if both statements are true return True | a < 5 and a < 10 |
| or | True if one of the statements is true return True | a < 5 or a < 4 |
| not | True is false and false is True | not(a < 5 and a < 10) |
Example Of Logical Operators
Logical operators return True or False after evaluating two operands or objects
a = 10
print(a > 5 and a < 20) # True
print(a > 5 or a < 20) # True
print(not(a > 5) ) # False
Identity And Membership Operators
| Operator | Description | Example |
|---|---|---|
| in | if value present in the object return True | a in b |
| not in | if value is not present in the object return True | a not in b |
| is | if both objects are same object return True | a is b |
| is not | if both objects are not same return True | a is not b |
Examples Of Membership Operators
Membership operators return True or False:
a = 100
b = "100"
x = "python"
y = "python"
print(a is b) # False
print(a is not b) # True
print(x is not y) # False
print("o" in b) # False
print("o" in y) # True
Bitwise Operators
Bitwise operators perform the operation in bits ( binary )
| Operator | Description | Uses |
|---|---|---|
| & | Bitwise AND | a & b |
| | | Bitwise OR | a | b |
| ~ | Bitwise NOT | ~x |
| ^ | Bitwise XOR | a ^ b |
| >> | Bitwise right shift | a >> 2 |
| << | Bitwise left shift | a << 2 |
Example Of Bitwise Operators :
a = 5
b = 8
print(bin(a)) # return 101 (binary)
print(bin(b)) # return 1000 (binary)
print(a & b) # return 0
print(a | b) # return 13
print( ~ a) # return -6
print(a ^ b) # return 13
print(a >> b) # return 0
print(a << b) # return 1280

