Python Program to Print Negative Numbers in a List

Write a Python Program to Print Negative Numbers in a List using For Loop, While Loop, and Functions with a practical example.

Python Program to Print Negative Numbers in a List using For Loop

In this python program, we are using For Loop to iterate each element in this List. Inside the Python for loop, we are using the If statement to check and print Negative numbers.

# Python Program to Print Negative Numbers in a List

NumList = []

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)

print("\nNegative Numbers in this List are : ")
for j in range(Number):
    if(NumList[j] < 0):
        print(NumList[j], end = '   ')
Python Program to Print Negative Numbers in a List 1

In this python program, the User entered List elements = [2, -12, 0, -17]

For Loop – First Iteration: for 0 in range(0, 4)
The condition is True. So, it enters into the If Statement

if(NumList[0] < 0) => if(2 < 0) – Condition is False
This Number skipped.

Second Iteration: for 1 in range(0, 4) – Condition is True
if(NumList[1] < 0) => if(-12 < 0) – Condition is True
This Negative Number printed.

Third Iteration: for 2 in range(0, 4) – Condition is True
if(NumList[2] < 0) => if(0 < 0) – Condition is False
This Number skipped.

Fourth Iteration: for 3 in range(0, 4) – Condition is True
if(-17 < 0) – Condition is True
This Negative Number printed.

Fifth Iteration: for 4 in range(0, 4) – Condition is False
So, it exits from Python For Loop

Python Program to Print Negative Numbers in a List using While loop

This Python program for Negative numbers in a list is the same as the above. We just replaced the For Loop with While loop.

# Python Program to Print Negative Numbers in a List

NumList = []
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)

print("\nNegative Numbers in this List are : ")
while(j < Number):
    if(NumList[j] < 0):
        print(NumList[j], end = '   ')
    j = j + 1

Print Negative numbers in a list using a while loop output

Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 12
Please enter the Value of 2 Element : -13
Please enter the Value of 3 Element : -15
Please enter the Value of 4 Element : 3
Please enter the Value of 5 Element : -22

Negative Numbers in this List are : 
-13   -15   -22   

Python Program to find Negative Numbers in a List using Functions

This Python negative list numbers program is the same as the first example. However, we separated the logic using Functions

# Python Program to Print Negative Numbers in a List

def negative_number(NumList):
    for j in range(Number):
        if(NumList[j] < 0):
            print(NumList[j], end = '   ')


NumList = []
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)

print("\nNegative Numbers in this List are : ")
negative_number(NumList)

Print Negative list numbers using functions and for loop output

Please enter the Total Number of List Elements: 6
Please enter the Value of 1 Element : -12
Please enter the Value of 2 Element : 5
Please enter the Value of 3 Element : -7
Please enter the Value of 4 Element : -8
Please enter the Value of 5 Element : 9
Please enter the Value of 6 Element : -10

Negative Numbers in this List are : 
-12   -7   -8   -10