How to run calculations [duplicate] - python

This question already has answers here:
How do I do exponentiation in python? [duplicate]
(3 answers)
Closed 2 years ago.
I am new to programming and just started a course and stuck on a demo section where I have to take a math calculation and program it in Python. When I program it do you follow BEDMAS order of figuring out or is there a logical order. The syntax is correct but I keep being told it is not right
See the equation below to be programmed into python. What are the rules for telling Python to calculate something correctly where 5 is the exponent
Use Python to calculate (((1+2)∗3)/4)5

Exponentiation in Python is done with the ** operator.
For instance:
2**8
is 256.

In Python to calculate exponent you use ** operator.
Similar to multiplication but double asterisk.
(((1+2)∗3)/4)**5
This will give you 32.
That is because you are using values which are all integers, so 9 / 4 will be 2, not 2.25.
Python has operator precedence that follows algebraic rules.

Related

what is the order of mathematical calculations in python? [duplicate]

This question already has answers here:
How do order of operations go on Python?
(5 answers)
Closed 2 years ago.
I tried to calculate the output of this problem in python: 4+6/2 and it was 7 then i reversed them as 6+4/2 and answer was 8. now whats the difference, and why this occurs? what is divided first?
Python uses PEMDAS, as do most languages. Division comes before addition, so it would be calculated as 4+(6/2)=4+3=7, and 6+(4/2)=8. This can be confirmed with any calculator.
PEMDAS is the standard order of operations:
P- Parentheses first
E- Exponents second
M/D- Multiplication or Division third (If there are multiple
multiplication or division signs in a row, then operate first to last)
A/S- Addition or Subtraction fourth (If there are multiple addition or
subtraction signs in a row, then operate first to last)

How does the following equation work in python x=4**3**5**7.Can someone please explain the order in which this is executed? [duplicate]

This question already has answers here:
What is the associativity of Python's ** operator?
(4 answers)
Closed 3 years ago.
I have come across an online quiz question where the above mentioned equation was asked.So,wanted to know how it is executed(The order) when we have multiple power operators(**).
This is something you can figure out with just a little experimentation:
>>> 2**3**4
2417851639229258349412352
>>> (2**3)**4
4096
>>> 2**(3**4)
2417851639229258349412352
Since the expression result matches what you get for 2**(3**4), that's the way it's interpreted.
This is confirmed in the documentation which states:
The following table summarizes the operator precedence in Python, from lowest precedence (least binding) to highest precedence (most binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for exponentiation, which groups from right to left).

Python issue with an arithmetic operation [duplicate]

This question already has answers here:
Why does Python return 0 for simple division calculation?
(6 answers)
What is the difference between '/' and '//' when used for division?
(16 answers)
Closed 4 years ago.
I have a line of Python code that doesn't work the way it should (at least to the best of my knowledge). Following is the code line:
print 'progress: {}%'.format((i/len(e_numbers))*100)
The value of i starts from 0 and goes up to length of e_numbers which is a list, while the length of e_numbers is around 17000. But the code always prints Progress: 0%.
Any idea why?
In Python 2, using / to divide two integers performs integer division by default, rounding the result down to the nearest integer value. Thus, as long as i is between 0 and len(e_numbers), i/len(e_numbers) is going to be 0. There are two possible solutions:
Cast one or both operands to a float before dividing, e.g., float(i)/len(e_numbers)
Put from __future__ import division at the top of your file so that / always produces a float.

round() function in python 2 and python 3 [duplicate]

This question already has answers here:
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 4 years ago.
I'm solving some coding challenges on CoderByte and unfortunately they provide Python2. One thing I noticed is that the round() function is giving different outputs on python3 and python2. When i write this on python 2:
print int(round(100/60))
I get output of 1 (please explain why)
But on python 3 the same command gives 2 which is correct.
In python 2 the divide operator on integers returns integer, so 100/60==1. This unintuitive C-like behaviour was changed in python 3.
To make this code work properly in python 2 you should convert one of the integers to float: print int(round(100/60.)), that . means 60.0.
The problem is not the rounding, but the interpretation of /.
In python 2 if dividing 2 integers with / you get an integer not a float.
In python 3 you get a float if you use / - the "pure" integer division is done by using //.
Python 2:
print(100/60) # ==> 1
print(100/60.0) # ==> 1.6666666666...
Python 3:
print (100/60) # ==> 1.6666
print (100//60) # ==> 1
They both get rounded accordingly, but if you input a 1 into round, it will result in 1.
You can read more about the reason for the changed behaviour here: PEP 238
The current division (/) operator has an ambiguous meaning for numerical arguments: it returns the floor of the mathematical result of division if the arguments are ints or longs, but it returns a reasonable approximation of the division result if the arguments are floats or complex. This makes expressions expecting float or complex results error-prone when integers are not expected but possible as inputs.
We propose to fix this by introducing different operators for different operations: x/y to return a reasonable approximation of the mathematical result of the division ("true division"), x//y to return the floor ("floor division"). We call the current, mixed meaning of x/y "classic division".

Python String Parsing Floats [duplicate]

This question already has answers here:
Convert fraction to float?
(9 answers)
Closed 8 years ago.
How to change a '14/15' string to a float?
I am trying to extract data from a text file would like to convert '1/3' to a float. float('1/3') doesn't work. I was thinking about splitting into two parts at '/' by 1 and 3 then dividing, but it seems cludgy. Is there a more pythonic way to do this? I'm using Python 2.7
If you only ever need to evaluate simple X/Y fractions:
s = "14/15"
num, denom = map(float, s.split("/", 1))
print(num / denom)
If you need a more complete expression evaluator, take a look at the asteval module.
Using eval() might also see like a nice easy way to do it, but I'd advise against it for security reasons.
If you trust your input:
from __future__ import division
eval('14/15')

Categories