Python Operators
What are Python Operators?
Operators are special symbols that perform arithmetic or logical computation.
Operators are used to perform operations on variables and values.
The value or the variable that the operates on is called the operand.
In the following example, the +
is the operator that performs addition. The value 4
and 6
are the operands, and 10
is the operation's result.
>>> 4 + 6
10
Python has different types of operators:
- Arithmetic operators
- Comparison operators
- Logical operators
- Assignment operators
- Identity operators
- Membership operators
- Bitwise operators
Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform mathematical operations like addition, subtraction, multiplication, etc.
Operator | Description | Example |
---|---|---|
+ | Add two operands. | a + b + 3 |
- | Subtract the right operand from the left. | a - b - 3 |
* | Multiply two operands. | a * b |
/ | Divide the left operand by the right one (The result will be a float). | a / b |
% | Modulus. It returns the remainder of the division of the left operand by the right. | a % b |
// | Floor division. It is a division that results in the whole number adjusted to the left in the number line. | a // b |
** | Exponent. It results in the left operand raised to the power of right. | a ** b |
Arithmetic Operators - Example
Let us see the following examples:
a = 17
b = 3
# a + b = 20
print("a + b =", a+b)
# a - b = 14
print("a - b =", a-b)
# a * b = 51
print("a * b =", a*b)
# a / b = 5.666666666666667
print("a / b =", a/b)
# a // b = 5
print("a // b =", a//b)
# a ** b = 4913
print("a ** b =", a**b)
After executing the above code, the output will be as follows:
a + b = 20
a - b = 14
a * b = 51
a / b = 5.666666666666667
a // b = 5
a ** b = 4913
Python Comparison Operators
Comparison operators are used to compare two values. It returns either True
or False
according to the condition.
Operator | Meaning | Example |
---|---|---|
== | Equal to. Returns True if both operands are equal. | a == b |
!= | Not equal to. Returns True if operands are not equal. | a != b |
> | Greater than. Returns True if the left operand is greater than the right operand. | a > b |
< | Less than. Returns True if the left operand is less than the right operand. | a < b |
>= | Greater than or equal to. Returns True if the left operand is greater than or equal to the right operand. | a >= b |
<= | Less than or equal to. Returns True if the left operand is less than or equal to the right operand. | a <= b |
Comparison Operators in Python - Example
Let us see the following examples:
a = 18
b = 6
# a > b is True
print("a > b is", a>b)
# a < b is False
print("a < b is", a<b)
# a == b is False
print("a == b is", a==b)
# a != b is True
print("a != b is", a!=b)
# a >= b is True
print("a >= b is", a>=b)
# a <= b is False
print("a <= b is", a<=b)
The output of the above code:
a > b is True
a < b is False
a == b is False
a != b is True
a >= b is True
a <= b is False
Python Logical Operators
Logical operators are used to combine conditional statements.
Operator | Description | Example |
---|---|---|
and | Returns True if both statements are true. | a < 10 and a > 6 |
or | Returns True if one of the statements is true. | a > 10 or a > 15 |
not | Reverse the result, returns False if the result is true. | not(a > 10) |
Logical Operators in Python - Example
Let us see the following examples:
a = True
b = False
print("a and b is", a and b)
print("a or b is", a or b)
print("not a is", not a)
The output :
a and b is False
a or b is True
not a is False
Python Assignment Operators
Assignment operators are used to assign values to variables.
The statement a = 6
assigns the value 6 on the right to the variable a
on the left.
There are different compound assignment operators like a += 6
that add to the variable and assigns simultaneously. It is equivalent to a = a + 6
Operator | Example | Equivalent to |
---|---|---|
= | a = 6 | a = 6 |
+= | a += 6 | a = a + 6 |
-= | a -= 6 | a = a - 6 |
*= | a *= 6 | a = a * 6 |
/= | a /= 6 | a = a / 6 |
%= | a %= 6 | a = a % 6 |
//= | a //= 6 | a = a // 6 |
**= | a **= 6 | a = a ** 6 |
&= | a &= 6 | a = a & 6 |
|= | a |= 6 | a = a | 6 |
^= | a ^= 6 | a = a ^ 6 |
>>= | a >>= 5 | a = a >> 5 |
<<= | a <<= 6 | a = a << 6 |
Python Bitwise operators
Bitwise operators are used to compare numbers. They operate bit by bit.
Operator | Name | Description |
---|---|---|
& | AND | Returns 1 if both bits are 1 |
| | OR | Returns 1 id one of two bits is 1 |
^ | XOR | Returns 1 if only one of two bits is 1 |
~ | NOT | Inverts all the bits |
<< | Zero fill left shift | Shift left by pushing zeros in from the right |
>> | Signed right shift | Shift right by pushing copies if the leftmost bit in from the left |
Python Identity Operators
Identity operators are used to check if two variables are located on the same part of the memory. Two equal variables do not imply that they are identical.
Operator | Description | Example |
---|---|---|
is | Returns True if both variables are the same object | a is b |
is not | Returns True if both variables are not the same object | a is not b |
Identity Operators in Python - Example
Let us see the following examples:
a1 = 9
b1 = 9
a2 = "Hi"
b2 = "Hi"
a3 = 6
b3 = 7
a4 = [1, 2, 3, 4, 5]
b4 = [1, 2, 3, 4, 5]
# Output True
print(a1 is b1)
# Output False
print(a2 is not b2)
# Output False
print(a3 is b3)
# Output False
print(a4 is b4)
The output of the above code is as follows:
True
False
False
False
As we can see above, a1
and b1
are integers of the same values, so they are equal as well identical. Same case for a2
and b2
that are strings. On the other side, a3
and b3
don't have the same value, so they are not identical.
The a4
and b4
are lists. They are equal on values but not identical because the Python interpreters locate them separately in memory.
Python Membership Operators
Membership operators are used to check if a value or a variable is found in a sequence (string, list, set, tuple, dictionary).
In a dictionary, you can only check for the presence of the key, not the value.
Operator | Description | Example |
---|---|---|
in | Returns True if the variable/value is founded in the sequence | 6 in a |
not in | Returns True if the variable/value is not founded in the sequence | 6 not in a |
Membership Operators in Python - Example
Let us see the following examples:
a = "python tutorial"
b = ["apple", "microsoft", "ibm", "dell"]
c = {1 : "orange", 2: "kiwi", 3: "waterlemon"}
# Output True
print("p" in a)
# Output True
print("hi" not in a)
# Output True
print("ibm" in b)
# Output True
print(2 in c)
# Output False
print("kiwi" in c)
The output:
True
True
True
True
False
As we can see above, p
is in the a
variable, hi
is not present in a
. The ibm
value is present in the b
list. The key 1
is present in the c
dictionary.