Python basic study notes (1)-operators and operational variables

The naming rules of variables:

Variable names can only be any combination of letters (az, AZ), numbers (0-9), or underscores (_ ).
The first character of a variable name cannot be a number and is case sensitive.
Keywords cannot be declared as variable names.

Variable operators:

1. Assignment operator

Operator Example
= c = a + b means that the value of a + b is assigned to c
+= a += b is equivalent to a = a+b
-= a += b is equivalent to a = a+b
*= a *= b is equivalent to a = a * b
/= a /= b is equivalent to a = a / b
%= a %= b is equivalent to a = a% b
**= a **= b is equivalent to a = a ** b
//= a // = b equivalence relation a = a // b

2. Arithmetic operator
Assume a=10, b=20, c=5

Operator description Example
+ addition c = a + b ( c = 30)
- Subtraction c = a - b (c = -10)
* multiplication c = a * b (c = 200)
/ division c = a / b (c = 0.5)
% Modular operation c = b % a (c = 0)
** Exponent (power) calculation a ** b, which means 10 to the 20th power
// Floor except 9//2 = 4 , 9.0//2.0 = 4.0 , -11//3 = -4 , -11.0//3 = -4.0

Note: ①// Floor division: No matter what type of operand, the result is always rounded off the decimal part, but if an operand is negative, the result will be retained, that is, rounded off from zero (to negative infinity).
② In the arithmetic operator, True represents 1, and Flase represents 0.

3. Comparison (relational) operator
Assuming a=10, b=20

Operator description Example
== equal (a == b) The result is: False
!= not equal to (a != b) The result is: True
> more than the (a> b) The result is: False
>= greater or equal to (a >= b) The result is: False
< Less than (a <b) The result is: True
<= Less than or equal to (a <= b) The result is: True

4. Logical operators
assume that a, b are True, and c is False

Operator description Example
and If the values ​​on both sides are true, the condition is true, and if either of the two sides is false, then it is false (a and b) The result is: True
(a and c) The result is: False
or If any value on either side of the operator is true, the condition is true (True), and if both are false, it is false (False) (a or b) The result is: True
(a or c) The result is: True
not Invert the logic state of the value on the right side of the operator The result of (not a) is: False The result of
(not c) is: True

5. Bitwise operator
Bitwise operator performs a bitwise operation, a form of binary number operation.
Python's built-in function bin() can be used to obtain the binary representation of an integer.
Convert the number to the form of two's complement for calculation. For example:
a=234 Two's complement is: 1110 1010
b=100 Two's complement is: 0110 0100

Operator description Example
& Bitwise AND (1&1=1, 1&0=0, 0&0=0, 0&1=0 ie x&0=0) a(1110 1010) &b(0110 0100)=c(0110 0000)
234 & 100 = 96
Bitwise OR (1丨1=1, 1丨0=1, 0丨0=0, 0丨1=1 ie x丨1=1) a(1110 1010)丨b(0110 0100)=c(1110 1110)
234 丨100 =238
^ 按位翻转(1=0,0=1)为单目运算符不能10可以1+0 ~a(1110 1010)=a(0001 0101)
<< 左移 ,将一个数字为x的二进制数向左移动N位 a(1110 1010)<<2=a(1010 1000)
>> 右移,将一个数字为想x的二进制数向右移动N位 a(1110 1010)>>2=a(0011 1010)

6、成员运算符
身份运算符比较两个对象的内存位置。常用的有两个身份运算符

操作符 描述
is 如果操作符任意一侧的变量指向相同的对象,则返回True,否则返回False
is not 如果操作符任意一侧的变量不是指向相同的对象,则返回True,否则返回False

7、运算优先级
注:从高到低排序,同一行优先级相同

优先级 操作符
1 **
2 *,/,//,%
3 +,-
4 <=,<,>,>=

Guess you like

Origin blog.csdn.net/qq_46485161/article/details/114365311
Recommended