How to Convert a Positive Number to a Negative Number in Python?

In this article, let us see how to convert positive numbers to negative numbers in Python. There are basically four ways to go about doing this. They are:

Nowadays, developers must incorporate this type of mechanism into a variety of applications, particularly gaming apps.

Converting Positive Number to a Negative Number in Python

Method #1: Using -abs() method

Approach:

  • Loop until the given range(1 to 10) using the for loop
  • Convert each number in the given range to a negative number using the -abs() function.
  • The Exit of the Program.

Below is the implementation:

# Loop until the given range(1 to 10) using the for loop
for k in range(1, 10):
    # Convert each number in the given range to a negative
    # number using the -abs() function
    print(-abs(k))

Output:

-1
-2
-3
-4
-5
-6
-7
-8
-9

Method #2: Using string concatenation

Approach:

  • Loop in the given range from 1 to 6(not included) using the for loop
  • Convert the iterator value to string using the str() and concatenate it with the minus(-) sign to make it a negative number.
  • Store it in the same variable.
  • Print the iterator value.
  • The Exit of the Program.

Below is the implementation:

# Loop in the given range from 1 to 6(not included) using the for loop
for k in range(1, 6):
    # Convert the iterator value to string using the str() and concatenate it
    # with the minus(-) sign to make it a negative number.
    # Store it in the same variable.
    k ='-' + str(k)
    # Print the iterator value
    print(k)

Output:

-1
-2
-3
-4
-5

Method #3: Multiplying with -1

Approach:

  • Give the list as static input and store it in a variable.
  • Loop in the given list using the for loop
  • Multiply each element of the given list with -1 and store it in another variable.
  • Print the above obtained negative value of each list element.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst =[10, 20, 30, 40, 50]
# Loop in the given list using the for loop
for itr in gvn_lst:
    # Multiply each element of the given list with -1 and store it 
    # in another variable.
    negative_num = itr * (-1)
    # Print the above obtained negative value of each list element
    print(negative_num)

Output:

-10
-20
-30
-40
-50

Method #4: Similar to 3rd method but for random Numbers

Approach:

  • Import random module using the import keyword
  • Create a new empty list and store it in a variable
  • Give some random length of the list as static input and store it in another variable.
  • Loop till the given list length using the for loop
  • Generate some random numbers in the range 0 to the given list length using the randint() function, multiply it with -1 to convert it into a negative number and append it to the above created empty list.
  • Print the above list.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword
import random
# Create a new empty list and store it in a variable
gvn_lst = []
# Give some random length of the list as static input and store it in another variable.
len_lst = 4
# Loop till the given list length using the for loop
for i in range(len_lst):
   # Generate some random numbers in the range 0 to given list length 
   # using the randint() function, multiply it with -1 to convert it into 
   # a negative number and append it to the above created empty list 
   gvn_lst.append((random.randint(0, len_lst)*-1))
# Print the above list
print(gvn_lst)

Output:

[-1, -4, -1, -3]