Operators in Python

 The following Python arithmetic operators
assume that variable a is 10 and variable b is 21:
Operator Description Example
+ Add - Add two objects a + b Output result 31
- Subtract - Get a negative number or subtract one number from another a - b output result - 11
* multiplication - multiply two numbers or return a string repeated several times a * b output result 210
/ divide - x divided by y b / a output result 2.1
% modulo - return the result of the division The remainder b % a outputs the result 1
** power - returns x to the y power a**b is 10 to the 21st power

// Round division - returns the integer part of the quotient 9//2 outputs the result 4 , 9.0//2.0 outputs the result 4.0


  Python comparison operators
The following assumes that variable a is 10 and variable b is 20:
operator description instance
== equals - compares objects for equality (a == b) returns False.
!= not equal - Compares whether two objects are not equal (a != b) returns True.
> greater than - Returns whether x is greater than y (a > b) returns False.
< Less than - Returns whether x is less than y. All comparison operators return 1 for true and 0 for false. This is equivalent to the special variables True and False, respectively. Note the capitalization of these variable names. (a < b) returns True.
>= greater than or equal to - Returns whether x is greater than or equal to y. (a >= b) returns False.

<= less than or equal to - Returns whether x is less than or equal to y. (a <= b) returns True.

Python assignment operator
The following assumes that variable a is 10 and variable b is 20:


Operator Description Example
= Simple assignment operator c = a + b Assigns the result of a + b to c
+= Addition assignment operator c += a is equivalent to c = c + a
-= subtraction assignment operator c -= a is equivalent to c = c - a
*= multiplication assignment operator c *= a is equivalent to c = c * a
/= division assignment operator The operator c /= a is equivalent to c = c / a
%= modulo assignment operator c %= a is equivalent to c = c % a
**= power assignment operator c **= a is equivalent to c = c **a

//= rounding division assignment operator c //= a is equivalent to c = c // a


Python logical operators
The Python language supports logical operators, the following assumes that the variables a is 10 and b is 20:
operator logical expression description instance
and x and y Boolean "and" - if x is False, x and y return False, otherwise It returns the computed value of y. (a and b) returns 20.
or x or y boolean "or" - if x is True, it returns the value of x, otherwise it returns the computed value of y. (a or b) returns 10.

not not x boolean "not" - Returns False if x is True. It returns True if x is False. not(a and b) returns False

Python member operators
In addition to some of the above operators, Python also supports member operators, and the test instance contains a series of members, including strings, lists or tuples.

The operator description instance
in returns True if a value is found in the specified sequence, and False otherwise. x is in the y sequence, returns True if x is in the y sequence.

not in Returns True if no value is found in the specified sequence, False otherwise. x is not in the y sequence, returns True if x is not in the y sequence.

Python identity operator

The identity operator is used to compare the storage unit of two objects

operator describe example
is is is to determine whether two identifiers refer to an object x is y , similar to  id(x) == id(y)  , returns True if it refers to the same object, otherwise returns False
is not is not is to determine whether two identifiers refer to different objects x is not y  , like  id(a) != id(b) . Returns True if the reference is not the same object, False otherwise.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325824024&siteId=291194637