All posts

Python Foundations: Types, Strings, and Collections

7 Sep 20261 min readStudy Notes

Python Foundations: Types, Strings, and Collections

The building blocks

Python is a readable, general-purpose language used for web apps, automation, data analysis, and AI. Every program starts with the same fundamentals: variables, expressions, operators, and a handful of built-in types.

x = 7             # int
y = 12.4          # float
name = "John"     # str
is_valid = True   # bool

print(type(x))    # <class 'int'>
  • Variables are created the moment you assign them — no separate declaration needed.
  • type() reveals a value's data type at runtime, which is useful when debugging unexpected behavior.
  • # starts a comment; Python ignores everything after it on that line.

Operators and decisions

Arithmetic operators work as you'd expect, with two Python-specific additions — floor division and modulo:

x, y = 9, 4
x + y    # 13   addition
x - y    # 5    subtraction
x * y    # 36   multiplication
x / y    # 2.25 division (always returns a float)
x // y   # 2    floor division (integer quotient)
x % y    # 1    modulo (remainder)
  • Comparison operators (==, !=, <, >=, ...) evaluate to a boolean and drive if/while logic.
  • Clear, well-named comments explain why code exists — they should never restate what the next line obviously does.

Working with strings

Strings are sequences of characters, so they support indexing and slicing just like lists:

s = "Hello"
s[0]                       # 'H'          indexing
s[1:4]                     # 'ell'        slicing
len(s)                     # 5            length
s.upper()                  # 'HELLO'
s.lower()                  # 'hello'
s.replace("Hello", "Hi")   # 'Hi'
s.split("l")               # ['He', '', 'o']
"  hi  ".strip()           # 'hi'
"Hello" + " " + "World"    # concatenation -> 'Hello World'
  • Indexing pulls out one character; slicing (start:end) pulls out a range.
  • Strings are immutable — every method above returns a new string rather than modifying the original.

Dictionaries and sets

Dictionaries store unique keys mapped to values — ideal for named, structured data:

person = {"name": "Ada", "role": "Engineer"}
person["role"]            # 'Engineer'
person["team"] = "Core"   # add a new key
del person["role"]        # remove a key
person.keys()             # dict_keys(['name', 'team'])
person.values()           # dict_values(['Ada', 'Core'])
person.items()            # dict_items([('name', 'Ada'), ('team', 'Core')])

Sets store unique, unordered elements and support classic set math:

a = {1, 2, 3}
b = {2, 3, 4}
a | b            # union        -> {1, 2, 3, 4}
a & b            # intersection -> {2, 3}
a - b            # difference   -> {1}
a.issubset(b)    # False

Reading errors, writing better code

Python surfaces mistakes as errors with a traceback pointing at the offending line — reading that message carefully is the fastest way to fix it:

>>> 1 + "1"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
  • Favor small functions with descriptive names over large, do-everything blocks.
  • Comment the why, not the what — the code already says what it does.
  • Experiment often: a Python REPL is the fastest way to confirm how a function behaves before using it in a larger program.

Tip: TypeError, NameError, and IndexError are the three most common beginner errors — they almost always mean a value's type, name, or position wasn't what the code expected.

More posts