3. Functions#

Functions are nothing new to you - we have used them before!

  • print

  • type

  • dict

  • list

A function is a piece of reusable code that is suited for a particular task, that can be reused. Python has thousands of functions built in, as well as across different packages we will come to in the future.

3.1. Examples of functions#

How do we use functions? With the following general approach:

output = function_name(work_on_this, more_arguments)

Two built in functions we can play with are min and max.

# Example
scores = [2.39, 1.12, 5.21, 8.92, 1.24]

# Find the range by using min and max
lowest = min(scores)
highest = max(scores)

# Compute the span
print(highest - lowest)
7.8

max and min are really simple functions, with a single input. Sometimes functions have more than one input, or argument. More than one argument is entered by separating with a ,.

A common data analysis function is round, which rounds numbers to a degree of precision set by you. Its first input is a value you want to round, and the second is the level of precision you want.

# Multi-argument function example
value = 3.193849291

# Use round 
rounded = round(value, 3)

print(rounded)
3.194

3.1.1. Default arguments#

Functions often have what are known as default arguments. What happens if we just give round a single argument, the number we want to round?

# Testing round
print(round(value))
3

Python has made a guess about what you want - rounded to a whole number. That is, the second argument has a default value - zero.

3.2. help!#

If you ever get confused about what a function does, or what arguments it requires, you can ask for help. help is a function whose job is to give you a print out of how a function works. What does it say about round?

# Get help on round
help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.
    
    The return value is an integer if ndigits is omitted or None.  Otherwise
    the return value has the same type as the number.  ndigits may be negative.

3.3. Methods#

We’ve seen methods before - the special functions that a list can use to make alterations to itself.

But methods are everywhere, and depend on the variable in question. You can use help on a specific data type to see what methods it has.

string data has a range of useful methods that make working with text data simple.

For example, we can use:

  • .upper()

  • .lower()

  • .count()

  • .split()

  • .join()

for various exciting possibilities.

# Define a string
basic = 'We are going to play with this!'
# How does .upper() work?
print(basic.upper())
WE ARE GOING TO PLAY WITH THIS!
# So lower should...
print(basic.lower())
we are going to play with this!
# How many 'a's in this sentence?
print(basic.count('a'))
2
# Chop this up by the number of space
print(basic.split(' '))
['We', 'are', 'going', 'to', 'play', 'with', 'this!']
# Join is cool!
split_list = basic.split(' ')
print('!'.join(split_list))
We!are!going!to!play!with!this!