字符串:

 参见https://www.runoob.com/python3/python3-string.html

列表:

 Python包含6中内建的序列,即列表、元组、字符串、Unicode字符串、buffer对象和 xrange 对象。序列通用的操作包括:索引、长度、组合(序列相加)、重复(乘法)、分片、检查成员、遍历、最小值和最大值

序列中的每个值都有对应的位置值,称之为索引,第一个索引是 0,第二个索引是 1,可以用索引访问的

list1=['Daisy','Runoob',1993,2000]
print(list1[2])#返回1993
print(list1[1:3])#截取列表值,前闭后开['Runoob', 1993]
list1[2]=1997#更新列表值['Daisy','Runoob',1997,2000]
list1.append('百度')#追加['Daisy', 'Runoob', 1997, 2000, '百度']
list1.insert(1,'first')#插入['Daisy', 'first', 'Runoob', 1997, 2000, '百度']
del list1[2]#删除列表元素['Daisy', 'first', 1997, 2000, '百度']
list1.remove(2000)#匹配的列表值进行移除、['Daisy', 'first', 1997, '百度']
list1.pop(2)#删除列表元素['Daisy', 'first', '百度']
print([1,2,3]+[2,'baidu'])#+,*操作符操作,和str一样
print([x*x for x in range(1,5)])#列表生成式,[1, 4, 9, 16]

list2=[['a', 'b', 'c'], [1, 2, 3]]#嵌套列表
print(list2[0][1])#b

list3=[1,5,3,90,76]
list3.sort()
print(list3)#排序[1, 3, 5, 76, 90]

 

  的结果是[1, 6, 8, 3, 22]

 b=a只是浅复制,指向内存地址是一样的。如果想要b不变的3种方式:

1)b  =  a * 1

2)b=a[:]

3)b=list(a)

字典:

my={1:'a',3:'b'}
for key,value in dict.items(my):
    print(key,value)
    if value=='b':
        print("哇,你好厉害,找到我了!")

for lis in my:
    print(lis,my[lis])
    if my[lis]=='b':
        print("我的健是:%s"%lis)
print(len(my))#返回2
print(my.get(1))#返回a
my.pop(3)#删除3健和值 {1: 'a'}
my[1]='c'#更新1的值{1: 'c'}
my[6]="d"#添加新的健值 {1: 'c', 6: 'd'}
#删除健1 del my[1]
#删除字典 del my
'''字典值可以是任何的 python 对象,既可以是标准的对象,也可以是用户定义的,但键不行。
两个重要的点需要记住:
1)不允许同一个键出现两次。创建时如果同一个键被赋值两次,后一个值会被记住
2)键必须不可变,所以可以用数字,字符串或元组充当,而用列表就不行
'''
print(my)

 

  

集合:

 

#集合(set)是一个无序的不重复元素序列。
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket)#{'orange', 'pear', 'apple', 'banana'}
basket.add('xiaxia')#添加元素

b = {x for x in 'abracadabra' if x not in 'abc'}
print(type(b))#<class 'set'>

a=set('ab,c,d3fdadfb')
print(a)#{'f', 'b', 'd', 'c', 'a', ',', '3'}

c=set(("pig","cat","car",1,'car'))#集合 {'cat', 1, 'pig', 'car'}
print(len(c))#4
c.update([1,5])#添加 {1, 5, 'car', 'pig', 'cat'}
c.remove('cat')#移除{1, 5, 'pig', 'car'}

d=("pig","cat","car",1)#元组
print(type(d))

 

  

posted on 2021-06-07 17:03  慢乌龟  阅读(73)  评论(0编辑  收藏  举报