Python Program to Print Positive Numbers in a List

How to write a Python Program to Print Positive Numbers in a List utilizing While Loop, For Loop, and Functions with a useful example.

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

In this python program, we are utilizing For Loop to iterate each element in this List. Inside the Python for loop, we used the If statement to verify and print positive numbers.

# Python Program to Print Positive 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("\nPositive Numbers in this List are : ")
for j in range(Number):
    if(NumList[j] >= 0):
        print(NumList[j], end = '   ')
Python Program to Print Positive Numbers in a List 1

The user entered List elements in this python program = [12, -14, 15, -22]

For Loop – First Iteration: for 0 in range(0, 4). The condition result is True. So, it enters into the If Statement
if(NumList[0] >= 0) => if(12 > = 0) – Condition is True. So, this Positive Number  printed.

Second Iteration: for 1 in range(0, 4) – Condition is True
if(NumList[1] >= 0) => if(-14 >= 0) – Condition is False
This Number Skipped.

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

Fourth Iteration: for 3 in range(0, 4) – Condition is True
if(-22 >= 0) – Condition is False
This Number skipped.

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

Program to Print Positive Numbers in a List using While loop

This Python list program for positive numbers is the same as the above. We replaced the For Loop with While loop.

# Python Program to Print Positive 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("\nPositive Numbers in this List are : ")
while(j < Number):
    if(NumList[j] >= 0):
        print(NumList[j], end = '   ')
    j = j + 1

Python printing positive list numbers 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 : 34
Please enter the Value of 3 Element : -12
Please enter the Value of 4 Element : 3
Please enter the Value of 5 Element : -22

Positive Numbers in this List are : 
12   34   3   

Python Program to Print Positive Numbers in a List using Functions

In this List program to print positive numbers, we used Functions to separate the logic.

def positive_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("\nPositive Numbers in this List are : ")
positive_number(NumList)

Python printing positive numbers in a list 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 : 33
Please enter the Value of 3 Element : -15
Please enter the Value of 4 Element : 9
Please enter the Value of 5 Element : -13
Please enter the Value of 6 Element : -17

Positive Numbers in this List are : 
33   9