Built in functions
Objectives
- Understand the concept of built-in functions in Python.
- Learn how to use some commonly used built-in functions for math, strings, and basic input/output.
- Explore the purpose and usage of functions like
print()
,len()
,input()
, and more.
As well as creating our own functions Python comes with a wide range of built-in functions that perform various tasks, making programming easier and more efficient. These functions are part of Python's standard library and can be used without the need for additional installations.
There are a whole host of other functions available to us that can be either import
ed to our program e.g. the random
library, or installed e.g. the pandas
library. We'll consider this approach in a later section
Example 5
Before running this code, read it and predict what you think will be displayed for the following:
- line 2: What will
input("Enter your name: ")
do? - line 6: What does the
len
function return for the string 'Climate'? - line 10 and 13: what will be output from the
type()
function in both cases? - line 16: What will be the result of
abs(-7.25)
? - line 20: What will
round(3.14159, 2)
return?
Run the program in a Python environment and compare the actual output with your predictions.
Commonly Used Built-In Functions
Function | Description | Example | Output |
---|---|---|---|
print() |
We have seen this used in previous sections. The print() function is used to display information on the screen. It can print text, variables, and the result of expressions. |
print("Welcome") |
"Welcome" |
len() |
The len() function returns the length of a string. It counts the number of characters in the string. |
len("Hello") |
5 |
input() |
The input() function allows user input. It waits for the user to enter data and returns it as a string. |
||
int() |
used for data type conversion. int() converts a value to an integer |
||
float() |
used for data type conversion. float() converts a value to an real number |
||
str() |
The str() function converts other data types to strings |
||
max() |
used to find the maximum value among a set of values | ||
min() |
used to find the minimum value among a set of values. | ||
abs() |
The abs() function returns the absolute value of a number. |
||
round() |
The round() function rounds a floating-point number to the nearest integer |
||
str.upper() |
Converts characters in str variable to upper case |
||
str.lower() |
Converts characters in str variable to lower case |
||
chr() |
chr() converts an ASCII code (integer) to a character |
||
ord() |
ord() converts a character to its ASCII integer code |
Activity
Copy and paste the following code into a code editor, before running the code read it and predict what you think the output will be. Then make changes to the code as indicated below.
- Change the string and observe the output of the
upper()
andlower()
methods. - Input a decimal number and see the rounded result.
- Input a negative integer and see the absolute value.
Climate Quest Project
Throughout this topic we'll be working on a large scale project: Climate Quest. In this project a player embarks on a journey to combat the effects of climate change by making decisions that impact the environment. Each choice affects the outcome of the game, emphasizing the importance of individual actions in addressing climate change.
Go to task 5 - Built-in Functions