Python includes many built-in functions: functions that are already part of the language and are available without installing any additional modules. Instead of writing your own code to perform common tasks, you can simply call one of these functions.
A function is used by writing its name followed by parentheses. Information passed to the function is placed inside the parentheses and is called an argument.
print("Hello")
abs(-5)
round(3.14159, 2)
Some functions perform an action, while others calculate and return a value that can be stored in a variable or used in an expression.
print() function
The print() function displays information on the screen. It is
primarily used to communicate results to the user or to help debug a program.
name = "Alice"
score = 92
print(name)
print(score)
print("The score is", score)
The print() function can display text, variables, or combinations
of both.
input() function
The input() function pauses the program and waits for the user to
type something. Whatever the user types is returned as a
string.
name = input("Enter your name: ")
print("Hello,", name)
Even if the user types a number, the result is still a string. If you want to perform calculations, you must convert the input to the appropriate numeric type.
temperature = float(input("Enter the temperature: "))
mass = int(input("Enter the number of samples: "))
Python provides several built-in functions for converting values from one data type to another.
| Function | Purpose |
|---|---|
int() | Convert a value to an integer |
float() | Convert a value to a floating-point number |
str() | Convert a value to a string |
bool() | Convert a value to True or False |
x = int("42")
y = float("3.14")
z = str(100)
Many common programming tasks can be accomplished with a single built-in function.
| Function | Example | Result |
|---|---|---|
type() | type(3.14) | float |
len() | len("Chemistry") | 9 |
abs() | abs(-7) | 7 |
round() | round(3.14159,2) | 3.14 |
min() | min(8,3,12) | 3 |
max() | max(8,3,12) | 12 |
sum() | sum([1,2,3,4]) | 10 |
Big idea: Built-in functions allow you to perform common tasks with a single line of code. Learning the most frequently used functions will make your programs shorter, easier to read, and less prone to errors. As you continue to learn Python, you will encounter many additional built-in functions and library functions that extend the language even further.
The print() function displays information on the screen. The
simplest use is to print a string of text.
print("Hello, world!")
Output
Hello, world!
Here, the text inside the quotation marks is called a string. Everything inside the string is displayed exactly as written.
The print() function can display several items at once.
Separate them with commas, and Python automatically inserts spaces
between them.
name = "Alice"
print("Hello,", name, "!")
Output
Hello, Alice !
Another way to produce similar output is
name = "Alice"
print("Hello, " + name + "!")
Note: Notice how the comma (,) behaves slightly differently than the plus sign (+)!
Each item separated by a comma becomes a separate argument to
print(). Python prints each argument in order.
Normally, every call to print() ends with a newline, so the next
thing printed begins on a new line. You can change this behavior using the
end argument.
print("Loading", end="")
print("...", end="")
print(" Done!")
Output
Loading... Done!
The default value of end is a newline ("\n").
By setting end="", nothing is printed after the text, so the
next print() continues on the same line.
| Problem 1 | |
|---|---|
| Which built-in function is used to display information on the screen? | |
input() |
|
print() |
|
type() |
|
len() |
|
| Problem 2 | |
|---|---|
What is printed by the following code?
|
|
| Hello, {name}! | |
| Hello, name! | |
| Hello! | |
| Hello, Alice! | |
| Problem 3 | |
|---|---|
Which statement about the input() function is correct?
|
|
| It always returns a string. | |
| It always returns an integer. | |
| It prints text on the screen. | |
| It automatically converts numbers to floats. | |
| Problem 4 | |
|---|---|
What is the purpose of the end="" argument in the
print() function?
|
|
| It ends the program after printing. | |
| It prints only the last word. | |
It changes what is printed after the text, allowing multiple print() calls to stay on the same line. |
|
| It removes spaces between words. | |
| Problem 5 | |
|---|---|
| Which statement correctly converts the user's input into a floating-point number? | |
temperature = input(float("Temperature: ")) |
|
temperature = float(input("Temperature: ")) |
|
temperature = print(input("Temperature: ")) |
|
temperature = input(int("Temperature: ")) |
|
function_name().
print() function displays information on the screen.
input() function reads text entered by the user and
always returns a string.
int(), float(), str(), and
bool() to convert values from one data type to another.
type() function reports the data type of a value or
variable.
end="" argument of print() allows multiple
print() statements to continue on the same line.
len(),
abs(),
round(),
min(),
max(), and
sum().
Big picture: Built-in functions are reusable tools that make Python programs shorter, easier to read, and easier to write. Learning the most common functions allows you to solve many programming problems without having to write the underlying code yourself.