Python Operators Overview
Introduction
Operators in Python are the constructs which are used to manipulate the value of operands. Simply put, operators are used to perform operations on variables and values. Python provides many types of operators, including arithmetic, comparison, assignment, logical, identity, membership, and bitwise operators.
Syntax
The basic syntax for operators in Python is as follows:
operand1 operator operand2
Here, operand1
and operand2
can be variables, expressions, or literals, and operator
specifies the type of operation to be performed.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division. The following table lists the commonly used arithmetic operators in Python:
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (remainder) |
** | Exponentiation |
// | Floor division |
Example
a = 10
b = 3
print(a + b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.33333333333
print(a % b) # Output: 1
print(a ** b) # Output: 1000
print(a // b) # Output: 3
Comparison Operators
Comparison operators are used to compare two values and return a Boolean value (True or False). The following table lists the commonly used comparison operators in Python:
Operator | Description |
---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Example
a = 10
b = 3
print(a == b) # Output: False
print(a != b) # Output: True
(a > b) # Output: True
print(a < b) # Output: False
print(a >= b) # Output: True
print(a <= b) # Output: False
Assignment Operators
Assignment operators are used to assign values to variables. The following table lists the commonly used assignment operators in Python:
Operator | Example | Equivalent to |
---|---|---|
= | 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 |
Example
a = 5
b = 3
a += b
print(a) # Output: 8
a *= b
print(a) # Output: 24
Logical Operators
Logical operators are used to combine two or more conditions and return a Boolean value (True or False). The following table lists the commonly used logical operators in Python:
Operator | Description |
---|---|
and | Returns True if both conditions are True |
or | Returns True if at least one condition is True |
not | Returns the opposite of the condition |
Example
a = 5
b = 3
c = 7
print(a > b and b < c) # Output: True
print(a > b or b > c) # Output: True
print(not(a > b and b < c)) # Output: False
Identity Operators
Identity operators are used to compare the memory addresses of two objects. The following table lists the commonly used identity operators in Python:
Operator | Description |
---|---|
is | Returns True if both variables are the same object |
is not | Returns True if both variables are not the same object |
Example
a = 5
b = 5
c = [1, 2, 3]
d = [1, 2, 3]
print(a is b) # Output: True
print(c is not d) # Output: True
Membership Operators
Membership operators are used to test if a value or variable exists within a sequence or container. The following table lists the commonly used membership operators in Python:
Operator | Description |
---|---|
in | Returns True if the value/variable exists in the sequence/container |
not in | Returns True if the value/variable does not exist in the sequence/container |
Example
a = 'Hello, World!'
b = [1, 2, 3]
print('l' in a) # Output: True
print(4 not in b) # Output: True
Bitwise Operators
Bitwise operators are used to perform operations on binary values. The following table lists the commonly used bitwise operators in Python:
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Bitwise Left Shift |
>> | Bitwise Right Shift |
Example
a = 5 # 0101
b = 3 # 0011
print(a & b) # Output: 1 (0001)
print(a | b) # Output: 7 (0111)
print(a ^ b) # Output: 6 (0110)
print(~a) # Output: -6 (11111010)
print(a << 2) # Output: 20 (10100)
print(a >> 2) # Output: 1 (0001)
Summary
In this tutorial, we covered the various types of operators available in Python, including arithmetic, comparison, assignment, logical, identity, membership, and bitwise operators. We also provided examples to illustrate their usage. Understanding operators is essential for writing effective Python code, and we hope this tutorial has helped you gain a better understanding of them.