1. The input function input() in Python
- Introduction to the input() function
- Basic use of input() function
-
code demo
- code writing
# 输入函数input() present = input("你想要什么礼物呢?") print(present, type(present))
- Result analysis
你想要什么礼物呢?巧克力 巧克力 <class 'str'>
2. Operators in Python
- Common Operators in Python
2.1 Arithmetic operators
2.1.1 Arithmetic operators
standard arithmetic operators | Add (+), Subtract (-), Multiply (*), Divide (/), Divide evenly (/) |
---|---|
remainder operator / modulo operator | % |
exponentiation operator | ****** |
-
code demo
- code writing
print(1 + 1) # 加法运算 print(1 - 1) # 减法运算 print(2 * 4) # 乘法运算 print(11 / 2) # 除法运算 print(11 // 2) # 整除运算 print(11 % 2) # 取余运算(取模运算) print(2 ** 3) # 幂运算,表示的是2的三次方
2.1.2 Operation rules
In the operation of remainder and integer division, special attention should be paid to follow the formula when the operation data is one positive and one negative
Take the remainder (one positive and one negative), the formula for taking the remainder: remainder = dividend - divisor * quotient ; to divide two numbers, the dividend is divided by the divisor
Divisibility (one positive and one negative), the formula for divisibility: round down **
operator | express | example | result |
---|---|---|---|
+ | add | 1+1 | 2 |
- | reduce | 1-1 | 0 |
* | take | 2*4 | 8 |
/ | remove | 1/2 | 0.5 |
% | Remainder (full positive) | 9%4 | 2 |
remainder (all negative) | -9%-4 | 2 | |
Take the remainder (one positive and one negative), the formula for taking the remainder: remainder = dividend - divisor * quotient; to divide two numbers, the dividend is divided by the divisor | -9%4 | -3 | |
** | exponentiation | 2**3 | 8 |
// | Evenly divisible (all positive), take the quotient value | 11/2 | 5 |
divisible (all negative) | -11/-2 | 5 | |
Divisibility (one positive and one negative), divisive formula: round down | -9 // 4 | -3 |
-
code demo
- code writing
# 一整一负的整除和取余运算 print(9 // 4) # 2 print(-9 // -4) # 2 # 整除运算,取商值 print(9 // -4) # -3 print(-9 // 4) # -3 # 一正一负,整除公式:向下取整 print(9 % -4) # -3=9-(-4)*(-3) print(-9 % 4) # 3=-9-(4)*(-3) # 一正一负,取余公式:余数=被除数-除数*商;两数相除,被除数 除以 除数
2.2 Assignment operator
-
Assignment operator symbol: =
-
Execution order: right --> left
-
Support chain assignment : a=b=c=20
-
Support parameter assignment : +=, -=, *=, /=, //=, %=
-
Support series unpacking assignment : a,b,c=20,30,40
-
code analysis
- code writing
i = 3 + 4 print(i) # 7 print("--------------链式赋值---------------") a = b = c = 20 # 链式赋值 print(a, id(a)) # 20 2474963921808 print(b, id(b)) # 20 2474963921808 print(c, id(c)) # 20 2474963921808 print("--------------支持参数赋值---------------") a = 20 a += 30 # 相当于a=a+30 print(a) # 50 a -= 10 # 相当于a=a-10 print(a) # 40 a *= 10 # 相当于a=a*10 print(a, type(a)) # 400< class 'int'> a /= 10 # 相当于a=a/10 print(a, type(a)) # 40.0 <class 'float'> print("--------------支持系列解包赋值---------------") a, b, c = 20, 30, 40 print(a, b, c) # 20 30 40 print("--------------交换两个对象的值---------------") a, b = 10, 20 print("交换前a和b的值", a, b) # 交换前a和b的值 10 20 # 交换 a, b = b, a print("交换后a和b的值", a, b) # 交换后a和b的值 20 10
2.3. Comparison Operators
- Comparison operators: compare the results of variables or expressions in terms of size , true or false, etc.
2.3.1 Comparison operators
comparison operator | |
---|---|
< > >= <= != | |
== | Comparison of object values |
is is not | Comparison of object ids |
-
code demo
- code writing
a, b = 10, 20 print("a>b吗?", a > b) # False print("a>b吗?", a < b) # True
2.3.2 == and =
-
A = becomes the assignment operator
-
Two == are called comparison operators
-
A variable consists of three parts, identification (id), type (type), value (value)
-
== compares the value of the object (value)
-
The identity (id) of the comparison object
-
code demo
- code writing
a = 10 b = 10 print(a == b) # True 说明a和b的值value相等 print(a is b) # True 说明a与b的标识id相等 print(a is not b) # False,a的id和b的id不相等 lst1 = [11, 22, 33, 44] lst2 = [11, 22, 33, 44] print(lst1 == lst2) # True print(lst1 is lst2) # False print(id(lst1)) # 1810724741952 print(id(lst2)) # 1810733359616 print(lst1 is not lst2) # True
2.4. Boolean operators
2.4.1 Illustration of Boolean operators
operator | Operand 1 | Operand 2 | operation result | Remark |
---|---|---|---|---|
and | True | True | True | The result of the operation is True only if both operands are True |
True | False | False | ||
False | True | False | ||
False | False | False | ||
or | True | True | True | As long as one of the operands is True, the result of the operation is True |
True | False | True | ||
False | True | True | ||
False | False | False | ||
not | True | False | If the operand is True, the result of the operation is False | |
False | True | If the operand is False, the result of the operation is True |
-
code demo
- code writing
a, b = 1, 2 print("----------and 并且-------------") print(a == 1 and b == 2) # True print(a == 1 and b < 2) # False print(a != 1 and b == 2) # False print(a != 1 and b != 2) # False print("----------or 或者-------------") print(a == 1 or b == 2) # True print(a == 1 or b < 2) # True print(a != 1 or b == 2) # True print(a != 1 or b != 2) # False print("----------not 取反-------------") f = True f2 = False print(not f) # False print(not f2) # True print("----------in 和 not in-------------") s = "hello world" print("w" in s) # True print("w" not in s) # False print("u" in s) # False print("u" not in s) # True
2.5 Bitwise operators
- Bit operator: convert data into binary for calculation
2.5.1 Illustration of bitwise operators
operator | significance | Calculation |
---|---|---|
& | bitwise AND operation | The corresponding digits are all 1, the result digit is 1, otherwise it is 0 |
| | bitwise OR operation | The corresponding digits are all 1, the result digit is 1, otherwise it is 0 |
<< | left shift operator | The high bit overflow is discarded, and the low bit is filled with 0 |
>> | right shift operator | The lower bit overflows and is discarded, and the higher bit is filled with 0 |
3. Operator precedence
- Operation Priority Icon
算术运算 > 位运算 > 比较运算 > 布尔运算 > 复制运算
有括号()先计算括号中的