数据类型转换
1 2 3 4 5 6 7
| name ='张三' age = 20 print(name,type(name)) print(age,type(age))
print(name + str(age))
|
张三 <class 'str'>
20 <class 'int'>
张三20
str() 其他数据转换为字符串
1 2 3 4 5
| a = 10 b = 198.01 c = False print(type(a),type(b),type(c)) print(str(a),str(b),str(c),type(str(a)),type(str(b)),type(str(c)))
|
<class 'int'> <class 'float'> <class 'bool'>
10 198.01 False <class 'str'> <class 'str'> <class 'str'>
int()其他数据转换为int
只能换船整数类型字符串
1 2 3 4 5 6 7 8 9 10
| s1 = '198' s2 = 98.1 s3 = '12.12' s4 = True s5 = 'hello' print(type(s1),type(s2),type(s3),type(s4),type(s5)) print(int(s1),type(int(s2)))
print(int(s4),type(int(s4)))
|
<class 'str'> <class 'float'> <class 'str'> <class 'bool'> <class 'str'>
198 <class 'int'>
1 <class 'int'>
float其他数据转换为float
1 2 3 4 5 6 7 8 9 10 11
| s1 = '198.98' s2 = '98' s3 = True s4 = 'hello' s5 = 100 print(type(s1),type(s2),type(s3),type(s4),type(s5)) print(float(s1),type(float(s1))) print(float(s2),type(float(s2))) print(float(s3),type(float(s3)))
print(float(s5),type(float(s5)))
|
<class 'str'> <class 'str'> <class 'bool'> <class 'str'> <class 'int'>
198.98 <class 'float'>
98.0 <class 'float'>
1.0 <class 'float'>
100.0 <class 'float'>
Python中的注释
注释
在代码中对代码的功能进行解释说明的标注性文字,可以提高代码的可读性
・注释的内容会被 Pythons解释器忽略通常包括三种类型的注释
- 单行注释以”#”开头,直到换行结束
- 多行注释、并没有单独的多行注释标记,将一对三引号之间的代码称为多行注释
- 中文编码声明注释→在文件开头加上中文声明注释,用以指定源码文件的编码格式