Python program to print positive and negative numbers in a list

Python program to print negative numbers in a list

Given a list of numbers, write a Python program to print all negative numbers in given list.

Example:

Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14 Input: list2 = [12, 14, -95, 3] Output: -95

Example #1: Print all negative numbers from given list using for loop

Iterate each element in the list using for loop and check if number is less than 0. If the condition satisfies, then only print the number.




# Python program to print negative Numbers in a List
# list of numbers
list1 = [11, -21, 0, 45, 66, -93]
# iterating each number in list
for num in list1:
# checking condition
if num < 0:
print(num, end = " ")

Output:



-21 -93


Example #2: Using while loop




# Python program to print negative Numbers in a List
# list of numbers
list1 = [-10, 21, -4, -45, -66, 93]
num = 0
# using while loop
while(num < len(list1)):
# checking condition
if list1[num] < 0:
print(list1[num], end = " ")
# increment num
num += 1

Output:

-10 -4 -45 -66


Example #3: Using list comprehension




# Python program to print negative Numbers in a List
# list of numbers
list1 = [-10, -21, -4, 45, -66, 93]
# using list comprehension
neg_nos = [num for num in list1 if num < 0]
print("Negative numbers in the list: ", *neg_nos)

Output:

Negative numbers in the list: -10 -21 -4 -66


Example #4: Using lambda expressions




# Python program to print negative Numbers in a List
# list of numbers
list1 = [-10, 21, 4, -45, -66, 93, -11]
# we can also print negative no's using lambda exp.
neg_nos = list(filter(lambda x: (x < 0), list1))
print("Negative numbers in the list: ", *neg_nos)

Output:

Negative numbers in the list: -10 -45 -66 -11

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python Programs
School Programming
Python list-programs
python-list
Practice Tags :
python-list

Python program to print positive or negative numbers in a list

Here, we are going to learn different methods to find and print all positive or negative numbers in a list in Python.
Submitted by Shivang Yadav, on April 08, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered sets of values enclosed in square brackets [].

Python Program to Put Positive and Negative Numbers in Separate List using For Loop

In thispython program, we are usingFor Loopto iterate every element in a givenList. Inside thePythonloop, we are using the If statement to check whether the list item is Positive or Negative. Based on the result, we are appending that item to the Positive list or the 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)Python Program to Put Positive and Negative Numbers in Separate List 1

In this python program, the User entered List items = [12, -34, 55, -87, 67]

For Loop – First Iteration:for 0 in range(0, 5). The condition is True. So, it enters into theIf 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. So, itenters 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 andit enters 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 it exits from Python For Loop

Python Program to Check if a Number is Positive, Negative or 0

In this example, you will learn to check whether a number entered by the user is positive, negative or zero. This problem is solved using if...elif...else and nested if...else statement.

To understand this example, you should have the knowledge of the following Python programming topics:

  • Python if...else Statement
  • Python Input, Output and Import

Python program to print negative numbers in a list

PythonServer Side ProgrammingProgramming

In this article, we will learn about the solution and approach to solve the given problem statement.

Count positive and negative numbers in a list in Python program

PythonServer Side ProgrammingProgramming

In this article, we will learn about the solution to the problem statement given below.

Problem statement− We are given a list iterable, we need to count positive and negative numbers in it and display them.

Introduction

In this section of python programming, we will understand the code to print negative numbers in the list. Previously we have seen the condition of positive number, unlike to that the condition for negative number is number should be less than 0.

Program to print negative numbers in the list