Write a program to read a list of n integers (positive as well as negative)

Path Walla

  • Home
  • Computer Science
  • _Class 11
  • __Notes
  • __Sumita Arora
  • __NCERT
  • _Class 12
  • __Notes
  • __Sumita Arora
  • __NCERT
  • __Preeti Arora
  • Information Practices
  • _Class 11
  • __Notes
  • __Sumita Arora
  • __NCERT
  • __Preeti Arora
  • _Class 12
  • __Notes
  • __Sumita Arora
  • __NCERT
  • __Preeti Arora
  • Previous year question paper
  • _Class 11
  • _Class 12
  • Essay
HomeLists

Python program to print positive numbers in a list

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

Example:

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

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

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




# Python program to print positive 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:



11 0 45 66


Example #2: Using while loop




# Python program to print positive 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:

21 93


Example #3: Using list comprehension




# Python program to print Positive Numbers in a List
# list of numbers
list1 = [-10, -21, -4, 45, -66, 93]
# using list comprehension
pos_nos = [num for num in list1 if num >= 0]
print["Positive numbers in the list: ", *pos_nos]

Output:

Positive numbers in the list: 45 93


Example #4: Using lambda expressions




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

Output:

Positive numbers in the list: 21, 4, 93

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
Read Full Article

Python program to count positive and negative numbers in a list

Given a list of numbers, write a Python program to count positive and negative numbers in a List.

Example:

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

Example #1: Count positive and negative numbers from given list using for loop

Iterate each element in the list using for loop and check if num >= 0, the condition to check positive numbers. If the condition satisfies, then increase pos_count else increase neg_count.




# Python program to count positive and negative numbers in a List
# list of numbers
list1 = [10, -21, 4, -45, 66, -93, 1]
pos_count, neg_count = 0, 0
# iterating each number in list
for num in list1:
# checking condition
if num >= 0:
pos_count += 1
else:
neg_count += 1
print["Positive numbers in the list: ", pos_count]
print["Negative numbers in the list: ", neg_count]

Output:



Positive numbers in the list: 4 Negative numbers in the list: 3

Example #2: Using while loop




# Python program to count positive and negative numbers in a List
# list of numbers
list1 = [-10, -21, -4, -45, -66, 93, 11]
pos_count, neg_count = 0, 0
num = 0
# using while loop
while[num < len[list1]]:
# checking condition
if list1[num] >= 0:
pos_count += 1
else:
neg_count += 1
# increment num
num += 1
print["Positive numbers in the list: ", pos_count]
print["Negative numbers in the list: ", neg_count]

Output:

Positive numbers in the list: 2 Negative numbers in the list: 5

Example #3 : Using Python Lambda Expressions




# Python program to count positive
# and negative numbers in a List
# list of numbers
list1 = [10, -21, -4, 45, 66, 93, -11]
neg_count = len[list[filter[lambda x: [x < 0], list1]]]
# we can also do len[list1] - neg_count
pos_count = len[list[filter[lambda x: [x >= 0], list1]]]
print["Positive numbers in the list: ", pos_count]
print["Negative numbers in the list: ", neg_count]

Output:

Positive numbers in the list: 4 Negative numbers in the list: 3

Example #4 : Using List Comprehension




# Python program to count positive
# and negative numbers in a List
# list of numbers
list1 = [-10, -21, -4, -45, -66, -93, 11]
only_pos = [num for num in list1 if num >= 1]
pos_count = len[only_pos]
print["Positive numbers in the list: ", pos_count]
print["Negative numbers in the list: ", len[list1] - pos_count]

Output:

Positive numbers in the list: 1 Negative numbers in the list: 6

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
Read Full Article

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]

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

Video liên quan

Bài mới nhất

Chủ Đề