DS 1501 · Lesson 18 of 24
Instructor: Corey Schafer21m
Sign in to watchDecompose complex problems into reusable, testable building blocks with def, clean signatures, and explicit return values. What you will learn: • Function definition (def name(params):), docstrings, and calling functions vs passing function objects. • Positional arguments, keyword arguments, default parameter values, and arbitrary *args / **kwargs. • Crucial difference between print() (side effect, returns None) and return (hands back computed value / tuple). • Pitfall of mutable default arguments (def add_item(x, box=[]) -> use box=None!). University Outline Mapping: Class 18 · CO2 · Reading: Chapter 4 (deitel) · Activities: Lecture, Slides, Demonstration.
Note
Outline: Class 18 | CO2 | Reading: Deitel Chapter 4 print vs return Trap (Most Common Exam Bug): def bad_sq(n): print(n * n) # bad_sq(4) + 1 -> TypeError (NoneType + int) def good_sq(n): return n * n # good_sq(4) + 1 -> 17 Mutable Default Rule: Always default mutable params to None: def f(items=None): items = [] if items is None else items.
Same resources as in the community course — sign in to watch the lesson and track progress.
Watch the whole course.
Sign in to play every lesson, track your progress, and ask questions alongside the community.
Already signed in? Open lesson in Community