Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
bellcode
/
lesson6-5-1
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
bfded5bb
authored
Jun 13, 2024
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
0db28b07
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
12 deletions
diy.py
diy.py
View file @
bfded5bb
# 臂力挑战赛
'''
# 快用你机灵的小脑瓜想想,如何将刘强和他的臂力值自动插入到hero列表中?
变量名/列表名/函数名的命名规则:
name
=
'刘强'
1. 不能用数字作为开头;
power
=
66
2. 由字母、数字、下划线_ 组成;
hero
=
[
'赵一'
,
30
,
'丁二'
,
37
,
'孙五'
,
52
,
'王猛'
,
89
,
'周亮'
,
98
]
3. 不能用关键字命名(for,and,or,int,break,continue)
# 自动排序
'''
for
i
in
range
(
len
(
hero
)):
bros
=
[
'刘备'
,
'关羽'
,
'张飞'
]
if
i
%
2
==
1
and
hero
[
i
]
>=
power
:
# 索引可以表示列表中元素的位置,索引从0开始,从左往右依次排列
hero
.
insert
(
i
-
1
,
name
)
bros
[
0
]
=
'关羽'
hero
.
insert
(
i
,
power
)
bros
[
1
]
=
'刘备'
print
(
hero
)
print
(
bros
)
\ No newline at end of file
# 列表名.remove() 删除首次出现的指定元素,括号里填要删除的那个元素
# 列表名.pop() 删除特定位置的元素,括号里填要删除元素的索引值
'''
列表中添加元素的方法:
1. 列表名.insert(索引,内容) 在列表的指定位置插入元素
2. 列表名.append(内容) 在列表末尾附加元素
3. 列表名.extend() 在原来列表末尾一次性追加多个元素,括号里填一个列表
列表切片
列表名[a:b] 取左不取右,不写全都取
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment