📖 Scenario: You work in a small company that wants to understand how much memory different data structures use. This helps them choose the best way to store data efficiently.
🎯 Goal: You will create a dictionary with some data, set a variable to track memory size, calculate the memory used by each item, and finally print the total memory used.
📋 What You'll Learn
Create a dictionary with exact keys and values
Create a variable to hold total memory usage
Use a for loop to calculate memory usage of each dictionary item
Print the total memory usage
💡 Why This Matters
🌍 Real World
Understanding memory usage helps companies optimize their software to run faster and use less computer memory.
💼 Career
Data scientists and software engineers often analyze memory to improve program efficiency and reduce costs.
Progress0 / 4 steps
1
Create the data dictionary
Create a dictionary called data with these exact entries: 'item1': [1, 2, 3], 'item2': [4, 5], 'item3': [6].
Data Analysis Python
Hint
Use curly braces {} to create a dictionary. Each key is a string and each value is a list of numbers.
2
Set up total memory variable
Create a variable called total_memory and set it to 0 to track the total memory used.
Data Analysis Python
Hint
Just write total_memory = 0 to start counting memory.
3
Calculate memory usage for each item
Use a for loop with variables key and value to iterate over data.items(). Inside the loop, add the memory size of value to total_memory using sys.getsizeof(value). Import sys at the top.
Data Analysis Python
Hint
Remember to import sys first. Then use for key, value in data.items(): to loop. Use sys.getsizeof(value) to get memory size.
4
Print the total memory used
Write a print statement to display the text Total memory used: followed by the value of total_memory.
Data Analysis Python
Hint
Use print(f"Total memory used: {total_memory}") to show the result.
Practice
(1/5)
1. What does the memory_usage(deep=True) method in pandas do?
easy
A. It counts the number of rows in the DataFrame.
B. It deletes unused memory from the DataFrame.
C. It compresses the DataFrame to use less memory.
D. It shows detailed memory usage of each column including object types.
Solution
Step 1: Understand memory_usage method
The memory_usage method reports memory used by each column in a DataFrame.
Step 2: Effect of deep=True parameter
Using deep=True makes it include memory used by object types like strings, giving detailed info.
Final Answer:
It shows detailed memory usage of each column including object types. -> Option D
Quick Check:
memory_usage(deep=True) = detailed memory info [OK]
Hint: Remember deep=True reveals full memory use including strings [OK]
Common Mistakes:
Thinking it deletes or compresses data
Confusing it with counting rows
Ignoring deep=True effect
2. Which of the following is the correct syntax to get memory usage of a pandas DataFrame df including deep analysis?
easy
A. memory_usage(df, deep=True)
B. df.memoryusage(deep=True)
C. df.memory_usage(deep=True)
D. df.get_memory_usage(deep=True)
Solution
Step 1: Recall pandas method name
The correct method is memory_usage with underscore, called on DataFrame.
Step 2: Check method call syntax
It is called as df.memory_usage(deep=True). Other options have wrong method names or syntax.
Final Answer:
df.memory_usage(deep=True) -> Option C
Quick Check:
Correct method name and call = df.memory_usage(deep=True) [OK]
Hint: Use exact method name: memory_usage with underscore [OK]
A. It does not show full memory usage of string data.
B. The method memory_usage does not exist.
C. deep=False causes a syntax error.
D. DataFrame must have numeric columns to use memory_usage.
Solution
Step 1: Check memory_usage with deep=False
By default or with deep=False, memory_usage does not count full memory used by object columns like strings.
Step 2: Effect on output
This means the reported memory for 'col' will be underestimated, missing actual string memory.
Final Answer:
It does not show full memory usage of string data. -> Option A
Quick Check:
deep=False skips detailed object memory [OK]
Hint: Use deep=True to get full string memory usage [OK]
Common Mistakes:
Thinking memory_usage method is missing
Assuming deep=False causes error
Believing numeric columns are required
5. You have a large DataFrame with many string columns. You want to reduce memory usage without losing data. Which approach helps most based on memory usage analysis?
hard
A. Convert string columns to categorical type after checking memory usage.
B. Drop all string columns to save memory.
C. Use memory_usage(deep=False) to estimate memory and ignore strings.
D. Convert all columns to float type regardless of original data.
Solution
Step 1: Analyze memory usage of string columns
Using memory_usage(deep=True) shows string columns use a lot of memory.
Step 2: Convert strings to categorical
Changing string columns to categorical reduces memory by storing unique values once.
Step 3: Avoid data loss
Converting to categorical keeps data but uses less memory, unlike dropping or wrong type conversion.
Final Answer:
Convert string columns to categorical type after checking memory usage. -> Option A
Quick Check:
Convert strings to categorical for memory saving [OK]
Hint: Convert strings to categorical to save memory without losing data [OK]
Common Mistakes:
Dropping important columns blindly
Ignoring deep=True memory insights
Converting all columns to float causing errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.