The purpose of this document is to summarize the relevant contents , Scattered knowledge is difficult to remember and learn .
This document is based on windows platform .
View a full range of documents :python Basics _CSDN Blog .
1. Arithmetic operator
Operator : With 1 + 2 For example ,1 and 2 Called operands ,“+” Called operator .
Operator
describe
+ Add two numbers 、 String splicing - negative 、 Subtract two numbers * Multiplication of two numbers 、 Returns a string that has been repeated several times / Divide two numbers , Non integer division % modulus - Returns the remainder of the Division ** power - x**y return x Of y The next power // Divide and conquer - Returns the integer part of the quotient
These operators are simple , If you want to see examples , It's recommended to read the document :Python3 Operator _w3cschool.
2. Comparison operator
The return is a Boolean value ,True perhaps False.
Operator
describe
== be equal to – Compare objects for equality != It's not equal to – Compare two objects for inequality > Greater than – return x Is it greater than y< Less than – return x Is less than y.>= Greater than or equal to – return x Greater than or equal to y.<= Less than or equal to – return x Less than or equal to y.
For continuous comparisons ,Python It is explained by this mechanism :
3>2>1 be equal to (3>2) and (2>1)
Equivalent to two comparisons , Then use and & Then combine .
& Bitwise and operator : Two values involved in the operation , If both corresponding bits are 1, The result of this bit is 1, Otherwise 0| bitwise or operator : As long as one of the two corresponding binary bits is 1 when , The result bit is 1.^ bitwise exclusive or operator : When two corresponding binary bits are different ( Different ) when , The result is 1~ Bitwise negation operator : Invert each binary bit of data , Namely the 1 Turn into 0, hold 0 Turn into 1<< Left move operator : All the binary bits of the operand are shifted to the left by several bits , from "<<" The number on the right specifies the number of digits to move , High level discard , Low complement 0.>> Move right operator : hold ">>“ Each binary of the left operand is shifted several bits to the right ,”>>" The number on the right specifies the number of digits to move
5. Logical operators
Python Language supports logical operators , But there are no other languages && and || grammar , Instead, more humanized English words and or not( It's all lowercase ).
Operator
describe
and Boolean " And " - If x by False,x and y return False, Otherwise it returns y Calculated value .or Boolean " or " - If x yes True, It returns x Value , Otherwise it returns y Calculated value .not Boolean " Not " - If x by True, return False . If x by False, It returns True.
6. member operator
in And not in Used to determine whether an object is one of the elements of a collection , Fast running speed . The result returned is Boolean True perhaps False.
Operator
describe
in Returns if a value is found in the specified sequence True, Otherwise return to False.not in If no value is found in the specified sequence, return True, Otherwise return to False.
7. Identity operator
Operator
describe
isis Is to determine whether two identifiers are referenced from an object is notis not Is to determine whether two identifiers are referenced from different objects
8. Operator priority
Operator
describe
** Index ( Highest priority )~ + - Flip by bit , One yuan plus sign and minus sign ( The last two methods are called [email protected] and [email protected])* / % // ride , except , Mold and division + - addition and subtraction >> << Move right , Shift left operator & position ‘AND’^ | An operator <= < > >= Comparison operator <> == != Comparison operator = %= /= //= -= += *= **= Assignment operator is is not Identity operator in not in member operator not or and Logical operators
Parentheses are not included in this table because they are not operators , But when parentheses appear in an arithmetic expression , You have to calculate the contents in brackets first .
The priority of operators of the same level is calculated from left to right .
9. Ternary expression
python The ternary operator in is not like the general representation of other languages : Judging conditions ? Results when true : Results when false .
stay python The format in is : Results when true if Judging conditions else Results when false
for example : True if 5>3 else False
Recommended references :
python Operator - Liu Jiang's python course (liujiangblog.com)
Python3 Basic grammar _w3cschool
notes : The content of this article is collected and summarized on the Internet , For learning only !