You do not have to write every function yourself. Python comes with a large collection of built-in functions — tools that are always available, ready to use. You have already used some of them without realising it: print() and input() are built-in functions. This article introduces the most useful ones for beginners.
Input and Output
| Function | What it does | Example |
|---|---|---|
print() | Displays values on screen | print("Hello!") |
input() | Reads a line of text from the user | name = input("Your name: ") |
name = input("What is your name? ")
print("Hello,", name)
input() always returns a string, even if the user types a number. To do arithmetic with user input, you must convert it first: age = int(input("Age: ")).
Type Functions
These functions convert values from one type to another:
| Function | Converts to | Example |
|---|---|---|
int() | Integer | int("42") → 42 |
float() | Decimal number | float("3.14") → 3.14 |
str() | String | str(100) → "100" |
bool() | Boolean | bool(0) → False |
Maths Functions
| Function | What it does | Example |
|---|---|---|
abs() | Absolute value | abs(-7) → 7 |
round() | Rounds a number | round(3.7) → 4 |
max() | Largest value in a group | max(3, 7, 2) → 7 |
min() | Smallest value in a group | min(3, 7, 2) → 2 |
sum() | Adds up a list of numbers | sum([1, 2, 3]) → 6 |
pow() | Raises to a power | pow(2, 8) → 256 |
scores = [85, 72, 91, 68, 77]
print(f"Highest: {max(scores)}")
print(f"Lowest: {min(scores)}")
print(f"Total: {sum(scores)}")
print(f"Average: {sum(scores) / len(scores)}")
Sequence Functions
| Function | What it does | Example |
|---|---|---|
len() | Length of a string or list | len("hello") → 5 |
range() | Generates a number sequence | range(1, 6) |
sorted() | Returns a sorted copy of a list | sorted([3,1,2]) → [1,2,3] |
reversed() | Returns an iterator in reverse | used inside a loop |
enumerate() | Pairs each item with its index | used inside a for loop |
fruits = ["banana", "apple", "cherry"]
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")
Output:
0: banana
1: apple
2: cherry
enumerate() is much cleaner than manually creating a counter variable. Whenever you need both the position and the value from a list, use enumerate().
type() — Checking the Type
When something behaves unexpectedly, check what type a value actually is:
x = input("Enter a number: ")
print(type(x)) # <class 'str'> — a reminder to convert it!
x = int(x)
print(type(x)) # <class 'int'>
Many beginner bugs come from forgetting that input() returns a string. Always convert the result before doing arithmetic with it.
help() — Your Documentation Lifeline
If you ever forget what a function does, call help() on it in the Python shell:
help(round)
This prints the official documentation for round directly in your terminal.
Practice Problems
What does len('computing') return? What type is the result?
Show answer
A student writes: score = input('Enter score: ') and then average = score / 2. What error will occur and how should they fix it?