In this article, we will show you, How to write a Python Program to Print Positive Numbers in a 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 Print Positive Numbers in a List using For Loop

In this python program, we are using For Loop to iterate each and every element in this Next. Inside the for loop loop we are using If statement to check 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 = ' ')

**OUTPUT**

**ANALYSIS**

User entered elements = [12, -14, 15, -22]

**For Loop – First Iteration: **for 0 in range(0, 4)

The condition is True so, compiler will enter into the If Statement

if(NumList[0] >= 0) => if(12 > = 0) – Condition is True

This Positive Number will be printed.

**Second Iteration:** for 1 in range(0, 4) – Condition is True

if(NumList[1] >= 0) => if(-14 >= 0) – Condition is False

This Number is 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 is printed.

**Fourth Iteration:** for 3 in range(0, 4) – Condition is True

if(-22 >= 0) – Condition is False

This Number is skipped.

**Fifth Iteration:** for 4 in range(0, 4) – Condition is False

So compiler will exit from For Loop

## Python Program to Print Positive Numbers in a List using While loop

This program for positive numbers is same as above. We just 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

**OUTPUT**

## Python Program to Print Positive Numbers in a List using Functions

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

# Python Program to Print Positive Numbers in a List 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)

**OUTPUT**