Skip to main content
Back to module
STEM Foundations: The Universal Skill Stack · Computing & Data Fluency

Programming basics and version control

Reading28mLesson 2 of 3 in this module

What you'll learn

  • Explain variables, loops and why version control exists
  • Read short programs built from variables, conditions, loops and functions
  • Tell syntax, logic and off-by-one errors apart, and test code with a known answer
  • Describe how Git commits, branches and merges keep a shared history of work

Understand variables, conditions and loops, and why teams keep their work in version control.

Why this matters

You do not have to become a software developer to benefit from programming. A lab analyst who can write ten lines of Python can process a week of results in seconds. An engineer can automate a calculation they do every day. An electrician can build a small load calculator. And anyone who works with code, documents or drawings on a team benefits from version control, which keeps a full history of changes. This lesson explains the core ideas behind both, so the dedicated courses make sense later.

A program is a list of instructions

A program is a set of precise instructions that a computer follows in order. Almost every program, in any language, is built from a few ideas.

Variables store values under a name:

Python
temperature_c = 21.5
sample_id = "S-104"

Operations calculate new values:

Python
temperature_f = temperature_c * 1.8 + 32

Conditions make decisions:

Python
if temperature_c > 25:
    print("Too warm: check the incubator")
else:
    print("Temperature OK")

Loops repeat work, which is where programming saves the most time:

Python
readings = [21.5, 22.0, 23.5, 22.5, 24.0]
for r in readings:
    print(r * 1.8 + 32)

That loop converts every reading in the list, whether there are 5 or 5 million.

Functions wrap steps in a reusable name:

Python
def c_to_f(c):
    return c * 1.8 + 32

print(c_to_f(100))   # prints 212.0

Worked example: averaging readings

To find the average of a list of readings, you do what you would on paper: start a running total at zero, add each reading, then divide by the count.

Python
readings = [21.5, 22.0, 23.5, 22.5, 24.0]
total = 0
for r in readings:
    total = total + r
average = total / len(readings)
print(average)    # 22.7

len(readings) gives the number of items (5). Python also has shortcuts such as sum(readings), but writing the loop yourself once shows what is happening underneath.

How code goes wrong

Computers do exactly what you write, not what you meant. Common beginner errors:

  • Syntax errors: a missing bracket or quote, or wrong indentation in Python. The program will not run, and the error message points to the line.
  • Logic errors: the program runs but gives the wrong answer, for example dividing by the wrong number. This is why you test with an example you can check by hand.
  • Off-by-one errors: a loop that runs one time too many or too few.

Read error messages from the bottom up. The last line usually says what went wrong, and the line number says where.

Version control: a time machine for your work

Have you ever had files called report_v2_final_FINAL.docx? Version control solves that problem properly. The most widely used tool is Git, often paired with a hosting service such as GitHub or GitLab.

With Git:

  • You save snapshots called commits. Each commit records what changed, who changed it, when, and a short message explaining why.
  • You can look back at any earlier version or undo a bad change.
  • People can work on separate branches at the same time and merge their work together, instead of emailing files back and forth.
  • A hosted repository (repo) gives you an off-site backup and a place to review each other's changes.

Git is used for code, but also for documentation, configuration files, data analysis scripts and even CAD files and lab protocols. Version history is also evidence: it shows exactly what was changed and when, which matters in regulated labs and engineering.

Common mistakes

  • Not testing with a known answer. Always try an input you can check by hand.
  • Writing everything at once. Write a few lines, run, check, then add more.
  • Committing rarely with vague messages like "stuff". Small commits with clear messages ("Add Fahrenheit conversion") are much easier to understand and undo.
  • Being scared of error messages. They are clues, not failures.

Quick recap

  • Programs are built from variables, operations, conditions, loops and functions.
  • Loops are where automation saves time.
  • Test code with examples you can check by hand.
  • Version control (Git) keeps a history of commits, lets teams work in parallel and makes mistakes reversible.

Try it

Open the Do tab. You will complete a short Python loop that totals and averages a list of sensor readings. The code already runs; you only need to fix one line.

Key terms (6)

variable
A name that stores a value, such as temperature_c = 21.5.
loop
Code that repeats the same steps for each item, such as converting every reading in a list.
function
A named, reusable set of steps that can take inputs and return a result.
logic error
A mistake that lets the program run but makes it produce the wrong answer.
commit
A saved snapshot in Git that records what changed, who changed it, when, and a message saying why.
branch
A separate line of work in Git that can later be merged back together with other people's changes.

Recap

  • Programs are built from variables, operations, conditions, loops and functions
  • Loops are where automation saves the most time
  • Test with an example you can check by hand, and read error messages from the bottom up
  • Git keeps a history of commits, lets teams work on branches in parallel and makes mistakes reversible
Also in this lesson: