← Home

Ch 2 — Types & Literals

Ch 2 — Types & Literals

Aryan’s RAM manager will handle many kinds of data: raw byte counts (integers), percentages (floats), process names (strings), and flags like “is swap enabled?” (booleans). Understanding Python’s built-in types prevents subtle bugs — like dividing an integer by a string — before they crash the monitor at midnight.


Integers

Whole numbers, positive or negative, with no size limit in Python 3.

total_ram_bytes = 17_179_869_184   # 16 GB in bytes (underscores for readability)
pid = 4821                          # process ID
page_size = 4096                    # bytes per memory page

Floats

Numbers with a decimal point. RAM percentages, temperatures, CPU clocks — all floats.

ram_used_pct = 73.5
swap_ratio = 0.02

Beware floating-point precision:

>>> 0.1 + 0.2
0.30000000000000004   # classic float quirk

For financial or precise calculations use decimal.Decimal. For RAM percentages, a float is fine.


Strings

Text, enclosed in single '...', double "...", or triple """...""" quotes.

process_name = "kernel_task"
alert_msg = 'RAM usage exceeded 90%'
report = """
Process: kernel_task
PID: 0
RAM: 1.2 GB
"""

Strings are immutable — you cannot change a character in place; you create a new string.


Booleans

True or False — capitalized in Python. They are a subclass of int (True == 1, False == 0).

swap_enabled = True
alert_sent = False

if swap_enabled:
    print("Swap is active")

None

None represents the absence of a value — Python’s null.

last_snapshot = None   # no snapshot taken yet

if last_snapshot is None:
    print("First run — initializing…")

Always compare with is None, not == None.


Type Conversion

Convert between types explicitly using built-in constructors.

raw = "8192"          # comes in as string from config file
ram_mb = int(raw)     # → 8192  (integer)
ram_gb = float(raw) / 1024  # → 8.0 (float)

pct_str = str(73.5)   # → "73.5"
flag = bool(ram_mb)   # → True (non-zero is truthy)

Implicit conversion (coercion) does NOT happen between str and numbers:

# "8192" + 1   →  TypeError: can only concatenate str to str

type()

Inspect the type of any object at runtime.

print(type(total_ram_bytes))   # <class 'int'>
print(type(ram_used_pct))      # <class 'float'>
print(type(process_name))      # <class 'str'>
print(type(swap_enabled))      # <class 'bool'>
print(type(last_snapshot))     # <class 'NoneType'>

Mutable vs Immutable

ImmutableMutable
int, float, str, bool, tuplelist, dict, set

Immutable objects cannot be changed after creation; any “modification” creates a new object.

name = "kernel"
name = name + "_task"   # new string; original "kernel" is unchanged

Mutable objects change in place:

pids = [1, 2, 3]
pids.append(4)    # same list object, now [1, 2, 3, 4]

Literals

A literal is a fixed value written directly in source code.

16          # integer literal
16.0        # float literal
"chrome"    # string literal
True        # boolean literal
None        # None literal
0xFF        # hex integer literal → 255
0b1010      # binary literal → 10
0o17        # octal literal → 15

Unicode

Python 3 strings are Unicode by default. Process names can contain emoji, CJK characters, or accented letters.

process_name = "メモリ管理"   # Japanese — works natively
emoji_label = "RAM: 🔥"       # emoji in output string

Encode/decode when writing to files or network sockets:

encoded = process_name.encode("utf-8")   # bytes
decoded = encoded.decode("utf-8")        # back to str

Python Type Hierarchy

flowchart TD
    A[Python Objects] --> B[Immutable]
    A --> C[Mutable]
    B --> D[int]
    B --> E[float]
    B --> F[str]
    B --> G[bool]
    B --> H[tuple]
    B --> I[NoneType]
    C --> J[list]
    C --> K[dict]
    C --> L[set]

Type Conversion Flow in the RAM Manager

flowchart LR
    A[psutil returns bytes as int] --> B[int → float division]
    B --> C[float GB value]
    C --> D[str for display]
    D --> E[print to terminal]
    A --> F[int → bool check]
    F --> G{RAM > 0?}
    G -->|True| H[Process is alive]
    G -->|False| I[Skip entry]

Key Takeaways

  • Python has five core scalar types: int, float, str, bool, and NoneType.
  • Use type() to inspect an object’s type at runtime.
  • Explicit conversion (int(), float(), str()) is required — Python will not silently coerce str to int.
  • None represents “no value”; compare with is None, not == None.
  • Immutable types (str, int, tuple) cannot be changed in place — modifications produce new objects.
  • Mutable types (list, dict, set) change in place, which matters for shared references.
  • Python 3 strings are Unicode natively — process names from any locale work without extra setup.