Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Print all Negative Numbers in a Range - Python
Next article icon

Print all Negative Numbers in a Range - Python

Last Updated : 01 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a range we need to print all negative number within specific range. For example, we are given start and end point start, end = -5, 0 we need to print all numbers so that output should be -5 -4 -3 -2 -1.

Using a loop

We can use a loop to iterate through a given range and check if each number is negative. If number is negative it gets printed inside loop.

Python
start, end = -5, 0   for i in range(start, end + 1):       if i < 0:  # Check if the number is negative         print(i)   

Output
-5 -4 -3 -2 -1 

Explanation:

  • Loop iterates through the range from -5 to 0 (inclusive) checking each number to see if it is negative.
  • If number is negative (i < 0), it gets printed.

Using list comprehension

We can use list comprehension to generate a list of negative numbers within the specified range by iterating through range and filtering values less than 0.

Python
start, end = -10, 0 # List comprehension to generate a list of negative numbers within the range n = [i for i in range(start, end + 1) if i < 0]   print(n)   

Output
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1] 

Explanation: List comprehension iterates through the range from -10 to 0 and includes only numbers less than 0.

Using filter()

We can use filter() to apply a condition (i < 0) to range of numbers returning only negative numbers.

Python
start, end = -10, 0 e  # Use filter() to keep only  negative numbers from  range and convert result to a list n = list(filter(lambda x: x < 0, range(start, end + 1)))    print(n)   

Output
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1] 

Explanation: filter() function applies a lambda function (lambda x: x < 0) to each element in the range from -10 to 0, keeping only negative numbers.

Using map()

We can use map() to iterate over the range and apply a lambda function that checks if each number is negative then filter out non-negative values.

Python
start, end = -10, 0   n = list(map(lambda x: x if x < 0 else None, range(start, end + 1)))    # Use list comprehension to remove None values (non-negative numbers) n = [num for num in n if num is not None]   print(n)   

Output
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1] 

Explanation:

  • The map() function applies a lambda that returns the number if it's negative, otherwise it returns None, resulting in a list where non-negative numbers are replaced by None.
  • A list comprehension filters out the None values, leaving only the negative numbers.

Next Article
Print all Negative Numbers in a Range - Python

S

Shivam_k
Improve
Article Tags :
  • Python
  • Python Programs
  • Computer Science Fundamentals
  • DSA
  • python-list
  • Python list-programs
Practice Tags :
  • python
  • python-list

Similar Reads

    Print all even numbers in a range - Python
    Our task is to print all even numbers within a given range. The simplest way to achieve this is by using a loop to iterate through the range and check each number for evenness. Let's explore some methods to do so.Using LoopWe can use a for loop with if conditional to check if a number is even.Python
    2 min read
    Python program to print negative numbers in a list
    In Python, it’s often necessary to find and print negative numbers from a list. In this article we will explore various approches to print negative numbers in a list. The most basic method for printing negative numbers is to use a for loop to iterate through the list and check each element.Pythona =
    2 min read
    Print Numbers in an Interval - Python
    In this article, we are going to learn how to print numbers within a given interval in Python using simple loops, list comprehensions, and step-based ranges.For Example:Input : i = 2, j = 5Output : 2 3 4 5Input : i = 10, j = 20 , s = 2Output : 10 12 14 16 18 20Let’s explore different methods to prin
    2 min read
    Working with Negative Numbers in Python
    Negative numbers play a crucial role in various mathematical and programming scenarios. In Python, handling negative numbers is a fundamental skill for developers. In this article, we will explore some commonly used methods for working with negative numbers in Python, along with examples to illustra
    3 min read
    Python program to print all positive numbers in a range
    In this article, we will explore various methods to print all positive numbers in a range. The simplest way to do this is by using a loop. Use a simple for loop to iterate through the given range and check if each number is greater than zero before printing it.Pythonstart = -5 end = 3 # Loop through
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences