How to convert String to Boolean Logic? [duplicate] - python

This question already has answers here:
Safely evaluate simple string equation
(4 answers)
Evaluating a mathematical expression in a string
(14 answers)
Closed 4 years ago.
I have sting input like:
12 == 21
(10 != 12) or (15 == 13)
True and (10 == 10)
And i need to return boolean value of this input. Can i convert it using exec() or bool()? I don't want to write big method to solve it .

Related

How do I calculate the total of all the prices in a list ? such as this one? [duplicate]

This question already has answers here:
Split Strings into words with multiple word boundary delimiters
(31 answers)
How do I parse a string to a float or int?
(32 answers)
How do I add together integers in a list in python?
(4 answers)
Closed 5 months ago.
expenses =['Grocery-200.00', 'Gas-100.78', \
'Rent-1200.00', 'Water-23.34', \
'Vacation-356.89', 'Phone-123.00']
Just split on the "-", cast to float and sum:
sum([float(x.split("-")[1]) for x in expenses])

Python float number comparison [duplicate]

This question already has answers here:
What is the best way to compare floats for almost-equality in Python?
(18 answers)
Is floating point math broken?
(31 answers)
Closed 3 years ago.
Why when decreasing decimal part after 10.0000000000000008 the comparison returning True?
>>> 10 == 10.000000000000001
False
>>> 10 == 10.0000000000000001
True

== and is not returning the same even though comparing an int [duplicate]

This question already has answers here:
"is" operator behaves unexpectedly with integers
(11 answers)
Is there a difference between "==" and "is"?
(13 answers)
Closed 3 years ago.
I'm using the pow function and found out I had a bug in my code because == and is were not having the same behavior.
Here goes an example: pow(3, 47159012670, 47159012671) == 1 returns True but pow(3, 47159012670, 47159012671) is 1 returns False.
I'd like to know what is it that I'm not getting.
The difference between is and == is:
a1 is a2 # return True
only when they share the same location or id in memory, that means when id(a1) and id(a2) are the same. I hope this will help more.

String to Int in Python [duplicate]

This question already has answers here:
Why does map return a map object instead of a list in Python 3?
(4 answers)
Getting a map() to return a list in Python 3.x
(11 answers)
Closed 5 years ago.
How to Convert a string like '123' to int 1,2 and 3 so that I can perform 1+2+3. I am new to python. Can you please help me? I am not able to split the list. I don't think splitting the string will be of any use as there are no delimiters. Can you help me to understand how can this string elements be separated and treated as intergers?
x = "123"
s = 0
for a in x:
s = int(a) + s

Why does 1/len(list) return 0? [duplicate]

This question already has answers here:
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 5 years ago.
lets say I have a list a and a variable b = 1/len(a) but when I display the value of b it gives me 0
Because of integer divided by integer.
Try b=1.0/len(a)

Categories