#programming #programming/python #semester-1
| Operator | Name |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus (Remainder) |
| ** | Exponentiation (Power) |
| // | Floor Division (Round Down) |
Assigns the value on the right onto the value on the left
| Operator | Example | Same As |
|---|---|---|
| = | x = 5 | x = 5 |
| += | x +=3 | x = x + 3 |
| -= | x -= 3 | x = x -3 |
| *= | x *= 3 | x = x * 3 |
| /= | x /=3 | x = x / 3 |
| %= | x %= 3 | x = x % 3 |
| //= | x //= 3 | x = x // 3 |
| = | x **= 3 | x = x ** 3 |
| &= | x &= 3 | x = x & 3 |
| |= | x |= 3 | x = x | 3 |
| ^= | x ^= 3 | x = x ^ 3 |
| >>= | x >>= 3 | x = x >> 3 |
| <<= | x <<= 3 | x = x << 3 |
| := | print(x := 3) | x = 3 print(x) |
The walrus operator returns and assigns a value to a certain variable. You can use it as an inline assignment shown here:
# Without Walrus
numbers = [2, 8, 0, 1, 1, 9, 7, 7]
num_length = len(numbers)
num_sum = sum(numbers)
description = {
"length": num_length,
"sum": num_sum,
"mean": num_sum / num_length,
}
# With Walrus :=
numbers = [2, 8, 0, 1, 1, 9, 7, 7]
description = {
"length": (num_length := len(numbers)),
"sum": (num_sum := sum(numbers)),
"mean": num_sum / num_length,
}Both of these code snippets are the same.
Comparison Operators return True or False:
| Operator | Name |
|---|---|
| == | Equal To |
| != | Not Equal To |
| > | Greater Than |
| < | Less Than |
| >= | Greater Than or Equal To |
| <= | Less Than or Equal To |
Logical operators are used to combine conditional statements:
| Operator | Description |
|---|---|
| and | Returns True if both statements are True |
| or | Returns True if one of the statements are true |
| not | * Reverses the result* |
Identity Operators are used to compare objects, not if they are equal but if they are the same object with the same memory location.
| Operator | Description |
|---|---|
| is | *Returns True if both variables are the same object * |
| is not | *Returns True if both variables are not the same object * |
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is z) # True
print(x is y) # False
print(x == y) # TrueMembership operators are used to test if a sequence is presented in an object
| Operator | Description |
|---|---|
| in | *Returns True if a sequence with the specified value is present in the object * |
| not in | *Returns True if a sequence with the specified value is not present in the object * |
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits) # True
text = "Hello World"
print("H" in text) # True
print("hello" in text) # False
print("z" not in text) # True| Operator | Name | Description |
|---|---|---|
| & | AND | Sets each bit to 1 if both bits are 1 |
| | | OR | Sets each bit to 1 if one of two bits is 1 |
| ^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
| ~ | NOT | * Inverts all the bits* |
| << | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off (e.g. x << 2) |
| >> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off (e.g. x >> 2) |
The binary representation of 6 is 0110 The binary representation of 3 is 0011 6 & 3 = 0010 = 2 in decimal
The bin() function can be used to see the binary form of an integer prefixed with 0b:
x = 6
print(bin(x)) # 0b110 -> StringThe order operators are used in from top to bottom are below:
| Operator | Description |
|---|---|
| () | Parenthesis |
| ** | Exponentiation |
| +x, -x, ~x | Unary plus, Unary minus, and bitwise NOT |
| *, /, //, % | Multiplication, Division, Floor Division, and Modulus |
| +, - | Addition and Subtraction |
| <<, >> | Bitwise left and right shift |
| & | Bitwise AND |
| ^ | Bitwise XOR |
| | | Bitwise OR |
| ==, !=, >, >=, <, <=, is, is not, in, not in | Comparisons, Identity and Membership Operators |
| not | Logical NOT |
| and | AND |
| or | OR |
The unary plus and minus are when i sign precedes a variable/number such as: -1, -x, +z
If two operators have the same order, then the expression is evaluated from left to right