Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

Print Odd and Even Number in List Using Python

Get the Value from user and Print the Odd and Even Number using List in Python

question from:https://stackoverflow.com/questions/65941953/print-odd-and-even-number-in-list-using-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

would you like to try this code which simplify the flow and make it more Pythonic:

nums = map(int, input("Input some numbers: ").split())  # get all numbers in one shot 

results = [[], []]      # declare the results to store evens and odds

for n in nums:          # put each number in their own list or bucket. one shot.
    results[n % 2].append(n)
    

print(results)

evens, odds = results             # unpacking these 2 lists

print(f' evens list: {evens}' )   # confirm the results is ok
print(f' odds list: {odds} ')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...