原文来自我的独立blog: http://www.yuanyong.org/blog/python/python-variable-print
我知道我写的一些代码至今仍然在运行,我觉得这是一个令人欣慰的贡献。
动态类型定义语言,因此它不需要像c一样需要在使用前使用显示数据类型声明,下面来验证python动态类型属性:
1 | # int test |
2 | int_test=5 |
3 | type(int_test) # Result: <type 'int'> |
4 | # float test |
5 | float_test=24 |
6 | type(float_test) # Result: <type 'float'> |
7 | # string test |
8 | str_test="willard" |
9 | type(str_test) # Result: <type 'str'> |
10 | # list test |
11 | alist=[1,2,3] |
12 | type(alist) # Result: <type 'list'> |
13 | # tuple(元组) test |
14 | atuple=(2,4,'willard',3.) |
15 | type(atuple) # Result: <type 'tuple'> |
16 | type(atuple[0]) # Result: <type 'int'> |
17 | type(atuple[2]) # Result: <type 'str'> |
18 | type(atuple[4]) # Result: <type 'float'> |
19 | # dict test |
20 | adict={'name':"willard",'language':"python",'age':18} |
21 | type(adict) # Result: <type 'dict'> |
22 | type(adict['name']) # Result: <type 'str'> |
23 | type(adict['age']) # Result: <type 'int'> |
24 | # file test |
25 | file_ex= open('hellopython', 'w') #打开一个名为hellopython的文件,往里面写入数据流,file_ex是文件句柄 |
26 | type(file_ex) # Result: <type 'file' > |
27 | file_ex.write('hello python\n') |
28 | file_ex.close |
先就到这里,困了,午休~
人生苦短,我用python~
Python数据类型实践
本文通过实例演示了Python中不同数据类型的使用方式,包括整型、浮点型、字符串、列表、元组、字典和文件操作等,展示了Python作为动态类型语言的特点。
1254

被折叠的 条评论
为什么被折叠?



