类型判断和强制转化

Checking the Type

单 词 意思 音 标
floor operation 向下取整
mutable 可变的 /ˈmjuːtəb(ə)l/
immutable 不可变的 /ɪˈmjuːtəb(ə)l/
assignment 赋值
函数 例子 注意事项
Checking the Type x=5 type(x) int We can’t concatenate a string and a number…and we shouldn’t be able
Converting Types x=3.3 int_y=int(x) type(int_y) int() is one way to perform a floor operation
Why the Type Matters float_x=5.5 string_y="5" float_x+float(string_y) 10.5 string_y+str(float_x) 55.5

Mutable or Immutable

函数 例子 输出
Mutable Can be changed once created –  a list L can have its first element replaced.
Immutable Can’t be changed once creating – a string S cannot have its first letter changed.
String = Immutable name="LiQing" name[0]="Q" TypeError: ‘str’ object does not support item assignment
if you want to change name="LiQing" new_name="Q"+name[1:] new_name ‘QiQing’
Lists = Mutable L=["1","2","3"] L[0]=100 L 输出结果 [100, ‘2’, ‘3’]
Numbers = Immutable