Operators in Python are essential for constructing meaningful programs. Similar to mathematical symbols, Python operators serve specific purposes. In this article will explore various types of operators in Python, explaining their functions and presenting sample code for clearer understanding.
Arithmetic operators are employed for basic mathematical operations. These operators facilitate primary calculations such as addition, subtraction, multiplication, and division between two operands.
a = 5
b = 3
print(a + b) # Addition
print(a - b) # Subtraction
print(a * b) # Multiplication
print(a / b) # Division
8
2
15
1.6666666666666667
Operator | Operation | Example | Result | Description |
---|---|---|---|---|
+ | Addition | 5 + 8 | 13 | Adds values on either side of the operator |
- | Subtraction | 8 - 5 | 3 | Subtracts right operand from the left |
* | Multiplication | 5 * 8 | 40 | Multiplies values on either side of the operator |
/ | Division | 8 / 2 | 4 | Divides left operand by the right one |
% | Modulus | 8 % 5 | 3 | Returns remainder of the division |
// | Floor Division | 8 // 5 | 1 | Division that results into whole number adjusted to the left in the number line |
** | Exponentiation | 2 ** 3 | 8 | Performs exponential (power) calculation on operators |
Relational operators are used to compare the values of two operands. Through these operators, evaluations are made to determine if two values are equal, if one value is greater than the other, or if one is less than the other. They return a Boolean value based on the result of the comparison.
print(a > b) # Greater than
print(a < b) # Less than
print(a == b) # Equal to
True
False
False
Operator | Operation | Example | Result | Description |
---|---|---|---|---|
== | Equal to | 5 == 8 | False | True if values of two operands are equal |
!= | Not equal to | 5 != 8 | True | True if values of two operands are not equal |
> | Greater than | 8 > 5 | True | True if left operand is greater than the right |
< | Less than | 5 < 8 | True | True if left operand is less than the right |
>= | Greater than or equal to | 8 >= 5 | True | True if left operand is greater than or equal to the right |
<= | Less than or equal to | 5 <= 8 | True | True if left operand is less than or equal to the right |
Logical operators evaluate the truth or falsity of multiple conditions. These operators are primarily used to control the flow of a program by combining various conditions to produce a final logical outcome (either true or false).
x = True
y = False
print(x and y) # Logical AND
print(x or y) # Logical OR
print(not x) # Logical NOT
False
True
False
Operator | Operation | Example | Result | Description |
---|---|---|---|---|
and | Logical AND | True and False | False | True if both operands are true |
or | Logical OR | True or False | True | True if at least one of the operands is true |
not | Logical NOT | not True | False | True if operand is false (complements the operand) |
Bitwise operators act on the binary representation of numbers. They perform operations on individual bits of the operands, facilitating logic operations, shifts, and negations at the bit level.
print(a & b) # Bitwise AND
print(a | b) # Bitwise OR
print(a ^ b) # Bitwise XOR
1
7
6
Operator | Operation | Example | Result | Description |
---|---|---|---|---|
& | Bitwise AND | 5 & 3 | 1 | Bits that are set in both operands are set |
| | Bitwise OR | 5 | 3 | 7 | Bits that are set in either operand are set |
^ | Bitwise XOR | 5 ^ 3 | 6 | Bits that are set in one operand but not both |
~ | Bitwise NOT | ~5 | -6 | Bits that are set are not set, and vice versa |
<< | Left Shift | 5 << 1 | 10 | Shifts the bits of the number to the left |
>> | Right Shift | 5 >> 1 | 2 | Shifts the bits of the number to the right |
Assignment operators are used to assign values to variables. Beyond basic assignments, these operators also offer functionalities that combine operations with assignments. For instance, they can add a specific number to a value and then assign the result to a variable.
a += 2 # Increment value of a by 2
b -= 1 # Decrement value of b by 1
a = 7
b = 2
Operator | Operation | Example | Result | Description |
---|---|---|---|---|
= | Assign | x = 7 | 7 | Assigns value from the right operand to the left operand |
+= | Add and assign | x += 3 | x + 3 | Adds right operand to the left operand and then assigns the result to the left operand |
-= | Subtract and assign | x -= 3 | x - 3 | Subtracts right operand from the left operand and then assigns the result to the left operand |
*= | Multiply and assign | x *= 3 | x * 3 | Multiplies left operand by the right operand and then assigns the result to the left operand |
/= | Divide and assign | x /= 3 | x / 3 | Divides left operand by the right operand and then assigns the result to the left operand |
%= | Modulus and assign | x %= 3 | x % 3 | Takes the modulus of the left operand with the right operand and then assigns the result to the left operand |
//= | Floor divide and assign | x //= 3 | x // 3 | Floor divides left operand by the right operand and then assigns the result to the left operand |
**= | Exponentiate and assign | x **= 3 | x ** 3 | Raises the left operand to the power of the right operand and then assigns the result to the left operand |
Membership operators test for the presence of a value within a sequence, such as a list, string, or tuple. They are mainly employed to check if a particular element exists within a given sequence.
list = [1, 2, 3, 4, 5]
print(3 in list) # Check if 3 is in list
print(6 not in list) # Check if 6 is not in list
True
True
Operator | Operation | Example | Result | Description |
---|---|---|---|---|
in | Evaluates to true if it finds a variable in the specified sequence and false otherwise | 5 in [1, 2, 3, 4, 5] | True | True if value/variable is found in the sequence |
not in | Evaluates to true if it doesn’t finds a variable in the specified sequence and false otherwise | 6 not in [1, 2, 3, 4, 5] | True | True if value/variable is not found in the sequence |
Identity operators are used to compare the memory locations of two objects. Their primary use is to determine if two variables refer to the same object in memory.
x = [1, 2, 3]
y = x
print(y is x) # True since y and x refer to the same object
print(y is not x) # False for the same reason
True
False
Operator | Operation | Example | Result | Description |
---|---|---|---|---|
is | Evaluates to true if the variables on either side of the operator point to the same object and false otherwise | x is y | Depends on the values of x and y | True if both variables are the same object |
is not | Evaluates to false if the variables on either side of the operator point to the same object and true otherwise | x is not y | Depends on the values of x and y | True if both variables are not the same object |
Understanding Python operators is pivotal for efficient programming. Whether comparing variables, adjusting values, or performing calculations, Python's versatile operators are at the core of these operations.
==
and =
in Python?==
compares the values of two operands to determine equality, returning a boolean (True
or False
). For example, 7 == 7
results in True
. On the other hand, =
is used to assign the value of the right operand to the variable on the left.//
operator work differently from the regular division /
operator?/
operator divides and returns a floating-point result. For instance, 5 / 2
yields 2.5
. In contrast, //
divides and rounds down to the nearest whole number, so 5 // 2
gives 2
.&
or |
?is
operator instead of ==
?is
operator checks if two variables reference the exact same object in memory, not just if they have equal values. For instance, two lists with identical values might be True
for ==
, but False
for is
if they're different objects in memory.in
and not in
work with different Python data types?in
verifies if a substring exists. In lists or tuples, it checks if an item is present. For example, 'apple' in 'pineapple'
is True
, and 3 in [1, 2, 3]
is also True
.CloneCoding
Innovation Starts with a Single Line of Code!