DEV Community

Cover image for The 123 of Python Integers
Christopher Ambala
Christopher Ambala

Posted on • Edited on

The 123 of Python Integers

Today we look at core python data types which are;

  • Int

Integer data type

Integers in Python are represented by the int data type (the abbreviation int comes from the word integer). To determine an integer of type int, a sequence of digits from 0 to 9 is used.

An explicitly specified numeric value in the program code is called an integer literal. When Python encounters an integer literal, it creates an int object that stores the specified value.

n = 17 # integer literal
m = 7 # integer literal
Enter fullscreen mode Exit fullscreen mode

The integer data type int is used not only because it occurs in the real world, but also because it naturally occurs when creating most programs.

Converting a string to an integer

To convert a string to an integer, we use the int() command:

num = int(input()) # converting a read string to an integer
Enter fullscreen mode Exit fullscreen mode

It is not necessary to use the input() command to convert a string to an integer.

The following code converts the string 12345 to an integer:

n = int('12345') # convert string to integer
Enter fullscreen mode Exit fullscreen mode

If the string is not a number, an error will occur during the conversion.

Integer operators

Python provides four basic arithmetic operators for working with integers (+, −, , /), and also three additional ones (% for remainder, // for integer division and * for exponentiation).

The following program demonstrates all integer operators:

a = 13
b = 7

total = a + b
diff = a - b
prod = a * b
div1 = a / b
div2 = a // b
mod = a % b
exp = a ** b

print(a, '+', b, '=', total)
print(a, '-', b, '=', diff)
print(a, '*', b, '=', prod)
print(a, '/', b, '=', div1)
print(a, '//', b, '=', div2)
print(a, '%', b, '=', mod)
print(a, '**', b, '=', exp)
Enter fullscreen mode Exit fullscreen mode

As a result of the operation of such a program , it will be output:

13 + 7 = 20
13–7 = 6
13 * 7 = 91
13 / 7 = 1.8571428571428572
13 // 7 = 1
13 % 7 = 6
13 ** 7 = 62748517
Enter fullscreen mode Exit fullscreen mode

With the usual division (/), a number that is not an integer is obtained. Dividing by zero leads to an error.

Long arithmetic

A distinctive feature of the Python language is the unlimited integer data type. In fact, the size of the number depends only on the availability of free memory on the computer. This distinguishes Python from languages such as C++, C, C#, Java where variables of the whole data type have a limitation. For example, in C#, the range of integers is limited from −263−263 to 263–1263–1.

atom = 10 ** 80 # number of atoms in the universe
print('Number of atoms =', atom)
Enter fullscreen mode Exit fullscreen mode

The result of the program will be a number with 81 digits:

Number of atoms = 100000000000000000000000000000000000000000
Enter fullscreen mode Exit fullscreen mode

Separator character

For easy reading of numbers, you can use the underscore character:

num1 = 25_000_000
num2 = 25000000
print(num1)
print(num2)
Enter fullscreen mode Exit fullscreen mode

The result of executing such code will be:

25000000
25000000
Enter fullscreen mode Exit fullscreen mode

Floating point numbers

Along with integers in Python, it is possible to work with fractional (real) numbers. So, for example, the numbers 2/3, π — are real and the integer type int is not enough to represent them.

Fractional (real) numbers in computer science are called float point numbers.

Python uses the float data type to represent floating-point numbers.

e = 2.71828 #floating point literal
pi = 3.1415 #floating point literal
Enter fullscreen mode Exit fullscreen mode

Unlike mathematics, where the separator is a comma, in computer science a dot is used.

Converting a string to a floating point number

To convert a string to a floating-point number, we use the float() command:

num = float(input()) #converting a read string to a floating-point number
Enter fullscreen mode Exit fullscreen mode

It is not necessary to use the input() command to convert a string to a floating-point number.

The following code converts the string 1.2345 to a floating point number:

n = float('1.2345') #converting a string to a floating-point number
Enter fullscreen mode Exit fullscreen mode

If the string is not a number, an error will occur during the conversion.

Arithmetic operators

Python provides four basic arithmetic operators for working with floating−point numbers (+, -, , /) and one additional (* for exponentiation).

The following program demonstrates arithmetic operators:

a = 13.5
b = 2.0

total = a + b
diff = a - b
prod = a * b
div = a / b
exp = a ** b

print(a, '+', b, '=', total)
print(a, '-', b, '=', diff)
print(a, '*', b, '=', prod)
print(a, '/', b, '=', div)
print(a, '**', b, '=', exp)
Enter fullscreen mode Exit fullscreen mode

As a result of the operation of such a program , it will be output:

13.5 + 2.0 = 15.5
13.5–2.0 = 11.5
13.5 * 2.0 = 27.0
13.5 / 2.0 = 6.75
13.5 ** 2.0 = 182.25
Enter fullscreen mode Exit fullscreen mode

Dividing by zero leads to an error.

Conversion between int and float

Implicit conversion. Any integer (int type) can be used where a floating-point number (float type) is expected, because Python automatically converts integers to floating-point numbers if necessary.

Explicit conversion. A floating-point number cannot be implicitly converted to an integer. For such a conversion, it is necessary to use an explicit conversion using the int() command.

num1 = 17.89
num2 = -13.56
num3 = int(num1)
num4 = int(num2)

print(num3)
print(num4)
Enter fullscreen mode Exit fullscreen mode

The result of executing such code will be:

17
-13
Enter fullscreen mode Exit fullscreen mode

Note that the conversion of floating-point numbers to an integer is performed with rounding towards zero, that is int(1.7) = 1, int(-1.7) = -1.For further reading refer to the Python Documentation

Top comments (0)