Operators are used to perform program code operations on more than one operand item. For example: 2+3, the operands are 2 and 3, and the operator is +. Operators in the calculator language can be roughly divided into five types: arithmetic operators, concatenation operators, relational operators, assignment operators and logical operators. Operators are also one of the more understandable human languages in computers, and anyone who knows a little math can read arithmetic operators. Common Python operators are: +, -, *, /, **/<, >,! =, / /, %, &, |, ^ ~, > >, < <, < =, > =, = =, the not, and, or. In the following posts, I’ll look at specific examples of these operators.
Let me write v out front
Python if you don’t know it, it’s probably okay, but once you know it, you’ll love it, right
Vpython operator base instance
Code body & Explanation:
Print (sumString) ="Nice work" print(sumString) ="Nice work" Print (subNumber) print(subNumber) print(subNumber) MultiplicationNumber = 1 *3 print(multiplicationNumber) 6 multiplicationString="hello"*2 print(multiplicationString) # DivisionNumber =9/2 print(divisionNumber) # Print (divisionNumber =9.0/2) print(divisionNumber =9/2.0) print(divisionNumber =9.0/2) If a divisor or dividend is a decimal number, the quotient will retain the decimal number, and if the integer is round, the quotient will retain the decimal number. Print powerNumber =1<2 print(lessThan) # print powerNumber =1 Print (moreThan) # print(moreThan) # print(moreThan) # print(moreThan) # print(moreThan) Print (moreThan) print(moreThan) print(moreThan) =2 print(notEqual) # DivisorNumber =10//3 print(divisorNumber) # DivisorNumber =10%3 divisorNumber=10%1 divisorNumber=10%1 divisorNumber 0/ -- returns 0 if there is no remainder --/ divisorNumberx=10//3 #divisorNumberx is the integer part of the quotient divisorNumbery=10%3 #divisorNumbery is the remainder DivisorNumberz =3 divisorNumberx+divisorNumbery #divisorNumberz is the integer part of the quotient multiplied by the divisor plus the remainder, DivisorNumberz print(divisorNumberz) # print divisorNumberz OperationNumber =7&18 print operationNumber # 2 "" This is a little bit convoluted, but if you're not familiar with binary, you can go to your computer's own calculator, press Win + Q, and type in" Calculator ". Then open the calculator and set it to programmer mode, which is View ->> Programmer. Then we convert 7 to binary :111, auto-complete 8 bits: 00000111, and then convert 18 to binary to complete 8 bits: 00010010 finally, 00000111 and 00010010 will be calculated by bit, /- For those who are not familiar with the operation, you can see the introduction of Baidu Encyclopedia, or very detailed. http://baike.baidu.com/link?url=lfGJREBvGCY5j7VdF2OO9n2mtIbSyNUD7lZyyY74QIetguL5lXIQUxY38Yr-p4z4WdUvHUKGjw9CDfagiun2Ea -/ Get the result: 00000010, we all know that the binary and decimal = 2, so 7 to 18 bitwise and as a result of the binary 10 (2) decimal "' | # bitwise or operation, bitwise or refers to a number into binary, Then the binary number according to a process or operation operationNumber = 7 | # 18: print operationNumber output: Print operationNumber=7^18 print operationNumber # 21 # the same way as the bitwise and operation, can refer to the bitwise and operation # bitwise flip ~ bitwise flip formula: ~x= - (x+1) operationNumber=~12 #~12=- (12+1) = -13 print operationNumber # For example, 18 moves its binary form 00100100 to the left, resulting in 00100100(36). The law of left shift: to move one unit to the left equals to multiply by 2, to move two units to the left equals to multiply by 4, to move three units to the left equals to multiply by 8, namely: OperationNumber =12<<1 print operationNumber # 24 operationNumber=3<<3 print operationNumber # Right shift is the inverse of left shift, moving the corresponding binary number to the right. The law of right shift: To move one unit to the right equals to divide by 2, to move two units to the right equals to divide by 4, to move three units to the right equals to divide by 8, namely: OperationNumber =12>>1 print operationNumber # 6 operationNumber=12>>2 print operationNumber OperationNumber =3<=3 print operationNumber # True operationNumber=3<=2 print operationNumber OperationNumber =2>=3 print operationNumber False operationNumber=3>=2 print operationNumber OperationNumber =3==2 print operationNumber # False operationString="hi"=="hi" print operationString # True # Logical not operationx=True operationy=not operationx print operationy False operationz=False print not operationz True and True = True True and False = False False and True = False False and False = False "" print True and True # True # logic or or "" True or True = True True or False = True False or True = True False or False = False" "print False or False # Output result: FalseCopy the code
So this is going to be a bit of a hassle, but it doesn’t matter if you use it a lot.
Vpython operator priority
1. Python operator priority ranking:
An expression may contain multiple data objects of different data types linked by different operators; Because the expression has a variety of operations, different operation sequence may get different results or even error operation error, because when the expression contains a variety of operations, must be combined in a certain order to ensure the rationality of operation and the correctness and uniqueness of the results.
The precedence is descending from top to bottom, with the highest precedence at the top and the lowest precedence at the comma operator. The order in which expressions are combined depends on the precedence of the various operators in the expression. Operators with higher precedence are combined first, and operators with lower precedence are combined later. Operators in the same line have the same precedence.
Code body & Explanation:
PriorityNumber =2+1*4 print priorityNumber The following priorities are ranked from high to low. In the same operation, the one with the highest priority is executed first and then the one with the lowest priority is executed, and so on. ** priorityNumber=2*2**3 print priorityNumber # 16 # Top 3: flip operation ~ # Top 4: plus or minus the print 1 + 2 * 3 # output: 5 # Top 5: *, /, % print # 2 + 1 * 2/5 output: 2 # Top 6: +, - print 3 < < 2 + 1 # output: 24 # Top 7: < <, > > # 8: Top bitwise &, # ^, | Top 9: comparison operator priority = 2 * 3 + 2 < = 2 + 1 * 7 print priority # output: True #Top 10: logical not and or #Top 11:lambda expressionCopy the code
2. Python operator priority usage rules:
Code body & Explanation:
Print 1+2+3*5+5 from left to right when no higher precedence operator is available Print priority priority=1+2 print priority priority=1+2 print priority priorityCopy the code
2. Python operator priority:
You might find the precedence of Python operators hard to remember and sometimes confusing. In practice, depending on the business logic, we might know what to do first, but forget the priority of the Python operator. What if? Here to teach you a killer, absolutely 100 try.
Code body & Explanation:
Let's say you have this requirement: go to the store and buy some water, buy 3 bottles of water, $5 each, and buy a green arrow (let's say the green arrow costs $3), how much is the total? Print (3*5)+3 # print (3*5)+3 # Better prepared than sorry.Copy the code
VPython expression
Basic concepts of Python expressions
In Python, the sum of values, variables, and operators is called an expression
Code body & Explanation:
#python expression #1 string expression "Hello" #2 Evaluation expression 2+3 #3 assignment expression first=0 #4 variable expression first #5 value expression 3 # etcCopy the code
V Post-bo practice
Basic Python syntax [2], basic Python to master [4] advice, give after browsing the blog exercises. If you have an interesting topic, you can also work on it together, or save it as a “post exercise” for your next blog post.
Practice:
- Figure out the and or non-binary operations
- Memorize python operator precedence relationships
- Go to the store to buy water, buy 3 bottles of water, 5 yuan per bottle, catch up with double 11, the store promotion, now each bottle reduced 1.5 yuan, how much money in total. (Need to use magnifying and not magnifying two ways)
V Blog Summary
If you have any questions or questions about python operators, please feel free to comment and we will discuss them together.
About the author: Focus on basic platform project development. If you have any questions or suggestions, please feel free to comment! Copyright notice: The copyright of this article belongs to the author and the blog garden, welcome to reprint, but without the consent of the author must retain this statement, and give the original text link in a prominent place on the page of the article. For the record: all comments and messages will be answered as soon as possible. You are welcome to correct your mistakes and make progress together. Or direct private message I support the blogger: if you think the article is helpful to you, you can click on the lower right corner of the article [recommendation]. Your encouragement is the author to adhere to the original and continuous writing of the biggest power! \