首页 > 代码库 > python类型
python类型
每天学习一点点,通过例子
字符串
>>> word=‘asdf‘
>>> word[:2]
‘as‘
>>> word[1:]
‘sdf‘
>>> a= [x for x in ‘abcdsf‘ if x not in ‘abc‘]
>>> a
[‘d‘, ‘s‘, ‘f‘]
链表
ques = [‘a‘,‘b‘,‘c‘]
ans = [‘1‘,‘2‘,‘3‘]
for q,a in zip(ques,ans)
print q,a
字典
>>> person ={‘name‘:‘xiaoli‘,‘age‘:21,‘sex‘:‘male‘}
>>> person
{‘age‘: 21, ‘name‘: ‘xiaoli‘, ‘sex‘: ‘male‘}
>>> person[‘name‘]
‘xiaoli‘
>>> person[‘name‘] = ‘xiaohong‘
>>> person
{‘age‘: 21, ‘name‘: ‘xiaohong‘, ‘sex‘: ‘male‘}
>>> del person[‘sex‘]
>>> person
{‘age‘: 21, ‘name‘: ‘xiaohong‘}
>>> list(person.keys())
[‘age‘, ‘name‘]
>>> for k,v in person.items():
... print (k,v)
...
(‘age‘, 21)
(‘name‘, ‘xiaohong‘)
元组
>>> a =123,456,‘asd‘
>>> a
(123, 456, ‘asd‘)
>>> a[0]
123
>>> b = a,‘ertr‘,‘dfg‘
>>> b
((123, 456, ‘asd‘), ‘ertr‘, ‘dfg‘)
>>> c = ([1,2,3],[3,2,1])
>>> c
([1, 2, 3], [3, 2, 1])
python类型