您的位置:首页 > 编程语言 > Python开发

Python基础:1.数据类型(空、布尔类型、整型、长整型、浮点型、字符串)

2015-12-14 14:22 836 查看
提示:python版本2.7,windows系统

Python提供的基本数据类型:空、布尔类型、整型、长整型、浮点型、字符串、列表、元组、字典、日期

1.空(None)

  None,是一个特殊的值,不能说是0,也不是字符串的'',None表示什么也没有,是一个空对象。

2.布尔类型(bool)

  bool,值为:True和False,Python中None,任何数值的0,空字符串'',空列表[],空元组(),空字典{}都是False,还有自定义为类型中实现了__nonzero__(),__len__()方法的返回值为False或0的对象也是False,其他的数据都为True。

>>> bool(True)
True
>>> bool(1)
True
>>> bool('hello world')
True
>>> bool([1])
True
>>> bool((1))
True
>>> bool({'name':'mouMiFan'})
True


>>> bool(False)
False
>>> bool(0)
False
>>> bool()
False
>>> bool({})
False
>>> bool([])
False
>>> bool(())
False
>>> bool(None)
False


3.整型(Int)

  Int,整数,范围为 -2 ** 31 到 2 ** 31 - 1 ,超出这个范围便是长整型,有2进制,8进制,10进制,16进制。用8进制表示整数时,前面要加'0'的前缀,16进制的前缀为'0x'

>>> 017       #8进制
15
>>> 0xF       #16进制
15


4.长整型(Long)

  Long,整数,超出范围为为 -2 ** 31 到 2 ** 31 - 1的数字。后缀为'L'。其他同【整型】

5.浮点型(Float)

  Float,小数,位数是可以变的,如:12.1 / (10 ** 8) 和 1.21 / (10 ** 7) 相等。对于很小或很大的数可以用科学计数法,如:1.21e-09。整型的除法是精确的,而浮点型的除法有可能是四舍五入的。

6.字符串(String)

 String,用单引号或双引号或三引号的括起来的数据。引号不做字符串,只代表一种符号。如:'abc','hello world',"I'm mouMiFan"。如果字符串中包含'或",则用【\】转义,字符串中的【\】本身也要转义。

  \r:回车,\t:制表符,\n:换行符

  三引号括起来的字符串可以跨多行。

>>> string = "string"
>>> print string
string
>>> string = 'string'
>>> print string
string
>>> string = 'I\'m string'
>>> print string
I'm string
>>> print 'this is \n , haha'
this is
, haha
>>> '''this
is
hello
world'''
'this\nis\nhello\nworld'
>>> print '''this
is
hello
world'''
this
is
hello
world
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: