Python 3 - convert mathematical action to integer [duplicate] - python

This question already has answers here:
Math operations from string [duplicate]
(8 answers)
Closed 6 years ago.
I have a string with a formula 5 - 3, and I need to get the result in integer. How could I do that?

use eval function:
eval("5 - 3") # 2

test = "5-3"
print(eval(test))
Gives 2

Related

Curious about the function of ~in the following code [duplicate]

This question already has answers here:
The tilde operator in Python
(9 answers)
Closed 1 year ago.
pd_selftest = pd_selftest[pd_selftest['SICCD'] != 0]
pd_selftest = pd_selftest[~pd_selftest['SICCD'].isnull()]
I'd like to know what the function of the ~ is in the above code.
That's the bit-wise invert or not operator. So, it returns only those lines where the SICCID column is not null. I would probably use the word not in this case.

Python exponential calculation [duplicate]

This question already has answers here:
Why is exponentiation applied right to left?
(4 answers)
Closed 2 years ago.
Am New to python trying out some basic python function. Came across exponential
In python
2 ** 2 ** 3 is 256
But while in mathematics getting as 64.
Use parentheses. This will give the correct answer.
(2 ** 2) ** 3
Use parentheses
x = (2**2)**3
or:
pow(2,2*3)

Is there any method to avoid Bankers rounding in Python 3? [duplicate]

This question already has answers here:
How to properly round-up half float numbers?
(21 answers)
Closed 2 years ago.
For example
round(18.5) gives me 18
round(19.5) gives me 20
I want the output of round(18.5) to be 19
math.floor(x + 0.5) will always round .5 up.

How to convert to log base 2? [duplicate]

This question already has an answer here:
NumPy: Logarithm with base n
(1 answer)
Closed 2 years ago.
How can i convert the following code to log base 2?
df["col1"] = df["Target"].map(lambda i: np.log(i) if i > 0 else 0)
I think you just want to use np.log2 instead of np.log.

Python 3 integer division [duplicate]

This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 6 years ago.
In Python 3 vs Python 2.6, I've noticed that I can divide two integers and get a float. How do you get the Python 2.6 behaviour back?
Is there a different method to get int/int = int?
Try this:
a = 1
b = 2
int_div = a // b

Categories

Resources