Python Tutorial 0/52 lessons ~6 min read Lesson 6

    Variables & Assignment

    In Python, variables are names bound to objects, not boxes that hold values.

    Course progress0%
    Focus
    8 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    In Python, variables are names bound to objects, not boxes that hold values. You don't declare types — Python infers them at runtime.

    Understanding the topic

    Core concepts to understand:

    • x = 42 binds the name x to an int object.
    • Reassignment changes the binding, not the object.
    • Multiple assignment: a, b = 1, 2.
    • Constants by convention: UPPER_CASE.

    Syntax reference

    Visual flow / code:

    python
    name = "Alice"
    age = 30
    is_admin = True
    # Multi / swap
    a, b = 1, 2
    a, b = b, a # swap
    # Type hints (optional but recommended)
    score: int = 100

    Execution workflow

    1Variables & Assignment Workflow
    1 / 4

    x = 42 binds the…

    x = 42 binds the name x to an int object.

    Real-world use

    Type hints, added in Python 3.5, are now standard in production code — they enable mypy, IDE autocomplete, and self-documenting APIs.

    Best practices

    • Use snake_case for variables.
    • Add type hints on public functions.
    • Avoid 1-letter names except in loops.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionIs Python statically or dynamically typed?+

    Answer

    Python is dynamically typed: types belong to objects, not to variable names, so the same name can be rebound to an int and then a string. It is also strongly typed, meaning it does not silently coerce incompatible types, so '1' + 1 raises TypeError. Optional type hints can be checked statically by tools like mypy or pyright, but the interpreter does not enforce them.

    Follow-up

    What is the difference between dynamic and weak typing?
    2BeginnerQuestionWhat's the difference between <code>=</code> and <code>==</code>?+

    Answer

    A single = is assignment: it binds a name to an object. == is the equality comparison, which calls __eq__ and returns a bool-like result. Writing if x = 1 is a syntax error in Python, though the walrus operator := allows assignment inside expressions.

    Follow-up

    What does the walrus operator := do?
    3IntermediateQuestionWhat are type hints?+

    Answer

    Type hints are optional annotations such as def f(x: int) -> str that describe expected types. CPython stores them in __annotations__ but does not enforce them at runtime. They are used by type checkers, IDEs, and libraries like pydantic or dataclasses that read the annotations to drive behavior.

    Follow-up

    Does Python check type hints at runtime?

    Summary

    In summary: Names → objects, not boxes. Type hints = production hygiene.

    Ready to mark this lesson complete?Track your journey across the entire course.