Python有五个标准数据类型。
- Numbers(数字):
数字数据类型用于储存数值。
整型 (int):100;-785;080;-0x260;0x260 | 长整型 (long):54266L;-15524L;0xASDQWFASD |
---|---|
浮点型(float)0.0;15.20;-21.9;32.3e+18;70.2E-12 | 复数(complex):3.14j;45.j;.854.j;4.53e-7j |
- String(字符串)
字符串或串是由数字,字母,下划线组成的一串字符,它是编程语言中表示文本的数据类型。
Python的字串列表有两种取值顺序:
1.从左到右索引默认从0开始,最大范围是字串长度-1。
2.从右到左索引默认从-1开始,最大范围是字串开头。
3.[头下标,尾下标]
原字符串 | a | b | c | d | e | f |
---|---|---|---|---|---|---|
从前索引 | 0 | 1 | 2 | 3 | 4 | 5 |
从后索引 | -6 | -5 | -4 | -3 | -2 | -1 |
E.g:
str = 'hello world'
print(str)
print(str[0])
print(str[2:5])
print(str[2:])
print(str*2)
print(str+'test')
letters=['a','b','c','d','e','f','g','h']
print(letters[1:8:2])
结果:
hello world
h
llo
llo world
hello worldhello world
hello worldtest
['b', 'd', 'f', 'h']
- List(列表)
列表用[头下标,尾下标]表示,是Python通用的复合数据类型。
E.g:
list=['runner',786,2.23,'john',70.2]
tinylist=[123,'john']
print(list)
print(list[0])
print(list[1:3])
print(list[2:])
print(tinylist*2)
print(list+tinylist)
结果:
['runner', 786, 2.23, 'john', 70.2]
runner
[786, 2.23]
[2.23, 'john', 70.2]
[123, 'john', 123, 'john']
['runner', 786, 2.23, 'john', 70.2, 123, 'john']
- Tuple(元组)
类似于List,用( )表示,元组内部用逗号隔开,并且元组不能二次赋值,相当于只读列表。
E.g:
tuple=('runner',786,2.23,'john',70.2)
tinytuple=(123,'john')
print(tuple)
print(tuple[0])
print(tuple[1:3])
print(tuple[2:])
print(tinytuple*2)
print(tuple+tinytuple)
结果:
('runner', 786, 2.23, 'john', 70.2)
runner
(786, 2.23)
(2.23, 'john', 70.2)
(123, 'john', 123, 'john')
('runner', 786, 2.23, 'john', 70.2, 123, 'john')
列表允许更新,元组不允许更新。
- Dictionary(字典)
列表是有序的对象集合,字典是无序的对象集合。
两者的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。
字典用{ }标识,字典由索引(key)和它对应的值(value)组成。
E.g:
dict={}
dict['one']='this is one.'
dict[2]='this is two.'
tinydict={'name':'john','code':89,'sex':'man'}
print(dict)
print(dict['one'])
print(dict[2])
print(tinydict)
print(tinydict.keys())
print(tinydict.values())
结果:
{'one': 'this is one.', 2: 'this is two.'}
this is one.
this is two.
{'name': 'john', 'code': 89, 'sex': 'man'}
dict_keys(['name', 'code', 'sex'])
dict_values(['john', 89, 'man'])
少年不被楼层误,余生不羁尽自由。
加油,加油!