FirstHack 2026 — a 36-hour online hackathon built for first-year students.•₹1,00,000 prize pool•22 Oct – 4 Nov•Open to every branch and every stream•Certificate for every team that submits•3 rounds•FirstHack 2026 — a 36-hour online hackathon built for first-year students.•₹1,00,000 prize pool•22 Oct – 4 Nov•Open to every branch and every stream•Certificate for every team that submits•3 rounds•
Writing functions properly, plus the mutable-default trap that silently corrupts data.
12 min read·Lesson 5 of 6·Pro
Why functions exist
Not to save typing. To give a chunk of logic a name, so that the rest of the program reads like a description of what it does instead of how.
Python 3
def percentage(marks, total=100):
"""Return marks as a percentage of total, rounded to 2 places."""
return round(marks / total * 100, 2)
print(percentage(72)) # 72.0
print(percentage(145, 200)) # 72.5
total=100 is a default argument. If the caller does not supply it, 100 is used. Defaults must come after non-default parameters, so def f(a=1, b) is a syntax error.
Positional and keyword arguments
The rest of this lesson is Pro
The free lessons of Python for Engineering Students finish what
they start — read those first if you have not. This one goes further, and it
is part of the paid half.
A pass opens the paid lessons of every course, the mock test papers
and the company-wise series. It ends on its own date; nothing renews by itself.