Python Operators
About Python Operators
Python operators are symbols that are used to perform operations on variables and values. They are used to manipulate data and perform calculations.
Categories of Python operators, based on the types of operations they perform, are as follows:
- 1. Arithmetic Operators
- 2. Comparison Operators
- 3. Assignment Operators
- 4. Logical Operators
- 5. Bitwise Operators
- 6. Membership Operators
- 7. Identity Operators
- 8. Ternary Operator (Conditional Expression)
- 9. Unary Operators
Arithmetic Operators in Python
In Python, arithmetic operators are used to perform mathematical operations on numerical values.
Arithmetic operators are:
- Addition (+): Adds two operands.
- Subtraction (-): Subtracts the second operand from the first.
- Multiplication (*): Multiplies two operands.
- Division (/): Divides the first operand by the second (result is always a float).
- Floor Division (//): Divides the first operand by the second and returns the integer part of the result (rounded down).
- Modulus (%): Returns the remainder of the division of the first operand by the second.
- Exponentiation (**): Raises the first operand to the power of the second operand.
Arithmetic operators with examples are shown in the following table:
| Arithmetic operators with examples | |||
|---|---|---|---|
| Operator Symbol | Operator Name | Example | Results |
| + | Addition | 5 + 8 | 13 |
| - | Subtraction | 8 - 5 | 3 |
| * | Multiplication | 8 * 5 | 40 |
| / | Division | 40 / 5 | 8.0 |
| // | Floor Division | 16 // 5 | 3 |
| % | Modulus | 11 % 5 | 1 |
| ** | Exponentiation | 5 ** 2 | 25 |
Example of arithmetic operators:
Output of the above example
Addition: 13
Subtraction: 3
Product of multiplication: 40
Quotient of division: 8.0
Result of floor division: 3
result of modulus (remainder): 1
Result of exponentiation 25
Comparison Operators in Python
In Python, comparison operators are used to compare values. They return boolean values 'True' or 'False' based on if the comparison is true or not.
Comparison operators are:
- Equal to (==): Checks if the values of two operands are equal.
- Not equal to (!=): Checks if the values of two operands are not equal.
- Greater than (>): Checks if the value of the left operand is greater than the value of the right operand.
- Less than (<): Checks if the value of the left operand is less than the value of the right operand.
- Greater than or equal to (>=): Checks if the value of the left operand is greater than or equal to the value of the right operand.
- Less than or equal to (<=): Checks if the value of the left operand is less than or equal to the value of the right operand.
Comparison operators with examples are shown in the following table:
| Comparison operators with examples | |||
|---|---|---|---|
| Operator Symbol | Operator Name | Example | Example Result |
| == | Equal to | 5 == 8 | False |
| != | Not equal to | 8 != 5 | True |
| > | Greater than | 8 > 5 | True |
| < | Less than | 8 < 5 | False |
| >= | Greater than or equal to | 8 >= 5 | True |
| <= | Less than or equal to | 5 <= 8 | True |
Example of comparison operators:
Output of the above example
Equal: False
Not equal to: True
Greater than: True
Less than: False
Greater than or equal to: True
Less than or equal to: True
Assignment Operators in Python
In Python, assignment operators are used to assign values to variables.
Assignment operators are:
- Simple Assignment (=): Assigns the value on the right to the variable on the left.
- Addition Assignment (+=): Adds the value on the right to the variable on the left and assigns the result to the variable.
- Subtract Assignment (-=): Subtracts the value on the right from the variable on the left and assigns the result to the variable.
- Multiplication Assignment (*=): Multiplies the variable on the left by the value on the right and assigns the result to the variable.
- Division Assignment (/=): Divides the variable on the left by the value on the right and assigns the result to the variable.
- Floor Division Assignment (//=): Performs floor division on the variable on the left by the value on the right and assigns the result to the variable.
- Modulus Assignment (%=): Computes the modulus of the variable on the left with the value on the right and assigns the result to the variable.
- Exponentiation Assignment (**=): Raises the variable on the left to the power of the value on the right and assigns the result to the variable.
- Bitwise AND Assignment (&=): Performs a bitwise AND operation between the variable on the left and the value on the right, and assigns the result to the variable.
- Bitwise OR Assignment (|=): Performs a bitwise OR operation between the variable on the left and the value on the right, and assigns the result to the variable.
- Bitwise XOR Assignment (^=): Performs a bitwise XOR operation between the variable on the left and the value on the right, and assigns the result to the variable.
- Bitwise Left Shift Assignment (<<=): Performs a bitwise left shift operation on the variable on the left by the number of bits specified by the value on the right, and assigns the result to the variable.
- Bitwise Right Shift Assignment (>>=): Performs a bitwise right shift operation on the variable on the left by the number of bits specified by the value on the right, and assigns the result to the variable.
Assignment operators with examples are shown in the following table where x = 5:
| Assignment operators with examples | |||
|---|---|---|---|
| Operator Symbol | Operator Name | Example | Equivalent to |
| = | Simple Assignment | x = 5 | x = 5 |
| += | Addition Assignment | x += 3 | x = x + 3 |
| -= | Subtract Assignment | x -= 2 | x = x - 2 |
| *= | Multiplication Assignment | x *= 3 | x = x * 3 |
| /= | Division Assignment | x /= 5 | x = x / 5 |
| //= | Floor Division Assignmen | x //= 2 | x = x // 2 |
| %= | Modulus Assignment | x %= 2 | x = x % 2 |
| **= | Exponentiation Assignment | x **= 2 | x = x ** 2 |
| &= | Bitwise AND Assignment | x &= 2 | x = x & 2 |
| |= | Bitwise OR Assignment | x |= 4 | x = x | 4 |
| ^= | Bitwise XOR Assignment | x ^= 1 | x = x ^ 1 |
| <<= | Bitwise Left Shift Assignment | x <<= 2 | x = x << 2 |
| >>= | Bitwise Right Shift Assignment | x >>= 1 | x = x >> 1 |
Please have a closer look at the following examples:
Example of Assignment operators:
Output of the above example
Simple assignment: 5
Addition Assignment: 8
Subtract Assignment: 3
Multiplication Assignment: 9
Division Assignment: 1.0
Floor Division Assignment: 2
Modulus Assignment: 1
Exponentiation Assignment: 25
Bitwise AND Assignment: 0
Bitwise OR Assignment: 5
Bitwise XOR Assignment: 4
Bitwise Left Shift Assignment: 20
Bitwise Right Shift Assignment: 2
Logical Operators in Python
In Python, logical operators are used to perform logical operations on True and False of boolean values. The logical operators in Python are:
- Logical and (and): returns True if both operands are True, otherwise returns False.
- Logical or (or): Returns True if at least one operand is True, otherwise returns False.
- Logical not (not): Returns True if the operand is False, and False if the operand is True.
Logical operators with examples are shown in the following table:
| Logical operators with examples | |||
|---|---|---|---|
| Operator Symbol | Operator Name | Example | Returns |
| and | Logical and | 8 > 5 and 6 > 5 | True |
| or | Logical or | 5 > 7 or 6 > 9 | False |
| not | Logical not | not 2 | False |
Please have a closer look at the following examples:
Example of logical operators in Python:
Output of the above example
True
False
True
True
False
Bitwise Operators in Python
In Python, bitwise operators are used to perform operations on individual bits of integers.
Bitwise operators work at the binary level. The bitwise operators are:
- Bitwise AND (&): Performs a bitwise AND operation between the corresponding bits of two operands where the result is 1 if both bits are 1; otherwise, it's 0.
- Bitwise OR (|): Performs a bitwise OR operation between the corresponding bits of two operands where the result is 1 if at least one of the bits is 1; otherwise, it's 0.
- Bitwise XOR (^): Performs a bitwise XOR (exclusive OR) operation between the corresponding bits of two operands where the result is 1 if the bits are different; otherwise, it's 0.
- Bitwise NOT (~): Pperforms a bitwise NOT operation on its single operand, which inverts (flips) all the bits (1 to 0 and 0 to 1).
- Left Shift (<<): Shifts the bits of the left operand to the left by a number of positions specified by the right operand.
- Right Shift (>>): This operator shifts the bits of the left operand to the right by a number of positions specified by the right operand.
Bitwise operators with examples are shown in the following table:
| Bitwise operators with examples | |||
|---|---|---|---|
| Operator Symbol | Operator Name | Example | Returns |
| & | Bitwise AND | 5 & 3 | 1 |
| | | Bitwise OR | 5 | 3 | 7 |
| ^ | Bitwise XOR | 5 ^ 3 | 6 |
| ~ | Bitwise NOT | ~5 | -6 |
| << | Left Shift | 5 << 1 | 10 |
| >> | Right Shift | 5 >> 1 | 2 |
Please have a closer look at the following examples:
Example of bitwise Operators operators in Python:
Output of the above example
1
7
6
-6
10
2
Membership Operators in Python
In Python, membership operators ('in' and 'not in') are used to test whether a value exists within a sequence (such as strings, lists, or tuples). The membership operators in Python are:
- 'in' operator : Checks if a value exists in a sequence. If the value is present in the sequence, the 'in' operator returns True; otherwise, it returns False.
- 'not in' operator: Checks if a value does not exist in a sequence. If the value is not present in the sequence, the 'not in' operator returns True; otherwise, it returns False.
Membership operators with examples are shown in the following table where my_list = [3, 5, 7, 8, 9]:
| Membership operators with examples | |||
|---|---|---|---|
| Operator Symbol | Operator Name | Example | Returns |
| in | in operator | 5 in my_list | True |
| not in | not in operator | 6 not in my_list | True |
Please have a closer look at the following examples:
Example of membership operators in Python:
Output of the above example
True
True
Identity Operators in Python
In Python, identity operators are used to check if two variables refer to the exact same object in memory. The identity operators in Python are:
- 'is' operator : Returns True if two variables point to the same object; otherwise, it returns False.
- 'is not' operator: Returns True if two variables do not point to the same object; otherwise, it returns False.
Identity operators with examples are shown in the following table where x = "Python", y = 7 and z = x :
| Identity operators with examples | |||
|---|---|---|---|
| Operator Symbol | Operator Name | Example | Returns |
| is | is operator | x is y | False |
| is | is operator | x is z | True |
| is not | is not operator | x is not y | True |
| is not | is not operator | x is not z | False |
Please have a closer look at the following examples:
Example of identity operators in Python:
Output of the above example
False
True
True
False
Ternary Operator in Python
In Python, ternary operator is used to provide a concise way to write conditional statements in a single line. Here is the syntext of ternary operator:
<expression_if_condition_is_true> if <condition> else <expression_if_condition_is_false>
In the following example, if the condition (if num % 2 == 0) is true, it will return "even number"; otherwise, it will return "odd number". As the num is 17, the condition will be false and it will return "odd number".
Please have a closer look at the following example:
Example of ternary operator in Python:
Output of the above example
17 is an odd number
Unary Operators in Python
In Python, Unary Operators act on a single operand.
They perform various operations such as negation, logical negation, bitwise negation, etc.
- Positive (+): Indicates a positive value.
- Negative (-): Negates the value of its operand.
- Logical NOT: Negates the boolean truth value of its operand (though not is a logical operator in Python).
- Bitwise NOT (~): Inverts the bits of its operand.
Please have a closer look at the following examples:
Example of unary operators in Python:
Output of the above example
-7
-6
False
Please note that Python does not have specific increment (++) and decrement (--) unary operators as some other languages do (like C or C# or Java). You can use assignment operators (+=) and (-=) for incrementing and decrementing respectively by specific number.
Python Compiler For Live Practice
Practice the above Python code in the following Python Editor. Create more code using Python operators and perform different operations in the following Python code editor and click Execute button. Output will be displayed in Python Code Output section.
Python Editor