Operators
Operators in Python are special symbols or keywords used to perform operations on variables and values. They act as building blocks of Python programming, allowing you to manipulate data, perform calculations, and more.
Types of Operators in Python
Python provides several categories of operators:
- Arithmetic Operators
- Comparison (Relational) Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Membership Operators
- Identity Operators
1. Arithmetic Operators
These operators perform mathematical operations like addition, subtraction, multiplication, etc.
Operator | Description | Example | Output |
---|---|---|---|
+ | Adds two values | 5 + 3 | 8 |
- | Subtracts one value | 5 - 3 | 2 |
* | Multiplies two values | 5 * 3 | 15 |
/ | Divides one value by another | 5 / 2 | 2.5 |
% | Modulus (remainder) | 5 % 2 | 1 |
** | Exponentiation | 5 ** 3 | 125 |
// | Floor division (integer part) | 5 // 2 | 2 |
2. Comparison (Relational) Operators
These operators compare two values and return a boolean result (True or False).
Operator | Description | Example | Output |
---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 5 > 3 | True |
< | Less than | 5 < 3 | False |
>= | Greater than or equal to | 5 >= 5 | True |
<= | Less than or equal to | 3 <= 5 | True |
3. Logical Operators
Logical operators are used to combine conditional statements.
Operator | Description | Example | Output |
---|---|---|---|
and | Returns True if both conditions are true | 5 > 3 and 3 > 2 | True |
or | Returns True if at least one condition is true | 5 > 3 or 3 < 2 | True |
not | Reverses the result (True becomes False, and vice versa) | not (5 > 3) | False |
4. Bitwise Operators
These operators work on bits (binary representation of numbers).
Operator | Description | Example | Output |
---|---|---|---|
& | AND | 5 & 3 | 1 |
| | OR | 5 | 3 | 7 |
^ | XOR | 5 ^ 3 | 6 |
~ | NOT (inverts bits) | ~5 | -6 |
<< | Left shift (adds zeros) | 5 << 1 | 10 |
&rt;&rt; | Right shift (removes bits) | 5 rt;&rt; 1 | 2 |
5. Assignment Operators
These are used to assign values to variables.
Operator | Description | Example | Output |
---|---|---|---|
= | Assigns a value | x = 5 | Assigns 5 to x |
+= | Adds and assigns | x += 3 | x = x + 3 |
-= | Subtracts and assigns | x -= 2 | x = x - 2 |
*= | Multiplies and assigns | x *= 4 | x = x * 4 |
/= | Divides and assigns | x /= 2 | x = x / 2 |
%= | Modulus and assigns | x %= 3 | x = x % 3 |
//= | Floor division and assigns | x //= 2 | x = x // 2 |
&= | Bitwise AND and assigns | x &= 3 | x = x & 3 |
|= | Bitwise OR and assigns | x |= 3 | x = x | 3 |
^= | Bitwise XOR and assigns | x ^= 3 | x = x ^ 3 |
<<= | Left shift and assigns | x <<= 1 | x = x << 1 |
>>= | Right shift and assigns | x >>= 1 | x = x >> 1 |
6. Membership Operators
These check for membership in sequences (like lists, strings, etc.).
Operator | Description | Example | Output |
---|---|---|---|
in | Returns True if a value is in the sequence. | 'a' in 'apple' | True |
not in | Returns True if a value is not in the sequence. | 'x' not in 'apple' | True |
7. Identity Operators
These compare the memory addresses of two objects.
Operator | Description | Example | Output |
---|---|---|---|
is | Returns True if two objects have the same memory address. | x is y | True/False |
is not | Returns True if two objects do not share the same memory address. | x is not y | True/False |
Operator Precedence
Some operators are evaluated before others in an expression. Here's a list of operator precedence in Python from highest to lowest:
- Parentheses ()
- Exponentiation **
- Unary operators (+, -, not)
- Multiplication, Division, Modulus, Floor Division (*, /, %, //)
- Addition, Subtraction (+, -)
- Bitwise operators (<<, >>, &, |, ^)
- Comparison (<, <=, >, >=, ==, !=)
- Logical operators (and, or, not)
- Assignment (=, +=, -=)