Python成长记二(基本数据类型列表、元组、集合、字典)
列表:
定义:type([1,2,3,4,5,6]) ---->list
特性:
type(["hello","world",1,9]) --->list
type(["hello","world",1,9,True,False])--->list
type([[1,2],[3,4],[True,False]])---->list (嵌套列表)
列表的基本操作:
["a","b","c","d"][0] --->'a'
["a","b","c","d"][0:2]-->['a', 'b']
["a","b","c","d"][-1:]--->['d']
["a","b","c","d"]+["e","f"]--->['a', 'b', 'c', 'd', 'e', 'f']
["a","b"]*2 ---> ['a', 'b', 'a', 'b']
元组:
定义:type((1,2,3,4,5,6))--->tuple
type((1,"-1",True))--->tuple
(1,2,3,4,5,6)[0]--->1
(1,2,3,4,5,6)[0:2]--->(1, 2)
(1,2,3)+(3,4,5)--->(1, 2, 3, 3, 4, 5)
(1,2,3)*2---->(1, 2, 3, 1, 2, 3)
[1,2,3,4,5][0:3]--->[1, 2, 3] (切片操作)
[1,2,3,4,5][-1:] --->[5]
"hello world"[0:8:2]-->'hlow'
序列共有特性:
3 in [1,2,3,4,5,6]--->True(判断元素是否在序列中)
10 in [1,2,3,4,5,6] -->False
3 not in [1,2,3,4,5,6]-->False
len([1,2,3,4,5])--->5
len("hello world")--->11
max(1,2,3,6,3,4)--->6
min(1,2,3,6,3,4)--->1
max('hello world')--->'w' (通过字符编码(ascll)判断)
min('hello world')--->' '
取ascll的方法:
ord('w')-->119
ord(" ")-->32
ord('d')--->100
集合set
特性:无序、不重复
定义:type({1,2,3,4,5})--->set
len({1,2,3})-->3
1 in {1,2,3}-->True
1 not in {1,2,3}-->False
{1,2,3,4,5,6} - {3,4} --->{1, 2, 5, 6} (两个集合的差集)
{1,2,3,4,5,6} & {3,4} --->{3,4} (交集)
{1,2,3,4,5,6} | {3,4} ---->{1, 2, 3, 4, 5, 6} (合集)
type({}) --->dict
定义空的集合:type(set())--->set
len(set())--->0
字典 dict
定义:{key:value,key2:value2....}
type({1:1,2:3,3:3})---->dict
操作:通过key 得到/访问 value
{1:1,2:3,3:3}[2] ---> 3
{1:1,2:3,1:3}--->{1: 3, 2: 3} (字典不能有重复的键)
{1:1,2:3,1:3}[1]---->3
{1:1,2:3,'1':3}---->{1: 1, 2: 3, '1': 3} (字符串和数字不同)
value:str int float list set dict (任意类型)
{1: 1, 2: {1:3}, '1': 3} ---->{1: 1, 2: {1: 3}, '1': 3}
key:必须是不可变类型(不能是任意类型),比如 int 、 'str'
定义:type([1,2,3,4,5,6]) ---->list
特性:
type(["hello","world",1,9]) --->list
type(["hello","world",1,9,True,False])--->list
type([[1,2],[3,4],[True,False]])---->list (嵌套列表)
列表的基本操作:
["a","b","c","d"][0] --->'a'
["a","b","c","d"][0:2]-->['a', 'b']
["a","b","c","d"][-1:]--->['d']
["a","b","c","d"]+["e","f"]--->['a', 'b', 'c', 'd', 'e', 'f']
["a","b"]*2 ---> ['a', 'b', 'a', 'b']
元组:
定义:type((1,2,3,4,5,6))--->tuple
type((1,"-1",True))--->tuple
(1,2,3,4,5,6)[0]--->1
(1,2,3,4,5,6)[0:2]--->(1, 2)
(1,2,3)+(3,4,5)--->(1, 2, 3, 3, 4, 5)
(1,2,3)*2---->(1, 2, 3, 1, 2, 3)
type((1))---->int (括号与数学运算符有冲突,规定只有一个元素时做数学运算)
type((1,)) --->tuple
type(())---->tuple
对比:type([1])--->list
str、list、tuple都是序列(1.有顺序,2.每个元素分配有序号)[1,2,3,4,5][0:3]--->[1, 2, 3] (切片操作)
[1,2,3,4,5][-1:] --->[5]
"hello world"[0:8:2]-->'hlow'
序列共有特性:
3 in [1,2,3,4,5,6]--->True(判断元素是否在序列中)
10 in [1,2,3,4,5,6] -->False
3 not in [1,2,3,4,5,6]-->False
len([1,2,3,4,5])--->5
len("hello world")--->11
max(1,2,3,6,3,4)--->6
min(1,2,3,6,3,4)--->1
max('hello world')--->'w' (通过字符编码(ascll)判断)
min('hello world')--->' '
取ascll的方法:
ord('w')-->119
ord(" ")-->32
ord('d')--->100
集合set
特性:无序、不重复
定义:type({1,2,3,4,5})--->set
len({1,2,3})-->3
1 in {1,2,3}-->True
1 not in {1,2,3}-->False
{1,2,3,4,5,6} - {3,4} --->{1, 2, 5, 6} (两个集合的差集)
{1,2,3,4,5,6} & {3,4} --->{3,4} (交集)
{1,2,3,4,5,6} | {3,4} ---->{1, 2, 3, 4, 5, 6} (合集)
type({}) --->dict
定义空的集合:type(set())--->set
len(set())--->0
字典 dict
Key Value
很多个key和value,集合类型(set),非序列定义:{key:value,key2:value2....}
type({1:1,2:3,3:3})---->dict
操作:通过key 得到/访问 value
{1:1,2:3,3:3}[2] ---> 3
{1:1,2:3,1:3}--->{1: 3, 2: 3} (字典不能有重复的键)
{1:1,2:3,1:3}[1]---->3
{1:1,2:3,'1':3}---->{1: 1, 2: 3, '1': 3} (字符串和数字不同)
value:str int float list set dict (任意类型)
{1: 1, 2: {1:3}, '1': 3} ---->{1: 1, 2: {1: 3}, '1': 3}
key:必须是不可变类型(不能是任意类型),比如 int 、 'str'
空字典定义:{}
总结:如下思维导图