What is an operator?
This section mainly describes the Python operators . To give a simple example 4 +5 = 9. In the example, 4 and 5 are called operands, and "+" is called an operator.
The Python language supports the following types of operators:
1. Arithmetic operators
2. Compare (relational) operators
3. Assignment operators
4. Logical operators
5. Bitwise operators
6. Member Operators
7. Identity operator
8. Operator Precedence
Next, let's learn about Python's operators.
Python arithmetic operators
The following hypothetical variables: a=10,b=20:
Operator
|
Describe
|
Instance
|
+
|
Add-Two objects added |
A + B output result 30 |
- |
Minus-get negative numbers or one number minus the other |
-B Output Result-10 |
* |
Multiply by two number or return a string that is repeated several times |
A * b output result 200 |
/ |
Except-X divided by Y |
B/A Output Results 2 |
%
|
Modulo-Returns the remainder of the division |
B% A output result 0 |
**
|
Power-Returns the Y power of X |
A**b is 10 of 20, output 100000000000000000000 |
// |
Rounding-Returns the integer portion of the quotient (rounded down) |
9//2 output result 4, 9.0//2.0 output 4.0 |
The following example shows the operation of all the arithmetic operators of Python:
#!/usr/bin/python#-*-coding:utf-8-*-a = b = ten c = 0 c = a + b print "1-c value:", c c = value of "A-D print" 2-c For: ", c c = a * b print" 3-c value is: ", c C = A/b print" 4-c value is: ", c C = a% b print" 5-c value is: ", c # Repair The change amount A, B, c a = 2 b = 3 c = a**b print "The value of 6-c is:", c A = ten b = 5 c = a//b print "7-c value is:", C
The result of the above example output:
The value of the 1-c is: 31
The value of the 2-c is: 11
The value of the 3-C is: 210
The value of the 4-C is: 2
The value of the 5-c is: 1
The value of the 6-C is: 8
The value of the 7-c is: 2