[Python Series 3] Work with Data Using Variables and Types

한국어 버전

With uv run ready, we can focus on handling data inside our code. A variable gives a value a name; a data type describes what kind of data it is and what we can do with it. This post walks through strings and numbers, lists and dictionaries, and ends with a short example that reads user input, processes it, and prints results.

Key terms

  1. snake_case: The lowercase-with-underscores naming style Python uses for variables.
  2. JSON: A text format made of key–value pairs and arrays; it mirrors Python dictionaries and lists.
  3. BMI: Body Mass Index, calculated by dividing weight by height squared.

Core ideas

Study memo

  • Time required: 45 minutes
  • Prereqs: Experience running scripts with uv run
  • Goal: Declare and convert basic types to complete the input–output flow

Variables are "names pointing to values"; types describe those values. Group strings, numbers, lists, and dictionaries together and the flow from input → processing → output clicks immediately.

Code examples

Declare and reassign variables

Python skips explicit type annotations by default, so focus on clearly labeling what the name represents.

user_name = "Jimin"
login_count = 1

print(user_name)
login_count = login_count + 1
print(login_count)

Stick to snake_case—lowercase words joined with underscores—to keep variable names readable.

Numbers and strings

  • Integers int: 1, 42, -5
  • Floats float: 0.1, 3.14
  • Strings str: "hello"

Use f-strings for readable interpolation.

team = "backend"
members = 4

message = f"The {team} team has {members} people."
print(message)

Booleans and comparisons

Booleans represent true/false values and pair naturally with comparison operators.

score = 78
is_pass = score >= 70

print(is_pass)  # True

Lists and dictionaries

  • Lists store ordered collections:
tasks = ["Check email", "Back up data", "Write report"]
print(tasks[0])
tasks.append("Review deployment logs")
  • Dictionaries store key–value pairs:
user = {
    "name": "Jimin",
    "role": "analyst",
    "active": True,
}

print(user["role"])
user["active"] = False

These structures line up with JSON, so they transfer directly to API responses later.

🧠 JSON in a sentence JavaScript Object Notation is a text format built from key–value pairs and arrays. Since it mirrors dictionaries and lists, Python's data-structure instincts translate seamlessly to web APIs and config files.

Practical example: auto-tidying a club attendance sheet

Rebuild CSV rows into dictionaries to validate data before pushing to a database.

rows = [
    "name,late,participation",
    "Minsu,0,5",
    "Harin,1,4",
]

headers = rows[0].split(",")
records = []

for row in rows[1:]:
    values = row.split(",")
    record = {key: value for key, value in zip(headers, values)}
    record["late"] = int(record["late"])
    record["participation"] = int(record["participation"])
    records.append(record)

print(records)

This structure can be serialized to JSON or exported back to CSV or a database with almost no extra work.

Input–process–output example

Use BMI calculation to combine types.

height_cm = float(input("Enter height (cm): "))
weight = float(input("Enter weight (kg): "))

height_m = height_cm / 100
bmi = weight / (height_m ** 2)

result = {
    "height": height_cm,
    "weight": weight,
    "bmi": round(bmi, 2),
}

print(f"BMI: {result['bmi']}")

Remember that input() always returns a string, so cast to float() or int() before doing math.

Why it matters

You need a feel for variables and data types before conditionals or loops make sense. Lists and dictionaries map almost 1

to JSON, so you'll cruise through API responses later. Run the input–processing–output pattern at least once and "storing and retrieving data" becomes muscle memory.

Practice

  • Follow along: Recreate the BMI example and print type(...) for each variable.
  • Extend: Store BMI results for three people in a list of dictionaries, then compute the average BMI.
  • Debug: Submit an empty string to trigger ValueError, then prevent it with if not value: checks.
  • Definition of done: You accept user input, store it in lists/dicts, and print a summary message end-to-end.

Wrap-up

Learning to "store and retrieve data" with variables and types sets up the need for conditionals and loops. Next, we'll combine those flow-control tools to decide how and when the data gets processed.

💬 댓글

이 글에 대한 의견을 남겨주세요