原文来自我的独立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~