Variables & Data Types
Foundation concepts of Python programming — learn how to store and work with data effectively.
Variables in Python
A variable is a name used to store data in memory. Python is dynamically typed and case-sensitive: age, Age, and AGE are three different variables.
# Creating variables — just assign a value x = 10 name = "Alice" is_student = True # Case sensitivity demo age = 20 Age = 30 print(age) # 20 print(Age) # 30 # Updating a variable count = 1 count = 2 # value updated
total_marksstudent_name_countvalue2
2marks— starts with numbertotal-marks— contains hyphenfor— Python keyword
Data Types in Python
| Type | Keyword | Example | Used For |
|---|---|---|---|
| Integer | int | age = 25 | Counting, indexing, loops |
| Float | float | pi = 3.14 | Measurements, prices |
| String | str | name = "Alice" | Text, names, messages |
| Boolean | bool | flag = True | Conditions, loops |
| None | NoneType | result = None | No value / empty state |
| Complex | complex | z = 3 + 4j | Scientific calculations |
int→float, int→complex, float→complex, int→str. NOT allowed: complex→int or complex→float directly (use int(z.real) instead).Running Python, Style & Tooling
Three ways to run code, and the one that matters for packages.
python script.py # run a file python -m pytest # run a module as a script — resolves imports properly python -c "print(1+1)" # one-off expression python # interactive REPL # On Unix, a shebang plus the executable bit makes it a command: #!/usr/bin/env python3 chmod +x script.py && ./script.py
Source files are UTF-8 by default since Python 3, so the old # -*- coding: utf-8 -*- line is no longer needed — you will still meet it in older code, where it told Python 2 how to read the bytes.
PEP 8 is the style guide the standard library follows: four-space indents, snake_case for functions and variables, PascalCase for classes, UPPER_CASE for constants, and a line length most projects set to 88 or 100. Its value is not the rules themselves but that everyone follows the same ones, so diffs show changes rather than reformatting.
ruff check . # linter: unused imports, shadowed names, likely bugs ruff format . # formatter: rewrites layout, no argument to have mypy src/ # static type checker, if you use annotations # older equivalents you will still see: flake8 . ; black . ; pylint src/
When something misbehaves, breakpoint() drops you into the debugger at that line — no import needed since Python 3.7.
def process(rows): breakpoint() # pdb: n=next, s=step in, c=continue, p expr=print return [transform(r) for r in rows] # Where the time actually goes: # python -m cProfile -s cumtime script.py # Micro-benchmark one line: # python -m timeit -s "xs=list(range(1000))" "sum(xs)"
cumtime points at the caller responsible rather than the leaf function it bottoms out in.