Built-in functions

Intermediate Python for Developers

George Boorman

Curriculum Manager, DataCamp

What we'll cover

  • Functions
    • Custom functions

 

  • Modules

 

 

  • Packages
Intermediate Python for Developers

Functions we know

# Printing
print("Display this as an output")
'Display this as an output'

 

# Checking data types
type(print)
builtin_function_or_method
# Looping through a range of numbers
for num in range(1, 5):
    print(num)
1
2
3
4
Intermediate Python for Developers

max() and min()

sales = [125.97, 84.32, 99.78, 154.21, 78.50, 83.67, 111.13]

# Find the largest sale max(sales)
154.21
# Find the smallest sale
min(sales)
78.5
Intermediate Python for Developers

sum() and round()

sum(sales)
737.5799999999999
# Store total sales
total_sales = sum(sales)

# Round to two decimal places round(total_sales, 2)
737.58
Intermediate Python for Developers

Nested functions

  • Call a function then call another function
# Store total sales
total_sales = sum(sales)

# Round to two decimal places
round(total_sales, 2)
737.58
  • Call a function within a function
# Store total sales
total_sales = round(sum(sales), 2)

# Round to two decimal places print(total_sales)
737.58
Intermediate Python for Developers

len()

  • Counts the number of elements
# Count the number of sales
len(sales)
7
# Calculate average sales
sum(sales) / len(sales)
105.36857142857141
Intermediate Python for Developers

len()

# Length of a string
len("Introduction to Programming for Developers")
42
# Length of dictionary
len({"a": 1, "b": 2, "c": 3})
3
  • Also works with sets and tuples
  • Does not work with floats, integers, or booleans
Intermediate Python for Developers

sorted()

# Sort the sales list in ascending order
sorted(sales)
[78.5, 83.67, 84.32, 99.78, 111.13, 
 125.97, 154.21]
# Sort a string alphabetically
sorted("George")
['G', 'e', 'e', 'g', 'o', 'r']
Intermediate Python for Developers

help()

# Get information about the sorted() function
help(sorted)
Help on built-in function sorted in module builtins:

sorted(iterable, /, *, key=None, reverse=False)
    Return a new list containing all items from the iterable in ascending order.

    A custom key function can be supplied to customize the sort order, and the
    reverse flag can be set to request the result in descending order.
  • Works with int, str, {}, [], list, etc.
Intermediate Python for Developers

Benefits of functions

  • Perform complex tasks with less code
# Find total sales
sum(sales)
737.5799999999999
Intermediate Python for Developers

Benefits of functions

# Find total sales
# Create a variable to increment
sales_count = 0

# Loop through sales for sale in sales:
# Increment sales_count by each sale sales_count += sale
print(sales_count)
  • sum() is reusable, shorter, cleaner, and less prone to errors!
125.97
210.29
310.07
464.28
542.78
626.4499999999999
737.5799999999999
Intermediate Python for Developers

Functions cheat sheet

Function Returns
print() Display an output, e.g., variable's values
max() Find the largest value in a data structure
min() Find the smallest value in a data structure
sum() Add up all elements in a data structure
round() Trim a float to a specified number of decimal places
len() Count the number of elements in a data structure
sorted() Sort elements in a data structure in ascending order
help() Get information about a function, variable, or value
1 https://docs.python.org/3/library/functions.html
Intermediate Python for Developers

Let's practice!

Intermediate Python for Developers

Preparing Video For Download...