🧠 Programming Fundamentals: The Big Picture
Before we dive into writing Python code, it helps to understand what programming actually is and why it matters for data analysis in nutrition, food, and sensory science.
🧱 What Is a Program?
A computer program is just a set of instructions (the code) and data (the values it works on). Imagine a hippo cookbook: the recipe is the instructions, and the ingredients are the data.
A simple Python program:
calories = 2500
protein = 80
print("This diet provides", calories, "kcal and", protein, "g of protein.")
🧩 Structure of a Program
Most programs are built from a few key elements: - Variables (store data) - Functions (perform actions) - Conditionals (make decisions) - Loops (repeat actions) - Comments (explain the code) - Error Handling (deal with unexpected problems)
These are like the pots, pans, and measuring spoons of the programming world. You don’t need them all at once—but you will use them often.
🧑🍳 Naming Things: Variables and Functions
Choose descriptive names for clarity. Avoid x
, y
, or stuff
unless it’s obvious what they are. Use snake_case
(lowercase with underscores) in Python.
Examples:
hippo_name = "Hilda"
calcium_intake_mg = 1200
🪶 Formatting and Indentation
Python is picky about layout! You must indent your code blocks using spaces (not tabs). This tells Python what belongs together.
Bad:
if iron_intake > 8.0:
print("Sufficient intake") # ← No indentation!
Good:
if iron_intake > 8.0:
print("Sufficient intake") # ← Indented!
Tip: Most editors fix this for you. Stick to 4 spaces.
🚨 Try–Except: Handling Errors Gracefully
Python has a friendly way to catch errors using try
/except
. For example:
try:
= 100 / 0
result except ZeroDivisionError:
print("Oops! Can't divide by zero.")
This helps keep your program running even when something goes wrong (and it will!).
🦛 In Summary
Writing code is giving clear, structured instructions—like explaining a nutrition experiment to a hippo. The clearer your code, the better it performs. 🧃
Next, we’ll explore the building blocks of Python: variables, data types, and functions.
🗣️ Comment Your Code
Comments are notes in your code, ignored by Python but read by humans.
Use comments to: - Explain why you’re doing something - Note assumptions or limitations - Make future you grateful