table of Contents
Blog 04
1. Data Type
(1) List
1. 作用:存储多个(任意数据类型)元素
2. 定义方式,[]内用逗号隔开多个元素(任意数据类型)
3. 使用方法:用索引操作
(2) Dictionary
1. 作用:存储多个值,但是每个值都由描述信息
2. 定义方式: {}内用逗号隔开多个键(描述,用字符串):值(具体的值,可以为任意数据类型)对
3. 使用方法:按键取值、修改值(字典没有索引)
(3) Boolean
#布尔类型只有两个值,一个为True,一个为False,布尔类型不直接打印,而是在特定条件下触发
#条件成立为True,条件不成立为False
print(1 > 2)
print(2 > 1)
print(1 != 1)
#所有数据类型自带布尔值,除了 0/None/空(空字符/空列表/空字典)/False 之外所有数据类型自带布尔值为True。
2. Extract
Solution (unlock) compression (data type containers): 2-3 for extracting only the elements of container type
#单个下划线表示这个东西不需要(约定俗成)
lt = [1, 2, 3, 4, 5]
s1,_,_,_,_ = lt
print(s1)
print(_) # 可以打印,但是不要去打印
#*_: *把后面的元素全部合成放到列表里去了
s1,*_,s5 = lt
print(s1)
print(_)
3. Fancy assignment
- Chain assignment
#对于
a = 10
b = 10
c = 10
#链式赋值
a = b = c = 10
Cross assignment
#将x变成20,y变成10 x = 10 y = 20 #基础方法: z = y y = x x = z #交叉赋值: x, y = y, x
4. python interaction with a user
- Users say, a computer (python program) answer one
s = input('请输入姓名:') # 1. 可以让程序暂停
Python2 and python3 difference in input: Enter what type is what type, after the input written raw_input, with the input of the same effect in python2 in python3
SUMMARY python3 are entered in the input string type
5. Output Formatting
- fstring method (strongly recommended to use this method, the other two methods basically cool)
- % S (the output string);% f (output Float);% d (output type)
- format method
m = input('输入一个数')m =float(m) ** 0.5 #只能求正数的平方根
print(' 它的平方根是: %0.3f %d 很好玩 ' % ( m,m))
print('{0:0.3f} 很好玩 {1}'.format(m,m))
print(f'它的平方根是: {m:0.3f}')
# %0.3f中 :小数点前的数不影响结果,小数点后的数表示保留的小数位数
#f 表示这是一个浮点数,也可以用%d替换%0.3f ,结果就会只取小数点前的整数'''