2.初识Python基本数据类型

Python中的变量不像其他语言那样需要先定义类型,而是会根据你赋的值来自动匹配类型,type是查看变量的函数,具体请看下面的程序:

>>> a = 2                  #整型
>>> print (a)
2
>>> print type(a)          #print后面跟的是(),不加就会出错,这是3.3版
SyntaxError: invalid syntax 
>>> print (type(a))
<class 'int'>
>>> a = 1.3                #浮点型
>>> print (a, type(a))     #print可以同时打印几个信息,但是需要逗号隔开
1.3 <class 'float'>
>>> a = "hello every"      #字符串
>>> print (a, type(a))
hello every <class 'str'>
>>> a = False              #布尔型
>>> print (a, type(a))
False <class 'bool'>
>>> a = (-1/100)           #在3.3中,会显示下面的结果,而在2.7中会显示-1,需要改为(-1.0/100)或者(-1/100.0)
>>> print (a, type(a))
-0.01 <class 'float'>
>>> 
此外还有复数,自己可以试下。

附上Vamei博客Python网址:

http://www.cnblogs.com/vamei/archive/2012/05/28/2522385.html

原文地址:https://www.cnblogs.com/v-BigdoG-v/p/7398650.html