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

創(chuàng)新互聯(lián)主要從事網(wǎng)站制作、做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)東臺(tái),10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專(zhuān)業(yè),歡迎來(lái)電咨詢建站服務(wù):13518219792
用 For 循環(huán)、While 循環(huán)和函數(shù)編寫(xiě)一個(gè) Python 程序,將正數(shù)和負(fù)數(shù)放在單獨(dú)的列表中,并給出一個(gè)實(shí)例。
Python 程序使用 For 循環(huán)將正數(shù)和負(fù)數(shù)放在單獨(dú)的列表中
在這個(gè) python 程序中,我們使用 For 循環(huán)來(lái)迭代給定列表中的每個(gè)元素。在 Python 循環(huán)中,我們使用 If 語(yǔ)句來(lái)檢查列表項(xiàng)是正數(shù)還是負(fù)數(shù)。根據(jù)結(jié)果,我們將該項(xiàng)附加到肯定列表或否定列表中。
# Python Program to Put Positive and Negative Numbers in Separate List
NumList = []
Positive = []
Negative = []
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.append(NumList[j])
else:
Negative.append(NumList[j])
print("Element in Positive List is : ", Positive)
print("Element in Negative List is : ", Negative)
在這個(gè) python 程序中,用戶輸入了列表項(xiàng)= [12,-34,55,-87,67]
對(duì)于循環(huán)–第一次迭代:對(duì)于范圍(0,5)中的 0。條件為真。因此,它進(jìn)入 If 語(yǔ)句 If(NumList[0]>= 0)=>If(12>= 0)–條件為真 正.追加(NumList[0]) = >正= [12]
第二次迭代:對(duì)于范圍(0,5)中的 1–條件為真 如果(NumList[1] > = 0) = >如果(-34>= 0)–條件為假。所以,它進(jìn)入了 Else 塊。 Negative . append(NumList[1])=>Negative =[-34]
第三次迭代:對(duì)于范圍(0,5)中的 2–條件為真 如果(NumList[2] > = 0) = >如果(55>= 0)–條件為真 正。追加(55) = >正=【12,55】
第四次迭代:對(duì)于范圍(0,5)中的 3-條件為真 如果(-87>= 0)–條件為假,則進(jìn)入否則塊。 陰性.追加(-87) = >陰性= [-34,-87]
第五次迭代:對(duì)于范圍(0,5)中的 4-條件為真 如果(67>= 0)-條件為真 正.追加(67) = >正=【12,55,67】
第六次迭代:對(duì)于范圍(5)中的 5–條件為假。所以它退出 Python For Loop
Python 程序使用 While 循環(huán)將正數(shù)和負(fù)數(shù)放在單獨(dú)的列表中
這個(gè) Python 程序?qū)⒄龜?shù)放在正數(shù)列表中,將負(fù)數(shù)放在負(fù)數(shù)列表中,與上面的相同。我們剛剛將 For Loop 替換為 While loop 。
# Python Program to Put Positive and Negative Numbers in Separate List
NumList = []
Positive = []
Negative = []
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.append(NumList[j])
else:
Negative.append(NumList[j])
j = j + 1
print("Element in Positive List is : ", Positive)
print("Element in Negative List is : ", Negative)使用 while 循環(huán)輸出的單獨(dú)列表中的正數(shù)和負(fù)數(shù)
Please enter the Total Number of List Elements : 6
Please enter the Value of 1 Element : 2
Please enter the Value of 2 Element : -3
Please enter the Value of 3 Element : -5
Please enter the Value of 4 Element : 9
Please enter the Value of 5 Element : -8
Please enter the Value of 6 Element : 7
Element in Positive List is : [2, 9, 7]
Element in Negative List is : [-3, -5, -8]
用函數(shù)將正數(shù)和負(fù)數(shù)分開(kāi)的 Python 程序
這個(gè) Python 單獨(dú)的正數(shù)和負(fù)數(shù)列表示例與第一個(gè)示例相同。但是,我們使用函數(shù)來(lái)分離邏輯。請(qǐng)記住,您也可以編寫(xiě)單個(gè)函數(shù),而不是為正數(shù)和負(fù)數(shù)編寫(xiě)單獨(dú)的函數(shù)。
# Python Program to Put Positive and Negative Numbers in Separate List
def positive_numbers(NumList):
Positive = []
for j in range(Number):
if(NumList[j] >= 0):
Positive.append(NumList[j])
print("Element in Positive List is : ", Positive)
def negative_numbers(NumList):
Negative = []
for j in range(Number):
if(NumList[j] < 0):
Negative.append(NumList[j])
print("Element in Negative List is : ", Negative)
NumList = []
Positive = []
Negative = []
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)
positive_numbers(NumList)
negative_numbers(NumList)單獨(dú)列表輸出中的正數(shù)和負(fù)數(shù)
Please enter the Total Number of List Elements : 6
Please enter the Value of 1 Element : 12
Please enter the Value of 2 Element : -23
Please enter the Value of 3 Element : -44
Please enter the Value of 4 Element : 67
Please enter the Value of 5 Element : -98
Please enter the Value of 6 Element : -3
Element in Positive List is : [12, 67]
Element in Negative List is : [-23, -44, -98, -3] 當(dāng)前名稱(chēng):Python程序:將正數(shù)和負(fù)數(shù)放在單獨(dú)的列表中
分享網(wǎng)址:http://www.dlmjj.cn/article/cosdcce.html


咨詢
建站咨詢
