A Guide to Integer Data Types in Python
Examining the Key Concepts of Python's Integer Data Types
Python is a flexible, dynamically typed programming language that accommodates a range of data types for managing numerical values. Among the essential numeric data types in Python is the integer. This article delves into integers in Python, discussing their attributes, operations, and real-world applications.
What is an Integer?
Integers in Python are whole numbers that can be either positive or negative and do not contain decimal points. They can represent a broad spectrum of values, from smaller integers such as -10 to larger ones like 1,000,000. Python's integer data type is unique in that it can automatically adapt to the necessary size to hold the value, which allows for seamless calculations without concerns about overflows. This flexibility sets Python apart from some other programming languages.
How are Integers Stored in Computers?
Integers are represented internally using base-2 (binary) digits, not decimal.
Let's take an example of the integer 19 and see how it is internally stored. First, we have to convert the integer into binary digits.
Converting numbers from the decimal (numeric) system to the binary system is a fundamental concept in computer science and digital electronics. This process involves representing a decimal number using only two digits: 0 and 1, which are the building blocks of binary code. In this article, we will explore how to perform numeric to binary conversion, understand the principles behind it, and see practical examples.
The Basics of Binary Representation
Decimal System: The decimal system, also known as the base-10 system, uses ten digits (0-9) to represent numbers. Each position in a decimal number has a place value, such as units, tens, hundreds, etc.
Binary System: The binary system, or base-2 system, uses only two digits (0 and 1). Each position in a binary number represents a power of 2, such as 1, 2, 4, 8, 16, and so on.
Converting Decimal to Binary
To convert a decimal number to binary, follow these steps:
Step 1: Start with the Decimal Number
Let's take the decimal number 19 as an example.
Step 2: Divide by 2
Divide the decimal number by 2 and record the quotient (integer division) and the remainder.
19 divided by 2 equals 9 with a remainder of 1.
Step 3: Repeat Division
Continue dividing the quotient from the previous step by 2 until the quotient becomes 0. Record the remainder at each step.
9 divided by 2: quotient = 4, remainder = 1
4 divided by 2: quotient = 2, remainder = 0
2 divided by 2: quotient = 1, remainder = 0
1 divided by 2: quotient = 0, remainder = 1
Step 4: Reverse the Remainder
Now, reverse the sequence of remainders you recorded in step 3. This reversed sequence represents the binary equivalent of the decimal number.
So, for the decimal number 19, the binary representation is 10011.
Python Code for Decimal to Binary Conversion
You can also use Python to perform decimal-to-binary conversion using the built-in bin()
function:
decimal_number = 19
binary_representation = bin(decimal_number)
print(binary_representation)
This code will output: 0b10011
, where the 0b
prefix indicates that the following digits represent a binary number.
Representing the decimal number 19 required 5 bits.
What is the largest integer number that can be represented using 8 bits?
The largest integer number that can be represented using 8 bits in binary (also known as a byte) is 255.
In binary, an 8-bit number can have 2^8 (2 raised to the power of 8) different combinations because each bit can be either 0 or 1. So, there are 256 possible combinations (from 0 to 255) when counting from 0.
Here's how you can calculate it:
2^8 = 256
However, you start counting from 0, so the largest representable integer is 255 (which is all bits set to 1):
11111111 (in binary) = 255 (in decimal)
So, in 8 bits, you can represent integers from 0 to 255.
If we care about handling negative integers as well, then 1 bit is reversed to represent the sign of the number leaving us with only 7 bits for the number itself out of the original 8 bits.
Here's how this works:
In an 8-bit signed representation, the leftmost bit (the most significant bit) is used to indicate the sign of the number. If the leftmost bit is 0, it represents a positive number, and if it's 1, it represents a negative number.
The remaining 7 bits are used to represent the magnitude of the number. In this case, all 7 bits are set to 1 to represent the largest magnitude, and the leftmost bit (the sign bit) is set to 1 to indicate negativity.
So, in 8 bits, the binary representation of the largest negative integer is:
10000000 (in binary) = -128 (in decimal)
Therefore, -128 is the largest negative integer that can be represented using 8 bits in a signed binary representation.
So using 8 bits we can represent all the integers in the range of [-128,127].
If we want to use 16 bits to store (signed) integer, our range would be :
2 ^ (16 - 1) = 2 ^ 15 = 32,768 Range : [-32,768 ...... 32,767]
Similarly, if we want to use 32 bits to store (singed) integer, our range would be :
2 ^ (32 - 1) = 2 ^ 31 = 2,147,483,648 Range : [2,147,483,648 .... 2,147,483,647]
If we had an unsigned integer type, using 32 bits our range would be :
[0 , 2^32] = [ 0 ...... 4,294,967,296]
So, how large an integer can be depends on how many bits are used to store the number.
Some languages (such as Java, C, …) provide multiple distinct integer data types that use a fixed number of bits:
Datatype | Bits | Range |
byte | signed 8-bit numbers | -128, …, 127 |
short | signed 16-bit numbers | -32,768, …, 32,767 |
int | signed 32-bit numbers | -2,147,483,648 to 2,147,483,647 |
long | signed 64-bit numbers | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
In Python, data types do not utilize a fixed number of bits as seen in some other programming languages. The 'int' object, for example, employs a variable number of bits. It can use 4 bytes (32 bits), 8 bytes (64 bits), 12 bytes (96 bits), and so on. Since integers in Python are treated as objects, there is an additional fixed overhead per integer. Theoretically, the limitation on the size of an integer is determined only by the available memory.
Integer Operations
Integer supports all arithmetic operations :
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Exponent (**)
But what is the result of each type of operation
int + int -> int
int - int -> int
int * int -> int
int ** int -> int
int/int -> float
From all the above operations we can see that an integer divided by an integer gives you a float datatype 3/4 gives you 0.75 which is float but 10/2 gives you 5.0 which is also a float datatype.
We are about to see two more operators in the integer arithmetic. Let's take an example of 155(numerator) / 4(denominator).
// is called floor division(div)
% is called modulo operator (mod)
And they always satisfy the below
n = d ( n // d) + (n % d)
Where n
Stands for the numerator and d
stands for the denominator
What is floor division exactly?
The floor of a real number a is the largest ( in standard number order ) integer <= a.
>>> import math
>>> math.floor(3.155)
3
>>> math.floor(1.9999)
1
>>> math.floor(2)
2
But watch out for the negative numbers
>>> math.floor(-3.2)
-4
Be Careful of negative numbers!
a // b is not the integer portion of a / b, it is the floor of a / b. For a > 0 and b > 0 these are indeed the same thing but beware of dealing the negative numbers!
a = -135
b = 4
-134 / 4 = -33.75
-134 // 4 = -34
-135 % 4 = 1
And , in fact = a = b * ( a // b ) + ( a % b )
= 4 * ( -135 // 4 ) + ( -135 % 4 )
= -136 + 1
= -135
This article explores integers in Python, discussing their attributes, operations, and real-world applications. It covers the basics of binary representation, the process of converting decimal numbers to binary, and how integers are stored in computers using a fixed or variable number of bits. The article also delves into integer arithmetic operations and the differences between floor division and truncation, particularly when dealing with negative numbers.