Like other programming languages, Python also has operators for executing program code operations, with 1 of commonly used operators. Python arithmetic operator 2. Python relational operator 3. Python assignment operator 4. Python logical operators, the following describes how each operator is used:
Arithmetic operators
In order to distinguish the differences between the operators more intuitively and clearly, we declare two variables a, B, and assign a=100 and b=12.
| + |
Two objects added |
The value of A+b is: 112 |
| - |
Two number subtraction |
The value of a-B is: 88 |
| * |
Two number multiplied or returns a string that is repeated several times |
The value of A*b is: 1200 |
| / |
Divide two numbers |
The value of A/b is: 8 |
| % |
Modulo-Returns the remainder of the division |
The value of A%b is: 4 |
| ** |
Power-Returns the power of B of a |
The value of A**b is: 1000000000000000000000000 |
| // |
Divide-Returns the integer part of the quotient |
The value of a//b is: 8 |
Program Run Results
650) this.width=650; "src=" Https://s5.51cto.com/oss/201710/19/2f7d07277110ce5daa76bc5528dbd635.png "title=" Qq20171019111923.png "alt=" 2f7d07277110ce5daa76bc5528dbd635.png "/>
Note: In python2.x, integers can only be derived by integer except integers. If you want to get a fractional part, change one of the numbers to a floating-point number.
As 100/12.0=8.33333333333, because 12.0 is a floating-point number
Relational operators
The above variables and assignments are also used here, a=100,b=12
| == |
Equals, compares objects for equality |
(A = = b) returns False |
| != |
does not equal, compares two objects that are not equal |
(A! = b) returns true |
| <> |
Not equal to, compare two objects are not equal, this operator is similar to! = |
(a <> B) returns True. |
| > |
Greater than, returns whether a is greater than B |
(A > B) returns True |
| < |
Less than, returns whether a is less than B |
All comparison operators return 1 for true, and return 0 for false. This distinction is equivalent to the special variable true and false. Note that the uppercase of these variable names, (a < b) returns false |
| >= |
Greater than or equal |
Returns whether a is greater than or equal to B,true |
| <= |
Less than or equal |
Returns whether a is less than or equal to B,false |
Program Run Results
650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/19/aa058a133d29ecc19ba667ca35ed8082.png "title=" Qq20171019114717.png "alt=" Aa058a133d29ecc19ba667ca35ed8082.png "/>
Assignment operators
The above variables and assignments are also used here, a=100,b=12
| = |
Simple assignment operator |
c = A + B assigns the result of the operation of A + B to c |
| += |
|
c + = B is equivalent to C = C + B, and returns |
| -= |
subtraction assignment operator |
c-= B is equivalent to C = C-b, and returns |
| *= |
|
c *= B is equivalent to C = c * B, and returns |
| /= |
|
c/= B is equivalent to C = c/b and returns |
| %= |
modulo assignment operator |
c%= B is equivalent to C = c% B, and returns |
| **= |
|
c **= B is equivalent to C = c * * b, and returns |
| //= |
Take the divisible assignment operator |
C//= B is equivalent to C = c//b and returns |
Program Run Results
logical operators
The above variables and assignments are also used here, a=100,b=12
| and |
Boolean "and"-if x is False,x and y returns FALSE, otherwise it returns the computed value of y |
12
|
| Or |
Boolean "or", if X is not 0, it returns the value of x, otherwise it returns the computed value of y |
100 |
| Not |
Boolean "Non", if X is True, returns FALSE. If X is False, it returns True
|
Flase
|
Program Run Results
This article is from the "Dreamscape" blog, make sure to keep this source http://dyqd2011.blog.51cto.com/3201444/1974115
Fourth session: Python operators