Python程序:打印列表中奇数

创新互联python教程:

成都创新互联成都企业网站建设服务,提供成都网站建设、成都网站设计网站开发,网站定制,建网站,网站搭建,网站设计,自适应网站建设,网页设计师打造企业风格网站,提供周到的售前咨询和贴心的售后服务。欢迎咨询做网站需要多少钱:028-86922220

编写一个 Python 程序,使用 For 循环、While 循环和函数打印列表中的奇数,并给出一个实例。

使用 For 循环打印列表中奇数的 Python 程序

在这个 python 程序中,我们使用 For 循环来迭代这个列表中的每个元素。在 Python for 循环中,我们使用 If 语句来检查和打印奇数。

# Python Program to Print Odd Numbers in a List

NumList = []

Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

print("\nOdd Numbers in this List are : ")
for j in range(Number):
    if(NumList[j] % 2 != 0):
        print(NumList[j], end = '   ')

用户输入了列表元素= [3,4,5,9]。在这个 python 程序中, For Loop 迭代是

对于循环–第一次迭代:对于范围(0,4) 中的 0,条件为真。所以,进入 If 语句

if(NumList[0] % 2!= 0) => if(3 % 2!= 0)–条件为真 该数字已打印。

第二次迭代:对于范围(0,4)中的 1–条件为真 如果(NumList[1] % 2!= 0) = > if(4 % 2!= 0)–条件为假 跳过该数字。

第三次迭代:对于范围(0,4)中的 2–条件为真 如果(NumList[2] % 2!= 0) = > if(5 % 2!= 0)–条件为真 该数字已打印。

第四次迭代:对于范围(0,4)中的 3–条件为真 如果(9 % 2!= 0)–条件为真 该数字也打印出来。

第五次迭代:对于范围(0,4)中的 4–条件为假 因此,它退出 Python For Loop

使用 While 循环打印列表中奇数的 Python 程序

这个列表中奇数的 Python 程序与上面的相同。我们刚刚将 For 循环替换为 While 循环。

# Python Program to Print Odd Numbers in a List

NumList = []
j = 0

Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

print("\nOdd Numbers in this List are : ")
while(j < Number):
    if(NumList[j] % 2 != 0):
        print(NumList[j], end = '   ')
    j = j + 1
Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 45
Please enter the Value of 2 Element : 65
Please enter the Value of 3 Element : 78
Please enter the Value of 4 Element : 98
Please enter the Value of 5 Element : 1

Odd Numbers in this List are : 
45   65   1 

用函数计算列表中奇数的 Python 程序

列表程序中的这个 Python 奇数与第一个示例相同。然而,我们使用函数来分离逻辑

def odd_numbers(NumList):
    for j in range(Number):
        if(NumList[j] % 2 != 0):
            print(NumList[j], end = '   ')

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

print("\nOdd Numbers in this List are : ")
odd_numbers(NumList)
Please enter the Total Number of List Elements: 6
Please enter the Value of 1 Element : 22
Please enter the Value of 2 Element : 43
Please enter the Value of 3 Element : 57
Please enter the Value of 4 Element : 98
Please enter the Value of 5 Element : 12
Please enter the Value of 6 Element : 49

Odd Numbers in this List are : 
43   57   49 

标题名称:Python程序:打印列表中奇数
文章网址:http://www.shufengxianlan.com/qtweb/news3/114703.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联