Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plus/minus operator for python ±

Tags:

I am looking for a way to do a plus/minus operation in python 2 or 3. I do not know the command or operator, and I cannot find a command or operator to do this.

Am I missing something?

like image 220
Whitequill Riclo Avatar asked Jan 10 '15 02:01

Whitequill Riclo


People also ask

Is there a +- operator in Python?

In Python, to print the plus or minus symbol, you can use the unicode for the plus or minus sign '\u00B1'.

What does +- mean in math?

Definition of plus/minus sign : the sign ± used to indicate a quantity (such as 2 in "the square root of 4 is ±2") taking on both an algebraically positive value and its negative and to indicate a plus or minus quantity (such as 4 in "the population age was 30 ± 4 years") — called also plus/minus symbol.

What does == mean in Python?

The == operator compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators == and != , except when you're comparing to None .

What is exponent operator in Python?

Power (exponent) operator The operator that can be used to perform the exponent arithmetic in Python is ** . Given two real number operands, one on each side of the operator, it performs the exponential calculation ( 2**5 translates to 2*2*2*2*2 ).


Video Answer


2 Answers

If you are looking to print the ± symbol, just use:

print(u"\u00B1") 
like image 197
mimoralea Avatar answered Oct 13 '22 23:10

mimoralea


Another possibility: uncertainties is a module for doing calculations with error tolerances, ie

(2.1 +/- 0.05) + (0.6 +/- 0.05)    # => (2.7 +/- 0.1) 

which would be written as

from uncertainties import ufloat  ufloat(2.1, 0.05) + ufloat(0.6, 0.05) 

Edit: I was getting some odd results, and after a bit more playing with this I figured out why: the specified error is not a tolerance (hard additive limits as in engineering blueprints) but a standard-deviation value - which is why the above calculation results in

ufloat(2.7, 0.07071)    # not 0.1 as I expected! 
like image 22
Hugh Bothwell Avatar answered Oct 13 '22 23:10

Hugh Bothwell