What is x y in Python 6

Calculating with python3

As the next step, we will test the calculator suitability of the python interpreter. Call the Python interpreter, if it is not already open, and off you go.

user @ linux: ~> python3 Python 3.2 (r32: 88445, Feb 21 2011, 04:07:45) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 1 + 2 + 3 6 >>> 1 * 2 * 3 6 >>> print (1 + 1) 2 >>> 1+ 1 2 >>> 1 + 1 2 >>> 1 +1 2> >> 3 * 5 15 >>> 3 * 5 -2 13 >>> 3 * (5 -2) 9 >>>

So the thing can really do arithmetic. We saw that the 1 + 1 and 1 + 1 work - python is tolerant of spaces in the expression (especially at the beginning of the expression there must be no space).
I prefer 1 + 1 for the sake of clarity.

The calculation rules are of course also mastered. Point calculation before line calculation, and brackets come before everyone else.
In addition to the basic calculation types, there is also the calculation of residual classes and exponentiation. "2 ** 4" is therefore "two to the power of four"

>>> 2**8 256 >>>

But be careful!

>>> -2**4 -16 >>>

I beg your pardon? -2 * -2 * -2 * -2 is 16, and NOT -16. That is also true. But here (in python) it behaves differently in that -1 * (2 ** 4) is executed. So you need the following notation:

>>> (-2)**4 16 >>>

You can get an invoice (in the interpreter, NOT in the script file) can also be divided into several stages. The fact that the results of a separate calculation are stored in (underscore) helps us to do this. An example is worth a thousand words:

>>> (10+15+1313+44+455121112)/5. 91024498.799999997 >>> 10+15+1313+44+455121112 455122494 >>> _ / 5. 91024498.799999997 >>> 8 * 2 16 >>> _ - 3 13 >>>

Now let's look at the quirks of dividing.

>>> 2 / 3 0.6666666666666666 >>> 2 // 3 0 >>> 2 // 3. 0.0 >>>

A whole number divided by another whole number results in a floating point number (float). If both (divisor and divident) are floats or just one of the two, a float also results.
results in 1.0.
The division is carried out with two slashes in such a way that the resultfloored is, that is, the floating point number is rounded down and output as.

Incidentally, a decimal point is written with and not with (as usual from school)!

>>> 2.4 / 1.2 2.0 >>>

Floating point numbers are also mentioned. In English, from which the term float has revealed.

>>> 2.0 / 3.0 0.66666666666666663 >>> 2. / 3. 0.66666666666666663 >>>

We see that both spellings work. Just adding the dot turns int into a float, and what I remember was the same with calculators in the 1970s.

The arithmetic symbols are referred to as.

>>> 14% 4 2 >>> (21 + 9)% 24 # 9 p.m. plus 9 hours 6 >>> 6.8 / 2.4 2.8333333333333335 >>> 6.8 // 2.4 2.0 >>>

We have already shown exponentiation above.

>>> 2 ** 3 8 >>> 2 ** 3. 8.0 >>>

If you can potentiate, what about pulling roots? Is the?

>>> 4 ** 0.5 2.0 >>> 27 ** 1./3. 9.0 >>>

Oups! Ah, the rules of precedence apply again!

>>> (27 ** 1.)/3. 9.0 >>> 27 ** (1./3.) 3.0 >>> (2**0.5)*(2**0.5) 2.0000000000000004 >>> 2**0.5*2**0.5 2.0000000000000004 >>>

Calculating with floating point numbers on the computer is associated with (rounding) errors in the microprocessor (CPU), which leads to small inaccuracies that can have a major impact in space travel.
In astronomy you have to take this into account. This is precisely where you need reliable arbitrary precision, and this is obtained by using the decimal module (part of the STL).
For conventional calculations, however, the accuracy of float calculations should be sufficiently accurate.

Another way of pulling roots, which is more common in python, we will soon work on in another link (when we get to know the module).

Fractions

Calculating with fractions is very simple. We want to get away from fractions-Module for the time being only the class Fraction to fetch. (If the terms module or class are still unknown to you, then just accept the lines of code and everything will be clarified later in the BaseRange on)

>>> from fractions import Fraction >>> fraction1 = Fraction (1,2) >>> fraction2 = Fraction (5,6) >>> print (fraction1) 1/2 >>> print (fraction2) 5/6> >> print (fraction 1 + fraction 2) 4/3 >>> print (fraction 1 / fraction 2) 3/5

So you can do a lot of math. You can also try out subtractions and multiplications of two fractions yourself.
With the built-in function divmod() you can see how often the divisor fits into the divident (first value of the return), and how much remainder remains (second value of the return).

>>> erg = fraction1 + 2 * fraction2 >>> divmod (erg.numerator, erg.denominator) (2, 1) >>> print (erg) 13/6 >>>

Here it goes to the top of the page and there it goes to the python overview