Working with Functions and Modules
# CHAPTER 7
Working with Functions and Modules
1. Chapter Introduction
If you find yourself copying and pasting the exact same block of code in three different Jupyter cells, you are doing it wrong. In programming, the DRY principle (Don't Repeat Yourself) is king. This chapter teaches you how to encapsulate code into reusable "Functions", and how to import "Modules" (libraries of code written by other people) to give your Jupyter Notebook superpowers.2. Defining Functions in Python
A function is a block of code that only runs when it is called. You can pass data (parameters) into it, and it can return data back as a result.
Syntax:
-
Use the
defkeyword.
-
Name the function (using
snake_case).
-
Add parentheses
()and a colon:.
- *Indent* the code block inside the function (Jupyter does this automatically).
Cell 1:
3. Multiple Arguments and Default Values
Functions can accept multiple inputs, and you can provide default values so an argument becomes optional.
Cell 2:
4. Importing Modules (The Standard Library)
Python comes with a "Standard Library"—a collection of pre-written modules you can use for common tasks like math, dates, and random numbers. You must import them to use them.
*Best Practice:* In Jupyter, place all your import statements in the very first cell of the notebook.
Cell 3:
5. Different Ways to Import
If a module's name is too long, or you only need one specific function, you can change how you import it.
Cell 4:
6. Package Management (pip) and Magic Commands
The Standard Library is great, but data scientists use external, third-party libraries (like Pandas and Scikit-Learn). To install these, you use Python's package manager, pip.
In Jupyter, you can run terminal commands directly in a code cell by prefixing the command with an exclamation mark !.
Cell 5:
*(After this finishes, you can import requests in the next cell).*
7. Mini Project: Utility Notebook Toolkit
Imagine you are analyzing temperatures. Instead of writing the math every time, you build a toolkit function.
Cell 6:
8. Common Mistakes
-
Indentation Errors: Python does not use curly brackets
{}to define code blocks. It relies strictly on indentation (usually 4 spaces). If yourreturnstatement is not indented properly, it will break.
-
Forgetting to run the definition cell: If you write a
deffunction in Cell 1, and try to call it in Cell 2 *before running Cell 1*, Jupyter will say the function is not defined. The Kernel must "read" the definition first.
9. MCQs
What keyword is used to define a function in Python?
What is the purpose of the return statement in a function?
If a function is defined as def calculate(x, y=10):, what happens if you call calculate(5)?
Where is the best place to put import statements in a Jupyter Notebook?
How do you import the statistics module and give it the nickname st?
What happens if you try to use a function you defined in Cell 3, but you haven't actually run Cell 3 yet?
How does Python know which lines of code belong inside a function?
What does placing an exclamation mark ! before a command in a Jupyter cell do?
Which command is used to install third-party Python packages?
A string enclosed in triple quotes """ right below a function definition is called a?
10. Interview Questions
-
Q: What is the difference between
print()andreturnin a Python function?
-
Q: Explain the difference between
import mathandfrom math import sqrt.
11. Summary
Functions encapsulate code into reusable blocks, adhering to the DRY principle. They are defined usingdef, rely on indentation, and use return to pass data back. Modules are files containing pre-written functions. You bring them into your notebook using import. For external libraries not included in Python, you can install them directly inside Jupyter using the !pip install magic command.