Sum of all positive and negative numbers in string python [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 5 months ago.
Sorry if this is a stupid question I am still very new to python but I was wondering,
If I have string, s = '1+6.3+3-7.6'
how could I take the sum of that?
I have tried using isdigit() but that just gets all the digits
I have also tried turning it to a float but I got could not convert string to float
I also tried turning it into an integer but I got invalid literal for int() with base 10.

Related

Getting a syntax error for leading zeros in python 3.x while using integer value [duplicate]

This question already has answers here:
Leading zeros are not allowed in Python?
(2 answers)
Closed 11 months ago.
I'm trying to run a simple program code which uses a variable representing an integer number.
fav_number = 07
print ("My favourite number is:",fav_number)
after running the code I got this error:
File "C:\Users\Desktop\Chapter 2\numbers.py", line 7
fav_number = 07
^
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o
prefix for octal integers
[Finished in 0.37s with exit code 1]
after looking on internet for solution I could only find that zero isn't allowed in start of an integer value? can someone some explain to me in much simpler way why it's not allowed and what's the solution to it?
Note: I'm beginner learner so I'd be thankful if someone gives me an additional basic explanation.
it is just how the language is defined. it causes some ambiguity, as a leading 0 can be used to represent a hex value, so for integer values, it is not allowed.

Python : Is a number too big? [duplicate]

This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 1 year ago.
I've learnt that python supports very large numbers with int itself.
But in this case :
print(int(12630717197566440579/10))
My answer is
1263071719756644096
and not
1263071719756644057
As it's supposed to be.
Can someone tell me why?

Is there a Math function to strip off decimals from a float (in Python)? [duplicate]

This question already has answers here:
Splitting a number into the integer and decimal parts
(9 answers)
How to get numbers after decimal point?
(37 answers)
Closed 2 years ago.
If I have a float like 32.879, and I want to end up with just 0.879, I can think of a few ways to do it, like:
Convert to string, strip off everything before the ., convert back to float; OR
32.879 - int(32.879)
Both of those feel like hacks. Is there no pure math operation that can do this?
Sort of like using abs() instead of if x < 0: x = x*-1
I'm dealing with Python, but if anyone can tell me the math name for this operation, I can probably google the Python way to do it.

How do i convert a 'string' maths equation to int on python? [duplicate]

This question already has answers here:
Evaluating a mathematical expression in a string
(14 answers)
Closed 2 years ago.
Say I have a string '1+1', is it possible to convert it into a simple 'int' equation (1+1) to get 2?
I've tried int('1+1') but I get
->ValueError: invalid literal for int() with base 10: '1+1'
You'll need a parser (or e.g. ast.literal_eval(), eval() wich basically are or are built upon a parser):
string = '1+1'
result = eval(string)
print(result)
Use eval():
print(eval(‘1+1’))
2

Use Python 3 str.format to truncate a float to have no more than x number of digits [duplicate]

This question already has answers here:
Formatting floats without trailing zeros
(21 answers)
Closed 4 years ago.
I am looking for an expression that will truncate a float to at most a certain number of digits. I want to preserve a certain number of decimals, without having unnecessary trailing 0s.
So, this almost works as desired:
"{0:3.f"}.format(number)
For input 3.123000001:
"{0:.3f}".format(3.1230000001)
'3.123'
Great. But for input 3:
"{0:.3f}".format(3)
'3.000'
I figured out the answer while I was writing the question. Just add .rstrip('0') to the expression. So:
"{0:3.f}".format(number).rstrip('0')

Categories

Resources