Write a Python Program to Put Positive and Negative Numbers in Separate List using For Loop, While Loop, and Functions with practical example.
Before you start, please refer List article to understand everything about Lists.
Python Program to Put Positive and Negative Numbers in Separate List using For Loop
In this python program, we are using For Loop to iterate each and every element in a given List. Inside the loop we are using If statement to check whether the list item is Positive or Negative. Based on the result, we are appending that item to Positive list or Negative list.
# 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)
OUTPUT
ANALYSIS
User entered items = [12, -34, 55, -87, 67]
For Loop – First Iteration: for 0 in range(0, 5)
The condition is True so, compiler will enter into the If Statement
if(NumList[0] >= 0) => if(12 >= 0) – Condition is True
Positive.append(NumList[0]) => Positive = [12]
Second Iteration:for 1 in range(0, 5) – Condition is True
if(NumList[1] >= 0) => if(-34 >= 0) – Condition is False
Compiler will enter into the Else block.
Negative.append(NumList[1]) => Negative = [-34]
Third Iteration:for 2 in range(0, 5) – Condition is True
if(NumList[2] >= 0) => if(55 >= 0) – Condition is True
Positive.append(55) => Positive = [12, 55]
Fourth Iteration:for 3 in range(0, 5) – Condition is True
if(-87 >= 0) – Condition is False
Compiler will enter into Else block.
Negative.append(-87) => Negative = [-34, -87]
Fifth Iteration:for 4 in range(0, 5) – Condition is True
if(67 >= 0) – Condition is True
Positive.append(67) => Positive = [12, 55, 67]
Sixth Iteration:for 5 in range(5) – Condition is False
So compiler will exit from For Loop
Python Program to Put Positive and Negative Numbers in Separate List using While loop
This program to place positive numbers in Positive List, and negative numbers in Negative List is same as above. We just replaced the For Loop with 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)
OUTPUT
Python Program to Put Positive and Negative Numbers in Separate List using Functions
This program is same as first example. However, we separated the logic using Functions. Remember, instead of writing separate functions for Positive and Negative, you can also write single function.
# 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)
OUTPUT