Mastering Python Operators: A Comprehensive Guide
Mastering Python Operators
In this tutorial, you'll master all Python operator categories—syntax, behavior, and practical examples—to write cleaner, more efficient code.
Video: Operators in Python
What are operators in Python?
Operators are special symbols that perform arithmetic or logical computations. The values they act upon are called operands.
For example:
>>>>> 2+3
5
Here, + is the operator performing addition, 2 and 3 are operands, and 5 is the result.
Arithmetic Operators
Arithmetic operators execute mathematical operations such as addition, subtraction, multiplication, and more.
| Operator | Meaning | Example |
|---|---|---|
| + | Add two operands or unary plus | x + y + 2 |
| - | Subtract right operand from left or unary minus | x - y - 2 |
| * | Multiply two operands | x * y |
| / | Divide left operand by right (always returns float) | x / y |
| % | Modulus – remainder of division | x % y (remainder of x/y) |
| // | Floor division – whole number result rounded down | x // y |
| ** | Exponentiation – left raised to right power | x ** y (x to the power y) |
Example 1: Arithmetic Operators in Python
x = 15
y = 4
print('x + y =', x + y)
print('x - y =', x - y)
print('x * y =', x * y)
print('x / y =', x / y)
print('x // y =', x // y)
print('x ** y =', x ** y)
Output
x + y = 19 x - y = 11 x * y = 60 x / y = 3.75 x // y = 3 x ** y = 50625
Comparison Operators
Comparison operators evaluate relationships between values, returning True or False.
| Operator | Meaning | Example |
|---|---|---|
| > | Greater than | x > y |
| < | Less than | x < y |
| == | Equal to | x == y |
| != | Not equal to | x != y |
| >= | Greater than or equal to | x >= y |
| <= | Less than or equal to | x <= y |
Example 2: Comparison Operators in Python
x = 10
y = 12
print('x > y is', x > y)
print('x < y is', x < y)
print('x == y is', x == y)
print('x != y is', x != y)
print('x >= y is', x >= y)
print('x <= y is', x <= y)
Output
x > y is False x < y is True x == y is False x != y is True x >= y is False x <= y is True
Logical Operators
Logical operators—and, or, not—combine Boolean expressions.
| Operator | Meaning | Example |
|---|---|---|
| and | True if both operands are true | x and y |
| or | True if at least one operand is true | x or y |
| not | Negates the operand | not x |
Example 3: Logical Operators in Python
x = True
y = False
print('x and y is', x and y)
print('x or y is', x or y)
print('not x is', not x)
Output
x and y is False x or y is True not x is False
Below is a truth table illustrating these operators.
Bitwise Operators
Bitwise operators manipulate individual bits of integer operands.
For example, 2 is 10 in binary, and 7 is 111.
Let x = 10 (00001010 binary) and y = 4 (00000100 binary).
| Operator | Meaning | Example |
|---|---|---|
| & | Bitwise AND | x & y = 0 (00000000) |
| | | Bitwise OR | x | y = 14 (00001110) |
| ~ | Bitwise NOT | ~x = -11 (11110101) |
| ^ | Bitwise XOR | x ^ y = 14 (00001110) |
| >> | Right shift | x >> 2 = 2 (00000010) |
| << | Left shift | x << 2 = 40 (00101000) |
Assignment Operators
Assignment operators assign values to variables, including compound forms that combine an operation with assignment.
| Operator | Example | Equivalent to |
|---|---|---|
| = | x = 5 | x = 5 |
| += | x += 5 | x = x + 5 |
| -= | x -= 5 | x = x - 5 |
| *= | x *= 5 | x = x * 5 |
| /= | x /= 5 | x = x / 5 |
| %= | x %= 5 | x = x % 5 |
| //= | x //= 5 | x = x // 5 |
| **= | x **= 5 | x = x ** 5 |
| &= | x &= 5 | x = x & 5 |
| |= | x |= 5 | x = x | 5 |
| ^= | x ^= 5 | x = x ^ 5 |
| >>= | x >>= 5 | x = x >> 5 |
| <<= | x <<= 5 | x = x << 5 |
Special Operators
Python also includes identity and membership operators for advanced comparisons.
Identity Operators
is and is not check whether two references point to the same object.
| Operator | Meaning | Example |
|---|---|---|
| is | True if operands refer to the same object | x is True |
| is not | True if operands refer to different objects | x is not True |
Example 4: Identity Operators in Python
x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]
print(x1 is not y1)
print(x2 is y2)
print(x3 is y3)
Output
False True False
Integers and short strings are interned, so identical literals often share the same object. Lists are distinct objects even when they contain the same items.
Membership Operators
in and not in test for the presence of a value within a sequence, set, or dictionary key set.
| Operator | Meaning | Example |
|---|---|---|
| in | True if value is found in the sequence | 5 in x |
| not in | True if value is absent from the sequence | 5 not in x |
Example 5: Membership Operators in Python
x = 'Hello world'
y = {1:'a',2:'b'}
print('H' in x)
print('hello' not in x)
print(1 in y)
print('a' in y)
Output
True True True False
Notice that string comparisons are case‑sensitive and dictionary checks only consider keys.
Python
- C# Operators – Comprehensive Guide to Operators in C#
- Mastering C++ Operators: A Complete Guide with Practical Examples
- Mastering C Programming Operators: A Comprehensive Guide
- Python List Operations: Creation, Access, Modification, and Advanced Techniques
- Mastering Python Tuples: Creation, Access, and Advanced Operations
- Mastering Python Dictionaries: Creation, Manipulation, and Advanced Techniques
- Master Java Operators: Types, Syntax, & Practical Examples
- Comprehensive Guide to C Operators: Types, Functions & Examples
- Mastering C# Operators: A Comprehensive Guide
- Master Python Basic Operators: Arithmetic, Comparison, Logical & More