close

How to convert positive numbers to negative in Python?

Hello guys, here we are going to learn about How to convert positive numbers to negative in Python? the conversion of positive numbers into negative as well as negative numbers into positive.

Example :

Input : [1,2,3]
Output : [-1,-2,-3]

Input : [-1,2,-3,4,5]
Output : [1,-2,3,-4,-5]

so here are the some methods which is used mostly to convert positive numbers to negative numbers.

How to convert positive numbers to negative in Python?

  1. How to convert positive numbers to negative in Python?

    to convert positive numbers to negative in Python It is the simplest method and mostly used in programmings: for num in range(1,6): print(-abs(num)) In this method, you have to use just -abs() and it will convert the numbers into negative phase. as you can see it in my output.

  2. convert positive numbers to negative in Python

    to convert positive numbers to negative in Python This is a list comprehension method. In this method use the numbers in a list like this way, In this method, use numbers in a list and in output it will give you the opposite phase numbers.

Method : 1

It is the simplest method and mostly used in programmings.

for num in range(1,6):
    print(-abs(num))

Output :

-1
-2
-3
-4
-5

In this method, you have to use just -abs() and it will convert the numbers into negative phase. as you can see it in my output.

Method : 2

This is a list comprehension method. In this method use the numbers in a list like this way,

def Convert(numbers):
    return [ -i for i in numbers ]
  
numbers = [1, -2, 3, -4]
print(Convert(numbers))

Output :

[-1, 2, -3, 4]

In this method, use numbers in a list and in output it will give you the opposite phase numbers.

Method : 3

You can also use this method,

for num in range(1,5):
    num ='-' + str(num).strip()
    print(num)

Output :

-1
-2
-3
-4

Thus, these are the methods you can use to change the numbers. from all of these methods, first one is most easy and widely use where abs() used for change into negative while -abs() used for to get positive numbers as well as.

Summary

It’s all About this tutorial. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?

Also, Read

Leave a Comment