Python Basics

Variables & Data Types

Foundation concepts of Python programming — learn how to store and work with data effectively.

Python 3 ✓ Beginner Friendly 2 Topics
01

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.

Python — 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
Naming Rules
✓ Valid Names
  • total_marks
  • student_name
  • _count
  • value2
✗ Invalid Names
  • 2marks — starts with number
  • total-marks — contains hyphen
  • for — Python keyword
02

Data Types in Python

TypeKeywordExampleUsed For
Integerintage = 25Counting, indexing, loops
Floatfloatpi = 3.14Measurements, prices
Stringstrname = "Alice"Text, names, messages
Booleanboolflag = TrueConditions, loops
NoneNoneTyperesult = NoneNo value / empty state
Complexcomplexz = 3 + 4jScientific calculations
Type Conversion Rules: Allowed: int→float, int→complex, float→complex, int→str. NOT allowed: complex→int or complex→float directly (use int(z.real) instead).
03

Running Python, Style & Tooling

Three ways to run code, and the one that matters for packages.

Shell — running Python
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.

Shell — the tools that enforce it
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/
A linter finds suspicious code; a formatter rewrites layout; a type checker proves things about types. Run all three in CI and none of them becomes an argument in code review.

When something misbehaves, breakpoint() drops you into the debugger at that line — no import needed since Python 3.7.

Python — debugging and profiling
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)"
Profile before optimising. The slow line is rarely the one you would have guessed, and cumtime points at the caller responsible rather than the leaf function it bottoms out in.
✓

Quick Quiz

1. Which of these is mutable in Python?
2. What does type(3.14) return?
3. Which value is falsy?
4. Valid f-string syntax?
5. What is None's type?