Integers in python are whole numbers, numbers that have no decimals, (1, 2, 3, 4, ....N), these numbers represent a definitive count without remainder, covering positive values, negative values, and zero.
Basic Math Operations (Addition, subtraction, multiplication)
Using integers in Python is very simple. You can perform addition (+), subtraction (-), multiplication (*), division (/), and other mathematical operations.
To perform mathematical operations in Python, simply place the *, +, -, or / operator between two numbers (the operands). For example, 5 + 10 tells Python to calculate the result, returning a new value of 15.
Operands Definition:
Operands are simply the values or variables that an operator acts upon. In the expression 5 + 10, the numbers 5 and 10 are the operands, while the + is the operator.
Lets look at an example :
Converting to Integers
Sometimes, a number might be stored as text (a string), such as "10". To perform mathematical calculations, the string needs to be converted to integer using the int() function. This process, known as casting, it tells Python to treat the value as a whole number so it can be used in mathematical operations.
Lets take a look:
Casting Definition:
Casting is the process of converting a value from one data type to another. In Python, you use it to transform data, like turning a string of text ("10") into integer (10) or a decimal (10.5) into whole integer (10).
Large Numbers & No Limits
In Python, unlike some other programming languages, integers have arbitrary precision. This means they can be as large as your computer's memory allows.
For perspective, if your computer allocates 1 GiB (1,024³ bytes) of memory to a single integer, that number could have approximately 2.58 billion digits.
Lets see how we arrive at that number:
Calculate Bytes: bytes.
Calculate Bits:
Convert to Decimal Digits: We use the formula .
So, with 1 GB of memory, Python can store an integer with over 2.58 billion digits.
Large integers - underscores
When working with very large integers, you can use underscores as visual separators to make them easier to read. Python ignores the underscores, so 1_000_000 is treated exactly the same as 1000000.
Summary:
- Integers are whole numbers without decimals.
- They have Arbitrary Precision (unlimited size).
- Use int() to convert text or decimals into integers.
