Preparing your learning space...
100% through Built-in Functions tutorials
Python comes with a set of built-in functions that are always available — no imports needed. These functions handle common tasks like getting the length of a list, converting data types, looping over numbers, and working with user input. Mastering them makes your code shorter, cleaner, and more readable.
print() writes text or values to the console. It is the most common way to see what your program is doing.
print("Hello, World!")
print(42)
print("The answer is", 42)
You can pass multiple items separated by commas. print() adds a space between them and a newline at the end by default.
name = "Alice"
age = 25
print(name, "is", age, "years old")
# Output: Alice is 25 years old
Change the separator with sep and the ending with end:
print("apple", "banana", "cherry", sep=", ")
# Output: apple, banana, cherry
print("Loading", end="...")
print("Done")
# Output: Loading...Done
type() returns the type of any value or variable. Use it to inspect what kind of data you are working with.
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type("hello")) # <class 'str'>
print(type([1, 2, 3])) # <class 'list'>
print(type(True)) # <class 'bool'>
This is especially useful when debugging — if your code crashes, type() helps you confirm the actual data type you are dealing with.
value = input("Enter a number: ")
print(type(value)) # <class 'str'> — input() always returns a string
len() returns the number of items in a sequence or collection: strings, lists, tuples, dictionaries, sets, etc.
print(len("Python")) # 6
print(len([10, 20, 30])) # 3
print(len({"a": 1, "b": 2})) # 2
Common use cases: checking if a list is empty, validating input length, or counting items before looping.
username = input("Enter username: ")
if len(username) < 3:
print("Username must be at least 3 characters.")
input() pauses the program and waits for the user to type something. It returns whatever the user enters as a string.
name = input("What is your name? ")
print("Hello,", name)
Remember: the return value is always a string. Convert it to a number if you need math.
age = input("How old are you? ")
age = int(age) # convert string to integer
print("Next year you will be", age + 1)
Shortcut for converting on the spot:
age = int(input("How old are you? "))
range() produces a sequence of numbers, commonly used in for loops. It is memory-efficient — it does not create a full list in memory.
for i in range(5):
print(i)
# Output: 0 1 2 3 4
Three forms:
| Form | Example | Result |
|---|---|---|
range(stop) | range(5) | 0, 1, 2, 3, 4 |
range(start, stop) | range(2, 6) | 2, 3, 4, 5 |
range(start, stop, step) | range(1, 10, 2) | 1, 3, 5, 7, 9 |
# Count down
for i in range(10, 0, -1):
print(i)
print("Liftoff!")
range() is often combined with len() to loop over a list by index:
fruits = ["apple", "banana", "cherry"]
for i in range(len(fruits)):
print(i, fruits[i])
sorted() takes any iterable and returns a new sorted list. The original data stays unchanged.
numbers = [4, 2, 9, 1, 5]
sorted_numbers = sorted(numbers)
print(sorted_numbers) # [1, 2, 4, 5, 9]
print(numbers) # [4, 2, 9, 1, 5] — unchanged
Works with strings, tuples, and any iterable:
print(sorted("python")) # ['h', 'n', 'o', 'p', 't', 'y']
Reverse and custom sorting:
print(sorted([4, 2, 9, 1, 5], reverse=True)) # [9, 5, 4, 2, 1]
# Sort by string length
words = ["cat", "elephant", "dog", "butterfly"]
print(sorted(words, key=len)) # ['cat', 'dog', 'elephant', 'butterfly']
The key parameter accepts a function that transforms each item before comparison.
zip() pairs up items from two or more iterables, stopping when the shortest iterable runs out.
names = ["Alice", "Bob", "Charlie"]
scores = [85, 92, 78]
for name, score in zip(names, scores):
print(name, score)
# Output:
# Alice 85
# Bob 92
# Charlie 78
Common uses: creating dictionaries, iterating over parallel lists, transposing data.
# Build a dictionary from two lists
students = dict(zip(names, scores))
print(students) # {'Alice': 85, 'Bob': 92, 'Charlie': 78}
# Unzip back
names2, scores2 = zip(*students.items())
print(names2) # ('Alice', 'Bob', 'Charlie')
print(scores2) # (85, 92, 78)
enumerate() adds a counter to an iterable, returning pairs of (index, item). It saves you from manually tracking an index variable.
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(index, fruit)
# Output:
# 0 apple
# 1 banana
# 2 cherry
The counter starts at 0 by default. Change it with the start parameter:
for index, fruit in enumerate(fruits, start=1):
print(index, fruit)
# Output:
# 1 apple
# 2 banana
# 3 cherry
This is cleaner and less error-prone than range(len(...)).
map() applies a function to every item in an iterable and returns an iterator with the results.
def square(x):
return x * x
numbers = [1, 2, 3, 4]
squared = map(square, numbers)
print(list(squared)) # [1, 4, 9, 16]
You can use map() with a lambda function for simple operations:
numbers = [1, 2, 3, 4]
doubled = map(lambda x: x * 2, numbers)
print(list(doubled)) # [2, 4, 6, 8]
Working with multiple iterables: map() accepts extra iterables that are passed as arguments to the function.
a = [1, 2, 3]
b = [4, 5, 6]
result = map(lambda x, y: x + y, a, b)
print(list(result)) # [5, 7, 9]
Note: map() returns an iterator, not a list. Wrap it with list() when you need to store or reuse the result.
filter() returns only the items for which a function returns True.
def is_even(n):
return n % 2 == 0
numbers = [1, 2, 3, 4, 5, 6]
evens = filter(is_even, numbers)
print(list(evens)) # [2, 4, 6]
Using a lambda for the condition:
numbers = [1, 2, 3, 4, 5, 6]
evens = filter(lambda n: n % 2 == 0, numbers)
print(list(evens)) # [2, 4, 6]
filter() also returns an iterator, so wrap it with list() when needed.
Filtering out falsy values (None, 0, empty strings, etc.):
data = [0, "hello", "", None, 42, False]
clean = filter(None, data)
print(list(clean)) # ['hello', 42]
Passing None as the function tells filter() to use the identity function — it keeps values that are truthy.
A small program that brings several of these functions together:
# Collect student scores
names = []
scores = []
for i in range(3):
name = input(f"Enter student {i+1} name: ")
score = int(input(f"Enter score for {name}: "))
names.append(name)
scores.append(score)
# Display results sorted by score (highest first)
print("\n--- Results ---")
sorted_pairs = sorted(zip(names, scores), key=lambda x: x[1], reverse=True)
for rank, (name, score) in enumerate(sorted_pairs, start=1):
print(f"{rank}. {name} — {score}")
# Show who passed (score >= 50)
passing = list(filter(lambda x: x[1] >= 50, sorted_pairs))
print(f"\nPassing students: {len(passing)}")
for name, score in passing:
print(f" {name} ({score})")
# Double all scores for a fantasy leaderboard
print("\n--- Fantasy League (doubled scores) ---")
fantasy = list(map(lambda x: (x[0], x[1] * 2), sorted_pairs))
for name, score in fantasy:
print(f"{name}: {score}")
Save your progress and earn XP for completing tutorials.
Keep learning
Technology
Python
Lesson group
Built-in Functions
Progress
100% complete