Intermediate Python For Developers
Intermediate Python For Developers
Transcript
1. Built-in functions
00:00 - 00:03
Welcome! I'm George, and I'll be your host!
2. What we'll cover
00:03 - 00:16
In this course, we'll dive into fundamental programming topics including built-in
functions, modules, and packages, before progressing to building our own custom
functions!
3. Functions we know
00:16 - 00:36
Let's recap the functions we already know. First there's print, which displays outputs.
There's type, which tells us what a variable, value, or function is. We also know
range, which can be used with a for loop to generate numbers.
4. max() and min()
00:36 - 01:04
Software and data engineers often work with numbers. Luckily, Python has lots of
useful built-in numeric functions to make our lives easier! Here, we have a list of
sales transactions. If we want to find the largest transaction, we can call the max
function, passing our list. Likewise, to get the smallest transaction, we use the min
function.
5. sum() and round()
01:04 - 01:30
To find the total number of sales, we use the sum function. That's a lot of decimal
places, let's round this! We store the calculation as a variable called total_sales. We
then use the round function, providing the value to be rounded followed by the
number of decimals to round to - in this case, two.
6. Nested functions
01:30 - 02:07
Notice that we called sum, stored the result as a variable, and then rounded that
variable. However, it is possible to call a function inside a function instead. Here, we
call the round function, inside which we calculate total sales by calling the sum
function. We close the parentheses for the sum function, then add a comma, after
which we include the number two representing how many digits to round the result
to. Printing the results confirms the same result.
7. len()
02:07 - 02:35
Another useful function is len, which counts the number of elements. Calling len on
our sales list confirms there are seven transactions. We now have the tools to
perform other calculations like the average or mean. We can get the sum of sales
and divide it by the number of transactions, producing a mean of around 105 dollars.
8. len()
02:35 - 03:07
We can use len on other data types, such as strings. It counts the number of
characters, including spaces. This could be useful if we need to enforce rules around
the number of characters in a password for a website. We can also use it to find the
number of key-value pairs in a dictionary. It will also work with sets and tuples but
isn't compatible with integers, floats, or booleans.
9. sorted()
03:07 - 03:31
Another useful function is sorted, which sorts a variable in ascending order. Here, we
use it on our sales list as well as on a string. Notice that the string "George" gets
sorted by uppercase characters alphabetically first, followed by lowercase
characters.
10. help()
03:31 - 04:06
If we want to know more about a function, then we can call the help function and
pass that function, like here for sorted. Help provides information about how to use a
function, such as what information we need to provide to it. In this case, it tells us we
provide an iterable and mentions we can provide values to arguments called key and
reverse, which will change the output. We can also call the help function on data
types and structures!
11. Benefits of functions
04:06 - 04:19
Functions allow us to perform complex tasks with less code. Let's demonstrate this
by looking at how to find the sum of a list of values if the sum function didn't exist.
12. Benefits of functions
04:19 - 04:55
We would have to create a variable to store our calculation, starting at zero. We then
loop through sales and, with each iteration, add each sale to the sales_count
variable. Printing the variable during each loop confirms the same final result, but it
took way more code! The sum function is reusable, as we just change the values
that we pass to it. It's also much shorter, cleaner, and less prone to mistakes.
13. Functions cheat sheet
04:55 - 05:05
This cheat sheet summarizes the functions we've just covered. Python has lots of
additional built-in functions, which you can access from the URL in the slide.
1. 1
https://docs.python.org/3/library/functions.html
One of them can be used to count the number of elements in a variable. You'll see three
variables in the following steps:
course_ratings - a dictionary variable containing course names as keys and average
ratings as values.
course_completions - a list variable containing the daily number of completions for an
individual course.
most_popular_course - a string variable containing the name of a course.
Instructions 1/3
o Use a function to count the number of key-value pairs in course_ratings, storing as a variable
called num_courses, then print the variable.
1. Modules
00:00 - 00:09
We know how useful Python's built-in functions are. Now let's look at another helpful
component of the Python ecosystem - modules.
2. What are modules?
00:09 - 00:38
Modules are Python scripts, meaning they are files ending with the .py extension.
Modules contain functions and attributes - more on those later. They can also
contain other modules! Python comes with several modules that are available for us
to use. As with built-in functions, modules help us avoid writing code that already
exists!
3. Python modules
00:38 - 01:25
There are around 200 built-in modules available to us! Popular modules include os,
which can be used to interact with your operating system, such as finding the current
working directory. There's the collections module, which offers a variety of advanced
data structures. String is a module for performing string operations, while logging can
be used to log information about a program during testing or production. There's also
subprocess, which, among many things, enables us to run terminal commands from
within a Python file. The full list of modules can be accessed from the link in the
slide.
4. Importing a module
01:25 - 01:46
If we want to use code from a module then we need to import it. To do this, we use
the import keyword followed by the name of the module. Here, we import the os
module. Checking the type confirms that os is a module.
5. Finding a module's functions
01:46 - 02:09
We have the module imported, but how do we know what functions are available?
The easiest approach is to look at the documentation - the URL is displayed in the
slide. We can also use the help function, but be warned, this will return a large output
and likely contain lots of information we don't need!
1. 1
https://docs.python.org/3/library/os.html#module-os
Transcript
1. Packages
00:00 - 00:07
Python's built-in modules are extensive, but if they don't offer what we need, we
have another option.
2. Modules are Python files
00:07 - 00:17
Remember that a module is a single Python file. This means that, while modules
come built into Python, anyone can make their own Python file!
1. 1
Image source: https://unsplash.com/@jstrippa
3. Packages
00:17 - 01:05
This is exactly what many developers have done, and they've even gone a step
further by creating collections of files. These collections are known as packages. We
might also hear them referred to as libraries. Packages are publicly available, free of
charge! We can use packages in the same way as Python's built-in modules, with
one extra step. To access a package, we first need to download it from the Python
Package Index, known as PyPI, which is essentially a directory of packages.
Afterwards, we can import the package, or parts of a package, using the same
approach as we took when working with modules.
1. 1
https://pypi.org/
4. Installing a package
01:05 - 01:44
To download a package from PyPI we'll need to open a terminal in MacOS or
Command Prompt in Windows. A terminal is a program that allows us to run
commands on our computer. Once we've opened a terminal we type the command
python3 -m pip install, followed by the package name, and press Return. Python3 is
used to execute Python code from the terminal, while pip stands for preferred
installer program, a tool used to install Python packages.
5. Installing a package
01:44 - 01:58
Let's look at a popular package for working with data called pandas, which is used
for data cleaning, manipulation, and analysis! We can install pandas using the
following command.
6. Importing with an alias
01:58 - 02:35
We can now use pandas by importing it, just like we did with the os module.
However, when using pandas functions, it would be great if we didn't have to write
pandas-dot every time. It's common to use an alias when importing some packages,
including pandas. To assign an alias, we import pandas, adding the as keyword
followed by our alias name. The convention is to name it pd, helping us shorten our
code.
7. Creating a DataFrame
02:35 - 03:20
Say we have a dictionary containing sales data. It has user_id and order_value keys,
each containing lists with respective values. We can use pandas to turn this into
tabular data, similar to what we see in a spreadsheet. To do this, we use the pandas
DataFrame function, writing pd-dot before we call it because we have imported
pandas under the alias of pd. Outputting the variable shows the data is now
organized as a table, with each row showing the user_id and their order value.
8. Reading in a CSV file
03:20 - 03:42
We might have our data saved in a CSV file and want to read it into Python as a
pandas DataFrame. To do this, we call the pd.read_csv function, passing the file
name as a string. Checking the type confirms it is a pandas DataFrame.
9. Previewing the file
03:42 - 04:10
If there are many rows in our data, we might not want to look at all of them. Instead,
we could preview the first five rows. pandas DataFrames have a method for this. We
call the sales_df.head method, and the first five rows are displayed! As pandas is so
widely used, Datacamp has several courses teaching how to use this package!
10. Functions versus methods
04:10 - 04:26
We've discussed functions and methods, but let's recap on how they differ. A
function is called to perform a task. A method is a function that is specific to a data
type.
11. Functions versus methods
04:26 - 05:00
Functions and methods appear similar, so let's distinguish them. Function examples
include the built-in sum function or the pandas function called DataFrame, where we
use the syntax of package name-dot-function. For methods, dot-head is a method
that is specific to a data type, in this case a pandas DataFrame. Therefore, it won't
work if we try to use it with other data types like lists.
12. Let's practice!
05:00 - 05:05
Time to practice working with packages!
Working with pandas
pandas is an example of a popular Python package.
In this exercise, the sales dictionary has been created and made available to you, and your task
is to convert it into a pandas DataFrame and preview the first five rows.
Instructions
100 XP
Transcript
Welcome back! Functions from Python's modules and packages are useful, but what
if they still don't give us the functionality that we need?
2. Calculating the average
00:10 - 00:26
An example is calculating the average when working with a list. We need to add up
the values and then divide by the number of elements. To make the results prettier
we can round the results to two decimal places.
3. When to make a custom function
00:26 - 00:54
If we can't find a module or package that provides the function that we need then we
can make our own custom functions! We should consider this approach if we would
be required to repeatedly write many lines of code, use complex syntax, and would
likely perform the task that this code would solve regularly. One common guidance is
"Don't repeat yourself", usually abbreviated to DRY.
4. Creating a custom function
00:54 - 01:01
To make a function we start with the def statement, which means "define".
5. Creating a custom function
01:01 - 01:13
We then provide the name of our function. It can have any name, but we should
make it descriptive yet concise, similar to how we think about naming variables.
6. Creating a custom function
01:13 - 01:16
Inside, we provide the value or data structure that we want the function to be
performed on, then close the parentheses. We can give this any name, so again we
should be descriptive. The item we place inside a function is known as an argument
and we'll learn more about this term later in the course.
8. Creating a custom function
01:40 - 01:43
The code inside the function is indented, and we can write as many lines as we like.
We start by calculating the average of the variable provided to the function, values.
10. Creating a custom function
01:58 - 02:04
Our task is complete, but we should note that any variables created in a function do
not automatically exist outside of it. If we don't need any outputs then we can finish
our function there. However, as we need an output in this case, we can accomplish
this by using the return keyword. This tells Python what we want to produce as an
output.
12. Creating a custom function
02:32 - 02:38
Let's use the average function on our sales variable. We call our average function,
inside which we pass the sales list. The output is as expected but we can now
complete this task in a single line of code, regardless of which variable we are
working with!
15. Storing a function's output
03:12 - 03:46
While calling our function returns an output, we might want to store this information
to use elsewhere in a program. As we've done with built-in functions, we can assign
a variable equal to a call of a function, which will store the result of the function as
the variable's value. Here, we create a variable called average-sales that is equal to
the call of average on the sales variable. Printing the variable confirms the same
value.
16. Let's practice!
03:46 - 03:49
In this exercise, you'll create a function that takes string data and:
Now we know how to build custom functions, let's look at how to extend their
capabilities!
2. Average
00:06 - 00:16
Recall our average function. We briefly discussed that the variable, values, provided
inside parentheses, was an argument.
3. Arguments
00:16 - 00:34
An argument might sound like a bad thing, but in a Python function or method, it
refers to a value or data structure that is provided. There are two types of arguments
for functions and methods, positional and keyword arguments.
4. Positional arguments
00:34 - 01:11
We follow with a comma, then set the ndigits keyword argument equal to two. We
get the same result!
7. Identifying keyword arguments
02:03 - 02:15
But how do we know what these keywords are? We can use the help function to get
this information! Let's look at the output of calling help on the round function.
8. Keyword arguments
02:15 - 02:31
We can see the first argument is called number, which is the keyword for this
argument. The second keyword argument is called ndigits. Notice that ndigits is set
equal to None within the output?
9. Default arguments
02:31 - 03:03
Default arguments are helpful as they force us to think about how people will use our
functions. If we expect an argument to have a specific value the majority of the time
then we can set it as a default argument. It potentially reduces the amount of code
required to run the function, if the default argument is not modified. Conversely, it
maintains flexibility whereby users can overwrite the default value to return different
results when calling a function!
11. Adding an argument
03:38 - 03:44
With this new knowledge, let's modify our previous average function, shown here.
12. Adding an argument
03:44 - 03:52
We add a keyword argument called rounded, with a default boolean value of False.
13. Adding an argument
03:52 - 04:05
Inside the function, we check if rounded has been changed to True. If so, we
calculate the average, round it to two decimal places, and then return this value.
14. Adding an argument
04:05 - 04:11
Using our sales data from earlier, let's test our function.
16. Using the modified average() function
04:16 - 04:36
We can call the function using positional arguments, providing sales, then the
boolean value of False so the results are not rounded. Alternatively, as the default
value of rounded is False, we can omit the rounded argument and the results will be
the same!
17. Using the modified average() function
04:36 - 04:51
Likewise, we can use keyword arguments. Here, we set values equal to sales and
rounded equal to True. This returns the average sales value rounded to two decimal
places.
18. Let's practice!
04:51 - 04:58
Hopefully, now you can see the flexibility and potential of custom functions!
# Convert to lowercase
clean_text = no_spaces.lower()
Instructions
o Define clean_text, which has two arguments: text, and lower, with the latter having a default
value of True.
# Define clean_text
def clean_text(text, lower =True):
if lower == False:
clean_text = text.replace(" ", "_")
return clean_text
else:
clean_text = text.replace(" ", "_")
clean_text = clean_text.lower()
return clean_text
Re-define clean_text with arguments of text followed by remove, with the latter having a
default value of None.
# Define clean_text
def clean_text(text,remove=None):
if remove != None:
clean_text = text.replace(remove, "")
clean_text = clean_text.lower()
return clean_text
else:
clean_text = text.lower()
return clean_text
Data structure converter function
Now you've learned about the types of arguments in functions, you'll put this into practice by
building a custom function that converts data into different structures.
Instructions
0 XP
Define convert_data_structure with two arguments: data and data_type, where the
latter has a default value of "list".
Add a condition to check if data_type is "tuple".
Else if data_type is "set", convert data into a set, saving it as a variable of the same
name.
Call the function on the data structure provided, using an appropriate keyword argument
value-pair to convert it to a set.
# Create the convert_data_structure function
def convert_data_structure(data, data_type="list"):
# If tuple, convert to a tuple
if data_type == "tuple":
data = tuple(data)
# Else if set, convert to a set
elif data_type == "set":
data = set(data)
else:
data = list(data)
return data
# Call the function
convert_data_structure({"a", 1, "b", 2, "c", 3}, data_typ
e="set")
1. Docstrings
00:00 - 00:18
Custom functions, when designed well, can be fairly intuitive. But it's great to make
things easy for ourselves and others to understand how to use our functions. With
that in mind, let's look at one more important element of custom functions:
docstrings.
2. Docstrings
00:18 - 00:44
So, we can get information about a function, including its docstring, by calling help.
4. Accessing a docstring
00:51 - 00:57
However, if we just want the docstring, then we write the function name,
5. Accessing a docstring
00:57 - 00:59
add a dot,
6. Accessing a docstring
00:59 - 01:01
and finish with two more underscores. Two sets of double underscores are referred
to as "dunder" in programming. In this case, we access the "dunder-doc" attribute of
the round function.
9. Accessing a docstring
01:23 - 01:46
The output shows the same information as the help function, except it has some
strange characters, a backslash followed by "n". This is actually code representing a
new line, which is why when we compare this to the output of help there are
sentences split across separate lines.
10. Creating a docstring
01:46 - 02:10
Let's add a docstring to the average function that we created earlier. To add a
docstring, we use triple quotation marks directly after defining the function. Here, we
provide a brief description, which is known as a one-line docstring. Just like any
other content within the function body, the docstring must be indented.
11. Accessing the docstring
02:10 - 02:17
We can also use a multi-line docstring to provide more information, which is useful if
the function is more complex or has lots of arguments.
14. Multi-line docstring
02:43 - 02:53
We keep the summary line at the start, leave a blank line, and then write Args,
representing the function's arguments.
15. Multi-line docstring
02:53 - 03:07
We then indent the next line and write the argument name, put its data type in
brackets, and add a brief description. We can repeat this if we have more
arguments.
16. Multi-line docstring
03:07 - 03:17
We then write Returns, and again indent. Here, we describe the variable that is
returned, including its data type.
17. Accessing the docstring
03:17 - 03:23
Calling help shows our full docstring including the blank line formatting.
18. Let's practice!
03:23 - 03:27
Multi-line docstrings
Sometimes single-line docstrings are sufficient, but if your function is more complex or has
several arguments, then it is generally a better choice to include a multi-line docstring.
You'll practice this by creating a multi-line docstring for the convert_data_structure function
you made earlier.
Instructions 3/3
Detail the Returns: section: data (list, tuple, or set): Converted data
structure.
# Create the convert_data_type function
def convert_data_structure(data, data_type="list"):
# Add a multi-line docstring
"""
Convert a data structure to a list, tuple, or set.
Args:
data (list, tuple, or set): A data structure to be co
nverted.
data_type (str): String representing the type of stru
cture to convert data to.
Returns:
data (list, tuple, or set): Converted data structure.
"""
if data_type == "tuple":
data = tuple(data)
elif data_type == "set":
data = set(data)
else:
data = list(data)
return data
print(help(convert_data_structure))
1. Arbitrary arguments
00:00 - 00:10
Let's finish up by discussing two techniques for adding flexibility to custom functions:
arbitrary positional arguments and arbitrary keyword arguments.
2. Limitations of defined arguments
00:10 - 01:11
Let's start by re-examining our custom average function. Notice that it expects one
argument called values? The way we have designed this function means that the
intended data structure is a list or set, so if we tried to provide another structure such
as a dictionary it wouldn't work. This means we need to think about the data
structure we need as an input to the function and the number of arguments the
function should accept. So far, we only accept one argument for the average function
that we created, which we presume to be a list or set. But what would happen if a
developer tries to pass more than one argument presuming that the function will still
calculate their average? Assume, for example, that they enter six individual integer
values separated by commas. We'll get an error because we've provided too many
arguments!
3. Arbitrary positional arguments
01:11 - 02:01
While docstrings help clarify how to use a function, Python provides other ways to
support developers in using our custom function. With a slight tweak to our function,
we can make it accept what are called arbitrary arguments, resulting in a function
that works with any number of positional, non-keyword arguments! We can now
provide one, five, or even one thousand arguments! To do this, we place an asterisk
in front of a single argument. The convention is to write asterisk-args, but any
argument name will work. This flexibility enables functions to be used in a variety of
ways while still producing the expected results!
4. Using arbitrary positional arguments
02:01 - 02:12
Now, calling the function with six comma-separated integer values produces the
correct output of 13-point-33.
5. Args create a single iterable
02:12 - 02:43
To Python, the asterisk means combining all positional arguments, so it places them
all inside a tuple. If we have lots of data across different structures, such as lists, and
want to find the average without needing to combine them into a single variable, we
can place an asterisk in front of each argument. The output is the same as, again, all
arguments are combined into a single structure.
6. Arbitrary keyword arguments
02:43 - 03:28
We can also use arbitrary keyword arguments by adding two asterisks instead of
one. The term generally used is kwargs, but any word will work. Remember the
general syntax of keyword argument equals value. This is equivalent to the key-
value pairs that we've seen in dictionaries. To enable this, we first modify our
function to accept arbitrary keyword arguments, then we need to change the code in
the body of the function to presume dictionaries will be used, now working with the
dot-values method of our kwargs.
7. Using arbitrary keyword arguments
03:28 - 04:18
As with args, we can provide multiple comma separated values. Except with kwargs
we must give each argument a name and assign a value, like so. The function
combines the keyword argument names and values into a single dictionary and
calculates the average of the values. We can also call average with a single keyword
argument in the form of a dictionary by using two asterisks in front. With the first
approach we manually assigned keyword argument names and values. In the
second approach using double asterisks, Python takes each dictionary key as a
keyword argument and each value as the assigned value.
8. Kwargs create a single iterable
04:18 - 04:33
As with args, we can split into multiple kwargs and use pairs of asterisks in front of
each argument, which again combines into a single iterable and provides the same
result!
9. Let's practice!
04:33 - 04:38
Using this power, you'll build a function that concatenates (joins together) strings, regardless of
how many blocks of text are provided!
Instructions
70 XP
Define a function called concat() that accepts arbitrary arguments called args.
Create a variable called result and assign an empty string to it.
Use a for loop to iterate over each arg in args.
Call the function to test that it works correctly.
# Define a function called concat
def concat(*args):
# Create an empty string
result = ""
# Iterate over the Python args tuple
for arg in args:
result += " " + arg
return result
# Call the function
print(concat("Python", "is", "great!"))
Arbitrary keyword arguments
Arbitrary positional arguments are one way to add flexibility when creating custom functions, but
you can also use arbitrary keyword arguments.
Your goal is to take the concat function that you created in the last exercise and modify it to
accept arbitrary keyword arguments. Good luck!
Instructions
0 XP
Define concat() as a function that accepts arbitrary keyword arguments called kwargs.
Inside the function, create an empty string.
Inside the function, loop over the keyword argument values, using kwarg as the iterator.
Call concat() with keyword arguments of start equal to "Python", middle equal to "is",
and end equal to "great!".
# Define a function called concat
def concat(**kwargs):
# Create an empty string
result = ""
# Iterate over the Python kwargs
for kwarg in kwargs.values():
result += " " + kwarg
return result
# Call the function
print(concat(start="Python", middle="is", end="great!"))
1. Lambda functions
00:00 - 00:10
We've worked with custom functions. Now, let's explore an approach that offers
flexibility without the need to build a complete function from scratch.
2. Simple functions
00:10 - 00:34
Custom functions are great, but can require a lot of code! As an example, this simple
function calculating the average from a sequence of values requires three lines of
code - one to define the function, one for the function body, and another to return an
output. However, there is a quicker way to achieve the same result!
3. Lambda functions
00:34 - 00:46
We can use the lambda keyword to create an anonymous function, which doesn't
require a name or need to be saved as a variable. The general syntax is lambda,
4. Lambda functions
00:46 - 00:49
a colon,
6. Lambda functions
00:50 - 01:18
and an expression. The convention is to use x for a single argument, although any
word will work. The expression is the equivalent of the function body. Also, no return
statement is required to produce an output! Lastly, while lambda functions can be
used anonymously, storing and then calling them is also possible. Let's explore both
approaches.
7. Creating a lambda function
01:18 - 01:36
Here, we write a lambda function to find the average value. It shows an output
pointing to a lambda function. This is the equivalent of defining a custom function
and displaying it without calling the function and providing an argument.
8. Using lambda functions
01:36 - 01:40
If we intend to use the lambda function more than once, we could store it as a
variable, like here. We can then call the variable, average, like a regular function,
and provide the list representing x. The result is the same!
11. Multiple parameters
02:11 - 02:30
We can extend lambda functions in a couple of ways. We can use more than one
argument, such as raising x to the power of y. The values of two and three represent
x and y respectively, producing an output of eight.
12. Lambda functions with iterables
02:30 - 03:30
We can use lambda functions to perform actions on values within an iterable, such
as a list. To do this, we need to use lambda inside Python's built-in map function,
which applies a function to the iterable we provide. Let's use this approach to
capitalize every value in a list called names. We call map, first providing the lambda
function which uses the string.capitalize method, followed by the iterable to apply the
function to, names. Printing this shows a map object pointing to the memory location
where this function is stored. To produce an output, we need to convert it to a data
structure. As we are working with a list, we do this by calling the list function and
providing our capitalize function inside. It returns all values in the names list with
capitalized first letters!
13. Custom vs. lambda functions
03:30 - 03:51
So when should we use lambda functions over custom functions? If the function is
complex, such as using if-else syntax, or will be used many times, we should make a
custom one. In contrast, if we need to do something once or it's relatively simple, we
should use lambda.
14. Let's practice!
03:51 - 03:55
Adding tax
Time to test out your lambda function skills!
In this exercise, you'll use a lambda function to add a tax of 20% to the cost of
the sale_price variable.
Instructions
Define the add_tax lambda function to multiply the argument provided to it, x, by 1.2.
Call add_tax() on the sale_price variable.
sale_price = 29.99
# Define a lambda function called add_tax
add_tax = (lambda x: x*1.2)
# Call the lambda function
print(add_tax(sale_price))
Calling lambda in-line
Remember, one of the key benefits of lambda is the ability to use functions in-line.
In this exercise, you'll modify the approach of the previous exercise to add tax to
the sales_price variable in-line without storing a lambda function as a variable first.
Instructions
0 XP
In a single line of code, make a lambda function that multiplies sale_price by 1.2 and
returns the results.
sale_price = 29.99
# Call a lambda function adding 20% to sale_price
print((lambda x: x * 1.2)(sale_price))
Lambda functions with iterables
You've used lambda functions to perform actions on a single value; now it's time to test yourself
on working with iterables.
You've been provided with a list called sales_prices containing sales prices for several items.
Your goal is to use a lambda function to add tax (20%) to each value in the list.
Instructions
0 XP
1. 1
https://docs.python.org/3/library/exceptions.html#exception-hierarchy
3. TypeError
00:37 - 00:58
Let's look at a couple of common errors. First is TypeError, which generally occurs
when we try to use an incompatible data type when performing a task. Here, we try
to add a string and an integer and get an error message that this action is not
supported on these two data types.
4. ValueError
00:58 - 01:20
There is ValueError, which occurs when a value provided is not within an acceptable
range. For example, we get a ValueError if we try to convert the string "Hello" to a
float. This differs from a TypeError, because no error is produced if we call float and
provide the number two as a string.
5. Tracebacks
01:20 - 01:29
Notice that the error outputs mentions the word "Traceback"? A traceback can be
thought of as a report,
6. Tracebacks
01:29 - 01:35
providing information including what type of error occurred, in this case a ValueError,
7. Tracebacks
01:35 - 01:40
and where it occurred in our code, in line 1 here.
8. Code in packages
01:40 - 02:20
We've looked at errors in our code, but what if an error occurs when using a
package? Remember, when we import packages and use their functions, we use
somebody else's code. The code for these packages is generally referred to as
source code. When we install a package, we download the source code so that it
works in our local environment, such as our computer. We don't see it when we call
a function from the package, such as the pandas pd.read_csv function, but the
source code gets executed behind the scenes.
9. Tracebacks from packages
02:20 - 02:33
Let's examine a traceback from using a package. Here, we import pandas, create a
DataFrame, and then try to access a column called tag, which doesn't exist.
10. Tracebacks from packages
02:33 - 02:37
That's a huge traceback! Let's break it down.
11. Tracebacks from packages
02:37 - 03:22
The first few lines show that the error occurred in line 3803 of the code. But we only
wrote eight lines. What's going on? We see a line starting with a file location. This
refers to the fact that line 3803 is in a file called base.py. Put another way, the
pandas' file base.py was executed when we tried to subset our DataFrame for the
tag column, and an error occurred in line 3803 of that file. At the bottom, we see it
was a KeyError and the following code caused this.
12. Tracebacks from packages
03:22 - 03:27
It then shows our code, highlighting the line that caused the error.
13. Tracebacks from packages
03:27 - 03:53
After this, we see that the code in frame.py was executed, where it tried to access
the key or column name. Lastly, in base.py, the code says raise KeyError, which is
where pandas developers have intentionally returned an error because the key does
not exist. We'll discuss this syntax more in the following video.
14. Let's practice!
03:53 - 03:58
For now, let's test your knowledge of errors.
Avoiding errors
In the video, you saw a couple of approaches for error handling that can be applied to custom
functions.
In this exercise, you'll test out one of the approaches that avoids raising an error, printing a
helpful message if an error occurs, but not terminating the script.
Instructions
100 XP
Use a keyword allowing you to attempt to run code that cleans text.
Swap a space for a single underscore in the text argument.
Use another keyword that prints a helpful message if an error occurs when calling
the snake_case() function.
def snake_case(text):
# Attempt to clean the text
try:
# Swap spaces for underscores
clean_text = clean_text.replace(" ", "_")
clean_text = clean_text.lower()
# Run this code if an error occurs
except:
print("The snake_case() function expects a string as
an argument, please check the data type provided.")
snake_case("User Name 187")
Returning errors
Time to try out the other approach for error handling.
Revise the snake_case() function to intentionally produce an error if an incorrect data type is
used.
Instructions
100 XP
Check whether the data type of the text argument is a string str.
Inside the else block, produce a TypeError() to prevent the script running and return a
descriptive message.
def snake_case(text):
# Check the data type
if type(text) == str:
clean_text = text.replace(" ", "_")
clean_text = clean_text.lower()
else:
# Return a TypeError error if the wrong data type was
used
raise TypeError("The snake_case() function expects a
string as an argument, please check the data type provide
d.")
snake_case("User Name 187")
Project Instructions
Create a validate_user() function, using some helper validation functions to
validate user input.
The function should take in the parameters: name, email and password.
The function should call each of the helper validation functions and raise
a ValueError with a descriptive error message if any of the inputs fail validation.
If all of the checks pass, the function should return a Boolean value of True.
Now that you've validated that all the user details are correct, you want to allow
users to register to the app. Create a register_user() function that registers the
user if all checks pass.
The function should take in the parameters: name, email and password.
The function should call validate_user() to ensure that the user input is valid.
If all checks pass, the function should create a dictionary with the
keys: name, email, password.
The function should return the new user dictionary, or the boolean value of False.
You are a junior developer working in a small start-up. Your managers have asked you to
develop a new account registration system for a mobile app. The system must validate user input
on the sign-up form before creating an account.
The previous junior developer wrote some helper functions that validate the name, email, and
password. Use these functions to register users, store their data, and implement some error
handling! These have been imported into the workspace for you. They will be a great help to you
when registering the user, but first you have to understand what the function does! Inspect the
docstrings of each of the helper
functions: validate_name, validate_email and validate_password.