Commit bfded5bb by BellCodeEditor

auto save

parent 0db28b07
Showing with 20 additions and 12 deletions
# 臂力挑战赛
# 快用你机灵的小脑瓜想想,如何将刘强和他的臂力值自动插入到hero列表中?
name='刘强'
power=66
hero=['赵一',30,'丁二',37,'孙五',52,'王猛',89,'周亮',98]
# 自动排序
for i in range(len(hero)):
if i%2==1 and hero[i]>=power:
hero.insert(i-1,name)
hero.insert(i,power)
print(hero)
\ No newline at end of file
'''
变量名/列表名/函数名的命名规则:
1. 不能用数字作为开头;
2. 由字母、数字、下划线_ 组成;
3. 不能用关键字命名(for,and,or,int,break,continue)
'''
bros=['刘备','关羽','张飞']
# 索引可以表示列表中元素的位置,索引从0开始,从左往右依次排列
bros[0]='关羽'
bros[1]='刘备'
print(bros)
# 列表名.remove() 删除首次出现的指定元素,括号里填要删除的那个元素
# 列表名.pop() 删除特定位置的元素,括号里填要删除元素的索引值
'''
列表中添加元素的方法:
1. 列表名.insert(索引,内容) 在列表的指定位置插入元素
2. 列表名.append(内容) 在列表末尾附加元素
3. 列表名.extend() 在原来列表末尾一次性追加多个元素,括号里填一个列表
列表切片
列表名[a:b] 取左不取右,不写全都取
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment