创新互联python教程:
编写一个 Python 程序,使用 for 循环范围打印元组中的偶数。if 语句(if(evTuple[i] % 2 == 0))检查每个 Tuple 项是否可以被 2 整除。如果为真,则打印该元组偶数。
# Tuple Even Numbers
evTuple = (3, 78, 67, 34, 88, 11, 23, 19)
print("Tuple Items = ", evTuple)
print("\nThe Even Numbers in this evTuple Tuple are:")
for i in range(len(evTuple)):
if(evTuple[i] % 2 == 0):
print(evTuple[i], end = " ")
Tuple Items = (3, 78, 67, 34, 88, 11, 23, 19)
The Even Numbers in this evTuple Tuple are:
78 34 88
在这个 Python 偶数示例中,我们使用 for 循环(针对 evTuple 中的 tup)来迭代实际的 Tuple 项。
# Tuple Even Numbers
evTuple = (19, 25, 32, 44, 17, 66, 11, 98)
print("Tuple Items = ", evTuple)
print("\nThe Even Numbers in this evTuple Tuple are:")
for tup in evTuple:
if(tup % 2 == 0):
print(tup, end = " ")
Tuple Items = (19, 25, 32, 44, 17, 66, 11, 98)
The Even Numbers in this evTuple Tuple are:
32 44 66 98
Python 程序使用 While 循环返回或显示元组中的偶数。
# Tuple Even Numbers
evTuple = (25, 12, 19, 44, 66, 79, 89, 22, 46)
print("Tuple Items = ", evTuple)
i = 0
print("\nThe Even Numbers in this evTuple Tuple are:")
while (i < len(evTuple)):
if(evTuple[i] % 2 == 0):
print(evTuple[i], end = " ")
i = i + 1
Tuple Items = (25, 12, 19, 44, 66, 79, 89, 22, 46)
The Even Numbers in this evTuple Tuple are:
12 44 66 22 46
在这个 Python 元组的例子中,我们创建了一个函数(tuplevennumbers(evTuple))来查找和打印偶数。
# Tuple Even Numbers
def tupleEvenNumbers(evTuple):
for tup in evTuple:
if(tup % 2 == 0):
print(tup, end = " ")
evTuple = (19, 32, 25, 77, 44, 56, 89, 45, 98, 122, 55, 12)
print("Tuple Items = ", evTuple)
print("\nThe Even Numbers in this evTuple Tuple are:")
tupleEvenNumbers(evTuple)
本文名称:Python 程序:打印元组中偶数
网页URL:http://www.shufengxianlan.com/qtweb/news45/311695.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联