Python 基础之变量类型
基本数据类型
Python有五个标准的数据类型:
- Number(数字)
- String(字符串)
- List(列表)
- Tuple(元组)
- Dictionary(字典)/Set(集合)
数字
Python支持四种不同的数值类型:
int(有符号整型)
long(长整型[也可以代表八进制和十六进制])
float(浮点型)
complex(复数)
数字是不可改变的数据类型,这意味着改变数字数据类型会分配一个新的对象。
例子1:
# coding=utf-8
__author__ = 'Z'
a = 1
b = 1.5
c = 123456789123
d = 123L
e = 2 + 3j
print 'a:', a, type(a)
print 'b:', b, type(b)
print 'c:', c, type(c)
print 'd:', d, type(d)
print 'e:', e, type(e)
print '2**32=', 2 ** 32
运行结果:
a: 1 <type 'int'>
b: 1.5 <type 'float'>
c: 123456789123 <type 'long'>
d: 123 <type 'long'>
e: (2+3j) <type 'complex'>
2**32= 4294967296
例子2:
# coding=utf-8
__author__ = 'Z'
a = 1
b = 2
print 'a:', a, id(a)
print 'b:', b, id(b)
del a
print a
运行结果:
a: 1 7011808
b: 2 7011796
Traceback (most recent call last):
File "test.py", line 10, in <module>
print a
NameError: name 'a' is not defined
布尔bool
布尔“数”,本质上是整型的子类,对应于整型的1和0。
# coding=utf-8
__author__ = 'zyt'
print int(True)
print int(False)
运行结果:
1
0
字符串str
raw strings例子,当不想让\
转义字符时,可在引号前放一个r
:
# coding=utf-8
__author__ = 'zyt'
print 'C:\some\name' # here \n means newline!
print r'C:\some\name' # raw strings:note the r before the quote
运行结果:
C:\some
ame
C:\some\name
- 谈谈
raw_input()
:
从标准输入读取字符串,读取值作为字符串形式返回
raw_input(…)
raw_input([prompt]) -> string
raw_input例子:
# coding=utf-8
__author__ = 'zyt'
a = raw_input("input something:")
print a, type(a)
运行结果:
input something:123
123 <type 'str'>
元组tuple
元组可用( )
创建。内部元素用逗号隔开。但是元素不能二次赋值,相当于只读列表。
注意当定义的元组仅有1个元素时,也要加上逗号,否则就跟数学公式中的小括号发生歧义了:
# coding=utf-8
__author__ = 'zyt'
a = (1) # 按数学公式小括号运算
b = (1,)
print a, type(a)
print b, type(b)
运行结果:
1 <type 'int'>
(1,) <type 'tuple'>
列表list
列表可用[ ]
创建。
# coding=utf-8
__author__ = 'Z'
a = [1, 2, 3]
b = ['hello', 'world']
print type(a)
print a[0:2]
print a + b # +号是连接运算符
print a * 3 # *号是重复操作次数
运行结果:
<type 'list'>
[1, 2]
[1, 2, 3, 'hello', 'world']
[1, 2, 3, 1, 2, 3, 1, 2, 3]
字典dict
字典可用{ }
创建。字典由索引(key)和它对应的值value即key:value
对组成。
几乎所有类型的Python对象都可以用作键,不过一般还是以不能改变的数字或者字符串最为常用。
# coding=utf-8
__author__ = 'zyt'
a = {'one': 1, 'two': 2, 'three': 3}
print a, type(a)
print a['two']
print a.keys()
print a.values()
运行结果:
{'three': 3, 'two': 2, 'one': 1} <type 'dict'>
2
['three', 'two', 'one']
[3, 2, 1]
集合set
集合也是可用{ }
创建,内部元素间用逗号分割。set和dict类似。
# coding=utf-8
__author__ = 'zyt'
s = {4, 5, 6, 7, 8}
print s, type(s)
print 5 in s
print 6 not in s
print 10 in s
if 5 in s:
print 'yes in it'
else:
print 'not in it'
运行结果:
set([8, 4, 5, 6, 7]) <type 'set'>
True
False
False
yes in it
对象值的比较、对象身份的比较
参考《Python核心编程(第二版)WesleyJChun著-宋吉广译-4.5.2节》
# coding=utf-8
__author__ = 'zyt'
a = b = 8.5
c = 7.0 + 1.5
print 'id(a):', id(a)
print 'id(b):', id(b)
print 'id(c):', id(c)
print 'a==b:', a == b
print 'a==c:', a == c
print 'a is b:', a is b # 等价于 id(a)==id(b)
print 'a is c:', a is c
运行结果:
id(a): 5393952
id(b): 5393952
id(c): 5393904
a==b: True
a==c: True
a is b: True
a is c: False
标准类型的分类
- 按存储模型分类
分类 | Python类型 |
---|---|
标量/原子类型 | 数值(所有的数值类型),字符串(全部是文字) |
容器类型 | 元组、列表、字典 |
- 按更新模型分类
分类 | Python类型 |
---|---|
可变类型 | 列表、字典 |
不可变类型 | 数字、字符串、元组 |
- 按访问模型分类
分类 | Python类型 |
---|---|
直接访问 | 数字 |
顺序访问 | 字符串、元组、列表 |
映射访问 | 字典 |