1. Introduction to Python Operators
1) What is an operator
In Python it is often necessary to manipulate one or more numbers, and the + in 2+3 is the operator, and the * in the "Hello" *20 is also the operator
2) What are the operators
+-*/*
< >! =//%
& | ^ ~ >>
<< <= >= = =
Not and OR
3) How to use operators
#Encoding:utf-8#+ two numbers addedA=7+8PrintA# the#+ two strings addedb="Hello"+" World"PrintB#HelloWorld#-Take the opposite number of a number or subtract two numbersC=-7PrintC#-7D=19-1PrintD# -#* Two number multiplication or string repetitionE=4*7PrintE# -f="Hello"*PrintF#Hellohellohello#/achieve dividing two numbersG=7/2PrintG#3 divisor and dividend are integral types, the result is also integral type (not rounded, is directly after the decimal point of the part)H=7.0/2PrintH#3.5 Floating-point type divided by integral type can be obtained floating point typei=7/2.0PrintI#3.5 integer divided by floating-point type can also get floating point type#* * exponentiation operationJ=2**3#equivalent to 2 of the three-time SquarePrintJ#8#< Symbol, returns a bool valueK=0<3PrintK#truel=0<0PrintL#false#> symbol, returns a bool valueM=3>7PrintM#falseN=3>3PrintN#false#! = symbol, which returns a bool valueO=2!=3PrintO#true#//division, and then returns the integer portion of its quotient, dropping the remainderP=10//3PrintP#3#% division operation, returns the remainder of its quotient, the drop-off quotientq=10%3PrintQ#1r=10%1PrintR#0#& Bitwise-and-arithmetic converts a number to binary, and then these binary digits are bitwise AND calculatedS=7&18#the binary of 7 is 00111#the binary of 18 is 10010#Bitwise take and as 00010#Convert to decimal 2PrintS#2#| Converts a number to a binary by bitwise OR operation, and then the binary number is bitwise OR OperationT=7|18#the binary of 7 is 00111#the binary of 18 is 10010#Bitwise or for 10111#quasi-transposition decimal isPrintT# at#^ Bitwise XOR operation, after conversion to binary, the difference is 1 the same is 0U=7^18#the binary of 7 is 00111#the binary of 18 is 10010#Bitwise XOR or 10101#quasi-transposition decimalPrintU# +#~ Bitwise rollover ~x=-(x+1)V=~18#v=-(18+1)PrintV#-19#<< left shift operation (several times to the left is multiplied by 2)W=18<<1#the binary of 18 is 10010#move left one is 100100#converted to two-tier system isPrintW# $X=3<<3PrintX# -#>> Right Shift operation (several sides of the right shift are divided by 2)Y=18>>1PrintY#9Z=18>>2PrintZ#4#<= SymbolA1=3<=3PrintA1#trueB1=4<=3PrintB1#false#>= SymbolC1=1>=3PrintC1#falseD1=4>=3PrintD1#true#= = Symbol Compares two objects for equalityE1=12==13PrintE1#falsef1="Hello"=="Hello"PrintF1#true#not logical non-g1=TRUEH1= notG1PrintH1#false#and logic and truth are true#or logic or false for the same
2. Python priority
Operators in Python are sequential in their execution, with precedence in Python divided into two categories, one for program priority and one for string precedence
# encoding:utf-8 # priority A=2+7*8print A # 58, first multiplication plus minus b =9>7print b # true > is greater than = # priority sort # priority rank first-function call, addressing, subscript # priority sort second-power operation * # priority sort third--rollover operation ~ # priority ranking fourth place--sign
# Priority ranking fifth place--*,/,% # Priority ranking sixth place--+,- # Priority ranking seventh place--<<, >> # Priority ranking eighth place--bitwise &, ^, |, where the order of precedence for these three is also available # Priority ranking Nineth Place--comparison operator # Priority ranking tenth place-logical not and OR # Priority Sort 11---lambda Expressions
3. Python Priority rule
4. Python expressions
In Python, when programming, values, variables, and operators collectively make up the whole we call it an expression.
The difference between an expression execution and print execution
If it is a number, the result is the same, if the string, the execution result has a single quotation mark (direct output from the command line), but no single quotation marks are printed
Python-10:python Syntax Basics-operators and expressions