One, the Python operator
(1) Assignment operator
The assignment operators include: = + = = *=/=%=
=: a = 123//Assign 123 to a x = ' abc '//assign ABC to x, note that the string is quoted, if not quoted Python will think of it as a variable, the variable assigned to the variable will be wrong
+ =: X + = 2//conversion to X = x + 2
-=: X-= 2//conversion to X = X-2
*=: X *= 2//conversion to X = x * 2
/=: X/= 2//conversion to X = X/2 (division sign, note is divisible), such as 5/2=2
%=: X%= 2//convert to X = x%2 (take surplus), such as 5%2=1
(2) Arithmetic operators
Arithmetic operators include: +-*///% * * (arithmetic operator precedence from high to Low: +-*/%//* *)
+: 3 + 4//result for 7 ' a ' + ' B ' result for ' AB '
-: 5–2
*: 3 * 3
/: 5/2//result is 2 (note is divisible), if you want to write a decimal: 5.0/2
: 5//2//Result 2 (double slash means only integer part), such as 5.0//2 result 2.0 instead of 2.5
%: 4 3//result is 1 (indicates the remainder)
* *: 2**3//Represents 2 of 3 squares, and two multiplication sign represents a exponentiation
(3) Relational operators
Relational operators include: > < >= <= = = = = (relational operator precedence from highest to lowest: < <= > >=! = = =)
>: 1 > 2//Result false
<: 1 < 2//result is true
>=: 1 >= 2
<=: 1 <= 2
= = = 1 = = 2//The result is false, indicating constant equals
! =: 1! = 2//result is true, indicating not equal to
What is the difference between "=" and "= ="?
The function of "=" is to assign the value on the right to the variable name on the left, and the function of "= =" is to check if both sides are equal
(4) Logical operators
Logical operators include: and (with) or (or) not (non)
And: Two results are true the end result is true, such as 2 > 1 and 3 > 2 result is True
Or: One of two results is true, the end result is true, such as 2 > 1 or 1 > 2 result is True
Not: Negate, if the result is true then the end result is false, such as not 2 > 1 result is False
Ii. Expressions for Python
An expression is a formula that joins different data (including variables, functions) with operators, such as: a = 123,b > 5
Three, exercises
Write a arithmetic in Python (i.e., subtraction), which requires that when the user enters two numbers, the results of these two numbers subtraction can be calculated automatically
Answer 1 (simple notation):
Answer 2 (complex notation):
Python operators and expressions