Last Updated:

How do you float  (real) numbers in Python?

How do you float  (real) numbers in Python?

Surely everyone who studies Python (and indeed any programming language) wonders how numbers work here. In today's article, we will deal with this issue in detail.

Real float numbers

 

float  real numbers in Python

The float numbers in the python are as follows: 4.35; 2.56 etc. Their distinguishing feature from the main thing is what they must necessarily have.

An interesting fact: when writing something like ".45", the compiler will not give an error, and it will understand that there should be a zero in front of the dot.

Int integers

 

Python also has the most common integers with the int data type. For example: 3, 6, 8, 24, 56. More information about all the possibilities with numbers will be described later. So far, we can only say that in python you can work with huge numbers (654767655465876674576756457568), unlike many other programming languages.

Float () in python

 

I would like to tell you a little about the accuracy of calculating float numbers. Sometimes things like this can happen:
0.1+.0.1+0.1 != 0.3 ( != - not equal)

Why is this happening? You have to go quite far for the answer, because it has to do with how the computer stores and processes numbers. But if you do not go deeper, then we can say that there is simply no such number in the python.

This is an ordinary string to which the compiler selects the closest number. For example, according to the following formulas:

1 / 10 ≈ J / (2**N) = 0.1; J ≈ 2**N / 10 = 0.1

Display a number in python

 

To output anything in python, you need to use the print function. Print(What you want to withdraw).

Round to integer

 

There are many different libraries in python. One of which we will now have to use to learn how to round numbers. To connect it, you need to specify at the very beginning of the file:

import math

This model contains a lot of different functions ranging from rounding to trigonometry, but all this can be found in the official documentation. For now, let's look only at rounding.

x = 23.4

round(x) — this function rounds the numbers to the nearest whole (the answer will be 23). If we pass, say, 1.5, then rounding will occur to the nearest even.

It is worth paying attention to the fact that in the standard python there is already a round function, which takes 2 arguments:

round(x,n) - the entry means the following: rounded me the number x, by n decimal places.
floor(x) — Rounds the number down. (if x = 13.8 will give - 13, if x = 4.2 will give 4)
ceil(x) — rounds the number up. (if x = 13.8 will give - 14, if x = 4.2 will give 5)

Mathematical operations with numbers

They support all operations (addition, division) as in ordinary mathematics.

x + y — adds two numbers.
x — y — subtracts two numbers.
x * y — multiplies two numbers.
x/y — divides two numbers.
x // y — gives the whole part after division.
x % y — gives out the remainder of the division.
abs(x) — gives a module of the number
x ** y raises x to y power.

To exponentiate, you can also use the function pow(x, y) - where x is a digit, and y is a degree. To extract the square root of a number, you should use the already familiar math library. It has a method that is called sqrt(x).

A couple of examples:

>>> 130 + 34
164
>>> 15 * 3
45
>>> 20 / 3
6.666666666666666 >>>
14 // 4
3
>>> 14 % 4
2
>>> 3 ** 3
27
>>> pow(3, 3)
27

Complex numbers

 

In Python, there are also complex numbers. To work with them, you can use the cmath library, which you can learn more about in the documentation.

  • If we enter: complex(1, 2) we get the output — (1+2j)
  • To output the conjugate, use the x.conjugate() method
  • To display the imaginary part – x.imag
  • To display the actual part — x.real
  • And you can't compare complex numbers.

Python Proficiency Test

 

And in conclusion, we recommend that you take a small test on the topic to check yourself, and at the same time fix the material.

What numbers are there in a python?

Which module is used to work with mathematical expressions?

a) math b) cmth c) muth d)maths

What will the compiler give for such an expression: 0.05 + 0.20 + 0.15 == 0.1 + 0.1 + 0.1?

a) True b) False

What will the compiler give for the following expression: sqrt(49) + ads(-3) // 3?

(a) Mistake b) 3.333333333333 c) 3 d) 1

What will the compiler give for the following expression: ceil(23.2) + floor(66.8)?

(a) 98  (b) 99 (c) 100 (d) 101 (e) 102

I hope that the article was useful to you and you learned a lot of new things for yourself. But the most important thing in programming is practice. Therefore, you should practice all the knowledge gained right away.

Good luck!