This question already has answers here:
Math operations from string [duplicate]
(8 answers)
Evaluating a mathematical expression in a string
(14 answers)
Closed 3 years ago.
I am fairly new to python/ programming in general and i am trying to write a function that will convert an equation passed in as a string to its numeric representation and do some basic calculations. I am having some trouble with the parenthesis as i am not sure how to represent them for order of operations.
Any help of tips would be greatly appreciated. Thank you!
EquationAsString ="( 2 + 3 ) * 5"
def toEquation(EquationAsString):
Equation = EquationAsString.split(' ')
#store info in list and use it like a stack, check the type etc.
answer = 25
return answer
You can use the eval method to do such a thing.
Example:
print(eval('(2+3)*5'))
Output:
25
And if you really wanted to put it in a function:
def evaluation_string(input):
print(eval(input))
Example
def evaluation_string(input):
print(eval(input))
string_equation = '(2+3)*5'
evaluation_string(string_equation)
Output:
25
Related
This question already has answers here:
Evaluating a mathematical expression in a string
(14 answers)
Closed 7 months ago.
How to make a function that can interpret a string as a code ?
var1 = 5
var2 = 10
var3 = 7
str = "(var1*var2)+var3"
result = calculate(str) # I need help to write this function
result should output 57
Use result = eval(str) instead of result = calculate(str).
Disclaimer: eval() should be used carefully since it can execute anything passed to it. More details can be found in this article.
This question already has an answer here:
Python 3 integer division [duplicate]
(1 answer)
Closed 2 years ago.
if I use integer type cast conversion technique then it doesn't work for large numbers like 12630717197566440063
I got wrong answer in some cases like below in python 3
a =12630717197566440063;
print(a)
temp = a/10
print(int(temp))
Then I am getting 1263071719756644096 as a answer instead of 1263071719756644006
You can use the // (floor division) operator:
temp = a//10
print(temp)
This question already has answers here:
Converting integer to binary in python
(17 answers)
Closed 3 years ago.
I have a string of 6 characters (only 0s and 1s)on which I have to use binary operations. I came across a few pages but most mentioned converting characters to binary and appending them to give the final result.
I have to use it in this function
def acmTeam(topic):
global finalCount
for i in range(len(topic)-1):
for j in range(i+1,len(topic)):
final=toBinary(topic[i])|toBinary(topic[j])
print(final)
One example value of topic is
['10101', '11100', '11010', '00101']
and i want 10101 and 11100 binary values
Here I can create my own function toBinary to convert it to binary equivalent and return, but is there any inbuilt function or more efficient way to do so in python?
Thanks in advance :)
Try this:
int(s, base=2)
for example,
for i in ['10101', '11100', '11010', '00101']:
print(int(i, base=2))
#metatoaster also mentioned this.
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
This question already has answers here:
Math operations from string [duplicate]
(8 answers)
Evaluating a mathematical expression in a string
(14 answers)
Simplifying code - perform mathematical operations based on operator
(2 answers)
Closed 5 years ago.
This is just a general question as I am curious. When I run the following code I get a syntax error which is understandable. However would there be a way to save the + sign in an variable so I get an answer of 4?
SIGN = +
UNIT = 1
UNIT2 = 3
print(UNIT SIGN UNIT2)
You can make a function for the + operator:
def add(a, b):
return a + b
and store it:
operation = add
unit = 1
unit2 = 3
print(operation(unit, unit2))
Functions for operators are even built into Python:
from operator import add