Python operators are symbols that perform specific operations on variables and values. Mastering them is crucial for effective Python programming. This tutorial explores Python’s diverse range of operators, categorized for clarity.
Table of Contents
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Special Operators (Membership & Identity)
1. Arithmetic Operators
These operators perform standard mathematical calculations.
Operator | Description | Example | Result |
---|---|---|---|
+ |
Addition | 10 + 5 |
15 |
- |
Subtraction | 10 - 5 |
5 |
* |
Multiplication | 10 * 5 |
50 |
/ |
Division | 10 / 5 |
2.0 |
// |
Floor Division | 10 // 5 |
2 |
% |
Modulus (Remainder) | 10 % 3 |
1 |
** |
Exponentiation | 10 ** 2 |
100 |
2. Comparison Operators
These operators compare two values, returning a Boolean (True
or False
) result.
Operator | Description | Example | Result |
---|---|---|---|
== |
Equal to | 10 == 5 |
False |
!= |
Not equal to | 10 != 5 |
True |
> |
Greater than | 10 > 5 |
True |
< |
Less than | 10 < 5 |
False |
>= |
Greater than or equal to | 10 >= 5 |
True |
<= |
Less than or equal to | 10 <= 5 |
False |
3. Logical Operators
These operators combine or modify Boolean expressions.
Operator | Description | Example | Result |
---|---|---|---|
and |
Logical AND | True and False |
False |
or |
Logical OR | True or False |
True |
not |
Logical NOT | not True |
False |
4. Bitwise Operators
These operators manipulate the individual bits of integers.
Operator | Description | Example | Result (Decimal) | Binary Representation |
---|---|---|---|---|
& |
Bitwise AND | 10 & 4 |
0 |
1010 & 0100 = 0000 |
| |
Bitwise OR | 10 | 4 |
14 |
1010 | 0100 = 1110 |
^ |
Bitwise XOR | 10 ^ 4 |
14 |
1010 ^ 0100 = 1110 |
~ |
Bitwise NOT | ~10 |
-11 |
~1010 = -1011 |
<< |
Left Shift | 10 << 2 |
40 |
1010 << 2 = 101000 |
>> |
Right Shift | 10 >> 2 |
2 |
1010 >> 2 = 0010 |
5. Assignment Operators
These operators assign values to variables, often combining assignment with another operation.
Operator | Description | Example | Equivalent to |
---|---|---|---|
= |
Assign | x = 10 |
x = 10 |
+= |
Add and assign | x += 5 |
x = x + 5 |
-= |
Subtract and assign | x -= 5 |
x = x - 5 |
*= |
Multiply and assign | x *= 5 |
x = x * 5 |
/= |
Divide and assign | x /= 5 |
x = x / 5 |
//= |
Floor divide and assign | x //= 5 |
x = x // 5 |
%= |
Modulus and assign | x %= 5 |
x = x % 5 |
**= |
Exponentiate and assign | x **= 5 |
x = x ** 5 |
&= |
Bitwise AND and assign | x &= 5 |
x = x & 5 |
|= |
Bitwise OR and assign | x |= 5 |
x = x | 5 |
^= |
Bitwise XOR and assign | x ^= 5 |
x = x ^ 5 |
<<= |
Left shift and assign | x <<= 5 |
x = x << 5 |
>>= |
Right shift and assign | x >>= 5 |
x = x >> 5 |
6. Special Operators (Membership & Identity)
These operators offer unique functionalities beyond basic arithmetic or comparisons.
- Membership Operators:
in
andnot in
check if a value exists within a sequence (string, list, tuple, etc.). - Identity Operators:
is
andis not
verify if two variables point to the same memory location (the same object).
This comprehensive guide provides a solid foundation in Python operators. Practice using these operators to build your programming skills.