日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python程序:統(tǒng)計列表中偶數(shù)和奇數(shù)

創(chuàng)新互聯(lián)Python教程:

寫一個 Python 程序,用 For 循環(huán)、While 循環(huán)和函數(shù)計算列表中的偶數(shù)和奇數(shù),并給出一個實例。

使用 For 循環(huán)計算列表中偶數(shù)和奇數(shù)的 Python 程序

在這個 python 程序中,我們使用 For 循環(huán)來迭代給定列表中的每個元素。在 Python 循環(huán)中,我們使用 If 語句來檢查和計數(shù)偶數(shù)和奇數(shù)。

# Python Program to Count Even and Odd Numbers in a List

NumList = []
Even_count = 0
Odd_count = 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)

for j in range(Number):
    if(NumList[j] % 2 == 0):
        Even_count = Even_count + 1
    else:
        Odd_count = Odd_count + 1

print("\nTotal Number of Even Numbers in this List =  ", Even_count)
print("Total Number of Odd Numbers in this List = ", Odd_count)

在這個 python 程序中,用戶輸入了列表元素= [2,3,5,7],偶數(shù)計數(shù)= 0,奇數(shù)計數(shù)= 0

對于循環(huán)–第一次迭代:對于范圍(0,4) 中的 0,條件為真。因此,它進入 If 語句T5 If(NumList[0]% 2 = = 0)=>If(2% 2 = = 0)–條件為真 偶數(shù) 計數(shù)=偶數(shù) 計數(shù)+ 1 = > 0 + 1 = 1

第二次迭代:對于范圍(0,4)中的 1–條件為真 如果(NumList[1] % 2 == 0) = >如果(3% 2 = = 0)–條件為假 那么,它進入 Else 塊。 奇數(shù) 計數(shù)=奇數(shù) 計數(shù)+ 1 = > 0 + 1 = 1

第三次迭代:對于范圍(0,4)中的 2–條件為真 如果(NumList[2] % 2 == 0) = >如果(5% 2 = = 0)–條件為假并進入否則塊。 奇數(shù)計數(shù)= 1 + 1 = 2

第四次迭代:對于范圍(0,4)中的 3–如果(7 % 2 == 0)條件為真【T0)–條件為假并進入否則塊。 奇數(shù)計數(shù)= 2 + 1 = 3

第五次迭代:對于范圍(4)中的 4–條件為假。因此,它從 For Loop 退出

Python 程序使用 While 循環(huán)計算列表中的偶數(shù)和奇數(shù)

這個計算偶數(shù)和奇數(shù)的 Python 程序與上面的相同。我們剛剛將 Python For Loop 替換為 While loop 。

NumList = []
Even_count = 0
Odd_count = 0
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)

while(j < Number):
    if(NumList[j] % 2 == 0):
        Even_count = Even_count + 1
    else:
        Odd_count = Odd_count + 1
    j = j + 1

print("\nTotal Number of Even Numbers in this List =  ", Even_count)
print("Total Number of Odd Numbers in this List = ", Odd_count)
Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 12
Please enter the Value of 2 Element : 13
Please enter the Value of 3 Element : 14
Please enter the Value of 4 Element : 15
Please enter the Value of 5 Element : 44

Total Number of Even Numbers in this List =   3
Total Number of Odd Numbers in this List =  2

使用函數(shù)計算列表中偶數(shù)和奇數(shù)的 Python 程序

這個計數(shù)偶數(shù)和奇數(shù)列表的程序與第一個例子相同。但是,我們使用函數(shù)來分離邏輯

def count_even(NumList):
    Even_count = 0
    for j in range(Number):
        if(NumList[j] % 2 == 0):
            Even_count = Even_count + 1
    return Even_count

def count_odd(NumList):
    Odd_count = 0
    for j in range(Number):
        if(NumList[j] % 2 != 0):
            Odd_count = Odd_count + 1
    return Odd_count

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)

even_cnt = count_even(NumList)
odd_cnt = count_odd(NumList)
print("\nTotal Number of Even Numbers in this List =  ", even_cnt)
print("Total Number of Odd Numbers in this List = ", odd_cnt)
Please enter the Total Number of List Elements: 6
Please enter the Value of 1 Element : 12
Please enter the Value of 2 Element : 22
Please enter the Value of 3 Element : 33
Please enter the Value of 4 Element : 44
Please enter the Value of 5 Element : 55
Please enter the Value of 6 Element : 66

Total Number of Even Numbers in this List =   4
Total Number of Odd Numbers in this List =  2

本文標題:Python程序:統(tǒng)計列表中偶數(shù)和奇數(shù)
當前路徑:http://www.5511xx.com/article/cdjoegh.html