To randomly generate a list of numbers in Python, you can use the random
module. The random
module provides many functions to generate random numbers and you can use it to create a list of random integers. Here's an example code:
main.py145 chars7 lines
The above code will generate a list of 10 random integers between -10 and 10 (inclusive).
Output:
main.py40 chars2 lines
To remove the negative values from the list, you can use list comprehension and conditional statements to filter out the negative integers. Here's an example code:
main.py145 chars5 lines
The above code will create a new list called positive_numbers
which contains only the non-negative integers from the original list.
Output:
main.py11 chars2 lines
gistlibby LogSnag