
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print All Negative Numbers in a Range using Python
Sometimes the task is to select the negative numbers from a given range. Here, in this Python article, first, the range is taken as input and then the integers within this range are specified. From these numbers only the negative numbers are then selected using the different methods in 4 different examples. In example 1, the negative numbers are picked and separated into another list. In the example 2, all the elements that are not negative are removed. In example 3, the sorted list is split upto zero and only negatives are retained. In example 4, filter is used to select the negative numbers.
Example 1 - Select only the negative numbers in a range and separate these to another list to print
Step 1 ? Specify the smallest and highest number in the range as input. The smallest number should be negative and highest number should be positive. Make a list of integers between the given range
Step 2 ? First separate all negative numbers in a separate list. Print these numbers.
Step 3 ? Run the program and then check the result.
The Python File Contains this
lowNum=-50 highNum=60 mainlist=[] listNeg=[] #Making the main list with integers starting from lowNum and upto HighNum for item in range(lowNum, highNum+1): mainlist.append(item) print("In the given range from ", lowNum, " to", highNum, " :") print("\nThe Main List :") print(mainlist) #dividing the main list into negatives and positives for item in mainlist: if (item < 0): listNeg.append(item) print("\nThe Negative Elements in the Range :") print(listNeg)
Viewing The Result - Example 1
For seeing the result run the Python file in the cmd window.
In the given range from -50 to 60 : The Main List : [-50, -49, -48, -47, -46, -45, -44, -43, -42, -41, -40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60] The Negative Elements in the Range : [-50, -49, -48, -47, -46, -45, -44, -43, -42, -41, -40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1]
Example 2: Remove all the non negative elements and print the remaining negative numbers
Algorithm
Step 1 ? First assign the smallest and highest number in the range. The smallest number should be negative and the highest number should be positive. Make a list of all integers between the given range
Step 2 ? First make a copy of this as a sorted list and then remove all greater than zero numbers from this.
Step 3 ? Print all the left-out negative numbers from the main list.
Step 4 ? Run the program and then check the result.
The Python File Contains this
lowNum=-40 highNum=50 mainlist=[] mainlistcopy=[] #Making the main list with integers starting from lowNum and upto HighNum for item in range(lowNum, highNum+1): mainlist.append(item) print("In the given range from ", lowNum, " to", highNum, " :") print("\nThe Main List :") print(mainlist) mainlistcopy=sorted(mainlist) for item in mainlistcopy: if (item >= 0): mainlist.remove(item) print("\nThe Negative Elements in the List :") print(mainlist)
Viewing The Result - Example 2
Open the cmd window and run the python file to see the result.
In the given range from -40 to 50 : The Main List : [-40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50] The Negative Elements in the List : [-40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1]
Example 3 - Split the sorted list upto zero and retain the negatives to print.
Algorithm
Step 1? Specify the smallest and highest number in the range. The smallest number should be negative and the highest number should be positive. Make a list of all the integers between the input range
Step 2? First make a copy of this list and then split this list and select less than zero numbers.
Step 3? Print all the left-out negative numbers in the list copy.
Step 4? Run the program and then check the result.
The Python File Contains this
lowNum=-40 highNum=60 mainlist=[] listNeg=[] #Making the main list with integers starting from lowNum and upto HighNum for item in range(lowNum, highNum+1): mainlist.append(item) print("In the given range from ", lowNum, " to", highNum, " :") print("\nThe Main List :") print(mainlist) listNeg=mainlist listNeg[:] = [item for item in listNeg if item < 0] print("\nThe negative Elements in the List :") print(listNeg)
Viewing The Result - Example 3
For seeing the result run the Python file in the cmd window.
In the given range from -40 to 60 : The Main List : [-40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60] The negative Elements in the List : [-40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1]
Fig 3: Showing the result in the command window.
Example 4: Using filter select the negative numbers and filter out the non negative ones
Algorithm
Step 1? Assign the value of the smallest and highest number in the range. The smallest number should be negative and the highest number should be positive. Make a list of all integers between this input range
Step 2? Use a filter with a lambda function.
Step 3? Filter out the negatives in a new list. Print all the filtered negative numbers in the new list.
Step 4? Run the program and then check the result.
The Python File Contains this
lowNum=-30 highNum=20 mainlist=[] listNeg=[] #Making the main list with integers starting from lowNum and upto HighNum for item in range(lowNum, highNum+1): mainlist.append(item) print("In the given range from ", lowNum, " to", highNum, " :") print("\nThe Main List :") print(mainlist) listNeg = list(filter(lambda item: item < 0, mainlist)) print("\nThe Negative Elements in the Range :") print(listNeg)
Viewing The Result
Open the cmd window and run the python file to see the result.
In the given range from -30 to 20 : The Main List : [-30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] The Negative Elements in the Range : [-30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1]
In this Python article, using four different examples, the ways to show how to find the negative numbers from a given range and to print these were given. First, in example 1, the negative numbers were separated into another list. In example 2, all the elements that were nonnegatives were removed. In example 3, the list was split and only negatives were retained. In example 4, the filter was used to select the negative numbers.