What is an operator?
This chapter mainly explains Python’s operators. Let’s take a simple example: 4 5 = 9. In the example, 4 and 5 are called operands, and " " is called the operator.
Python language supports the following types of operators:
1. Arithmetic operators
2. Comparison (relational) operators
3. Assignment operations Operator
4. Logical operator
5. Bit operator
6. Member operator
7. Identity operator
8.Operator precedence
Next let’s learn Python’s operators one by one.
Python arithmetic operators
The following assume variables: a=10, b=20:
Operator |
Description |
Instance |
|
Add - Add two objects | a b Output result 30 |
- | Subtract - get a negative number or one number minus another number | a - b Output result- 10 |
* | Multiply - Multiply two numbers or return a string repeated several times | a * b Output result 200 |
/ | Divide - x divided by y | b / a Output result 2 |
% |
Modulo - Returns the remainder of division | b % a Output result 0 |
** |
Power - Returns the y power of Integer division - Returns the integer part of the quotient (rounded down) | 9//2 The output result is 4, 9.0//2.0 The output result is 4.0 |
or less The example demonstrates the operation of all arithmetic operators in Python: | #!/usr/bin/python# -*- coding: UTF-8 -*- a = 21 b = 10 c = 0 c = a + b print "1 - c 的值为:", c c = a - b print "2 - c 的值为:", c c = a * b print "3 - c 的值为:", c c = a / b print "4 - c 的值为:", c c = a % b print "5 - c 的值为:", c # 修改变量 a 、b 、c a = 2 b = 3 c = a**b print "6 - c 的值为:", c a = 10 b = 5 c = a//b print "7 - c 的值为:", c Copy after login | The output result of the above example is: 1 - The value of c is: 31 |
3 - The value of c is: 210
4 - The value of c is: 2
5 - The value of c is: 1
6 - The value of c is: 8
7 - The value of c is: 2
The above is the detailed content of What is an operator? The role of Python operators and the meaning of each python operator. For more information, please follow other related articles on the PHP Chinese website!