Python3.x Python3 Operators
\\n\\n\\n\\n
What is an operator?
\\n\\nThis chapter mainly explains Python's operators.\\n\\nTake a simple example:\\n\\n4 + 5 = 9\\nIn the example, 4 and 5 are called operands, and + is called the operator.\\n\\nThe Python language supports the following types of operators:\\n\\n- \\n
- Arithmetic Operators \\n
- Comparison (Relational) Operators \\n
- Assignment Operators \\n
- Logical Operators \\n
- Bitwise Operators \\n
- Membership Operators \\n
- Identity Operators \\n
- Operator Precedence \\n
\\n\\n
Python Arithmetic Operators
\\n\\nAssuming variable a=10 and variable b=21:\\n\\n| Operator | \\nDescription | \\nExample | \\n
|---|---|---|
| + | \\nAdd - Adds two objects | \\na + b outputs 31 | \\n
| - | \\nSubtract - Gets a negative number or subtracts one number from another | \\na - b outputs -11 | \\n
| * | \\nMultiply - Multiplies two numbers or returns a string repeated several times | \\na * b outputs 210 | \\n
| / | \\nDivide - x divided by y | \\nb / a outputs 2.1 | \\n
| % | \\nModulo - Returns the remainder of division | \\nb % a outputs 1 | \\n
| ** | \\nPower - Returns x raised to the power of y | \\na**b is 10 to the power of 21 | \\n
| // | \\nFloor Division - Returns the integer part of the quotient (rounds down) | \\n>>> 9//24>>> -9//2-5 | \\n
Example(Python 3.0+)
\\n\\na = 21\\nb = 10\\nc = 0\\n\\nc = a + b\\nprint("1 - c The value is:", c)\\n\\nc = a - b\\nprint("2 - c The value is:", c)\\n\\nc = a * b\\nprint("3 - c The value is:", c)\\n\\nc = a / b\\nprint("4 - c The value is:", c)\\n\\nc = a % b\\nprint("5 - c The value is:", c)\\n\\na = 2\\nb = 3\\nc = a**b\\nprint("6 - c The value is:", c)\\n\\na = 10\\nb = 5\\nc = a//b\\nprint("7 - c The value is:", c)\\n\\n\\nOutput of the above example:\\n\\n1 - c The value is: 31\\n2 - c The value is: 11\\n3 - c The value is: 210\\n4 - c The value is: 2.1\\n5 - c The value is: 1\\n6 - c The value is: 8\\n7 - c The value is: 2\\n\\n\\n\\n\\n
Python Comparison Operators
\\n\\nAssuming variable a is 10 and variable b is 20:\\n\\n| Operator | \\nDescription | \\nExample | \\n
|---|---|---|
| == | \\nEqual - Compares whether objects are equal | \\n(a == b) returns False. | \\n
| != | \\nNot Equal - Compares whether two objects are not equal | \\n(a != b) returns True. | \\n
| > | \\nGreater than - Returns whether x is greater than y | \\n(a > b) returns False. | \\n
| < | \\nLess than - Returns whether x is less than y. All comparison operators return 1 for true and 0 for false. This is respectively equivalent to the special variables True and False. Note the capitalization of these variable names. | \\n(a < b) returns True. | \\n
| >= | \\nGreater than or equal - Returns whether x is greater than or equal to y. | \\n(a >= b) returns False. | \\n
| <= | \\nLess than or equal - Returns whether x is less than or equal to y. | \\n(a <= b) returns True. | \\n
Example(Python 3.0+)
\\n\\na = 21\\nb = 10\\nc = 0\\n\\nif (a == b):\\n print("1 - a equal to b")\\nelse:\\n print("1 - a Noequal to b")\\n\\nif (a != b):\\n print("2 - a Noequal to b")\\nelse:\\n print("2 - a equal to b")\\n\\nif (a < b):\\n print("3 - a less than b")\\nelse:\\n print("3 - a Greater than or equal to b")\\n\\nif (a > b):\\n print("4 - a greater than b")\\nelse:\\n print("4 - a Less than or equal to b")\\n\\na = 5\\nb = 20\\nif (a <= b):\\n print("5 - a Less than or equal to b")\\nelse:\\n print("5 - a greater than b")\\n\\nif (b >= a):\\n print("6 - b greater than or equal to a")\\nelse:\\n print("6 - b less than a")\\n\\n\\nOutput of the above example:\\n\\n1 - a Noequal to b\\n2 - a Noequal to b\\n3 - a Greater than or equal to b\\n4 - a greater than b\\n5 - a Less than or equal to b\\n6 - b greater than or equal to a\\n\\n\\n\\n\\n
Python Assignment Operators
\\n\\nAssuming variable a is 10 and variable b is 20:\\n\\n| Operator | \\nDescription | \\nExample | \\n
|---|---|---|
| = | \\nSimple assignment operator | \\nc = a + b assigns the result of a + b to c | \\n
| += | \\nAddition assignment operator | \\nc += a is equivalent to c = c + a | \\n
| -= | \\nSubtraction assignment operator | \\nc -= a is equivalent to c = c - a | \\n
| *= | \\nMultiplication assignment operator | \\nc *= a is equivalent to c = c * a | \\n
| /= | \\nDivision assignment operator | \\nc /= a is equivalent to c = c / a | \\n
| %= | \\nModulo assignment operator | \\nc %= a is equivalent to c = c % a | \\n
| **= | \\nExponentiation assignment operator | \\nc **= a is equivalent to c = c ** a | \\n
| //= | \\nFloor division assignment operator | \\nc //= a is equivalent to c = c // a | \\n
| := | \\nWalrus operator, the main purpose of this operator is to assign a value and return that value simultaneously within an expression. Operator added in Python 3.8. | \\nIn this example, the assignment expression can avoid calling len() twice: if (n := len(a)) > 10: print(f"List is too long ({n} elements, expected |
YouTip