π¬ Receive new lessons straight to your inbox (once a month) and join 40K+
developers in learning how to responsibly deliver value with ML.
Variables
Variables are containers for holding data and they're defined by a name and value.
1234
# Integer variablex=5print(x)print(type(x))
5
<class 'int'>
Here we use the variable name x in our examples but when you're working on a specific task, be sure to be explicit (ex. first_name) when creating variables (applies to functions, classes, etc. as well).
We can change the value of a variable by simply assigning a new value to it.
1234
# String variablex="hello"print(x)print(type(x))
hello
<class 'str'>
There are many different types of variables: integers, floats, strings, boolean etc.
123
# int variablex=5print(x,type(x))
5 <class 'int'>
123
# float variablex=5.0print(x,type(x))
5.0 <class 'float'>
123
# text variablex="5"print(x,type(x))
5 <class 'str'>
123
# boolean variablex=Trueprint(x,type(x))
True <class 'bool'>
We can also do operations with variables:
12345
# Variables can be used with each othera=1b=2c=a+bprint(c)
3
Know your types!
We should always know what types of variables we're dealing with so we can do the right operations with them. Here's a common mistake that can happen if we're using the wrong variable type.
1234
# int variablesa=5b=3print(a+b)
Show answer
8
1234
# string variablesa="5"b="3"print(a+b)
Show answer
53
Lists
Lists are an ordered, mutable (changeable) collection of values that are comma separated and enclosed by square brackets. A list can be comprised of many different types of variables. Below is a list with an integer, string and a float:
123
# Creating a listx=[3,"hello",1.2]print(x)
[3, 'hello', 1.2]
12
# Length of a listlen(x)
3
We can add to a list by using the append function:
1234
# Adding to a listx.append(7)print(x)print(len(x))
[3, 'hello', 1.2, 7]
4
and just as easily replace existing items:
123
# Replacing items in a listx[1]="bye"print(x)
[3, 'bye', 1.2, 7]
and perform operations with lists:
1234
# Operationsy=[2.4,"world"]z=x+yprint(z)
[3, 'bye', 1.2, 7, 2.4, 'world']
Tuples
Tuples are collections that are ordered and immutable (unchangeable). We will use tuples to store values that will never be changed.
123
# Creating a tuplex=(3.0,"hello")# tuples start and end with ()print(x)
(3.0, 'hello')
123
# Adding values to a tuplex=x+(5.6,4)print(x)
(3.0, 'hello', 5.6, 4)
12
# Try to change (it won't work and we get an error)x[0]=1.2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
----> 1 x[0] = 1.2
TypeError: 'tuple' object does not support item assignment
Sets
Sets are collections that are unordered and mutable. However, every item in a set much be unique.
1234
# Setstext="Learn ML with Made With ML"print(set(text))print(set(text.split(" ")))
Indexing and slicing from lists allow us to retrieve specific values within lists. Note that indices can be positive (starting from 0) or negative (-1 and lower, where -1 is the last item in the list).
123456
# Indexingx=[3,"hello",1.2]print("x[0]: ",x[0])print("x[1]: ",x[1])print("x[-1]: ",x[-1])# the last itemprint("x[-2]: ",x[-2])# the second to last item
x[0]: 3
x[1]: hello
x[-1]: 1.2
x[-2]: hello
12345
# Slicingprint("x[:]: ",x[:])# all indicesprint("x[1:]: ",x[1:])# index 1 to the end of the listprint("x[1:2]: ",x[1:2])# index 1 to index 2 (not including index 2)print("x[:-1]: ",x[:-1])# index 0 to last index (not including last index)
Though this does produce results, we should always explicitly use the length of the list to index items from it to avoid incorrect assumptions for downstream processes.
Dictionaries
Dictionaries are an unordered, mutable collection of key-value pairs. You can retrieve values based on the key and a dictionary cannot have two of the same keys.
123456
# Creating a dictionaryperson={"name":"Goku","eye_color":"brown"}print(person)print(person["name"])print(person["eye_color"])
{"name": "Goku", "eye_color": "brown"}
Goku
brown
123
# Changing the value for a keyperson["eye_color"]="green"print(person)
{"name": "Goku", "eye_color": "green"}
123
# Adding new key-value pairsperson["age"]=24print(person)
{"name": "Goku", "eye_color": "green", "age": 24}
12
# Length of a dictionaryprint(len(person))
3
Sort of the structures
See if you can recall and sort out the similarities and differences of the foundational data structures we've seen so far.
Mutable
Ordered
Indexable
Unique
List
β
β
β
β
Tuple
β
β
β
β
Set
β
β
β
β
Dictionary
β
β
β
β
Show answer
Mutable
Ordered
Indexable
Unique
List
β
β
β
β
Tuple
β
β
β
β
Set
β
β
β
β
Dictionary
β
β
β
β keys β values
But of course, there is pretty much a way to do accomplish anything with Python. For example, even though native dictionaries are unordered, we can leverage the OrderedDict data structure to change that (useful if we want to iterate through keys in a certain order, etc.).
1
fromcollectionsimportOrderedDict
123456
# Native dictd={}d["a"]=2d["c"]=3d["b"]=1print(d)
{'a': 2, 'c': 3, 'b': 1}
After Python 3.7+, native dictionaries are insertion ordered.
12
# Dictionary itemsprint(d.items())
dict_items([('a', 2), ('c', 3), ('b', 1)])
12
# Order by keysprint(OrderedDict(sorted(d.items())))
OrderedDict([('a', 2), ('b', 1), ('c', 3)])
12
# Order by valuesprint(OrderedDict(sorted(d.items(),key=lambdax:x[1])))
OrderedDict([('b', 1), ('a', 2), ('c', 3)])
If statements
We can use if statements to conditionally do something. The conditions are defined by the words if, elif (which stands for else if) and else. We can have as many elif statements as we want. The indented code below each condition is the code that will execute if the condition is True.
123456789
# If statementx=4ifx<1:score="low"elifx<=4:# elif = else ifscore="medium"else:score="high"print(score)
medium
1234
# If statement with a booleanx=Trueifx:print("it worked")
it worked
Loops
For loops
A for loop can iterate over a collection of values (lists, tuples, dictionaries, etc.) The indented code is executed for each item in the collection of values.
1234
# For loopveggies=["carrots","broccoli","beans"]forveggieinveggies:print(veggie)
carrots
broccoli
beans
When the loop encounters the break command, the loop will terminate immediately. If there were more items in the list, they will not be processed.
123456
# `break` from a for loopveggies=["carrots","broccoli","beans"]forveggieinveggies:ifveggie=="broccoli":breakprint(veggie)
carrots
When the loop encounters the continue command, the loop will skip all other operations for that item in the list only. If there were more items in the list, the loop will continue normally.
123456
# `continue` to the next iterationveggies=["carrots","broccoli","beans"]forveggieinveggies:ifveggie=="broccoli":continueprint(veggie)
carrots
beans
While loops
A while loop can perform repeatedly as long as a condition is True. We can use continue and break commands in while loops as well.
12345
# While loopx=3whilex>0:x-=1# same as x = x - 1print(x)
2
1
0
List comprehension
We can combine our knowledge of lists and for loops to leverage list comprehensions to create succinct code.
1234567
# For loopx=[1,2,3,4,5]y=[]foriteminx:ifitem>2:y.append(item)print(y)
[3, 4, 5]
123
# List comprehensiony=[itemforiteminxifitem>2]print(y)
[3, 4, 5]
List comprehension for nested for loops
For the nested for loop below, which list comprehension is correct?
12345678
# Nested for loopswords=[["Am","ate","ATOM","apple"],["bE","boy","ball","bloom"]]small_words=[]forletter_listinwords:forwordinletter_list:iflen(word)<3:small_words.append(word.lower())print(small_words)
Python syntax is usually very straight forward, so the correct answer involves just directly copying the statements from the nested for loop from top to bottom!
Functions are a way to modularize reusable pieces of code. They're defined by the keyword def which stands for definition and they can have the following components.
12345
# Define the functiondefadd_two(x):"""Increase x by 2."""x+=2returnx
Here are the components that may be required when we want to use the function. we need to ensure that the function name and the input parameters match with how we defined the function above.
1234
# Use the functionscore=0new_score=add_two(x=score)print(new_score)
2
A function can have as many input parameters and outputs as we want.
12345
# Function with multiple inputsdefjoin_name(first_name,last_name):"""Combine first name and last name."""joined_name=first_name+" "+last_namereturnjoined_name
123456
# Use the functionfirst_name="Goku"last_name="Mohandas"joined_name=join_name(first_name=first_name,last_name=last_name)print(joined_name)
Goku Mohandas
We can be even more explicit with our function definitions by specifying the types of our input and output arguments. We cover this in our documentation lesson because the typing information is automatically leveraged to create very intuitive documentation.
It's good practice to always use keyword argument when using a function so that it's very clear what input variable belongs to what function input parameter. On a related note, you will often see the terms *args and **kwargs which stand for arguments and keyword arguments. You can extract them when they are passed into a function. The significance of the * is that any number of arguments and keyword arguments can be passed into the function.
Classes are object constructors and are a fundamental component of object oriented programming in Python. They are composed of a set of functions that define the class and it's operations.
Magic methods
Classes can be customized with magic methods like __init__ and __str__, to enable powerful operations. These are also known as dunder methods (ex. dunder init), which stands for double underscores due to the leading and trailing underscores.
The __init__ function is used when an instance of the class is initialized.
12345678
# Creating the classclassPet(object):"""Class object for a pet."""def__init__(self,species,name):"""Initialize a Pet."""self.species=speciesself.name=name
12345
# Creating an instance of a classmy_dog=Pet(species="dog",name="Scooby")print(my_dog)print(my_dog.name)
<__main__.Pet object at 0x7fe487e9c358>
Scooby
The print (my_dog) command printed something not so relevant to us. Let's fix that with the __str__ function.
1 2 3 4 5 6 7 8 910111213
# Creating the class# Creating the classclassPet(object):"""Class object for a pet."""def__init__(self,species,name):"""Initialize a Pet."""self.species=speciesself.name=namedef__str__(self):"""Output when printing an instance of a Pet."""returnf"{self.species} named {self.name}"
12345
# Creating an instance of a classmy_dog=Pet(species="dog",name="Scooby")print(my_dog)print(my_dog.name)
dog named Scooby
Scooby
We'll be exploring additional built-in functions in subsequent notebooks (like __len__, __iter__ and __getitem__, etc.) but if you're curious, here is a tutorial on more magic methods.
Object functions
Besides these magic functions, classes can also have object functions.
1 2 3 4 5 6 7 8 910111213141516
# Creating the classclassPet(object):"""Class object for a pet."""def__init__(self,species,name):"""Initialize a Pet."""self.species=speciesself.name=namedef__str__(self):"""Output when printing an instance of a Pet."""returnf"{self.species} named {self.name}"defchange_name(self,new_name):"""Change the name of your Pet."""self.name=new_name
1234
# Creating an instance of a classmy_dog=Pet(species="dog",name="Scooby")print(my_dog)print(my_dog.name)
dog named Scooby
Scooby
1234
# Using a class's functionmy_dog.change_name(new_name="Scrappy")print(my_dog)print(my_dog.name)
dog named Scrappy
Scrappy
Inheritance
We can also build classes on top of one another using inheritance, which allows us to inherit all the properties and methods from another class (the parent).
1234567
classDog(Pet):def__init__(self,name,breed):super().__init__(species="dog",name=name)self.breed=breeddef__str__(self):returnf"A {self.breed} doggo named {self.name}"
Notice how we inherited the initialized variables from the parent Pet class like species and name. We also inherited functions such as change_name().
Which function is executed?
Which function is executed if the parent and child functions have functions with the same name?
Show answer
As you can see, both our parent class (Pet) and the child class (Dog) have different __str__ functions defined but share the same function name. The child class inherits everything from the parent classes but when there is conflict between function names, the child class' functions take precedence and overwrite the parent class' functions.
Methods
There are two important decorator methods to know about when it comes to classes: @classmethod and @staticmethod. We'll learn about decorators in the next section below but these specific methods pertain to classes so we'll cover them here.
1 2 3 4 5 6 7 8 9101112131415
classDog(Pet):def__init__(self,name,breed):super().__init__(species="dog",name=name)self.breed=breeddef__str__(self):returnf"{self.breed} named {self.name}"@classmethoddeffrom_dict(cls,d):returncls(name=d["name"],breed=d["breed"])@staticmethoddefis_cute(breed):returnTrue# all animals are cute!
A @classmethod allows us to create class instances by passing in the uninstantiated class itself (cls). This is a great way to create (or load) classes from objects (ie. dictionaries).
A @staticmethod can be called from an uninstantiated class object so we can do things like this:
12
# Static methodDog.is_cute(breed="Border Collie")
True
Decorators
Recall that functions allow us to modularize code and reuse them. However, we'll often want to add some functionality before or after the main function executes and we may want to do this for many different functions. Instead of adding more code to the original function, we can use decorators!
decorators: augment a function with pre/post-processing. Decorators wrap around the main function and allow us to operate on the inputs and or outputs.
Suppose we have a function called operations which increments the input value x by 1.
Now let's say we want to increment our input x by 1 before and after the operations function executes and, to illustrate this example, let's say the increments have to be separate steps. Here's how we would do it by changing the original code:
We were able to achieve what we want but we now increased the size of our operations function and if we want to do the same incrementing for any other function, we have to add the same code to all of those as well ... not very efficient. To solve this, let's create a decorator called add which increments x by 1 before and after the main function f executes.
Creating a decorator
The decorator function accepts a function f which is the function we wish to wrap around, in our case, it's operations(). The output of the decorator is its wrapper function which receives the arguments and keyword arguments passed to function f.
Inside the wrapper function, we can:
extract the input parameters passed to function f.
make any changes we want to the function inputs.
function f is executed
make any changes to the function outputs
wrapper function returns some value(s), which is what the decorator returns as well since it returns wrapper.
1 2 3 4 5 6 7 8 910
# Decoratordefadd(f):defwrapper(*args,**kwargs):"""Wrapper function for @add."""x=kwargs.pop("x")# .get() if not altering xx+=1# executes before function fx=f(*args,**kwargs,x=x)x+=1# executes after function freturnxreturnwrapper
We can use this decorator by simply adding it to the top of our main function preceded by the @ symbol.
Suppose we wanted to debug and see what function actually executed with operations().
1
operations.__name__,operations.__doc__
('wrapper', 'Wrapper function for @add.')
The function name and docstring are not what we're looking for but it appears this way because the wrapper function is what was executed. In order to fix this, Python offers functools.wraps which carries the main function's metadata.
1
fromfunctoolsimportwraps
1 2 3 4 5 6 7 8 91011
# Decoratordefadd(f):@wraps(f)defwrap(*args,**kwargs):"""Wrapper function for @add."""x=kwargs.pop("x")x+=1x=f(*args,**kwargs,x=x)x+=1returnxreturnwrap
Awesome! We were able to decorate our main function operation() to achieve the customization we wanted without actually altering the function. We can reuse our decorator for other functions that may need the same customization!
This was a dummy example to show how decorators work but we'll be using them heavily during our MLOps lessons. A simple scenario would be using decorators to create uniform JSON responses from each API endpoint without including the bulky code in each endpoint.
Callbacks
Decorators allow for customized operations before and after the main function's execution but what about in between? Suppose we want to conditionally/situationally do some operations. Instead of writing a whole bunch of if-statements and make our functions bulky, we can use callbacks!
callbacks: conditional/situational processing within the function.
Our callbacks will be classes that have functions with key names that will execute at various periods during the main function's execution. The function names are up to us but we need to invoke the same callback functions within our main function.
It seems like we've just done some operations before and after the function's main process? Isn't that what a decorator is for?
Show answer
With callbacks, it's easier to keep track of objects since it's all defined in a separate callback class. It's also now possible to interact with our function, not just before or after but throughout the entire process! Imagine a function with:
multiple processes where we want to execute operations in between them
execute operations repeatedly when loops are involved in functions
Putting it all together
decorators + callbacks = powerful customization before, during and after the main functionβs execution without increasing its complexity. We will be using this duo to create powerful ML training scripts that are highly customizable in future lessons.
1
fromfunctoolsimportwraps
1 2 3 4 5 6 7 8 91011
# Decoratordefadd(f):@wraps(f)defwrap(*args,**kwargs):"""Wrapper function for @add."""x=kwargs.pop("x")# .get() if not altering xx+=1# executes before function fx=f(*args,**kwargs,x=x)# can do things post function f as wellreturnxreturnwrap
# Main function@adddefoperations(x,callbacks=[]):"""Basic operations."""forcallbackincallbacks:callback.at_start(x)x+=1forcallbackincallbacks:callback.at_end(x)returnx