Integers (int) in Python
Integers (int) in Python are whole numbers without any decimal points. They can be positive, negative, or zero, and are represented as objects of variable size, meaning they can be as large as the memory of the computer allows.
Characteristics of Integers
Unlimited Size
In Python, integers have unlimited size, which means they are not constrained by machine size or underlying data type. The size of an integer is limited only by the available memory.
Example
# Very large integers big_number = 123456789012345678901234567890 print(big_number) # Prints the very large number
Arithmetic Operations
Integers support basic arithmetic operations such as addition, subtraction, multiplication, division, integer division, and modulo.
Examples
a = 10 b = 3 # Addition print(a + b) # 13 # Subtraction print(a - b) # 7 # Multiplication print(a * b) # 30 # Division (returns a float) print(a / b) # 3.3333333333333335 # Integer Division print(a // b) # 3 # Modulo print(a % b) # 1
Representation and Notation
Decimal Notation
Integers in Python are usually represented in decimal notation, which is base 10.
Example
# Integers in decimal notation decimal_int = 123 print(decimal_int) # 123
Binary, Octal, and Hexadecimal Notation
Python also supports representing integers in binary, octal, and hexadecimal notation.
- Binary: Prefix 0b or 0B
- Octal: Prefix 0o or 0O
- Hexadecimal: Prefix 0x or 0X
Examples
# Binary binary_int = 0b1010 # 10 in base 2 print(binary_int) # 10 # Octal octal_int = 0o12 # 10 in base 8 print(octal_int) # 10 # Hexadecimal hex_int = 0xA # 10 in base 16 print(hex_int) # 10
Type Conversion
Integers can be converted to and from other numeric types. Python provides built-in functions for these conversions.
- int(): Converts to integer
- float(): Converts to float
- complex(): Converts to complex number
Conversion Examples
# Convert float to integer float_number = 3.99 int_from_float = int(float_number) # 3, the decimal part is truncated print(int_from_float) # 3 # Convert string to integer string_number = "42" int_from_string = int(string_number) # 42 print(int_from_string) # 42 # Convert integer to float int_number = 7 float_from_int = float(int_number) # 7.0 print(float_from_int) # 7.0
Advanced Operations with Integers
Exponentiation
Exponentiation can be performed using the ** operator or the pow() function.
Examples
# Exponentiation with the ** operator result1 = 2 ** 3 # 2 raised to the power of 3 print(result1) # 8 # Exponentiation with the pow() function result2 = pow(2, 3) # 2 raised to the power of 3 print(result2) # 8
The divmod() Function
The divmod() function returns a tuple containing the quotient and remainder of integer division.
Example
quotient, remainder = divmod(10, 3) print(quotient) # 3 print(remainder) # 1
Usage of Integers in Programming
Integers are commonly used in loops, array indices, and for counting occurrences.
Example with Loops
# Using integers in a loop for i in range(5): # i will take values 0, 1, 2, 3, 4 print(i)
Example with Array Indices
# Using integers as indices elements = ['a', 'b', 'c'] for i in range(len(elements)): print(f"Index {i} has value {elements[i]}")
Checking Types
It is often useful to check the type of a variable to ensure it is an integer.
Example
# Checking type x = 123 print(type(x) == int) # True
Handling Positive and Negative Integers
Python handles both positive and negative integers uniformly, and there is no special distinction in their manipulation compared to positive integers.
Example
positive_int = 42 negative_int = -42 print(positive_int + negative_int) # 0 print(positive_int - negative_int) # 84