python 中有5种标准类型
1 numbers(数字)
2 string(字符串)
3 list(列表)
4 tuple(元组)
5 dict(字典)
数字
python支持4种类型的数字
- int (有符号整型)
- long (长整型[八进制或十六进制])
- float (浮点型)
- complex (复数)
字符串、列表、元组
- str=” ” , list = [] , tuple=()
- 2种索引,从左到右默认下标从0开始,从右到左默认下标从-1开始
- 可以用[头下标:尾下标]来选取部分元素
- +表示连接操作,*表示重复操作
- 列表和元组可以嵌套,元组中的元素不可以改变,列表中可以
str = 'hello world'
print str[0:3]
print str[-1]
print str * 2
print str + "ok"
list = ['sleepy', 134, 'canwe', 456]
tinylist = [ list, 123]
print list
print list[0][1]
print list[2:]
print list[0][1:] * 2
print list + tinylist
运行结果
hel
d
hello worldhello world
hello worldok
['sleepy', 134, 'canwe', 456]
l
['canwe', 456]
leepyleepy
['sleepy', 134, 'canwe', 456, ['sleepy', 134, 'canwe', 456], 123]
字典
- 字典用{}来表示
- 字典由索引(keys)和键值(values)组成
list = ['sleepy', 134, 'canwe', 456]
dict = {}
dict['one'] = "this is one"
dict[2] = "this is two"
dict[3] = [123, list]
print dict
print dict[3]
print dict.keys()
print dict.values()
运行结果
{2: 'this is two', 3: [123, ['sleepy', 134, 'canwe', 456]], 'one': 'this is one'}
[123, ['sleepy', 134, 'canwe', 456]]
[2, 3, 'one']
['this is two', [123, ['sleepy', 134, 'canwe', 456]], 'this is one']