Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert positive numbers to negative in Python?

I know that abs() can be used to convert numbers to positive, but is there somthing that does the opposite?

I have an array full of numbers which I need to convert to negative:

array1 = []
arrayLength = 25
for i in arrayLength:
   array1.append(random.randint(0, arrayLength)

I thought perhaps I could convert the numbers as they're being added, not after the array is finished. Anyone knows the code for that?

Many thanks in advance

like image 572
Aya Noaman Avatar asked Oct 20 '20 12:10

Aya Noaman


1 Answers

If you want to force a number to negative, regardless of whether it's initially positive or negative, you can use:

    -abs(n)

Note that integer 0 will remain 0.

like image 95
Tom Karzes Avatar answered Nov 11 '22 15:11

Tom Karzes