Python comes with a rich set of built-in functions that we don’t need to import. Knowing them saves us from reinventing the wheel and makes our code cleaner.
enumerate() — Index + Value Together
Instead of tracking an index manually, enumerate() gives us both.
fruits = ["apple", "banana", "cherry"]
for i, fruit in enumerate(fruits):
print(f"{i}: {fruit}")
# Start counting from 1 instead of 0
for i, fruit in enumerate(fruits, start=1):
print(f"{i}. {fruit}")
sorted() vs .sort()
sorted() returns a new sorted list. .sort() sorts a list in place and returns None.
nums = [3, 1, 4, 1, 5]
sorted_nums = sorted(nums) # [1, 1, 3, 4, 5] — nums is unchanged
nums.sort() # None — but nums is now [1, 1, 3, 4, 5]
# Reverse sort
sorted(nums, reverse=True) # [5, 4, 3, 1, 1]
# Sort by custom key
words = ["banana", "apple", "fig"]
sorted(words, key=len) # ['fig', 'apple', 'banana']
The key difference: sorted() works on any iterable (strings, tuples, sets, dicts), while .sort() only works on lists.
reversed()
Returns a reverse iterator. Doesn’t modify the original.
for n in reversed([1, 2, 3]):
print(n) # 3, 2, 1
# Convert to list if needed
list(reversed([1, 2, 3])) # [3, 2, 1]
any() and all()
These are incredibly useful for checking conditions across an iterable.
any()— returnsTrueif at least one element is truthyall()— returnsTrueif every element is truthy
nums = [0, 1, 2, 3]
any(nums) # True — at least one non-zero
all(nums) # False — 0 is falsy
# With conditions
scores = [85, 92, 78, 90]
all(s >= 70 for s in scores) # True — everyone passed
any(s == 100 for s in scores) # False — no perfect score
isinstance() and type()
isinstance() checks if an object is an instance of a type (respects inheritance). type() gives the exact type.
x = 42
isinstance(x, int) # True
isinstance(x, (int, float)) # True — check multiple types
type(x) # <class 'int'>
type(x) is int # True — exact match only
Use isinstance() in most cases. Use type() only when we need the exact type without inheritance.
id() and hash()
id() returns the memory address of an object. hash() returns the hash value (used in dicts and sets).
x = "hello"
id(x) # e.g., 140234567890 — unique memory address
hash(x) # e.g., 8464330393063589907 — hash value
# Only immutable types are hashable
hash([1, 2]) # TypeError — lists aren't hashable
len(), range(), abs(), round()
The everyday workhorses.
len([1, 2, 3]) # 3
len("hello") # 5
len({"a": 1, "b": 2}) # 2
list(range(5)) # [0, 1, 2, 3, 4]
list(range(2, 8)) # [2, 3, 4, 5, 6, 7]
list(range(0, 10, 2)) # [0, 2, 4, 6, 8]
abs(-42) # 42
round(3.14159, 2) # 3.14
round(2.5) # 2 — banker's rounding (rounds to even)
Watch out: round(2.5) returns 2, not 3. Python uses banker’s rounding (round half to even) to reduce bias.
min() and max() with key
These accept an optional key argument, just like sorted().
nums = [3, -7, 2, -4, 5]
min(nums) # -7
max(nums) # 5
# By absolute value
min(nums, key=abs) # 2
max(nums, key=abs) # -7
# With dicts
users = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]
youngest = min(users, key=lambda u: u["age"])
# {'name': 'Bob', 'age': 25}
repr() vs str()
str() gives a human-readable string. repr() gives an unambiguous developer-friendly string.
s = "hello\nworld"
print(str(s)) # hello
# world
print(repr(s)) # 'hello\nworld'
# In f-strings, !r uses repr
name = "Manish"
f"{name!r}" # "'Manish'" — with quotes
input()
Reads a line from the user. Always returns a string.
name = input("What's your name? ") # returns str
age = int(input("How old are you? ")) # convert to int ourselves
In simple language, Python’s built-in functions are our Swiss Army knife. Learning them well means writing less code and solving problems faster. When in doubt, check if there’s a built-in for it first.