Storing Data with Variables

Variables in Python are names bound to values. When you write x = 10, you are not declaring a typed storage slot — you are attaching the name x to the integer object 10. If you later write x = 'hello', the name x now refers to a string, and the previous integer is eligible for garbage collection if nothing else holds a reference to it.

Python is dynamically typed but strongly typed: values carry a type at runtime, and the interpreter refuses to silently mix incompatible types (you cannot add a string and an integer without converting). The core built-in types include int, float, bool, str, bytes, list, tuple, dict, set, and None. Use type(value) to ask the runtime what a value is, or isinstance(value, target_type) when you also want to accept subclasses.

Operators fall into groups: arithmetic (+ - * / // %% **), comparison (== != < > <= >=), logical (and or not), bitwise (& | ^ ~ << >>), and assignment (= += -= ...). Python evaluates expressions with standard mathematical precedence, and parentheses always win when you want to be explicit about the order.

Syntactic structure in Python is defined by indentation rather than braces. Four spaces per level is the community standard (PEP 8). Comments start with # and run to end of line; docstrings (triple-quoted strings as the first statement in a module, class, or function) are preserved at runtime and surfaced by help(). Every convention in Storing Data with Variables exists to make the code readable by the next person (often you, a year from now).

Core syntax and built-ins used in Storing Data with Variables. Skim the table before reading the code example, and click any name to open the official documentation.

ToolPurpose
=
assignment operator
Binds a value to a name.
type()
built-in function
Reports the type of a value.
int() / float() / str() / bool()
built-in functions
Convert between basic data types.
print()
built-in function
Writes values to standard output.
input()
built-in function
Reads one line of user input as a string.
==, !=, <, >, <=, >=
comparison operators
Produce Boolean results.
and, or, not
logical operators
Combine or invert Boolean values.
#
comment marker
Starts a single-line comment ignored by Python.

Storing Data with Variables Code example

Create a robust starter script specific to Storing Data with Variables.

# Lesson: Storing Data with Variables
# Goal: show a small, production-style script with imports, a helper, the main
# logic, and a realistic follow-up use so the example is not a single line.
from __future__ import annotations

DEFAULT_GREETING = "Hello"


def build_message(name: str, lesson: str) -> str:
    '''Return a greeting that names the learner and the current lesson.'''
    if not name.strip():
        raise ValueError("name is required")
    return f"{DEFAULT_GREETING}, {name}. This is {lesson}."


# --- main script ---------------------------------------------------------
user_name = "PythonDeck learner"
lesson = "storing_data_with_variables"
print(build_message(user_name, lesson))

# follow-up use: loop over more learners to show reuse
for learner in [user_name, "new student", "teacher"]:
    print(build_message(learner, lesson))

Walk through each part before moving to the next lesson:

1) The example defines one clear unit of logic.
2) Inputs are explicit, so behavior is reproducible.
3) Output lines are designed for quick validation.
4) The self-test section confirms expected behavior.

Try both examples below to strengthen understanding.

# Example A: defensive input cleanup
user_text_storing_data_with_variables = "  Storing Data with Variables  "
print(user_text_storing_data_with_variables.strip().upper())

# Example B: dictionary-driven lookup
levels_storing_data_with_variables = {"easy": 1, "medium": 2, "hard": 3, "storing_data_with_variables": 27}
print(levels_storing_data_with_variables.get("medium", 0))
print(levels_storing_data_with_variables.get("storing_data_with_variables"))

Run these assertions after the example. If nothing prints, tests passed.

assert user_name
assert lesson
assert "Hello" in build_message(user_name, lesson)

Your exact values can vary slightly (especially dates/times), but the structure should match.

Hello, PythonDeck learner. This is storing_data_with_variables.