获取迭代对象总个数 len()

  • s1=‘hello’

  • print(len(s1))

for循环

  • 有限循环

  •  s1=‘hello’
    for  i in s1:
        print(i)#自动从前到后打印s1所有元素
s1=‘321’

for i in s1:

   print('倒计时{}秒'.format(i))

print(i)  #返回 1,i是在for循环里创建的变量,一直在系统内存里

列表 list 容器型数据类型

  • str 存储少量的数据

  • str 操作得到的都是字符串,存储数据单一

  • 表示:l1=[‘abc’,1234,True],存储任意数据类型,存储大量数据

  • 列表是有序的,可索引,可切片

  • 列表查询速度很慢

  • str字符串的replace方法不能改变原字符串的值

列表索引和切片

li = [100,'abc',True,[1,2,3]]
# index
print(li[0],type(li[0])) #100 <class 'int'>
print(li[3],type(li[3])) #[1, 2, 3] <class 'list'>
# 切片
print(li[:3:2]) #[100, True]
print(li[1:3]) #['abc', True]
print(li[1:4:2]) #['abc', [1, 2, 3]]

列表的增删改查

  • 创建

    #创建
    #方式一
    li = [1,3,4]
    print(li) #[1, 3, 4]
    #方式二
    l2=list('hello')
    print(l2) #['h', 'e', 'l', 'l', 'o']
  • 增删改

#增删改
l1 = ['Tom','Cat','Lily']
l1.append('lucy')
print(l1) #['Tom', 'Cat', 'Lily', 'lucy']

#小测试,模拟持续录入员工,按q 退出
while 1:
   name = input('Entry a name:')
   if name == 'q':
       break
   else:
       l1.append(name)
       print(l1)
#insert 插入指定位置
l1.insert(1,'Jerry')
print(l1)  #['Tom', 'Jerry', 'Cat', 'Lily', 'lucy']
#extend 把str 分割以后插入到列表
l1 = ['Tom','Cat','Lily']
l1.extend('abc')
print(l1) #['Tom', 'Cat', 'Lily', 'a', 'b', 'c']
l1.extend(['tt',3,4])
print(l1) #['Tom', 'Cat', 'Lily', 'a', 'b', 'c', 'tt', 3, 4]
#pop 删
l1 = ['Tom','Cat','Lily']
name=l1.pop(-1) #pop删除返回的是索引处的元素 默认删除最后一个   l1.pop()
print(name) #Lily
print(l1)
#remove  删除指定元素,重名的话删除第一个
l1 = ['Tom','Cat','Lily','Cat']
l1.remove('Cat')
print(l1) #['Tom', 'Lily', 'Cat']
#del 按照索引删除
l1 = ['Tom','Cat','Lily','Cat']
# del l1[-1]
print(l1) #['Tom', 'Cat', 'Lily']
del l1[1:3] #按照切片删除
print(l1) #['Tom', 'Cat']
#改(说明list的值是可变的)
l1 = ['Tom','Cat','Lily','Cat']
l1[0]='Jerry' #按照索引改
print(l1) #['Jerry', 'Cat', 'Lily', 'Cat']

列表的嵌套

l1=[1,2,'abc',[3,'hello ',4]]

  • 将l1中的abc变成大写放回原处

l1=[1,2,'abc',[3,'hello ',4]]
l1[2]=l1[2].upper()
print(l1)
  • 将l1中的[3,'hello ',4]追加一个元素5

l1=[1,2,'abc',[3,'hello ',4]]
l1[3].append(5)
print(l1)
  • 将l1中的hello 追加一个world

l1=[1,2,'abc',[3,'hello ',4]]
l1[3][1]=l1[3][1]+'world'
print(l1)

元组不能增删改,只能查,但是元组中的列表可以增删改

  • 应用:用户名,密码,个人信息等不想让人改动的信息可以存储在元组中

  • 元组拆包:a,b=(1,2)

  • print(a,b) #返回 1 2

range

  • 类似列表,里面只能是数字

r = range(10)
for i in r:
   print(i) #打印 0 -9
  • 可以一句代码表示

for i in range(10):print(i)

字典

  • 列表可以存储大量的数据,但是数据之间的关联性不强

  • 列表查询速度慢

  • dict 字典 是容器数据类型

  • 数据类型分类(可变和不可变)

  • 可变类型:list,dict,set

  • 不可变类型:int str bool tuple

  • dict {}括起来,以键值对的形式存储容器型数据类型

dic = {'Lee':{'name':'Leo','age':'20'},

'score':[85,70,66,95]}
  • 键必须是不可变的数据类型:int,str

  • 值可以是任意数据类型,包括对象。3.5以前版本都是无序的,3.6以后的会按照初次建立字典的顺序排列,3.7版本以后都是有序的

  • 查询速度快,原因是以空间换时间

字典的创建方式

  • 方式一,元组

dic = dict((('first',1),('second',2),('third',3)))

print(dic) #返回 {'first': 1, 'second': 2, 'third': 3}
  • 方式二

dic2 = dict(first=1,second=2,third=3)
print(dic2) #返回 {'first': 1, 'second': 2, 'third': 3}
  • 方式三,官方写法

dic3 = dict({'first':1,'second':2,'third':3})
print(dic3) #返回 {'first': 1, 'second': 2, 'third': 3}
posted on 2020-07-03 20:02  94小渣渣  阅读(137)  评论(0)    收藏  举报