Calculate by user input [duplicate] - python

This question already has answers here:
How to evaluate a user input of a string as a math expression without using eval()?
(2 answers)
Closed 12 months ago.
I want my program to do this:
Input formula:
(2*(3+4)-5)/6
Output answer of formula:
1.5
Is there anyway to do it? I can't find anything from the Internet. Asking here is my last attempt.

you can use eval
>>> a = eval('(2*(3+4)-5)/6')
>>> print(a)
1.5

Related

reversing a section of string in python with [start:stop:step] [duplicate]

This question already has answers here:
Understanding negative steps in list slicing
(2 answers)
Closed 1 year ago.
I am learning python and I can't solve this problem:
I am ok to reverse the whole string but I want to get only the "Hello" part
astring = "Hello world!"
I was expecting print(astring[0:4:-1]) would do the work but it does not.
print(astring[5:0:-1]) is better but the H is still missing. I get "olle"
Is there anyway to solve this?
Thank you,
Try this:
print(astring[4::-1])

Is there a function to evaluate a string into a integer (python 3.7) [duplicate]

This question already has answers here:
Evaluating a mathematical expression in a string
(14 answers)
Closed 2 years ago.
You may haven't understood the question correctly. So here is it in detailed way:
There is a string, let's say, x='200+350'
so now if I do int(x), it'll give me an error.
I want it to evaluate to 550 which is integer.
How may I do that?
try to use eval method
eval("200+350")
You can use eval()
x = '(2+3)*2'
print( eval(x) )
prints out 10

how can I write fractional powers in python code like x^(2/3)? [duplicate]

This question already has answers here:
How do I do exponentiation in python? [duplicate]
(3 answers)
Closed 4 years ago.
how can I write x^(2/3) in python code?
I want to include 1/3 and 2/3 in my calculation but unable to do so. please help
As you would do it with integer powers:
x ** (2 / 3)
Try this code here:
Also, you can initialize x
x=3
x**(2/3)

Why won't this regex work? [duplicate]

This question already has answers here:
Why doesn't [01-12] range work as expected?
(7 answers)
Closed 5 years ago.
Im try to match ip addresses and have come up with the following regular expression for python. I just cannot understand why this wont work. Any help would be greatly appreciated!
r"[0-255]\.[0-255]\.[0-255]\.[0-255]"
Because [0-255] means any char between 0 to 2 or 5. switch to something like
r"^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"

Large integers addition error in python 2.7.10 [duplicate]

This question already has answers here:
What do numbers starting with 0 mean in python?
(9 answers)
Closed 7 years ago.
I am trying to solve a problem using python. In which I have to deal with large integers (upto 500 digits). According to my current stage of understanding, python can handle any numbers in same traditional way. But I have problem in simple addition like this:
>>> p= 1001101111101011011100101100100110111011111011000100111100111110111101011011011100111001100011111010
>>> q= 0011111011111010111101111110101101111001111111100011111101101100100011010011111011111110110011111000
>>> p+q
1001101111105557844987142979708366943425581971579987152809865568761000527613931421735161949470823522L
Can anyone please explain why i got such an error.
Var q starts with a zero, making it an octal number, rather than decimal

Categories