Functions, Versions, and Tests
Write a small function, test it, and keep a history of your changes with version control.
What you will learn
By the end of this lesson, you can write a Python function, test it with simple checks, and explain why teams use version control.
Functions: name a job, reuse it
A function is a named set of steps you can use again and again. You give it inputs. It gives back an output.
def tutors_needed(students, students_per_tutor):
tutors = students // students_per_tutor
if students % students_per_tutor > 0:
tutors = tutors + 1
return tutors
This function answers: how many tutors do we need? // divides and drops the remainder. % gives the remainder. If anyone is left over, we add one more tutor.
Tests: prove it works
A test is a small check that the code does what you expect. Tests catch mistakes before real people do. In Python, assert stops the program with an error if something is false.
Good tests check normal cases and edge cases. An edge case is an unusual input, like zero students.
Version control: a history you can trust
Version control keeps a saved history of every change to your code. The most common tool is Git. Free sites like GitHub store that history online so a team can share it.
Why it matters:
- You can see who changed what, and why.
- You can undo a bad change.
- Several people can work at once without overwriting each other.
Write a clear message with each saved change, called a commit. "Fix tutor count when students do not divide evenly" helps your team. "stuff" does not.
A national example
AI coding helpers: use with judgment
AI tools can suggest code. They can also suggest code that looks right and is wrong, or that has security holes. If you use one:
- Read and understand every line before you keep it.
- Run tests. AI-written code needs the same checks as yours.
- Never paste passwords, keys, or private data into an AI tool.
Before you save a change
0 of 5 doneGo deeper: Software Development, Simply: How to Build the Right Thing, Faster, With Fewer Headaches
Discussion
Ask. Answer. Build together.
Loading…