# Work with built-in functions

Previously, you explored built-in functions in Python, including <var>print()</var>, <var>type()</var>, <var>max()</var>, and <var>sorted()</var>. **Built-in functions** are functions that exist within Python and can be called directly. In this reading, you’ll explore these further and also learn about the <var>min()</var> function. In addition, you'll review how to pass the output of one function into another function.

## print() 

The <var>print()</var> function outputs a specified object to the screen. The <var>print()</var> function is one of the most commonly used functions in Python because it allows you to output any detail from your code.

To use the <var>print()</var> function, you pass the object you want to print as an argument to the function. The <var>print()</var> function takes in any number of arguments, separated by a comma, and prints all of them. For example, you can run the following code that prints a string, a variable, another string, and an integer together:

```python
month = "September"
print("Investigate failed login attempts during", month, "if more than", 100)
```

```
Investigate failed login attempts during September if more than 100
```

## type() 

The <var>type()</var> function returns the data type of its argument. The <var>type()</var> function helps you keep track of the data types of variables to avoid errors throughout your code.

To use it, you pass the object as an argument, and it returns its data type. It only accepts one argument. For example, you could specify <var>type("security")</var> or <var>type(7)</var>.

### Passing one function into another

When working with functions, you often need to pass them through <var>print()</var> if you want to output the data type to the screen. This is the case when using a function like <var>type()</var>. Consider the following code:

```python
print(type("This is a string"))
```

```
<class 'str'>
```

It displays <var>str</var>, which means that the argument passed to the <var>type()</var> function is a string. This happens because the <var>type()</var> function is processed first and its output is passed as an argument to the <var>print()</var> function.

## max() and min() 

The <var>max()</var> function returns the largest numeric input passed into it. The <var>min()</var> function returns the smallest numeric input passed into it.

The <var>max()</var> and <var>min()</var> functions accept arguments of either multiple numeric values or of an iterable like a list, and they return the largest or smallest value respectively.

In a cybersecurity context, you could use these functions to identify the longest or shortest session that a user logged in for. If a specific user logged in seven times during a week, and you stored their access times in minutes in a list, you can use the <var>max()</var> and <var>min()</var> functions to find and print their longest and shortest sessions:

```python
time_list = [12, 2, 32, 19, 57, 22, 14]
print(min(time_list))
print(max(time_list))
```

```
2
57
```

## sorted()

The <var>sorted()</var> function sorts the components of a list. The <var>sorted()</var> function also works on any iterable, like a string, and returns the sorted elements in a list. By default, it sorts them in ascending order. When given an iterable that contains numbers, it sorts them from smallest to largest; this includes iterables that contain numeric data as well as iterables that contain string data beginning with numbers. An iterable that contains strings that begin with alphabetic characters will be sorted alphabetically.

The <var>sorted()</var> function takes an iterable, like a list or a string, as an input. So, for example, you can use the following code to sort the list of login sessions from shortest to longest:

```python
time_list = [12, 2, 32, 19, 57, 22, 14]
print(sorted(time_list))
```

```
[2, 12, 14, 19, 22, 32, 57]
```

This displays the sorted list.

The <var>sorted()</var> function does not change the iterable that it sorts. The following code illustrates this:

```python
time_list = [12, 2, 32, 19, 57, 22, 14]
print(sorted(time_list))
print(time_list)
```

```
[2, 12, 14, 19, 22, 32, 57]
[12, 2, 32, 19, 57, 22, 14]
```

The first <var>print()</var> function displays the sorted list. However, the second <var>print()</var> function, which does not include the <var>sorted()</var> function, displays the list as assigned to <var>time\_list</var> in the first line of code.

One more important detail about the <var>sorted()</var> function is that it cannot take lists or strings that have elements of more than one data type. For example, you can’t use the list <var>\[1, 2, "hello"\]</var>.

## Key takeaways

Built-in functions are powerful tools in Python that allow you to perform tasks with one simple command. The <var>print()</var> function prints its arguments to the screen, the <var>type()</var> function returns the data type of its argument, the <var>min()</var> and <var>max()</var> functions return the smallest and largest values of an iterable respectively, and <var>sorted()</var> organizes its argument.

## Resources for more information

These were just a few of Python's built-in functions. You can continue learning about others on your own:

- [The Python Standard Library documentation<svg aria-labelledby="cds-react-aria-643-title" class="css-1lzqdox" fill="none" focusable="false" height="16" id="bkmrk--1" role="img" viewbox="0 0 16 16" width="16"></svg>](https://docs.python.org/3/library/functions.html)

: A list of Python’s built-in functions and information on how to use them