03. Operators in Python entry

1. What is an operator?

Take a simple example ** 4 +5 = 9 . In the example, 4 and 5 are called operands, and "+**" are called operators.

The Python language supports the following types of operators:

Two, Python arithmetic operators?

The following hypothetical variables: ** a=10, b=20**:

Operator description example
Operator description example
+ Add-Add two objects a + b output result 30
- Subtraction-get a negative number or subtract one number from another a-b output the result -10
* Multiply-Multiply two numbers or return a string that has been repeated several times a * b outputs the result 200
/ Division-x divided by y b / a output result 2
% Modulo-returns the remainder of the division b% a outputs the result 0
** Power-returns x to the y power a**b is 10 to the 20th power, and the output is 100000000000000000000
// Round Divide-Returns the integer part of the quotient (rounded down) >>> 9//2 4 >>> -9//2 -5

Three, Python comparison operator##

The following assumes that the variable a is 10 and the variable b is 20:

Operator description example
== Equal to-compare whether the objects are equal (a == b) returns False.
!= Not Equal-Compare whether two objects are not equal (a != b) returns true.
<> Not equal-Compare whether two objects are not equal. python3 is obsolete. (a <> b) returns true. This operator is similar to !=.
> 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. (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.

Four, Python assignment operator##

The following assumes that the variable a is 10 and the variable b is 20:

Operator description example
= The simple assignment operator c = a + b assigns the result of a + b to c
+= The addition assignment operator c += a is equivalent to c = c + a
- = The subtraction assignment operator c -= a is equivalent to c = c-a
*= The multiplication assignment operator c *= a is equivalent to c = c * a
/= Division assignment operator c /= a is equivalent to c = c / a
%= Modulus assignment operator c %= a is equivalent to c = c% a
**= The power assignment operator c **= a is equivalent to c = c ** a
//= Round division assignment operator c //= a is equivalent to c = c // a

Five, Python bit operator##

Bitwise operators treat numbers as binary for calculation. The rules of bitwise operators in Python are as follows:

a =00111100
b =00001101-----------------

a&b =00001100
a|b =00111101
a^b =00110001~a  =11000011
Operator description example
& Bitwise AND operator: the two values involved in the operation, if the two corresponding bits are both 1, the result of the bit is 1, otherwise it is 0 (a & b) The output result is 12, binary interpretation: 0000 1100
Bitwise OR operator: As long as one of the two corresponding binary bits is 1, the result bit is 1.
^ Bitwise XOR operator: When two corresponding binary bits are different, the result is 1 (a ^ b) Output result 49, binary interpretation: 0011 0001
~ Bitwise inversion operator: Invert each binary bit of the data, that is, change 1 to 0 and 0 to 1. ~x is similar to -x-1 (~a) The output result is -61, binary interpretation: 1100 0011, in the complement form of a signed binary number.
<< Left shift operator: all binary bits of the operand are shifted to the left by several bits. The number to the right of << specifies the number of bits to move, the high bits are discarded, and the low bits are filled with 0. a << 2 Output result 240, binary interpretation: 1111 0000
>> Right shift operator: Shift all the binary bits of the operand on the left of ">>" by several digits to the right. The number on the right of >> specifies the number of digits to move a >> 2 The output result is 15, binary interpretation: 0000 1111

Six, Python logical operators##

Python language supports logical operators, the following assumes that the variable a is 10 and b is 20

Operator logical expression description example
and x and y Boolean "and"-If x is False, x and y return False, otherwise it returns the calculated value of y. (a and b) returns 20.
or x or y Boolean "or"-If x is non-zero, it returns the value of x, otherwise it returns the calculated value of y. (a or b) returns 10.
not not x Boolean "not"-If x is True, return False. If x is False, it returns True. not(a and b) returns False

Seven, Python member operator##

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

Operator description example
in Returns True if the value is found in the specified sequence, otherwise it returns False. x in the y sequence, returns True if x is in the y sequence.
not in If the value is not found in the specified sequence, it returns True, otherwise it returns False. x is not in the y sequence, if x is not in the y sequence, return True.

The following example demonstrates the operation of all member operators in Python:

a =10
b =20

list =[1,2,3,4,5,20]if( a in list):print('Variable a is in the given list')else:print('Variable a is not in the given list')if( b in list):print('Variable b is in the given list')else:print('Variable b is not in the given list')

# The output of the above example:
# Variable a is not in the given list
# Variable b is in the given list

Short-circuit operator and If the first expression is false, there is no need to calculate later, this logical expression must be Falseor if the first expression is True, there is no need to calculate later , This logical expression must be True

Eight, Python identity operator##

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

Operator description example
is is is to judge whether two identifiers refer to an object x is y, similar to id(x) == id(y), if the same object is referred to, it returns True, otherwise it returns False
is not is not is to judge whether two identifiers refer to different objects x is not y, similar to id(a) != id(b). If the reference is not the same object, it returns True, otherwise it returns False.

Nine, Python operator precedence##

The following table lists all operators from highest to lowest precedence:

Operator description
Operator description
** Index (highest priority)
~ + - Bitwise flip, unary plus and minus signs (the last two methods are named +@ and -@)
* /% // Multiplication, division, modulus and division
+ - Addition and Subtraction
>> << Right shift, left shift operator
& Bit'AND'
^
<= < > >= Comparison operator
<> == != Equal operator
= %= /= //= -= += *= **= Assignment Operator
is is not identity operator
in not in member operator
not and or logical operators

Recommended Posts

03. Operators in Python entry
Arithmetic operators in python
Python entry-3
Functions in python
Python basic operators
Python entry learning materials
Join function in Python
Python3 entry learning four.md
12. Network Programming in Python3
print statement in python
Concurrent requests in Python
Install python in Ubuntu
Context management in Python
Python3 entry learning three.md
Python3 entry learning one.md
Write gui in python
MongoDB usage in Python
Str string in Python
Python3 entry learning two.md
Computational Geometry in Python
Concurrent requests in Python (part 2)
Subscripts of tuples in Python
Talking about inheritance in Python
Python entry tutorial notes (3) array
Noteworthy update points in Python 3.9
Containerize Python applications in 3 minutes
What is introspection in python
05. Python entry value loop statement
What is object-oriented in python
Generators and iterators in Python
Talking about strings in Python
The usage of wheel in python
Summary of logarithm method in Python
Use of Pandas in Python development
Use nohup command instructions in python
What is list comprehension in python
Is there function overloading in python
Getting started with Numpy in Python
Python entry notes [basic grammar (below)]
Detailed sorting algorithm (implemented in Python)
How to wrap in python code
What does rc1 mean in python
Common errors and solutions in python
Use of numpy in Python development
What does def in python do
How to omit parentheses in Python
Detailed usage of dictionary in Python
Usage of os package in python
Learn about garbage collection in Python
How to write classes in python
How to filter numbers in python
​What are the numbers in Python?
How to read Excel in Python
There are several keywords in Python
Everything in Python is an object
How to view errors in python
Python from entry to proficiency (2): Introduction to Python
What does np do in python
How to write return in python
Talking about the modules in Python
How to understand variables in Python