Python expressions categorized by their type
Comparison and Assignment Operators
This a comprehensive table of various Python expressions categorized by their type. Each expression is accompanied by a sample value to illustrate its usage.
Category | Expression | Sample Value |
---|---|---|
Arithmetic | 2 + 3 |
5 |
5 - 2 |
3 |
|
4 * 3 |
12 |
|
10 / 2 |
5.0 |
|
10 // 3 |
3 |
|
10 % 3 |
1 |
|
2 ** 3 |
8 |
|
Comparison | 5 > 3 |
True |
5 < 3 |
False |
|
5 == 5 |
True |
|
5 != 4 |
True |
|
5 >= 5 |
True |
|
5 <= 4 |
False |
|
Logical | True and False |
False |
True or False |
True |
|
not True |
False |
|
Bitwise | 0b1010 & 0b0110 |
0b0010 (2 ) |
0b1010 | 0b0110 |
0b1110 (14 ) |
|
0b1010 ^ 0b0110 |
0b1100 (12 ) |
|
~0b1010 |
-0b1011 (-11 ) |
|
0b1010 << 2 |
0b101000 (40 ) |
|
0b1010 >> 2 |
0b10 (2 ) |
|
Assignment | x = 5 |
x is 5 |
x += 3 |
x becomes 8 |
|
x -= 2 |
x becomes 6 |
|
x *= 4 |
x becomes 24 |
|
x /= 3 |
x becomes 8.0 |
|
x %= 5 |
x becomes 3.0 |
|
x **= 2 |
x becomes 9.0 |
|
Membership | 'a' in 'apple' |
True |
'b' not in 'apple' |
True |
|
Identity | x is y |
True or False |
x is not y |
True or False |
|
Ternary | a if condition else b |
Depends on condition |
Lambda | lambda x: x + 1 |
Function adding 1 to input |
List Comprehension | [x*2 for x in range(3)] |
[0, 2, 4] |
Function Call | max(1, 2, 3) |
3 |
Attribute Access | object.attribute |
Depends on object |
Indexing | my_list[0] |
First element of my_list |
Slicing | my_list[1:3] |
Sublist from index 1 to 2 |
String Formatting | f"Hello, {name}" |
e.g., "Hello, Alice" |