• We just launched and are currently in beta. Join us as we build and grow the community.

How to Print all Negative Numbers in a Range in Python

AUS_HARRY

Future Tech Evangelist
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
135
Likes
65
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial, we’ll learn how to program “How to Print All Negative Numbers in a Range in Python.” The objective is to safely print all the negative numbers in a range. This tutorial will guide you through the process step by step, ensuring that the result is correct and efficient. So, let’s get started!

This topic is straightforward to understand. Just follow the instructions I provide, and you’ll complete it with ease. The program I’ll demonstrate will illustrate the proper way to print all the negative numbers. So, let’s dive into the coding process!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. start,

    end =

    -15

    ,

    0


  2. while

    True

    :
  3. print

    (

    "\n

    ================= Print all Negative Numbers in a Range =================\n

    \n

    "

    )
  4. n =

    list

    (

    map

    (

    lambda

    x: x if

    x <

    0

    else

    None

    ,

    range

    (

    start,

    end + 1

    )

    )

    )


  5. n =

    [

    num for

    num in

    n if

    num is

    not

    None

    ]
  6. print

    (

    n)

  7. opt =

    input

    (

    "\n

    Do you want to try again?(yes/no): "

    )

  8. if

    opt.lower

    (

    )

    ==

    'yes'

    :
  9. ret=

    False
  10. elif

    opt.lower

    (

    )

    ==

    'no'

    :
  11. ret=

    True
  12. print

    (

    "Exiting program...."

    )
  13. else

    :
  14. print

    (

    "Please enter yes/no:"

    )
  15. break

  16. if

    ret ==

    False

    :
  17. continue

This program prints all negative numbers within a specified range using a lambda function combined with map() and list comprehension. It iterates through the range from -15 to 0, filters out only the negative numbers, and displays them as a list. The program repeats the process until the user decides to exit.

Output:

how-to-print-all-negative-numbers-in-a-range-in-python-1.png

There you have it we successfully created How to Print all Negative Numbers in a Range in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top