Table of Contents » Chapter 2 : Data (Input) : Data Types : Integers
Integers
Contents
Overview
An integer in the context of programming and mathematics is a whole number (sometimes called a counting number) that can be positive, negative, or zero, but it does not include any fractional or decimal parts. In programming languages like Python, an integer is a fundamental data type that is used to represent these whole numbers. Integers are used extensively in various types of calculations and loops, as well as in indexing and more complex mathematical operations. I
In most programming languages, integers have a certain range of values they can represent, determined by the number of bits allocated for storing them in memory. In Python, however, integers have unlimited precision, meaning they can grow to have as many digits as needed, limited only by the available memory. This is a significant benefit of Python, this means we can specify an integer of (virtually) any length.
Examples of Integers in Python:
- 10
- 157
- 24785
- 12234987923847928374982739847928374687693589056890347
In Python, we can perform mathematical calculations using Integers using operators which are symbols to represent valid arithmatic operations for integer values. Here are the first of these that we will explore:
| Operation | Operator | Description & Examples |
|---|---|---|
| Addition |
Combining two or more numeric values into a single total sum.
|
|
| Subtraction |
Taking away one numeric value (or quantity) from another to find the difference between them.
|
|
| Multiplication |
Adding a number a certain number of times to produce a product.
|
|
| Division |
Splitting a number into a specified number (the divisior) of equal parts. The result of dividing two integers (such as 10 / 3) will result in a fractional (decimal) part in the result, which is relevant related to data types. Integer division that results in a fractionl part will produce a floating point result. In computing, division by zero will cause an error because division by zero is mathematicaly undefined.
|
|
| Floor (Integer) Division |
The same as division (above), however the result will be an integer and any fractional (decimal) part in the result will be discarded.
|
|
| Modulus |
Same as division (above), however it determins if there is a remainder in the result of a division operation.
|
|
| Exponentiation |
This operation raises the first number (the base) to the power of the second number (the exponent). The exponent signifies how many times the base is multiplied by itself.
|
Any of the mathematical operators can be used in more complex (combined) mathematical calculations. Here are some examples:
print(10 + 20 - 15 * 5)
print(10 + (100 / 2)) # See the note about the use of parenthses below
print(10 ** (2 * 30))) # See the note about the use of parenthses below
In Python, the order of operations for mathematical operators follows the conventional mathematical hierarchy, which is consistent with the rules of arithmetic that are typically taught in mathematics (often remembered by the acronym PEMDAS/BODMAS). For the operators you've listed (+, -, *, /, //, %, and **), the order of precedence is as follows:
- Exponents (**)
- Multiplication (*) and Division (/, //, and %)
- Addition (+) and Subtraction (-)
In Python, like math, we can use parenthses ( ) to isolate and group factors of our equations to override andn control the order of operations. See the examples in the Combining Operations section above.