Python Program to Put Positive and Negative Numbers in Separate Lists

20ITA14 Rupa Shiva Dharshini
2 min readJun 13, 2021

--

This python program separates the positive and negative numbers in two different lists.

Prerequisites:

  1. Knowing about lists in python
  2. The for loop in python
  3. If else statement in python
  4. The append() method/function

1.Lists in python:

The list is one of the datatypes available in Python which can be written as a list of comma-separated values (items) between square brackets. For example,

list1=[15,3,2,88]

list2=[200,536, ‘lion’,2]

To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index.

Here, list1[0]=15 and list2[2]=lion.

2.For loop in python:

The for loop in Python is used to iterate over a sequence or other iterable objects.

Syntax:

for val in sequence:
Body of for

Here, val is the variable that takes the value of the item inside the sequence on each iteration.

Loop continues until we reach the last item in the sequence. At times, for loop is used along with the range() function. We can generate a sequence of numbers using range() function.

For example, range(10)will generate numbers from 0 to 9 (10 numbers).

Syntax:

range(start, stop, step_size)

Here, start and stop refer to the values to be begun with and terminated with respectively. Step size is optional, which defaults to 1 if not given.

3.If..else in python:

If..else is a conditional statement in python. When the condition of if is true, the statement part of if is executed; if the condition is false, the else part is executed.

Syntax:

if test expression:
Body of if
else:
Body of else

4.Append() function in python:

You can add elements to a list using the append method. The append() method adds a single element towards the end of a list.

Syntax:

list.append(element)

The parameter ‘element’ can be of any data type like numbers,strings..

Code:

This program reads the data for the positive and negative values from the user and executes the for loop; where the numbers are classified and appended to the created positive and negative lists under the if..else conditional statement.

The for loop works with the range function here.

Output:

To learn python and such courses:

--

--

No responses yet