Floats and integers

Floats and integers represent rational numbers with and without a decimal part, respectively. Those types are quite intuitive to use, as they can be added, subtracted, multiplied, divided, and more. Let's take a look at the following example. First, we assign two variables:

A = 6
B = 5

Now, let's go over some possible operations:

>>> A + B
11

>>> A - B
1

>>> A / B
1.2

>>> A * B
30

You can also raise numbers to a power by using double asterisks:

>>> 2**3
8

>>> 3**2
9

You might have noticed that the division of two integers will result in a float. This happens even if the remainder is zero:

>>> 10 / 2
5.0

If you need to keep the integer division result as an integer (rounded, if needed), then use a double slash instead; this operation has significantly higher performance. However, if any of the two values is a float, then it will keep the result as a float, although rounded. The following code shows this:

>>> 9 // 4
3

>>> 10.0 // 4
2.0

Finally, you can also get the remainder (or modulus) by using a percentage sign:

>>> 10 % 3
1
Starting with Python 3.5, the @ symbol is reserved for matrix multiplication. Core Python does not have a notion of matrices, and therefore can't use it. Multiple third-party libraries, however, respect and accept this symbol.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.133.206