标签:应用 asd 一次循环 内容 ever 反转 数据类型 场景 val
users = ["ji",1,3,"gyhj"]
1.len #计算长度
users = ["ji",1,3,"gyhj"]
val = len(users)
print(val) # 3
2.索引 #输出某一个元素
users = ["ji",1,3,"gyhj"]
val = users[1]
print(val) # 1
3.切片 #输出某一段元素
users = ["ji",1,3,"gyhj"]
val = users[0:2]
print(val) # "ji",1
4.修改(字符串/数字/布尔除外)
users = ["ji",1,3,"gyhj"]
users[2] = 66
print(users) # ['ji', 1, 66, 'gyhj']
5.步长 #选取列表中第几个元素
users = ["ji",1,3,"gyhj"]
val = users[0:3:2]
print(val) # ['ji', 3]
6.for循环
users = ["ji",1,3,"gyhj"]
for i in users:
print(i) # 每个元素一次打印
"""
users = ["ji","asdgfh","tryu","gyhj"]
for i in users:
# 第一次循环:i=“ji”
for e in i:
print(e) # 打印第一个元素中每个字符
"""
# 练习题:请通过for循环和数字计数器实现:users = ['李邵奇','利奇航','张三丰','李子森']
1 qwe
2 asd
3 zxc
4 rty
# 方式一
users = ['qwe','asd','zxc','rty']
count = 1
for i in users:
print(count,i)
count += 1
# 方式二
users = ['qwe','asd' ,'zxc','rty']
users_len = len(users) # 4
for index in range(0,users_len): # [0,1,2,3]
print(index,users[index])
注意:for和while的应用场景:
1.append,在列表的最后追加一个元素
# 示例
users = ["sdxfc"]
while True:
name = input('请输入姓名:')
users.append(name)
print(users) # ['sdxfc', 'sdf']
2.insert #在指定索引位置进行插入元素
users = ['qwe','asd','zxc','rty']
users.insert(1,'zxc')
print(users) # ['qwe', 'zxc', 'asd', 'zxc', 'rty']
3.remove # 删除 注意:()里不加删除内容报错
.pop # 删除 注意:()里不加数字,默认删除最后一个
del # 删除 注意:()里不加数字报错
.clear # 删除列表里所有元素 注意:()里不能加数字
users = ["ji",1,3,"gyhj"]
# 方式一
users.remove('ji')
print(users) # [1, 3, 'gyhj']
# 方式二
users.pop(1)
print(users) #['ji', 3, 'gyhj']
# 方式三
del users[1]
print(users) #['ji', 3, 'gyhj']
# 方式四
users.clear()
print(users) # []
注意:
4.".reverse" #反转
v1 = [1,2,3111,32,13]
print(v1) # [1, 2, 3111, 32, 13]
v1.reverse()
print(v1) # [13, 32, 3111, 2, 1]
5."sort" # 排序
v1 = [1,2,3111,32,13]
v1.sort(reverse=False) # 从小到大(默认)
print(v1) # [11, 13, 22, 32, 3111]
v1.sort(reverse=True) # 从大到小
print(v1) # [3111, 32, 22, 13, 11]
1.增:
2.删:
3.改:
4.查:
5.列表嵌套
users = ["a",0,True,[1,2,3,"kjhg"],[4,['sdf','dfg'],5,6]]
users[0] # a
users[2] # True
users[3][2] # 3
users[3][-1] # "kjhg"
users[3][-1][-1] # 'g'
users[3] = 666 # ['a', 0, True, 666, [4, ['sdf', 'dfg'], 5, 6]]
1.索引(排除:int/bool)
users = (11,22,33)
print(users[0]) # 11
print(users[-1]) # 33
2.切片(排除:int/bool)
users = (11,22,33)
print(users[0:2])
3.步长(排除:int/bool)
users = (11,22,33,"qwerty")
print(users[0:2:2])
4.for循环(排除:int/bool)
users = (11,22,33,"qwerty")
for item in users:
print(item)
5.len(排除:int/bool)
users = (11,22,33,"qwerty")
print(len(users))
元组中的元素(儿子)不可被修改/删除
# 示例一:
v1 = (11,22,33)
v1[1] = 999 # 错误
v1 = 999 # 正确
# 示例二:可以嵌套
v1 = (11,22,33,(44,55,66),(11,2,(99,88,),3))
# 示例三:嵌套
v2 = [11,22,33,(11,22,33)]
v2[-1][1] = 99 # 错误
v2[-1] = 123 # 正确
# 示例四:嵌套
v3 = (11,[1,2,3],22,33)
v3[1] = 666 # 错误
v3[1][2] = 123
公共
独有功能(无)
元组嵌套
# 错误示例
v3 = (11,[1,2,3],22,33)
v3[1] = 666
v3[1][2] = 123
?
标签:应用 asd 一次循环 内容 ever 反转 数据类型 场景 val
原文地址:https://www.cnblogs.com/dpsy921/p/10660324.html