Write a program to read a list of n integers (positive as well as negative). Create two new lists, one having all positive numbers and the other having all negative numbers from the given list. Print all three lists.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
This comment has been deleted
It is possible, yes.
Here is a quick example showing how you use a list and see if it’s elements are negative or positive numbers. Once you know you just need to add them to another list and later on display them:
for number in ListOfPositiveAndNegative:
if number < 0:
...
So what do we want to do? If they are positive add them to one list, if they are negative add them to another.
for number in ListOfPositiveAndNegative:
if number < 0:
PositiveList.append(number)
else:
NegativeList.append(number)
at the end it will look something similar to this:
ListOfPositiveAndNegative = [1,-2,3] NegativeList = [] PositiveList = [] for number in ListOfPositiveAndNegative:
if number < 0:
NegativeList.append(number)
else:
PositiveList.append(number) print("Elements of the List:\n") print(NegativeList)
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.