Built-in Functions Reference πΒΆ
Prerequisites: Python Basics
Mentor's Note: Python comes with a "Tool Belt" of functions that are always ready to use. You don't need to import anything to use these! π‘
π The Scenario: The Master Tool Belt π οΈΒΆ
Imagine you are a master carpenter.
- The Logic: Instead of building a hammer or a screwdriver from scratch every time, you carry them in your Tool Belt. π¦
- The Result: You just reach down and grab the tool you need (like
printorlen) and get to work instantly. β
π Essential Built-insΒΆ
1. The Output ToolsΒΆ
print(): Displays text or data on the screen. π€input(): Takes a string from the user. β¨οΈ
2. The Measurement ToolsΒΆ
len(): Returns the length (count of items) of a sequence. π’type(): Tells you which data type a variable is. π§
3. The Math ToolsΒΆ
abs(): Returns the absolute value (makes it positive).sum(): Adds up all items in a list.max()/min(): Finds the biggest or smallest item.
4. The Loop ToolsΒΆ
range(stop): Creates a sequence of numbers (0 to stop-1).enumerate(): Adds a counter to a list (Index, Value).
π¨ Visual Logic: The Tool Category MapΒΆ
mindmap
root((Python Tools))
I/O
print
input
Math
sum
abs
round
Collections
len
sorted
list
Logic
bool
all
any
π» Implementation: Quick ExamplesΒΆ
π Sample Dry Run (range)ΒΆ
Instruction: list(range(1, 4))
| Step | Action | Logic | Result |
|---|---|---|---|
| 1 | Start at 1 | Initial value | [1] |
| 2 | Move to 2 | Increment | [1, 2] |
| 3 | Move to 3 | Increment | [1, 2, 3] |
| 4 | Reach 4 | STOP (Don't include 4) | [1, 2, 3] β
|
π‘ Interview Tip πΒΆ
"Interviewers often ask: 'What is the difference between
range(5)andlist(range(5))?' Answer:range(5)is a memory-efficient generator; it doesn't create the numbers until you loop over them.list()forces it to create all numbers in memory at once!"
π‘ Pro Tip: "Computers are good at following instructions, but not at reading your mind. Be precise with your built-in tools!" - Donald Knuth