Jupyter Notebook best practices in Data Analysis Python - Time & Space Complexity
We want to understand how the time it takes to run code in a Jupyter Notebook grows as the notebook gets bigger or more complex.
How does adding more cells or data affect the time to run the notebook?
Analyze the time complexity of the following code snippet.
import pandas as pd
data = pd.read_csv('large_data.csv')
for i in range(len(data)):
row = data.iloc[i]
# process each row
print(row['value'])
This code reads a large CSV file and then processes each row one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each row of the data.
- How many times: Once for every row in the dataset.
As the number of rows grows, the time to process grows roughly the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: The time grows directly with the number of rows. Double the rows, double the work.
Time Complexity: O(n)
This means the time to run the code grows in a straight line with the size of the data.
[X] Wrong: "Running more cells or adding comments does not affect execution time."
[OK] Correct: While comments do not affect time, adding many cells with heavy computations or repeated data loading can increase total run time significantly.
Understanding how code execution time grows in notebooks helps you write efficient data analysis and explain your approach clearly in real projects or interviews.
"What if we replaced the loop with vectorized operations using pandas? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of Markdown cells
Markdown cells are used to write text, titles, and explanations, not code.Step 2: Identify the benefit of explanations
Adding explanations helps others and yourself understand the notebook's purpose and steps.Final Answer:
To add titles and explanations that make the notebook easier to understand -> Option AQuick Check:
Markdown cells = explanations [OK]
- Thinking Markdown runs code
- Using Markdown to store data
- Ignoring explanations
Solution
Step 1: Recall Python comment syntax
Python uses the hash symbol (#) to start a comment.Step 2: Identify correct comment style
Only # This is a comment uses the correct Python comment syntax.Final Answer:
# This is a comment -> Option BQuick Check:
Python comments start with # [OK]
- Using // which is for other languages
- Using HTML comment syntax
- Using C-style comments
1. x = 5 2. y = x + 2 3. print(y)
What will be the output after running all cells from top to bottom?
Solution
Step 1: Trace variable assignments
x is assigned 5, then y is assigned x + 2, which is 7.Step 2: Understand print output
Printing y will output 7.Final Answer:
7 -> Option DQuick Check:
5 + 2 = 7 [OK]
- Assuming print shows x instead of y
- Forgetting to run cells in order
- Expecting error without reason
1. a = 10 2. print(a) 3. a = 20
If you run cells in order 1, 3, 2, what will happen?
Solution
Step 1: Analyze cell execution order
Cell 1 sets a=10, cell 3 sets a=20, then cell 2 prints a.Step 2: Determine printed value
Since a was last set to 20 before printing, print(a) outputs 20.Final Answer:
Prints 20 -> Option AQuick Check:
Last assignment before print = 20 [OK]
- Assuming print shows old value
- Thinking print runs before assignment
- Expecting error due to order
Solution
Step 1: Understand notebook execution state
Running all cells top to bottom ensures outputs match code and order.Step 2: Identify sharing best practice
Sharing a fully run notebook avoids confusion and missing outputs.Final Answer:
Run all cells from top to bottom before sharing -> Option CQuick Check:
Run all cells = consistent notebook [OK]
- Sharing without running cells
- Deleting Markdown removes explanations
- Sharing only scripts loses notebook features
Ready to prove it?
Pass the test and this pattern moves unknown
