📖 Scenario: You work in a CNC machine shop. You need to organize your tool library to keep track of tools by their ID, type, and size. This helps the machine operator select the right tool quickly.
🎯 Goal: Create a tool library as a dictionary with tool details, add a configuration for minimum tool size, filter tools by size, and display the filtered list.
📋 What You'll Learn
Create a dictionary called tool_library with specific tool entries
Add a variable called min_size to set the minimum tool size
Use a dictionary comprehension to create filtered_tools with tools having size >= min_size
Print the filtered_tools dictionary
💡 Why This Matters
🌍 Real World
Organizing and filtering tool libraries helps CNC operators quickly find the right tool for machining jobs, improving efficiency and reducing errors.
💼 Career
This skill is useful for CNC programmers and machine operators who manage tool inventories and automate tool selection processes.
Progress0 / 4 steps
1
Create the tool library dictionary
Create a dictionary called tool_library with these exact entries: 101: {'type': 'drill', 'size': 5}, 102: {'type': 'mill', 'size': 10}, 103: {'type': 'lathe', 'size': 8}, 104: {'type': 'drill', 'size': 3}
CNC Programming
Hint
Use curly braces to create a dictionary. Each key is a number and each value is another dictionary with keys 'type' and 'size'.
2
Set the minimum tool size
Create a variable called min_size and set it to 5 to filter tools by size.
CNC Programming
Hint
Just assign the number 5 to the variable min_size.
3
Filter tools by minimum size
Use a dictionary comprehension to create a new dictionary called filtered_tools that includes only tools from tool_library with size greater than or equal to min_size. Use tool_id and details as the loop variables.
CNC Programming
Hint
Use a dictionary comprehension with for tool_id, details in tool_library.items() and an if condition checking details['size'] >= min_size.
4
Print the filtered tools
Print the filtered_tools dictionary to display the tools with size greater than or equal to min_size.
CNC Programming
Hint
Use print(filtered_tools) to show the filtered dictionary.
Practice
(1/5)
1. What is the main purpose of a tool library in CNC programming?
easy
A. To record operator work hours
B. To store CNC machine operating manuals
C. To log CNC machine errors
D. To organize all CNC tools with their sizes and types
Solution
Step 1: Understand the role of a tool library
A tool library is designed to keep a list of all tools used in CNC machines, including their sizes and types.
Step 2: Compare options with the definition
Only To organize all CNC tools with their sizes and types correctly describes this purpose. Other options describe unrelated functions.
Final Answer:
To organize all CNC tools with their sizes and types -> Option D
Quick Check:
Tool library purpose = Organize tools [OK]
Hint: Tool library = organized list of tools [OK]
Common Mistakes:
Confusing tool library with machine manuals
Thinking it logs errors or operator info
Assuming it stores program code
2. Which of the following is the correct syntax to add a new tool entry in a CNC tool library script?
easy
A. add_tool(name='Drill', diameter=10, type='End Mill')
B. tool_add('Drill', 10, 'End Mill')
C. insertTool(name: 'Drill', size: 10, category: 'End Mill')
D. newTool = {name: 'Drill', diameter: 10, type: 'End Mill'}
Solution
Step 1: Identify correct function syntax
In CNC scripting, adding a tool usually uses a function like add_tool with named parameters.
Step 2: Check each option's format
add_tool(name='Drill', diameter=10, type='End Mill') uses named parameters correctly. tool_add('Drill', 10, 'End Mill') uses an unknown function name. insertTool(name: 'Drill', size: 10, category: 'End Mill') uses incorrect parameter syntax. newTool = {name: 'Drill', diameter: 10, type: 'End Mill'} is an object assignment, not a function call.
Final Answer:
add_tool(name='Drill', diameter=10, type='End Mill') -> Option A
Which Python code snippet correctly finds the tool with the smallest diameter?
hard
A. smallest = min(tools, key=lambda t: t['diameter'])
B. smallest = max(tools, key=lambda t: t['diameter'])
C. smallest = tools.sort(key=lambda t: t['diameter'])[0]
D. smallest = tools[0]
Solution
Step 1: Understand the goal to find smallest diameter tool
We need to find the dictionary with the minimum 'diameter' value.
Step 2: Evaluate each option's approach
smallest = min(tools, key=lambda t: t['diameter']) uses min() with a key function correctly. smallest = max(tools, key=lambda t: t['diameter']) finds the maximum, which is wrong. smallest = tools.sort(key=lambda t: t['diameter'])[0] tries to sort but sort() returns None, so indexing fails. smallest = tools[0] just picks the first tool without checking diameter.
Final Answer:
smallest = min(tools, key=lambda t: t['diameter']) -> Option A
Quick Check:
Use min() with key for smallest item [OK]
Hint: Use min() with key=lambda for smallest value [OK]
Common Mistakes:
Using max() instead of min()
Trying to index result of sort() directly
Assuming first list item is smallest
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.