序列总结(str, list, tuple)

str,list,tuple 都是序列,序列即是有序的

序列的共同操作:

1、通过序号访问数据

>>> 'hello world'[0]
'h'
>>> [1,2,3,4,'abc'][2]
3
>>> (2,3,'python',4)[2]
'python'

2、切片操作

>>> [1,2,3,4,'abc','python'][-3:-1]
[4, 'abc']
>>> 'hello world'[0:2]
'he'
>>> (1,2,3,4,'abc','python')[-3:-1]
(4, 'abc')

3、+ 、 *  操作

>>> 'hello' + 'world'
'helloworld'
>>> 'hello world'*3
'hello worldhello worldhello world'

4、in, not in 操作

>>> 'h' in 'hello world'
True
>>> 'python' in [1,2,3,4,'abc','python']
True
>>> 'python' not in [1,2,3,4,'abc','python']
False

5、len() ,max() , min()  操作

>>> len('hello world')
11
>>> len([1,2,3,4,'abc','python'])
6
>>> len((1,3,4,'abc','python'))
5
>>> max('hello world')
'w'
>>> min('hello world')
' '
>>> max([1,2,3,4,'abc','python'])
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    max([1,2,3,4,'abc','python'])
TypeError: '>' not supported between instances of 'str' and 'int'
>>> min([1,2,3,4])
1
>>> max((1,2,3,4))
4

查询’w‘的ascii的位置

>>> ord('w')
119
>>> ord(' ')
32
>>> ord(1)
Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    ord(1)
TypeError: ord() expected string of length 1, but int found


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值