python列表的循环遍历

数据容器:一个可以存储多个元素的Python数据类型

有哪些数据容器:list(列表),tuple(元组),str(字符串),set(集合),dict(字典)

可以对数据容器中的元素作增(insert)删改(index【1】="哈哈")操作

append元素:在列表尾部增加单个新元素

列表.extend(另一个数据容器):在列表尾部增加另一个列表

删除元素:1.del 2.pop 3.remove(删除列表中的第一个元素)

复制代码
del list[2]
print(list)

list=["apple","orange","banana"]
element=list.pop(2)
print(list)

列表.count(元素):统计元素个数

len(列表):统计列表元素个数

遍历:将容器内的元素依次取出并处理

使用python的时候,要特别注意缩进

定义一个列表[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

遍历列表,取出偶数,并存入一个新的列表对象中

复制代码
#通过append增加元素
list=[]
i=0
for i in range(1,11):
    list.append(i)
    i+=1
print(list)

list1=[]
def one():
    index=0
    while index<=len(list)-1:
        if list[index]%2==0:
            a=list[index]
            list1.append(a)
        index+=1
    print(list1)
def two():
    for i in list:
        if i%2==0:
            list1.append(i)
        i+=1
    print(list1)

one()
# two()
相关推荐
极客小云1 分钟前
【IEEE Transactions系列期刊全览:计算机领域核心期刊深度解析】
android·论文阅读·python
じ☆冷颜〃11 分钟前
基于多数据结构融合的密码学性能增强框架
数据结构·经验分享·笔记·python·密码学
无所事事的海绵宝宝15 分钟前
python基础
开发语言·python
dagouaofei18 分钟前
实测!6款AI自动生成PPT工具体验分享
人工智能·python·powerpoint
Font Tian18 分钟前
Pandas 3.0 全解:从默认字符串类型到 Copy-on-Write 的一场“内存模型重构”
python·重构·数据分析·pandas
轻竹办公PPT21 分钟前
写 2026 年工作计划,用 AI 生成 PPT 哪种方式更高效
人工智能·python·powerpoint
大模型铲屎官25 分钟前
【操作系统-Day 47】揭秘Linux文件系统基石:图解索引分配(inode)与多级索引
linux·运维·服务器·人工智能·python·操作系统·计算机组成原理
Chen不旧25 分钟前
Java模拟死锁
java·开发语言·synchronized·reentrantlock·死锁
dagouaofei31 分钟前
2026 年工作计划 PPT 怎么做?多款 AI 生成方案对比分析
人工智能·python·powerpoint
菩提树下的凡夫33 分钟前
如何将python的程序py文件转换为exe程序
开发语言·python