Operators in Python
Operators in Python
In Python, operators are symbols used to perform operations on variables and values. Python supports various types of operators, including arithmetic, comparison, logical, and more. Let's go over them in detail:
1. Arithmetic Operators
These operators are used to perform basic mathematical operations.
| Operator | Description | Example |
|---|---|---|
+ | Addition | 5 + 3 ? 8 |
- | Subtraction | 5 - 3 ? 2 |
* | Multiplication | 5 * 3 ? 15 |
/ | Division (float result) | 5 / 2 ? 2.5 |
// | Floor Division | 5 // 2 ? 2 |
% | Modulo (remainder) | 5 % 2 ? 1 |
** | Exponentiation | 5 ** 3 ? 125 |
Example:
a = 10b = 3print(a + b) # Output: 13print(a - b) # Output: 7print(a * b) # Output: 30print(a / b) # Output: 3.3333...print(a // b) # Output: 3print(a % b) # Output: 1print(a ** b) # Output: 10002. Comparison Operators
These operators are used to compare values. They return True or False based on the comparison.
| Operator | Description | Example |
|---|---|---|
== | Equal to | 5 == 3 ? False |
!= | Not equal to | 5 != 3 ? True |
> | Greater than | 5 > 3 ? True |
< | Less than | 5 < 3 ? False |
>= | Greater than or equal to | 5 >= 3 ? True |
<= | Less than or equal to | 5 <= 3 ? False |
Example:
x = 5y = 3print(x == y) # Output: Falseprint(x != y) # Output: Trueprint(x > y) # Output: Trueprint(x < y) # Output: Falseprint(x >= y) # Output: Trueprint(x <= y) # Output: False3. Logical Operators
Logical operators are used to combine conditional statements.
| Operator | Description | Example |
|---|---|---|
and | Returns True if both conditions are true | True and False ? False |
or | Returns True if one of the conditions is true | True or False ? True |
not | Returns True if the condition is false | not True ? False |
Example:
a = Trueb = Falseprint(a and b) # Output: Falseprint(a or b) # Output: Trueprint(not a) # Output: False4. Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Description | Example |
|---|---|---|
= | Assigns a value to a variable | x = 5 |
+= | Adds and assigns to a variable | x += 3 ? x = x + 3 |
-= | Subtracts and assigns | x -= 2 ? x = x - 2 |
*= | Multiplies and assigns | x *= 2 ? x = x * 2 |
/= | Divides and assigns | x /= 2 ? x = x / 2 |
//= | Floor division and assigns | x //= 2 ? x = x // 2 |
%= | Modulo and assigns | x %= 2 ? x = x % 2 |
**= | Exponentiation and assigns | x **= 3 ? x = x ** 3 |
Example:
x = 10x += 5 # x = x + 5 ? 15x -= 3 # x = x - 3 ? 12x *= 2 # x = x * 2 ? 24x /= 4 # x = x / 4 ? 6.0x //= 3 # x = x // 3 ? 2x %= 2 # x = x % 2 ? 0x **= 3 # x = x ** 3 ? 05. Membership Operators
These operators are used to test whether a value is found in a sequence (like a list, tuple, or string).
| Operator | Description | Example |
|---|---|---|
in | Returns True if the value is present in the sequence | 'a' in 'apple' ? True |
not in | Returns True if the value is not present in the sequence | 'b' not in 'apple' ? True |
Example:
x = [1, 2, 3, 4, 5]print(3 in x) # Output: Trueprint(6 not in x) # Output: True6. Identity Operators
These operators are used to compare the memory location of two objects.
| Operator | Description | Example |
|---|---|---|
is | Returns True if both variables refer to the same object | x is y ? True |
is not | Returns True if both variables refer to different objects | x is not y ? False |
Example:
a = [1, 2, 3]b = [1, 2, 3]c = aprint(a is b) # Output: False (different objects)print(a is c) # Output: True (same object)print(a is not b) # Output: True7. Bitwise Operators
Bitwise operators work on the binary representations of numbers.
| Operator | Description | Example |
|---|---|---|
& | Bitwise AND | 5 & 3 ? 1 |
| ` | ` | Bitwise OR |
^ | Bitwise XOR | 5 ^ 3 ? 6 |
~ | Bitwise NOT | ~5 ? -6 |
<< | Bitwise left shift | 5 << 1 ? 10 |
>> | Bitwise right shift | 5 >> 1 ? 2 |
Example:
a = 5 # In binary: 101b = 3 # In binary: 011print(a & b) # Output: 1 (binary: 001)print(a | b) # Output: 7 (binary: 111)print(a ^ b) # Output: 6 (binary: 110)print(~a) # Output: -6 (binary: -110)print(a << 1) # Output: 10 (binary: 1010)print(a >> 1) # Output: 2 (binary: 10)8. Ternary Conditional Operator
The ternary operator allows you to write simple if-else conditions in a single line.
x = 10y = 20# If condition is true, x is assigned; otherwise, y is assignedresult = x if x > y else yprint(result) # Output: 20Summary of Python Operators
| Operator Type | Operators | Example |
|---|---|---|
| Arithmetic | +, -, *, /, //, %, ** | 5 + 3, 5 // 2 |
| Comparison | ==, !=, >, <, >=, <= | 5 == 3, 5 > 3 |
| Logical | and, or, not | True and False |
| Assignment | =, +=, -=, *=, /=, `%= |