Numeric variables

Numeric variables in Python are used to store numbers. Numbers are usually stored as integers, floating-point values, or complex numbers, depending on the requirements of the program. For a start, we will describe the most common types – integer and floating-point values.

Integers

Integers are whole numbers, such as 1, 20, 3000, 10200305, etc. They are declared simply by writing a whole number. For example, the statement x = 5 will store the integer number 5 in the variable x. For 64-bit platforms, you can store any whole numbers between -9223372036854775808 and 9223372036854775807 in an integer variable in Python.

 

In Python 3.x, integer and long were merged into a single integer type. In Python 2.x, they were two different data types.

 

Floating-point values

Numbers with a decimal portion are floating-point values. They are declared by writing a number with a decimal part. For example, the statement x=5.3 will store the floating-point value of 5.3 in the variable x. You can store incredibly large numbers, ranging from 2.2250738585072014 x 10308 to 1.7976931348623157 x 10308.

You can convert an integer number to a floating-point value using the float (NUMBER) function, like in this example:

x = 5
print(x)

print(float(x))

When executed, the code above produces the following output:

>>> 
5
5.0
>>>
To convert a floating-point value to an integer number, use the int(NUMBER) function.

 

Now, consider what happens if we divide two integer numbers:

>>> x = 5
>>> y = 2
>>> z = x / y
>>> print(z)
2.5
>>>

Notice how, although numbers are defined as integers (whole numbers), the result of the division operation was a floating-point number. This is a Python 3.x feature – Python 2.x would perform integer division and return the result of 2.

Geek University 2022