Built-in Functions
Learn how to use Python’s built-in functions so you can perform useful tasks with just a single line of code.
Skill Level: Beginner
Prerequisites: Variables, Data Types, Type Conversion
Estimated Time: 20 minutes
Story Time
As you write more programs, you will often need to count letters in a word, find the largest value in a list, or round a decimal number. Writing the logic for each of these tasks from scratch would take time.
Instead, Python provides many ready-made functions you can call directly. These built-in functions help you work more quickly, reduce errors, and focus on the data analysis instead of low-level details.
Learning how to read and use these functions now will make later lessons on data analysis and visualization feel much more natural.
What You’ll Learn
By the end of this lesson, you will be able to:
- Explain what a function is.
- Use common Python built-in functions.
- Read function syntax and understand arguments.
- Apply functions to solve simple problems in your own code.
What Is a Function?
A function is a reusable piece of code that performs a specific task.
You call a function, give it input (if needed), and it returns an output.
For example:
print("Hello")
The print() function displays information on the screen.
Functions help you avoid repeating the same logic over and over.
Function Syntax
Most functions follow a simple pattern:
function_name(argument)
Example:
print("Python")
printis the function name."Python"is the argument.
Some functions accept multiple arguments (separated by commas), while others do not require any arguments at all.
Common Built-in Functions
Here are several built-in functions you will use frequently:
| Function | Purpose |
|---|---|
print() | Display output |
type() | Show the data type |
len() | Count the number of items |
input() | Read user input as text |
int() | Convert to integer |
float() | Convert to float |
str() | Convert to string |
bool() | Convert to Boolean |
max() | Largest value |
min() | Smallest value |
round() | Round decimal values |
These are especially helpful when you begin working with datasets and user input.
The print() Function
print("Hello, World!")
Output:
Hello, World!
print() displays information on the screen. It is useful for showing messages, results, and intermediate outputs as your program runs.
The type() Function
age = 21
print(type(age))
Output:
<class 'int'>
type() shows you the data type of a value or variable. This is helpful when you are not sure how Python is interpreting your data.
The len() Function
len() returns the length of an object.
course = "Python"
print(len(course))
Output:
6
It also works with lists:
numbers =[2][3][4]
print(len(numbers))
Output:
3
len() works with collections such as strings, lists, and other sequence types. It does not work with plain numbers.
The max() Function
marks =
print(max(marks))
Output:
91
max() returns the largest value in a collection. This is useful for finding top scores or maximum measurements.
The min() Function
marks =
print(min(marks))
Output:
68
min() returns the smallest value in a collection.
The round() Function
value = 3.14159
print(round(value, 2))
Output:
3.14
The first argument is the number to round, and the second argument is how many decimal places to keep. If you omit the second argument, Python rounds to the nearest whole number.
The input() Function
name = input("Enter your name: ")
print(name)
input() displays a prompt, waits for the user to type something, and then returns what they typed as a string.
Important:
input()always returns text.- If you want to perform calculations, you will need to convert the input into a numeric type first, such as with
int()orfloat().
Worked Example
student = "Emma"
marks =
print(student)
print(len(student))
print(max(marks))
print(min(marks))
print(type(student))
Output:
Emma
4
91
72
<class 'str'>
This example shows how built-in functions can quickly answer questions about your data: length, highest value, lowest value, and type.
Quick Check
Predict the output before running this code:
city = "Gainesville"
print(len(city))
Then run it in Google Colab to confirm your prediction.
Practice in the Notebook
After completing the activity in Google Colab, mark it as complete below.
Create a variable containing your own name:
name = "Ava"
Use:
print()to display the name.len()to count the number of characters in the name.type()to show the data type.
Example pattern:
print(name)
print(len(name))
print(type(name))
Try It Yourself
Create the following list:
scores =
Use built-in functions to determine:
- The highest score (
max()). - The lowest score (
min()). - The number of scores (
len()).
Print each result with a short label so the output is easy to read.
Challenge
A researcher records daily temperatures:
temperatures = [23.6, 25.4, 27.8, 22.9, 24.1]
Use built-in functions to determine:
- The highest temperature.
- The lowest temperature.
- The number of observations.
Then round the highest temperature to one decimal place using round().
Common Mistakes
Forgetting parentheses
Incorrect:
print
Correct:
print()
Without parentheses, you are referring to the function object itself, not calling the function.
Using len() with numbers
Incorrect:
len(25)
len() works with collections (like strings and lists), not with plain integers.
Confusing type and type()
Incorrect:
print(type)
Correct:
print(type(age))
type by itself refers to the built-in type object. You must use type() with parentheses and an argument to see the data type of a value.
Key Takeaways
- A function is reusable code that performs a specific task.
- Built-in functions such as
print(),type(),len(),max(),min(),round(), andinput()make common tasks simple. input()always returns a string, which often must be converted before calculations.- Understanding function syntax helps you read documentation and apply new functions as you learn more Python.
Self Evaluation
Check Your Understanding
1. What is a function in Python?
2. Which built-in function can tell you how many items are in a list?
3. What does input() return by default?
4. Which function returns the largest value in a list of numbers?
5. Which line correctly calls the print() function?
Continue Your Journey
In the next lesson, you will explore Input and Output in more detail, learning how to interact with users and display results in clearer ways.