This tutorial will cover:
This tutorial is based on:
DEMO-ONLY TUTORIAL BEGINS HERE
We’re going to be using a lot of functions from Python data libraries, so lets summarise their key facts:
Functions are like small Python scripts that we can call to perform some operation or return some value.
Functions are called using parentheses, with arguments inside the parentheses.
print('hello')
Using a function’s name without parentheses refers to the function itself as a value:
print
Functions attached to values are called methods
'hello'.upper()
Some functions take multiple arguments
round(3.14159, 2)
Functions may have default values for some arguments
round()
defaults to rounding a number to zero decimal
places:
round(3.14159)
Every function returns a value
Even functions that don’t seem to return a value (like
print
) actually return a special value called
None
(similar to null
in SQL or other
languages):
returned_value = print('hello')
returned_value
type(returned_value)
Functions accept named arguments (and some require it):
round(3.14159, ndigits=2)
Use the built-in function help to get help for a function
help(round)
help('hello'.upper)
Use dir()
and dir(__builtins__)
to
list available functions (and non-function variables)
dir()
dir(__builtins__)
Don’t worry about the underscored names
Use dir(some_value)
to list methods (and
non-function variables called attributes) of a value
dir('hello')
FOLLOW-ALONG TUTORIAL BEGINS HERE
scores = [95, 89, 64, 91]
print(scores)
Like strings, we can get the length of a list:
len(scores)
Also similar to strings, we can use positional indexing and slicing to get specific elements out of a list:
print(scores[0])
print(scores[-1])
print(scores[1:2])
Unlike strings, we can replace the value at a specific index:
scores[2] = 99
print(scores)
We can append an element to the end of a list with the
.append()
method:
scores.append(75)
print(scores)
One of the most useful things we can do with lists is to loop over a list and execute some code for each elment:
for score in scores:
print('Score:', score)
print('Score percent:', score / 100)
Pay careful attention to the colon at the end of the first line, and the indented line(s) after
user = {'first_name': 'Bob', 'last_name': 'Smith'}
print(user)
Dictionary keys are most commonly strings, but many other data types can also be used.
Similar to strings and lists, we can use len()
to count
the number of key/value pairs in a dictionary:
len(user)
Instead of indexing values within a dictionary by their positional index, we index them by their associated key:
user['first_name']
Similar to lists, we can update the value for a given key:
user['first_name'] = 'Alice'
print(user)
We can also insert new key/value pairs by simply “updating” a key that doesn’t already exist in the dictionary:
user['middle_name'] = 'Mallory'
print(user)
We can also remove an item from the dictionary with the
del
statement:
del user['middle_name']
print(user)
Looping over a dictionary will loop over its keys:
for key in user:
print(key)
It is more common to use .items()
to loop over each
key/value pair:
for key, value in user.items():
print(key, value)
Extra: We can also loop over the keys and values with
.keys()
and.values()
respectively. These methods return list-like iterables that can be looped over, and can also be turned into proper lists by wrapping them with thelist()
function.
user['first_name'] = user['first_name'].lower()
user['last_name'] = user['last_name'].lower()
text_keys = ['first_name', 'last_name']
for text_key in text_keys:
user[text_key] = user[text_key].str.lower()
names = ['Alice', 'Bob', 'Mallory']
name_lengths = []
for name in names:
name_lengths.append(len(name))
name_lengths
Python’s list comprehensions provide a more succinct and idiomatic way to do this:
names = ['Alice', 'Bob', 'Mallory']
name_lengths = [len(name) for name in names]
name_lengths
List comprehensions are particularly useful when we don’t even want to keep the list itself in a variable, but just to use it as an intermediate value in some computation:
names = ['Alice', 'Bob', 'Mallory']
max_name_length = max([len(name) for name in names])
max_name_length
List comprehensions can also be used to filter out items from a list:
longest_names = [
name for name in names
if len(name) == max_name_length
]
longest_names