网站首页 > 技术文章 正文
python的for循环是一个序列迭代器,可以遍历任何可迭代对象,比如字符串、列表、元组等。
1.1 python的for循环格式
python的for循环,会逐个将可迭代对象中的元素,赋值给for循环的变量。
python的for循环格式如下,其中,break,continue,else都是可选部分:
for 变量 in 可迭代对象:
循环语句
if 分支条件1:
break
if 分支条件2:
continue
else:
未执行break,循环结束后执行
1.2 python简单的for循环
python简单的for循环,没有break、continue、else部分。
示例
>>> S='梯阅线条'
>>> for c in S:
print(c,end=' ')
梯 阅 线 条
>>> sum=0
>>> for x in range(1,100):
sum+=x
>>> sum
4950
1.3 python的多变量for循环
python的for循环可以使用多个变量来遍历迭代对象。也可以用于序列解包。
示例
>>> T=[(1,2),(3,4),(5,6)]
>>> for (a,b) in T:
print(a,b,end=',')
1 2,3 4,5 6,
# for 循环序列解包
>>> L=[tuple(range(i,i+4)) for i in range(1,13,4)]
>>> L
[(1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12)]
>>> for (a,b,*c) in L:
print(a,b,c)
1 2 [3, 4]
5 6 [7, 8]
9 10 [11, 12]
1.4 python的for循环遍历字典
python的for循环,遍历字典可以通过for k in D遍历键,或者通过for k,v in D.items()遍历键值对。
示例
# in D 遍历 key
>>> D={'name':'梯阅线条','no':9555,'url':'tyxt'}
>>> for k in D:
print(k,'=',D[k])
name = 梯阅线条
no = 9555
url = tyxt
>>> list(D.items())
[('name', '梯阅线条'), ('no', 9555), ('url', 'tyxt')]
# D.items() 遍历 key和value
>>> for k,v in D.items():
print(k,'=',v)
name = 梯阅线条
no = 9555
url = tyxt
1.5 python的嵌套for循环
1.5.1 break和else
在列表中查找是否存在指定的多个元素,可以用到break和else,以及嵌套for循环。
示例
>>> L1=['梯阅线条',9555,'tyxt','软件测试开发']
>>> L1=['梯阅线条',(9555,9556),'tyxt','软件测试开发']
>>> L2=['hi',(9555,9556)]
>>> for x in L2:
for y in L1:
if x == y:
print('{}在{}里面'.format(x,L1))
break
else:
print('{}不在{}里面'.format(x,L1))
hi不在['梯阅线条', (9555, 9556), 'tyxt', '软件测试开发']里面
(9555, 9556)在['梯阅线条', (9555, 9556), 'tyxt', '软件测试开发']里面
用成员关系in替换嵌套和break及else
示例
>>> L1
['梯阅线条', (9555, 9556), 'tyxt', '软件测试开发']
>>> L2
['hi', (9555, 9556)]
>>> for x in L2:
if x in L1:
print('{}在{}里面'.format(x,L1))
else:
print('{}不在{}里面'.format(x,L1))
hi不在['梯阅线条', (9555, 9556), 'tyxt', '软件测试开发']里面
(9555, 9556)在['梯阅线条', (9555, 9556), 'tyxt', '软件测试开发']里面
1.6 python的for循环查找相同字符
python的for循环结合成员关系in,可以查找两个序列中相同元素,比如查找两个字符串中相同的字符。
示例
>>> S1='梯阅线条'
>>> S2='提月线条'
>>> L=[]
>>> for x in S1:
if x in S2:
L.append(x)
>>> L
['线', '条']
版权声明?:
本文首发微信公众号:梯阅线条,
原创不易,转载请注明出处。
更多内容参考python知识分享或软件测试开发目录。
猜你喜欢
- 2024-12-30 在 Python 中使用 f-String 格式化字符串
- 2024-12-30 CSnakes:在.NET项目中嵌入Python代码的工具
- 2024-12-30 10个小技巧,让你的 Python 代码更加优雅
- 2024-12-30 什么是Python中的DSL领域特定语言?
- 2024-12-30 python笔记:for循环嵌套。end=""的作用,图形打印
- 2024-12-30 PyScript:让Python在HTML中运行(python处理html文件)
- 2024-12-30 PythonNet:实现Python与.Net代码相互调用!
- 2024-12-30 Python嵌入版(python嵌入版区别)
- 2024-12-30 Python教程-嵌套字典(python字典嵌套字典取值)
- 2024-12-30 Python短文,Python中的嵌套条件语句(六)
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)