<jupyter_start><jupyter_text>Integers: Data Typesint - ex: 0, 10, -100, ...- are represented internally using base-2- the number of bits determines the value range- uses a variable number of bits so an integer can be as large as the memory allows- the larger the number, the slower operations run<jupyter_code>type(100)
import sys<jupyter_output><empty_output><jupyter_text>The overhead of storing an integer in bytes<jupyter_code>sys.getsizeof(0)
sys.getsizeof(1)
sys.getsizeof(2**1000)
# Number of bits
(160 - 24) * 8<jupyter_output><empty_output><jupyter_text>Operation is slower as the integer value increases<jupyter_code>import time
def calc(a):
    start = time.perf_counter()
    
    for i in range(10_000_000):
        a * 2
    
    end = time.perf_counter()
    print(end - start)
calc(10)
calc(2 ** 100)
calc( 2 ** 10_000)<jupyter_output>6.508364222943783<jupyter_text>Integers: OperationsStandard arithmetic operations| Operation Name | Operator | Example | Result Type || --- | --- | --- | --- || addition | + | int + int | int || subtraction | - | int - int | int || multiplication | * | int * int | int || division | / | int / int | float || exponents | ** | int ** int | int || floor division | // | int // int | int || modulo | % | int % int | int |floor of a real number is the largest integer <= than the real number.Ex.: - floor(33.75) --> 33- floor(-33.75)--> -34<jupyter_code>type(1 + 1)
type(2 * 3)
type(4 - 10)
type(3 ** 6)
type(2 / 3)
# Even divisible is still a float
type(10 / 5)
10/5<jupyter_output><empty_output><jupyter_text>Floor<jupyter_code>import math
math.floor(3.15)
math.floor(-3.15)<jupyter_output><empty_output><jupyter_text>Floor has a limited precision<jupyter_code>math.floor(-3.000000000000001)
math.floor(-3.0000000000000001)
a = 33
b = 16
print(a/b)
print(a//b)
print(math.floor(a/b))
a = -33
b = 16
print(a/b)
print(a//b)
print(math.floor(a/b))
a = -33
b = 16
print(a/b)
print(a//b)
print(math.floor(a/b))
print(math.trunc(a/b))<jupyter_output>-2.0625
-3
-3
-2<jupyter_text>a: dividendb: divsora // b: quotienta % b: remaindera = b * (a // b) + (a % b)<jupyter_code>a = 13
b = 4
print(f"{a} / {b} = {a / b}")
print(f"{a} // {b} = {a // b}")
print(f"{a} % {b} = {a % b}")

print( a == b * (a // b) + (a % b))
a = -13
b = 4
print(f"{a} / {b} = {a / b}")
print(f"{a} // {b} = {a // b}")
print(f"{a} % {b} = {a % b}")

print( a == b * (a // b) + (a % b))
a = 13
b = -4
print(f"{a} / {b} = {a / b}")
print(f"{a} // {b} = {a // b}")
print(f"{a} % {b} = {a % b}")

print( a == b * (a // b) + (a % b))
a = -13
b = -4
print(f"{a} / {b} = {a / b}")
print(f"{a} // {b} = {a // b}")
print(f"{a} % {b} = {a % b}")

print( a == b * (a // b) + (a % b))<jupyter_output>-13 / -4 = 3.25
-13 // -4 = 3
-13 % -4 = -1
True<jupyter_text>Integers: Constructors and Bases- is an object, instance of the int clas- provides multiple constructors- a constructor with a single numerical parameter- a constructor with a string and a second optional parameterSupported arguments of the int constructor| Type | Example | Result || --- | --- | --- || float | int(10.9) | 10 - uses truncation || -float | int(-10.9) | -10 - uses truncation || bool | int(True) | 1 || Decimal | int(Decimal("10.9")) | 10 - uses truncation |Constructor with non-numerical argumentint(str, base=10)| Example | Result || --- | --- || int("123") | 123 || int("1010", base=2) | 10 || int("A12F", base=16) | 41264 || int("354", base=8 | 348 |Built-in functions to output integer to as string with another base representation| Function | Example | Result ||--- | --- | --- || bin | bin(10) | "0b1010" || oct | oct(10) | "0o12" || hex | hex(10) | "0xa" |Literal integers with base can be used, example: a = 0b1010 is the same as a=10  Code examples<jupyter_code>int("12345")
int("101", base=2)
int("ff", base=16)
bin(10)
oct(10)
hex(10)
a = int("101", base=2)
b = 0b101
a
b<jupyter_output><empty_output><jupyter_text>Function that output a base 10 number as a representation of the passed base<jupyter_code>def from_base10(n, b):
    if b < 2:
        raise ValueError("Base b must be >= 2")
    
    if n < 0:
        raise ValueError("Number n must be >= 0")
    
    if n == 0:
        return [0]

    digits = []
    
    while n > 0:
        n, m = divmod(n, b)
        digits.insert(0, m)
    
    return digits
from_base10(10, 2)
from_base10(255, 16)
def encode(digits, digit_map):
    if max(digits) >= len(digit_map):
        raise ValueError("digits_map is not long enough to encode the digits")
    
    return "".join([digit_map[digit] for digit in digits])
encode([15, 15], "0123456789ABCDEF")
def rebase_from10(number, base):
    digit_map = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    
    if base < 2 or base > 36:
        raise ValueError("Invalid base: 2 <= base <= 36")
    
    sign = -1 if number < 0 else 1
    
    number *= sign
    
    digits = from_base10(number, base)
    encoding = encode(digits, digit_map)
    
    if sign == -1:
        encoding = "-" + encoding
    
    return encoding
e = rebase_from10(314, 2)
print(e)
print(int(e, base=2))
e = rebase_from10(-314, 2)
print(e)
print(int(e, base=2))
e = rebase_from10(3451, 16)
print(e)
print(int(e, base=16))<jupyter_output>D7B
3451
{'fraction_non_alphanumeric': 0.177645477242174, 'fraction_numerical': 0.06894565008642213, 'mean_word_length': 3.2653562653562656, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '31347', 'license': 'MIT', 'n_tokens_mistral': 2237, 'n_tokens_neox': 1996, 'n_words': 574, 'path': 'python-deepdive/deepdive1/section04/section_04_integers.ipynb', 'repo_name': 'erictapia/devstacklab', 'size': '5207'}