序列总结

str,list,tuple 都是序列

序列很重要的特性:

  • 每一个元素都会被分配一个序号
  • 切片
>>> [1,2,3][1]
2
>>> [1,2,3][-1:]
[3]
>>> "hello world"[1]
'e'
>>> "hello world"[0:8]
'hello wo'
>>> "hello world"[0:8:2]
'hlow'

如何判断是否是序列中的元素

1, in

>>> 3 in [1,2,3,4,5]
True
>>> "A" in "ABC"
True
>>> "A" in "BC"
False

2, not in

>>> 1 not in [1,2,3]
False
>>> 1 not in (2,3)
True
>>> "1" not in "123"
False

序列的长度len()

>>> len("abc")
3
>>> len([1,2,3])
3
>>> len(())
0
>>> len((1,2,3))
3

max()/min()求序列中的最大数和最小数

str: ASCII的映射关系

>>> max([1,2,3])
3
>>> max((1,2,3))
3
>>> max("abc")
'c'
>>> max("123")
'3'
>>> min([1,2,3])
1
>>> min((1,2,3))
1
>>> min("abc")
'a' #97
>>> min("123")
'1'
>>> min("aBA")
'A' #65

如何查看ASCII值?

>>> ord("a")
97
>>> ord("1")
49
>>> ord("a")
97
>>> ord("A")
65
>>> ord("0")
48
>>> ord("\n")
10

results matching ""

    No results matching ""