新聞中心
創(chuàng)新互聯(lián)python教程:

創(chuàng)新互聯(lián)公司,是成都地區(qū)的互聯(lián)網(wǎng)解決方案提供商,用心服務(wù)為企業(yè)提供網(wǎng)站建設(shè)、成都APP應(yīng)用開發(fā)、重慶小程序開發(fā)公司、系統(tǒng)定制網(wǎng)站和微信代運(yùn)營(yíng)服務(wù)。經(jīng)過數(shù)十載的沉淀與積累,沉淀的是技術(shù)和服務(wù),讓客戶少走彎路,踏實(shí)做事,誠實(shí)做人,用情服務(wù),致力做一個(gè)負(fù)責(zé)任、受尊敬的企業(yè)。對(duì)客戶負(fù)責(zé),就是對(duì)自己負(fù)責(zé),對(duì)企業(yè)負(fù)責(zé)。
寫一個(gè) Python 程序,用 For 循環(huán)、While 循環(huán)和函數(shù)計(jì)算列表中的正數(shù)和負(fù)數(shù),并給出一個(gè)實(shí)例。
使用 For 循環(huán)計(jì)算列表中正數(shù)和負(fù)數(shù)的 Python 程序
在這個(gè) python 程序中,我們使用 For 循環(huán)來迭代給定列表中的每個(gè)元素。在 Python for 循環(huán)中,我們使用 If 語句來檢查和計(jì)數(shù)正數(shù)和負(fù)數(shù)。
# Python Program to Count Positive and Negative Numbers in a List
NumList = []
Positive_count = 0
Negative_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] >= 0):
Positive_count = Positive_count + 1
else:
Negative_count = Negative_count + 1
print("\nTotal Number of Positive Numbers in this List = ", Positive_count)
print("Total Number of Negative Numbers in this List = ", Negative_count)
在這個(gè) python 程序中,用戶輸入了列表元素= [12,-22,3,5],正 計(jì)數(shù)= 0,負(fù) 計(jì)數(shù)= 0
對(duì)于循環(huán)–第一次迭代:對(duì)于范圍(0,4) 中的 0,條件為真。因此,進(jìn)入 If 語句T3 If(NumList[0]>= 0)=>If(12>= 0)–條件為真 正 計(jì)數(shù)=正 計(jì)數(shù)+ 1 = > 0 + 1 = 1
第二次迭代:對(duì)于范圍(0,4)中的 1–條件為真 如果(NumList[1] > = 0) = >如果(-22>= 0)–條件為假,則進(jìn)入 Else 塊。 負(fù) 計(jì)數(shù)=負(fù) 計(jì)數(shù)+ 1 = > 0 + 1 = 1
第三次迭代:對(duì)于范圍(0,4)中的 2–條件為真 如果(NumList[2] > = 0) = >如果(3>= 0)–條件為真 正 _ 計(jì)數(shù)= 1 + 1 = > 2
第四次迭代:對(duì)于范圍(0,4)中的 3,如果(5>= 0)–條件為真,則條件為真 。所以,它進(jìn)入了 Else 塊。 陽性計(jì)數(shù)= 2 + 1 = > 3
第五次迭代:對(duì)于范圍(4)中的 4–條件為假。所以,蟒蛇從 離開
Python 程序使用 While 循環(huán)計(jì)算列表中的正數(shù)和負(fù)數(shù)
這個(gè)計(jì)算正數(shù)和負(fù)數(shù)的 Python 程序與上面的相同。我們剛剛將 For Loop 替換為 While loop 。
# Python Program to Count Positive and Negative Numbers in a List
NumList = []
Positive_count = 0
Negative_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] >= 0):
Positive_count = Positive_count + 1
else:
Negative_count = Negative_count + 1
j = j + 1
print("\nTotal Number of Positive Numbers in this List = ", Positive_count)
print("Total Number of Negative Numbers in this List = ", Negative_count)Python 使用 while 循環(huán)輸出計(jì)算正負(fù)列表數(shù)
Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : -3
Please enter the Value of 2 Element : -5
Please enter the Value of 3 Element : 9
Please enter the Value of 4 Element : 8
Please enter the Value of 5 Element : -6
Total Number of Positive Numbers in this List = 2
Total Number of Negative Numbers in this List = 3
使用函數(shù)計(jì)算列表中正負(fù)項(xiàng)目的 Python 程序
這個(gè) Python 計(jì)算正負(fù)列表項(xiàng)目的程序與第一個(gè)示例相同。但是,我們使用函數(shù)來分離邏輯
def count_Positive(NumList):
Positive_count = 0
for j in range(Number):
if(NumList[j] >= 0):
Positive_count = Positive_count + 1
return Positive_count
def count_Negative(NumList):
Negative_count = 0
for j in range(Number):
if(NumList[j] % 2 != 0):
Negative_count = Negative_count + 1
return Negative_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)
Positive_cnt = count_Positive(NumList)
Negative_cnt = count_Negative(NumList)
print("\nTotal Number of Positive Numbers in this List = ", Positive_cnt)
print("Total Number of Negative Numbers in this List = ", Negative_cnt)
Please enter the Total Number of List Elements: 6
Please enter the Value of 1 Element : -11
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 Positive Numbers in this List = 3
Total Number of Negative Numbers in this List = 3 標(biāo)題名稱:Python程序:統(tǒng)計(jì)列表中正數(shù)和負(fù)數(shù)
URL標(biāo)題:http://www.fisionsoft.com.cn/article/djsddsi.html


咨詢
建站咨詢
