ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

python_2

2026/8/3 20:43:00 拓冰建站 浏览量
python_2

python_2

数据结构

list列表

定义

my_course=["javascript","webview","spring-boot","HTML5"]
print(my_course)
print(my_course[1])#list索引是从0开始的 //结果为webview

切片

print(my_course[1:3]) #从1开始,到3为止(不包括3) //结果为['webview', 'spring-boot']
some_course=my_course[1:3]
print(some_course)

修改列表元素的值

插入
append
my_course[0]="typescript"
print(my_course)#typescript
my_course.append("python")#append:Append object to the end of the list.
print(my_course)#['typescript', 'webview', 'spring-boot', 'HTML5', 'python']
insert
my_course.insert(1,"AI程序设计")#Insert object before index(索引).
print(my_course)#['typescript', 'AI程序设计', 'webview', 'spring-boot', 'HTML5', 'python']
print(my_course[1])#AI程序设计
删除
根据给定内容删除元素
my_course.remove("AI程序设计")
print(my_course)#['typescript', 'webview', 'spring-boot', 'HTML5', 'python']
按位置删除
del(my_course[1])
print(my_course)#['typescript', 'spring-boot', 'HTML5', 'python']

list的迭代

for course in my_course:#此处的course为python自动创建的临时变量,可以为任意print(course)#typescript spring-boot HTML5 python