IBM PY0101EN - Python Basics For Data Science
IBM PY0101EN - Python Basics For Data Science
Author
JOSEPH SANTARCANGELO
IBM Senior Data Scientist
Compiled By
OSVALDO ALENCAR
Brasilware Engineering
brasilware@gmail.com
IBM PY0101EN
This course is available at edx.org
Joseph Santarcangelo has a PhD in Electrical Engineering, his research focused on using machine
learning, signal processing, and computer vision to determine how videos impact human
cognition. Joseph has been working for IBM since he completed his PhD.
GENERAL INFORMATION
Kickstart your learning of Python for data science, as well as programming in general, with this
beginner-friendly introduction to Python.
Python is one of the world’s most popular programming languages, and there has never been
greater demand for professionals with the ability to apply Python fundamentals to drive business
solutions across industries.
This course will take you from zero to programming in Python in a matter of hours— no prior
programming experience necessary! You will learn Python fundamentals, including data
structures and data analysis, complete hands-on exercises throughout the course modules, and
create a final project to demonstrate your new skills.
By the end of this course, you’ll feel comfortable creating basic programs, working with data, and
solving real-world problems in Python. You’ll gain a strong foundation for more advanced
learning in the field and develop skills to help advance your career.
FINAL EXAM
PYTHON CODE QUICK REFERENCE GUIDE
✓ 6 String Operations 15
✓ 7 Lists in Python 15
✓ 7 Tuples in Python 15
2 1:15
✓ 8 Dictionaries in Python 25
✓ 9 Sets in Python 20
✓ 10 Conditions in Python 20
✓ 11 Loops in Python 20
✓ 13 Exception Handling 15
Figure 1
The best part is Python is super easy to learn and is often one of the first languages people turn to,
when trying to learn to code. Python is very powerful. It has a huge ecosystem of libraries that will
help you get the most complex things done with just a few lines of code.
Figure 2
Figure 3
This course is designed for beginners, but if you know how to program, you can also take this
course and quickly learn Python.
Figure 4
Figure 5
In module 2 you will cover Python data structures including lists, tuples, dictionaries, and sets.
Figure 6
In Module 3 I will teach you Python programming fundamentals such as conditions, branching,
loops, functions and objects and classes.
Figure 7
Figure 8
You will apply what you learn by doing projects using real-world datasets.
Figure 9
If you have any questions or require clarification, feel free to post on the course discussion
forum.
Good luck and happy learning.
CHANGE LOG
2024-1-30
• Updated version of the course published on edX.org
• Improved the practice and graded quizzes to more thoroughly assess the knowledge and
skills gained in the course.
2020-09-01
• Updated version of the course published on edX.org
• Replaced links to labs with links from SN Asset Library
2019-03-25
• Assignment added
2017-12-01
• Notebooks slightly changed J.S
2017-12-01
• Notebook order changed J.S
2017-11-12
• Rephrased some sentences for clarity. Fixed some typos.
2017-09-15
• Newly renovated course released
INTRODUCTION TO PYTHON
This module teaches the basics of Python and begins by exploring some of the different data
types such as integers, real numbers, and strings. Continue with the module and learn how to use
expressions in mathematical operations, store values in variables, and the many different ways
to manipulate strings.
LEARNING OBJECTIVES
In this module, you will:
• Demonstrate an understanding of types in Python by converting or casting data types
such as strings, floats, and integers.
• Interpret variables and solve expressions by applying mathematical operations.
• Describe how to manipulate strings by using a variety of methods and operations.
• Build a program in JupyterLab to demonstrate your knowledge of types, expressions,
and variables.
• Work with, manipulate, and perform operations on strings in Python.
Figure 10
Figure 11
Python is a powerhouse of a language. It is the most widely used and most popular programming
language used in the data science industry.
According to the 2019 Kaggle Data Science and Machine Learning Survey, ¾ of the over 10,000
respondents worldwide reported that they use Python regularly. Glassdoor reported that in 2019
more than 75% of data science positions listed included Python in their job descriptions. When
asked which language an aspiring data scientist should learn first, most data scientists say
Python.
Figure 13
Figure 14
Figure 15
Figure 16
SUMMARY
In the video, you learned that:
• Python uses clear and readable syntax.
• Python has a huge global community and a wealth of documentation.
• For data science, you can use python's scientific computing libraries like Pandas,
NumPy, SciPy, and Matplotlib.
• Python can also be used for Natural Language Processing (NLP) using the Natural
Language Toolkit (NLTK).
• Python community has a well-documented history of paving the way for diversity
and inclusion efforts in the tech industry as a whole.
Figure 17
Figure 18
Figure 19
In the lab session of this module, you can launch a notebook using the Skills Network virtual
environment. After selecting the check box, click the Open tool tab, and the environment will open
the Jupyter Lab.
Figure 20
On opening the notebook, you can change the name of the notebook.
Figure 21
Click File, then click Rename Notebook to give the required name. And you can now start working
on your new notebook.
Figure 22
Figure 23
Then click the Run button to show that the environment is giving the correct output. On the main
menu bar at the top, click Run. In the drop-down menu, click Run Selected Cells to run the current
highlighted cells. Alternatively, you can use a shortcut, press Shift + Enter.
Figure 25
In case you have multiple code cells, click Run All cells to run the code in all the cells.
Figure 26
Figure 27
In addition, you can delete a cell. Highlight the cell and on the main menu bar, click Edit, and then
click Delete Cells. Alternatively, you can use a shortcut by pressing D twice on the highlighted
cell.
Figure 28
Figure 29
So, now you have learned to work with a single notebook. Next, let’s learn to work with multiple
notebooks.
Figure 30
Click the plus button on the toolbar and select the file you want to open. Another notebook will
open.
Figure 31
Figure 32
And when you open the new file, you can move them around. For example, as shown, you can
place the notebooks side by side.
Figure 33
On one notebook, you can assign variable one to the number 1, and variable two to the number
2 and then you can print the result of adding the numbers one and two.
Figure 34
Figure 35
Now, when you have completed working with your notebook or notebooks, you can shut them
down. Shutting down notebooks release their memory. Click the stop icon on the sidebar, it is the
second icon from the top. You can terminate all sessions at once or shut them down individually.
And after you shut down the notebook session, you will see “no kernel” at the top right. This
confirms it is no longer active, you can now close the tabs.
Figure 36
RECAP
In the video, you learned how to:
• Run, delete, and insert a code cell.
• Run multiple notebooks at the same time.
• Present a notebook using a combination of Markdown and code cells.
• Shut down your notebook sessions after you have completed your work.
Objectives
Table of Contents
When learning a new programming language, it is customary to start with an "hello world"
example. As simple as it is, this one line of code will ensure that we know how to print a string
in output and how to execute code within cells in a notebook.
[Tip]: To execute the Python code in the code cell, click on the cell to select it and press Shift + Enter.
After executing the cell above, you should see that Python prints Hello, Python!.
Congratulations on running your first Python code!
[Tip:] print() is a function. You passed the string 'Hello, Python!' as an argument to instruct Python on
what to print.
There are two popular versions of the Python programming language in use today: Python 2 and
Python 3. The Python community has decided to move on from Python 2 to Python 3, and many
popular libraries have announced that they will no longer support Python 2.
Since Python 3 is the future, in this course we will be using it exclusively. How do we know that
our notebook is executed by a Python 3 runtime? We can look in the top- right hand corner of this
notebook and see "Python 3".
[Tip:] sys is a built-in module that contains many system-specific parameters and functions, including the
Python version in use. Before using it, we must explicitly import it.
In addition to writing code, note that it's always a good idea to add comments to your code. It will
help others understand what you were trying to accomplish (the reason why you wrote a given
snippet of code). Not only does this help other people understand your code, it can also
serve as a reminder to you when you come back to it weeks or months later.
To write comments in Python, use the number symbol # before writing your comment. When you
run your code, Python will ignore everything past the # on a given line.
Hello, Python!
After executing the cell above, you should notice that This line prints a string did not
appear in the output, because it was a comment (and thus ignored by Python).
The second line was also not executed because print('Hi') was preceded by the number sign
(#) as well! Since this isn't an explanatory comment from the programmer, but an actual line
of code, we might say that the programmer commented out that second line of code.
Errors in Python
Everyone makes mistakes. For many types of mistakes, Python will tell you that you have made
a mistake by giving you an error message. It is important to read error messages carefully to really
understand where you made a mistake and how you may go about correcting it.
For example, if you spell print as frint, Python will display an error message. Give it a try:
frint("Hello, Python!")
NameError Traceback (most recent call last)
Cell In[22], line 3
1 # Print string as error message
----> 3 frint("Hello, Python!")
Does Python know about your error before it runs your code?
Python is what is called an interpreted language. Compiled languages examine your entire
program at compile time and are able to warn you about a whole class of errors prior to execution.
In contrast, Python interprets your script line by line as it executes it. Python will stop executing
the entire program when it encounters an error (unless the error is expected and handled by the
programmer, a more advanced subject that we'll cover later on in this course).
Try to run the code in the cell below and see what happens:
Generations of programmers have started their coding careers by simply printing "Hello,
world!". You will be following in their footsteps.
In the code cell below, use the print() function to print out the phrase: Hello, world!
# Write your code below. Don't forget to press Shift+Enter to execute the cell
print("Hello, world!")
Hello, world!
Now, let's enhance your code with a comment. In the code cell below, print out the phrase: Hello,
world! and comment it with the phrase Print the traditional hello world all in one
line of code.
# Write your code below. Don't forget to press Shift+Enter to execute the cell
# Print the traditional hello world
print("Hello, world!")
# Write your code below. Don't forget to press Shift+Enter to execute the cell
x = 10
y = 7
Z = x + y
Z
17
Congratulations, you have completed your first lesson and hands-on lab in Python.
Figure 37
DATA TYPES
A type is how Python represents different types of data. In the video, we will discuss some widely
used types in Python. You can have different types in Python. They can be integers like 11, real
numbers like 21.213, they can even be words. Integers, real numbers, and words can be
expressed as different data types.
The following chart summarizes three data types for the last examples:
• The first column indicates the expression.
• The second column indicates the data type.
Figure 38
We can see the actual data type in Python by using the type command. We can have int, which
stands for an integer and float that stands for float, essentially a real number. The type string is a
sequence of characters.
Figure 39
Figure 40
FLOAT
Floats are real numbers. They include the integers but also numbers in between the integers.
Consider the numbers between 0 and 1. We can select numbers in between them. These numbers
are floats. Similarly, consider the numbers between 0.5 and 0.6. We can select numbers in
between them. These are floats as well. We can continue the process zooming in for different
numbers. Of course, there is a limit, but it is quite small.
Figure 41
Figure 42
If a string contains an integer value, you can convert it to int. If we convert a string that contains
a non-integer value, we get an error.
Check out more examples in the lab.
Figure 43
Figure 44
If we cast a Boolean True to an integer or float, we will get a 1. If we cast a Boolean False to an
integer or float, we get a 0.
Figure 45
If you cast a 1 to a Boolean, you get a True. Similarly, if you cast a 0 to a Boolean, you get a False.
Figure 46
Check the labs for more examples or check Python.org for other kinds of types in Python.
OBJECTIVES
After completing this lab you will be able to:
• Work with various types of data in Python.
• Convert the data from one type to another.
TABLE OF CONTENTS
Types of objects in Python
• Integers
• Floats
• Converting from one object type to a different object type
• Boolean data type
• Exercise: Types
Figure 47
# Integer
11
11
#float
2.14
2.14
You can get Python to tell you the type of an expression by using the built-in type() function.
You'll notice that Python refers to integers as int, floats as float, and character strings as str.
# Type of 12
type(12)
int
# Type of 2.14
type(2.14)
float
In the code cell below, use the type() function to check the object type of 12.0.
# Write your code below. Don't forget to press Shift+Enter to execute the cell
Type(12.0)
float
Integers
Here are some examples of integers. Integers can be negative or positive numbers:
Figure 48
We can verify this is the case by using, you guessed it, the type() function:
Floats represent real numbers; they are a superset of integer numbers but also include "numbers
with decimals". There are some limitations when it comes to machines representing real numbers,
but floating point numbers are a good representation in most cases. You can learn more about
the specifics of floats for your runtime environment, by checking the value of sys.float_info. This
will also tell you what's the largest and smallest number that can be represented with them.
Once again, can test some examples with the type() function:
You can change the type of the object in Python; this is called typecasting. For example, you
can convert an integer into a float (e.g. 2 to 2.0).
Let's try it:
# Convert 2 to a float
float(2)
2.0
But if you try to do so with a string that is not a perfect match for a number, you'll get an error. Try
the following:
ValueError: invalid literal for int() with base 10: '1 or 2 people'
You can also convert strings containing floating point numbers into float objects:
[Tip:] Note that strings can be represented with single quotes ('1.2') or double quotes ("1.2"), but you can't mix
both (e.g., "1.2').
And there is no reason why we shouldn't be able to make floats into strings as well:
Boolean is another important type in Python. An object of type Boolean can take on one of two
values: True or False:
# Value true
True
True
Notice that the value True has an uppercase "T". The same is true for False (i.e. you must use the
uppercase "F").
# Value false
False
False
When you ask Python to display the type of a boolean object it will show bool which stands for
boolean:
# Type of True
type(True)
bool
# Type of False
type(False)
bool
We can cast boolean objects to other data types. If we cast a boolean with a value of True to an
integer or float we will get a one. If we cast a boolean with a value of False to an integer or float
we will get a zero. Similarly, if we cast a 1 to a Boolean, you get a True. And if we cast a 0 to a
Boolean we will get a False. Let's give it a try:
# Convert 1 to Boolean
bool(1)
True
# Convert 0 to Boolean
bool(0)
False
# Write your code below. Don't forget to press Shift+Enter to execute the cell
type(6/2) #float
float
What is the type of the result of: 6//2? (Note the double slash //.)
type("Hello, World!")
str
Write the code to convert the following number representing employeeid "1001" to an integer
Type(“1001”) #1001
1001
Write the code to convert this number representing financial value "1234.56" to a floating
point number
Float(“1234.56”) #1234.56
31234.56
Str((“123-456-7890”) #’123-456-7890’
'123-456-7890'
Figure 49
EXPRESSIONS
Expressions describe a type of operation that computers perform. Expressions are operations that
python performs. For example, basic arithmetic operations like adding multiple numbers. The
result in this case is 160. We call the numbers operands, and the math symbols in this case,
addition, are called operators.
Figure 50
We can perform operations such as subtraction using the subtraction sign. In this case, the result
is a negative number. We can perform multiplication operations using the asterisk. The result is
25. In this case, the operations are given by negative and asterisk.
Figure 51
At :31 in the video the voice over and the transcript should be subtraction instead of traction.
At :43 in the video the voice over and transcript should be operator instead of operand.
Figure 52
We can use the double slash for integer division, where the result is rounded. Be aware, in
some cases the results are not the same as regular division.
Figure 53
Figure 54
Figure 55
VARIABLES
Now, let's look at variables. We can use variables to store values. In this case, we assign a value
of 1 to the variable my_variable using the assignment operator, i.e, the equal sign. We can then
use the value somewhere else in the code by typing the exact name of the variable. We will use
a colon to denote the value of the variable.
Figure 56
We can assign a new value to my_variable using the assignment operator. We assign a value
of 10. The variable now has a value of 10. The old value of the variable is not important.
Figure 57
Figure 58
We can also perform operations on x and assign the value x. The variable x now has a value:
2.666.
Figure 59
As before, the old value of x is not important. We can use the type command in variables as well.
Figure 60
Figure 61
We call the variable that contains the total number of minutes "total_min". It's common to use
the underscore to represent the start of a new word. You could also use a capital letter. We call
the variable that contains the total number of hours, total_hour. We can obtain the total
number of hours by dividing total_min by 60. The result is approximately 2.367 hours.
Figure 62
If we modify the value of the first variable, the value of the variable will change. The final result
values change accordingly, but we do not have to modify the rest of the code.
Figure 63
OBJECTIVES
Expressions
Expressions in Python can include operations among compatible types (e.g., integers and floats).
For example, basic arithmetic operations like adding multiple numbers:
We can perform subtraction operations using the minus operator. In this case the result is a
negative number:
Let's write an expression that calculates how many hours there are in 160 minutes:
# Write your code below. Don't forget to press Shift+Enter to execute the cell
160//60
2
# Mathematical expression
30 + 2 * 60
150
And just like mathematics, expressions enclosed in parentheses have priority. So, the following
multiplies 32 by 60.
Variables
Just like with most programming languages, we can store values in variables, so we can use them
later on. For example:
To see the value of x in a Notebook, we can simply place it on the last line of a cell:
We can also perform operations on x and save the result to a new variable:
# Use another variable to store the result of the operation between variable and value
y = x / 60
y
2.6666666666666665
It's a good practice to use meaningful variable names, so you and others can read the code and
understand it more easily:
In the cells above we added the length of three albums in minutes and stored it in total_min. We
then divided it by 60 to calculate total length total_hours in hours. You can also do it all at once in
a single expression, as long as you use parenthesis to add the albums length before you divide, as
shown below.
# ComplicateD expression
total_hours = (43 + 42 + 57) / 60 # Total hours in a single expression
total_hours
2.3666666666666667
If you'd rather have total hours as an integer, you can of course replace the floating point division
with integer division (i.e., //).
# Write your code below. Don't forget to press Shift+Enter to execute the cell
x = 30+20-40
print(“x = “,x)
x = 10
# Write your code below. Don't forget to press Shift+Enter to execute the cell
x =(55-5)/10
Print(“x =”,x)
x = 5.0
# Write your code below. Don't forget to press Shift+Enter to execute the cell
x = (6*10)/12
print(“x =”,x)
W = 5.0
# Write your code below. Don't forget to press Shift+Enter to execute the cell
x = 3+3*2
x
7
# Write your code below. Don't forget to press Shift+Enter to execute the cell
y = (3+2)*2
y
10
# Write your code below. Don't forget to press Shift+Enter to execute the cell
z = x + y #x = 7 + 10
z
17
Congratulations, you have completed your hands-on lab on Expressions and Variables in Python.
Figure 64
Figure 65
We can bind or assign a string to another variable. It is helpful to think of a string as an ordered
sequence. Each element in the sequence can be accessed using an index represented by the array
of numbers. The first index can be accessed as follows:
We can access index six. Moreover, we can access the 13th index.
Figure 66
Figure 67
We can bind a string to another variable. It is helpful to think of string as a list or tuple. We can
treat the string as a sequence and perform sequence operations.
Figure 68
We can also input a stride value as follows: The two indicates we'd select every second
variable. We can also incorporate slicing.
Figure 69
Figure 70
We can use the len command to obtain the length of the string. As there are 15 elements,
the result is 15.
Figure 71
At 1:13 in the video it says "Tuples: Slicing" and it should say "Strings: Slicing."
We can concatenate or combine strings. We use the addition symbols. The result is a new string
that is a combination of both.
Figure 72
Figure 73
This means you cannot change the value of the string, but you can create a new string. For example,
you can create a new string by setting it to the original variable and concatenate it with a new
string. The result is a new string that changes from Michael Jackson to Michael Jackson is the best.
Figure 74
Strings are immutable. Back slashes represent the beginning of escape sequences. Escape
sequences represent strings that may be difficult to input. For example, backslashes "n"
represents a new line. The output is given by a new line after the backslashes "n" is encountered.
Figure 75
Figure 76
If you want to place a backslash in your string, use a double backslash. The result is a backslash
after the escape sequence. We can also place an "r" in front of the string.
Figure 77
Figure 78
Strings are sequences and as such, have apply methods that work on lists and tuples. Strings also
have a second set of methods that just work on strings.
Figure 79
When we apply a method to the string A, we get a new string B that is different from A.
Figure 80
Figure 81
REPLACE
The method replaces a segment of the string- i.e. a substring with a new string. We input the part
of the string we would like to change.
Figure 82
The second argument is what we would like to exchange the segment with. The result is a
new string with a segment changed.
Figure 83
Figure 84
OBJECTIVES
After completing this lab you will be able to:
• Work with Strings
• Perform operations on String
• Manipulate Strings using indexing and escape sequences
TABLE OF CONTENTS
• What are Strings?
• Indexing
o Negative Indexing
o Slicing
o Stride
o Concatenate Strings
• Escape Sequences
• String Operations
• Quiz on Strings
Indexing
It is helpful to think of a string as an ordered sequence. Each element in the sequence can be
accessed using an index represented by the array of numbers:
Figure 85
[Tip]: Because indexing starts at 0, it means the first index is on the index 0.
Figure 86
Negative index can help us to count the element from the end of the string. The last element is
given by the index -1:
We can find the number of characters in a string by using len, short for length:
Slicing
We can obtain multiple characters from a string using slicing, we can obtain the 0 to 4th and 8th
to the 12th element:
Figure 87
[Tip]: When taking the slice, the first number means the index (start at 0), and the second number means the
length from the index to the last element you want (start at 1)
Stride
We can also input a stride value as follows, with the '2' indicating that we are selecting
every second variable:
Figure 88
'McalJcsn'
We can also incorporate slicing with the stride. In this case, we select the first five elements
and then use the stride:
Concatenate Strings
We can concatenate or combine strings by using the addition symbols, and the result is a new
string that is a combination of both:
To replicate values of a string we simply multiply the string by the number of times we would like
to replicate it. In this case, the number is three. The result is a new string, and this new string
consists of three copies of the original string:
You can create a new string by setting it to the original variable. Concatenated with a new string,
the result is a new string that changes from Michael Jackson to “Michael Jackson is the best".
# Concatenate strings
name = "Michael Jackson"
name = name + " is the best" name
'Michael Jacksonis the best'
ESCAPE SEQUENCES
Back slashes represent the beginning of escape sequences. Escape sequences represent strings
that may be difficult to input. For example, back slash "n" represents a new line. The output is
given by a new line after the back slash "n" is encountered:
If you want to place a back slash in your string, use a double back slash:
STRING OPERATIONS
There are many string operation methods in Python that can be used to manipulate the data. We
are going to use some basic string operations on the data.
Upper operation
Let's try with the method upper; this method converts lower case characters to upper case
characters:
Let's try with the method lower; this method converts upper case characters to lower case
characters:
Replace operation
The method replace replaces a segment of the string, i.e. a substring with a new string. We input
the part of the string we would like to change. The second argument is what we would like to
exchange the segment with, and the result is a new string with the segment changed:
# Replace the old substring with the new target substring by removing some
punctuations.
Find operation
The method find finds a sub-string. The argument is the substring you would like to find, and the
output is the first index of the sequence. We can find the sub- string jack or el.
Figure 89
# Find the substring in the string. Only the index of the first elment of substring in
string will be the output
name = "Michael Jackson"
name.find('el')
5
If the sub-string is not in the string then the output is a negative one. For example, the string
'Jasdfasdasdf' is not a substring:
-1
Split operation
The method Split splits the string at the specified separator, and returns a list:
RegEx
In Python, RegEx (short for Regular Expression) is a tool for matching and handling strings.
This RegEx module provides several functions for working with regular expressions, including
search, split, findall, and sub.
Python provides a built-in module called re, which allows you to work with regular
expressions. First, import the re module.
import re
Search operation
The search() function searches for specified patterns within a string. Here is an example that
explains how to use the search() function to search for the word "Jackson" in the string "Michael
Jackson is the best".
# Use the search() function to search for the pattern in the string result =
re.search(pattern, s1)
Matches the boundary between a "cat" matches "\bcat\b" in "The cat sat on
word character and a non-word the mat"
\b character
Matches any position that is not a "cat" matches "\Bcat\B" in "category" but
\B
word boundary not in "The cat sat on the mat"
if match:
print("Phone number found:", match.group()) else:
print("No match")
else:
print("Nom match!”)
Phone number found: 1234567890
The regular expression pattern is defined as r"\d\d\d\d\d\d\d\d\d\d", which uses the \d special
sequence to match any digit character (0-9), and the \d sequence is repeated ten times to
match ten consecutive digits.
A simple example of using the \W special sequence in a regular expression pattern with Python
code:
Findall operation
The findall() function finds all occurrences of a specified pattern within a string.
# Use the findall() function to find all occurrences of the "as" in the string
result = re.findall("as", s2)
Split Function
A regular expression's split() function splits a string into an array of substrings based on a
specified pattern.
# Use the split function to split the string by the "\s" split_array = re.split("\s",
s2)
Sub Function
The sub function of a regular expression in Python is used to replace all occurrences of a pattern
in a string with a specified replacement.
# The new_string contains the original string with the pattern replaced by the
replacement string
print(new_string)
Michael Jackson was a singer and known as the 'legend'
What is the value of the variable b after the following code is executed?
What is the value of the variable c after the following code is executed?
Consider the variable d use slicing to print out the first three elements:
d = "ABCDEFG"
print(d[:3])
ABC
Use a stride value of 2 to print out every second character of the string e:
e = 'clocrkr1e1c1t'
print(e[::2])
correct
#or
print(r"\ ")
\\\\
\
Consider the variable g, and find the first index of the sub-string snow:
g = "Mary had a little lamb Little lamb, little lamb Mary had a little lamb \
Its fleece was white as snow And everywhere that Mary went Mary went, Mary went \
Everywhere that Mary went The lamb was sure to go"
g.find("snow")
95
# Write your code below and press Shift+Enter to execute g.replace("Mary", "Bob")
'Bob had a little lamb Little lamb, little lamb Bob had a little lamb Its fleece was
white as snow And everywhere that Bob went Bob went, Bob went Everywhere that Bob went The
lamb was sure to go'
'Bob had a little lamb Little lamb, little lamb Bob had a little lamb Its fleece was white as
snow And everywhere that Bob went Bob went, Bob went Everywhere that Bob went The lamb was sure to
go'
'Mary had a little lamb Little lamb. little lamb Mary had a little lamb Its fleece was
white as snow And everywhere that Mary went Mary went. Mary went Everywhere that Mary went
The lamb was sure to go'
'Mary had a little lamb Little lamb. little lamb Mary had a little lamb Its fleece was white as
snow And everywhere that Mary went Mary went. Mary went Everywhere that Mary went The lamb was sure
to go'
In the string s3, find the four consecutive digit character using \d and search()
function:
In the string str1, replace the sub-string fox with bear using sub() function:
str1= "The quick brown fox jumps over the lazy dog."
# Write your code below and press Shift+Enter to execute
# Use re.sub() to replace "fox" with "bear"
new_str1 = re.sub(r"fox", "bear", str1)
print(new_str1)
The quick brown bear jumps over the lazy dog.
In the string str2 find all the occurrences of woo using findall() function:
str2= "How much wood would a woodchuck chuck, if a woodchuck could chuck wood?"
print(matches)
['woo', 'woo', 'woo', 'woo']
Congratulations, you have completed your first lesson and hands-on lab in Python.
Figure 90
o "i"
o "M"
o "o"
o "n"
Correct: The index having a value of -1 denotes the final position within a cyclic sequence.
Question 2
What is the result of the following? print("AB\nC\nDE")
o AB\nC\nDE
o ABC DE
o AB CD E
o AB
o C DE
Correct: When the "print" function comes across the "\n" character, it displays a new line.
Question 3
What is the result of following?
"hello Mike".find("Mike")
If you are unsure, copy and paste the code into Jupyter Notebook and check.
o 2
o 6
o 5
o 6,7,8
Correct: The method helps you locate the position of the first character in a given string that
matches the first character of a specified substring.
Example:
1. result = "Hello" + "
John"</td>
Example:
1. my_string="Hello"
2. length = len(my_string)
Example:
1. name="John" # assigning John to
variable name
2. x = 5 # assigning 5 to variable x
Data type Data type refers to the type of value a variable has and what type of
mathematical, relational or logical operations can be applied without
causing an error.
Double quote Symbol “ “ used to represent strings in Python.
INTRODUCTION
This module begins a journey into Python data structures by explaining the use of lists and tuples
and how they can store data collections in a single variable.
Next, learn about dictionaries and how they function by storing data in pairs of keys and values,
and end with Python sets to learn how this type of collection can appear in any order and will
only contain unique elements.
LEARNING OBJECTIVES
In this module, you will:
• Describe and manipulate tuple combinations and list data structures.
• Execute basic tuple operations in Python.
• Perform list operations in Python.
• Write structures with correct keys and values to demonstrate understanding of
dictionaries.
• Work with and perform operations on dictionaries in Python.
• Create sets to demonstrate understanding of the differences between sets, tuples, and
lists.
• Work with sets in Python, including operations and logic operations.
Figure 91
TUPLES
Figure 92
Tuples are an ordered sequence. Here is a tuple ratings. Tuples are expressed as comma
separated elements within parentheses. These are values inside the parentheses.
Figure 93
Figure 94
The following table represents the relationship between the index and the elements in the tuple.
The first element can be accessed by the name of the tuple followed by a square bracket with the
index number, in this case zero. We can access the second element as follows. We can also access
the last element.
Figure 95
In Python, we can use negative index. The relationship is as follows. The corresponding
values are shown here.
Figure 96
Figure 97
SLICING
If we would like multiple elements from a tuple, we could also slice tuples. For example, if we
want the first three elements, we use the following command. The last index is one larger than
the index you want.
Figure 98
Similarly, if we want the last two elements, we use the following command. Notice, how the last
index is one larger than the last index of the tuple.
Figure 99
In the video at 1:32 the line says: "Notice how the last index is one larger than the length of the tuple." The line should be read
as "Notice how the last index is one larger than the last index of the tuple."
Figure 100
Tuples are immutable which means we can't change them. To see why this is important, let's see
what happens when we set the variable ratings 1 to ratings. Let's use the image to provide a
simplified explanation of what's going on.
Each variable does not contain a tuple but references the same immutable tuple object.
Figure 101
See the objects and classes module for more about objects.
Figure 102
Figure 103
Figure 104
Figure 105
NESTING
A tuple can contain other tuples as well as other complex data types. This is called nesting. We
can access these elements using the standard indexing methods. If we select an index with a
tuple, the same index convention applies. As such, we can then access values in the tuple. For
example, we could access the second element. We can apply this indexing directly to the tuple
variable NT. It is helpful to visualize this as a tree.
Figure 106
We can visualize this nesting as a tree. The tuple has the following indexes. If we consider indexes
with other tuples, we see the tuple at index 2 contains a tuple with two elements. We can access
those two indexes. The same convention applies to index 3. We can access the elements in those
tuples as well. We can continue the process.
Figure 107
Figure 108
LISTS
Figure 109
Lists are also a popular data structure in Python. Lists are also an ordered sequence.
Here is a list, "L." A list is represented with square brackets. In many aspects, lists are like tuples.
One key difference is they are mutable.
Figure 110
Figure 111
Like tuples, each element of a list can be accessed via an index. The following table represents
the relationship between the index and the elements in the list. The first element can be accessed
by the name of the list followed by a square bracket with the index number, in this case zero. We
can access the second element as follows. We can also access the last element. In Python, we
can use a negative index. The relationship is as follows. The corresponding indexes are as follows.
Figure 112
We can also perform slicing in lists. For example, if we want the last two elements in this list
we use the following command. Notice how the last index is one larger than the length of the
list. The index conventions for lists and tuples are identical. Check the labs for more examples.
Figure 113
Figure 114
EXTEND
Lists are mutable, therefore we can change them. For example, we apply the method extend by
adding a dot followed by the name of the method then parentheses. The argument inside the
parentheses is a new list that we are going to concatenate to the original list. In this case, instead
of creating a new list, "L1," the original list, "L," is modified by adding two new elements.
To learn more about methods check out our video on objects and classes.
Figure 115
APPEND
Another similar method is append. If we apply append instead of extend, we add one element to
the list. If we look at the index there is only one more element. Index 3 contains the list we
appended.
Figure 116
Figure 117
As lists are mutable we can change them. For example, we can change the first element as
follows. The list now becomes hard rock 10 1.2.
Figure 118
DEL
We can delete an element of a list using the del command. We simply indicate the list item we
would like to remove as an argument. For example, if we would like to remove the first element
the result becomes 10 1.2. We can delete the second element. This operation removes the second
element off the list.
Figure 119
Figure 120
We can use the split function to separate strings on a specific character known, as a delimiter.
We simply pass the delimiter we would like to split on as an argument, in this case a comma. The
result is a list. Each element corresponds to a set of characters that have been separated by a
comma.
Figure 121
ALIASING
When we set one variable B equal to A, both A and B are referencing the same list. Multiple
names referring to the same object is known as aliasing.
Figure 122
Figure 123
A and B are referencing the same list, therefore if we change A, list B also changes. If we check
the first element of B after changing list A, we get banana instead of hard rock.
Figure 124
CLONE
You can clone list A by using the following syntax. Variable A references one list. Variable B
references a new copy or clone of the original list.
Figure 125
Figure 126
Figure 127
OBJECTIVES
After completing this lab you will be able to:
• Perform list operations in Python, including indexing, list manipulation, and copy/clone
list.
TABLE OF CONTENTS
• About the Dataset
• Lists
o Indexing
o List Content
o List Operations
o Copy and Clone List
• Quiz on Lists
Imagine you received album recommendations from your friends and compiled all of the
recommendations into a table, with specific information about each album.
The table has one row for each movie and several columns:
• artist - Name of the artist
• album - Name of the album
• released_year - Year the album was released
• length_min_sec - Length of the album (hours,minutes,seconds)
• genre - Genre of the album
• music_recording_sales_millions - Music recording sales (millions in USD) on
SONG://DATABASE
• claimed_sales_millions - Album's claimed sales (millions in USD) on
SONG://DATABASE
• date_released - Date on which the album was released
• soundtrack - Indicates if the album is the movie soundtrack (Y) or (N)
• rating_of_friends - Indicates the rating from your friends from 1 to 10
A: Artist
B: Album
C: Released
D: Length
E: Genre
F: Music Recording Sales (Milliions)
G: Claimed Sales (Milliions)
H: Released
I: Soundtrack
J: Rating (Friends)
Lists
Indexing
We are going to take a look at lists in Python. A list is a sequenced collection of different objects
such as integers, strings, and even other lists as well. The address of each element within a list
is called an index. An index is used to access and refer to items within a list.
Figure 128
# Create a list
L = ["Michael Jackson", 10.1, 1982] L
['Michael Jackson', 10.1, 1982]
Figure 129
the same element using negative and positive indexing: Postive: Michael Jackson
Negative: Michael Jackson
the same element using negative and positive indexing: Postive: 10.1
Negative: 10.1
the same element using negative and positive indexing: Postive: 1982
Negative: 1982
List Content
Lists can contain strings, floats, and integers. We can nest other lists, and we can also nest
tuples and other data structures. The same indexing conventions apply for nesting:
# Sample List
["Michael Jackson", 10.1, 1982, [1, 2], ("A", 1)]
['Michael Jackson', 10.1, 1982, [1, 2], ('A', 1)]
slicing
We can also perform slicing in lists. For example, if we want the last two elements, we use the
following command:
# Sample List
L = ["Michael Jackson", 10.1,1982,"MJ",1] L
['Michael Jackson', 10.1, 1982, 'MJ', 1]
Figure 130
# List slicing
L[3:5]
['MJ', 1]
extend
We can use the method extend to add new elements to the list:
append
Another similar method is append. If we apply append instead of extend, we add one element
to the list:
Each time we apply a method, the list changes. If we apply extend we add two new elements to
the list. The list L is then modified by adding two new elements:
If we append the list ['a','b'] we have one new element consisting of a nested list:
split
We can convert a string to a list using split. For example, the method split translates every group
of characters separated by a space into an element in a list:
We can use the split function to separate strings on a specific character which we call a delimiter.
We pass the character we would like to split on into the argument, which in this case is a comma.
The result is a list, and each element corresponds to a set of characters that have been separated
by a comma:
Figure 131
Figure 132
B = A[:] B
['banana', 10, 1.2]
Variable B references a new copy or clone of the original list. This is demonstrated in the
following figure:
Figure 133
print('B[0]:', B[0])
A[0] = "hard rock" print('B[0]:', B[0])
B[0]: banana
B[0]: banana
TABLE OF CONTENTS
• About the Dataset
• Tuples
o Indexing
o Slicing
o Sorting
• Quiz on Tuples
Imagine you received album recommendations from your friends and compiled all of the
recommendations into a table, with specific information about each album.
The table has one row for each movie and several columns:
• artist - Name of the artist
• album - Name of the album
• released_year - Year the album was released
• length_min_sec - Length of the album (hours,minutes,seconds)
• genre - Genre of the album
• music_recording_sales_millions - Music recording sales (millions in USD) on
SONG://DATABASE
• claimed_sales_millions - Album's claimed sales (millions in USD) on
SONG://DATABASE
• date_released - Date on which the album was released
• soundtrack - Indicates if the album is the movie soundtrack (Y) or (N)
• rating_of_friends - Indicates the rating from your friends from 1 to 10
A B C D E F G H I J
Michael Jackson Thriller 1982 00:42:19 Pop, rock, R&B 46 65 30/nov/82 10.0
AC/DC Back in Black 1980 00:42:11 Hard rock 26.1 50 25/jul/80 8.5
Pink Floyd The Dark Side of the 1973 00:42:49 Progressive rock 24.2 45 01/mar/73 9.5
Moon
Whitney The Bodyguard 1992 00:57:44 Soundtrack/R&B, 26.1 50 25/jul/80 Y 7.0
Houston soul, pop
Meat Loaf Bat Out of Hell 1977 00:46:33 Hard rock, 20.6 43 21-Oct-77 7.0
progressive rock
Eagles Their Greatest Hits 1976 00:43:08 Rock, soft rock, folk 32.2 42 17-Feb-76 9.5
(1971-1975) rock
Bee Gees Saturday Night Fever 1977 01:15:54 Disco 20.6 40 15/nov/77 Y 9.0
Fleetwood Mac Rumours 1977 00:40:01 Soft rock 27.9 40 04-Feb-77 9.5
A: Artist
B: Album
C: Released
D: Length
E: Genre
F: Music Recording Sales (Milliions)
G: Claimed Sales (Milliions)
H: Released
I: Soundtrack
J: Rating (Friends)
TUPLES
In Python, there are different data types: string, integer, and float. These data types can all be
contained in a tuple as follows:
Figure 134
Now, let us create your first tuple with string, integer and float.
type(tuple1)
tuple
Indexing
Each element of a tuple can be accessed via an index. The following table represents the
relationship between the index and the items in the tuple. Each element can be obtained by the
name of the tuple followed by a square bracket with the index number:
Figure 135
We can also use negative indexing. We use the same table above with corresponding negative
values:
Figure 136
# Use negative index to get the value of the second last element
tuple1[-2]
10
# Use negative index to get the value of the third last element
tuple1[-3]
'disco'
Concatenate Tuples
We can slice tuples obtaining multiple values as demonstrated by the figure below:
Figure 137
Slicing
We can slice tuples, obtaining new tuples with the corresponding elements:
Figure 138
Sorting
# A sample tuple
Ratings = (0, 9, 6, 5, 10, 8, 9, 6, 2)
Nested Tuple
A tuple can contain another tuple as well as other more complex data types. This process is
called 'nesting'. Consider the following tuple with several elements:
Each element in the tuple, including other tuples, can be obtained via an index as shown in the
figure:
Figure 139
Figure 140
We can access strings in the second nested tuples using a third index:
Figure 141
Similarly, we can access elements nested deeper in the tree with a third index:
Figure 142
QUIZ ON TUPLES
Consider the following tuple:
# sample tuple
genres_tuple = ("pop", "rock", "soul", "hard rock", "soft rock", \
"R&B", "progressive rock", "disco")
genres_tuple
('pop',
'rock',
'soul',
'hard rock',
'soft rock',
'R&B',
'progressive rock',
'disco')
Figure 143
List
Example:
fruits = ["apple", "banana",
"orange"]
fruits.append("mango")
print(fruits)
copy() The `copy()` method is used to create a Example 1:
shallow copy of a list. my_list = [1, 2, 3, 4, 5]
new_list = my_list.copy()
print(new_list)
# Output: [1, 2, 3, 4, 5]
Example:
my_list = [1, 2, 3, 4, 5]
my_list.insert(2, 6)
print(my_list)
Example 2:
my_list = [10, 20, 30, 40, 50]
removed_element = my_list.pop()
# Removes and returns the last
element
print(removed_element)
# Output: 50
print(my_list)
# Output: [10, 20, 30, 40]
Example:
my_list = [1, 2, 3, 4, 5]
print(my_list[:3])
# Output: [1, 2, 3] (elements from
the beginning up to index 2)
print(my_list[2:])
# Output: [3, 4, 5] (elements from
index 2 to the end)
print(my_list[::2])
# Output: [1, 3, 5] (every second
element)
Example 2:
my_list = [5, 2, 8, 1, 9]
my_list.sort(reverse=True)
print(my_list)
# Output: [9, 8, 5, 2, 1]
Dictionary
Example:
name = person["name"]
age = person["age"]
used further.
copy() Creates a shallow copy of the dictionary. Syntax:
The new dictionary contains the same new_dict = dict_name.copy()
key existence You can check for the existence of a key Example:
in a dictionary using the `in` keyword if "name" in person:
2. print("Name exists in the
dictionary.")
keys() Retrieves all keys from the dictionary and Syntax:
1. keys_list =
converts them into a list. Useful for
list(dict_name.keys())
iterating or processing keys using list
Example:
methods.
1. person_keys = list(person.keys())
set operations.
discard() Use the `discard()` method to remove a Syntax:
1. set_name.discard(element)
specific element from the set. Ignores if
the element is not found. Example:
1. fruits.discard("apple")
4. sym_diff_set =
set1.symmetric_difference(set2)
4. sym_diff =
fruits.symmetric_difference(colors)
Figure 144
If you recall, a list is integer indexes. These are like addresses. A list also has elements. A
dictionary has keys and values. The key is analogous to the index. They are like addresses, but
they don't have to be integers. They are usually characters. The values are similar to the element
in a list and contain information.
Figure 145
To create a dictionary, we use curly brackets. The keys are the first elements. They must be
immutable and unique. Each key is followed by a value separated by a colon. The values can be
immutable, mutable, and duplicates. Each key and value pair is separated by a comma.
Figure 146
Figure 147
We can use yellow to highlight the keys and leave the values in white.
Figure 148
It is helpful to use the table to visualize a dictionary where the first column represents the keys,
and the second column represents the values. We can add a few more examples to the
dictionary.
Figure 149
Figure 150
Figure 151
Using the key of "Back in Black," this returns the value of 1980.
Figure 152
The key, "The Dark Side Of The Moon," gives us the value of 1973.
Figure 153
Figure 154
Figure 155
This will add the value 2007 with a new key called "Graduation."
Figure 156
We can delete an entry as follows. This gets rid of the key "Thriller" and it's value.
Figure 157
Figure 158
The command checks the keys. If they are in the dictionary, they return a true.
Figure 159
If we try the same command with a key that is not in the dictionary, we get a false.
Figure 160
In order to see all the keys in the dictionary, we can use the method keys to get the keys. The
output is a list-like object with all the keys.
Figure 161
Figure 162
Check out the labs for more examples and info. on dictionaries.
TABLE OF CONTENTS
• Dictionaries
o What are Dictionaries?
o Keys
• Quiz on Dictionaries
Dictionaries
Figure 163
Dict = {"key1": 1, "key2": "2", "key3": [3, 3, 3], "key4": (4, 4, 4),
('key5'): 5, (0, 1): 6}
Dict
{'key1': 1,
'key2': '2',
'key3': [3, 3, 3],
'key4': (4, 4, 4),
'key5': 5,
(0, 1): 6}
• "key1": 1- The key is a string "key1", and its corresponding value is the integer 1.
• "key2": "2" - The key is a string "key2", and its corresponding value is the string "2".
• "key3": [3, 3, 3] - The key is a string "key3", and its corresponding value is a list [3, 3,
3]. The list contains three elements, all of which are integer 3.
• "key4": (4, 4, 4) - The key is a string "key4", and its corresponding value is a tuple (4, 4,
4). The tuple contains three elements, all of which are the integer 4.
• ('key5'): 5 - The key is a tuple ('key5',), and its corresponding value is the integer 5. Note
that the key is enclosed in parentheses to indicate it as a tuple. In this case, the tuple
contains a single element: the string "key5".
• (0, 1): 6 - The key is a tuple (0, 1), and its corresponding value is the integer
This tuple contains two elements: 0 and 1. The keys can be strings:
Each key is separated from its value by a colon ":". Commas separate the items, and the whole
dictionary is enclosed in curly braces. An empty dictionary without any items is written with just
two curly braces, like this "{}".
Figure 164
Keys
You can retrieve the values based on the names:
Figure 165
Figure 166
Now let us retrieve the keys of the dictionary using the method keys():
values
You can retrieve the values using the method values():
add
We can add an entry:
del
We can delete an entry:
Verify (in)
We can verify if an element is in the dictionary:
You will need this dictionary for the next two questions:
The Albums Back in Black, The Bodyguard and Thriller have the following music recording
sales in millions 50, 50 and 65 respectively:
a) Create a dictionary album_sales_dict where the keys are the album name and the
sales in millions are the values.
c) Find the names of the albums from the dictionary using the method keys():
d) Find the values of the recording sales from the dictionary using the method
values:
Question 2
Consider the following Python Dictionary:
Dict={"A":1,"B":"2","C":[3,3,3],"D":(4,4,4),'E':5,'F':6}
What is the result of the following operation? Dict["D"]
o '4,4,4'
o (4, 4, 4)
o 1
o [3,3,3]
Correct: This corresponds to the key 'D' or Dict['D'].
Question 3
Which of the following is the correct syntax to extract the keys of a dictionary as a list?
o list(keys(dict))
o list(dict.keys())
o dict.keys().list()
o keys(dict.list())
SETS
Let's cover sets. They are also a type of collection.
• Sets are a type of collection: This means that like lists and tuples, you can input different
Python types.
• Unlike lists and tuples, they are unordered: This means sets do not record element
position.
• Sets only have unique elements: This means there is only one of a particular element in
a set.
CREATING A SET
To define a set, you use curly brackets. You place the elements of a set within the curly brackets.
You notice there are duplicate items. When the actual set is created, duplicate items will not be
present.
Figure 167
You can convert a list to a set by using the function set. This is called type casting.
Figure 168
Figure 169
Let's go over an example. We start off with a list. We input the list to the function set.
Figure 170
The function set returns a set. Notice how there are no duplicate elements.
Figure 171
add
We can add an item to a set using the add-method. We just put the set name followed by a dot,
then the add-method. The argument is the new element of the set we would like to add, in this
case, NSYNC.
Figure 172
Figure 173
If we add the same item twice, nothing will happen as there can be no duplicates in a set.
Figure 174
Let's say we would like to remove NSYNC from set A. We can also remove an item from a set
using the remove-method. We just put the set name followed by a dot, then the remove-
method.
Figure 175
The argument is the element of the set we would like to remove, in this case, NSYNC.
After the remove-method is applied to the set, set A does not contain the item NSYNC. You
can use this method for any item in the set.
Figure 176
Verify (in)
Figure 177
Figure 178
and
There are lots of useful mathematical operations we can do between sets.
Let's define the set album_set_1. We can represent it using a red circle or venn diagram.
Figure 179
Similarly, we can define the set album_set_2. We can also represent it using a blue circle or
venn diagram.
Figure 180
Figure 181
The two circles that represent the sets combine, the overlap, represents the new set. As the
overlap is comprised with the red circle and blue circle, we define the intersection in terms of
and. In Python, we use an ampersand (&) to find the intersection of the two sets.
Figure 182
If we overlay the values of the set over the circle placing the common elements in the
overlapping area, we see the correspondence.
Figure 183
Figure 184
In Python, we simply just place the ampersand between the two sets. We see that both AC/DC
and Back in Black are in both sets. The result is a new set album_set_3 containing all the
elements in both album_set_1 and album_set_2.
Figure 185
The union of two sets is the new set of elements which contain all the items in both sets. We can
find the union of the sets album_set_1 and album_set_2 as follows.
Figure 186
Figure 187
Consider the new album album_set_3. The set contains the elements AC/DC and Back in
Black.
Figure 188
issubset
We can represent this with a Venn diagram, as all the elements and album set three are in
album_set_1. The circle representing album_set_1 encapsulates the circle representing
album_set_3. We can check if a set is a subset using the issubset method. As album_set_3
is a subset of the album_set_1, the result is true.
Figure 189
There is a lot more you can do with sets. Check out the lab for more examples.
OBJECTIVES
After completing this lab you will be able to:
• Work with sets in Python, including operations and logic operations.
TABLE OF CONTENTS
• Sets
o Set Content
o Set Operations
o Sets Logic Operations
• Quiz on Sets
Sets
Set Content
A set is a unique collection of objects in Python. You can denote a set with a pair of curly brackets
{}. Python will automatically remove duplicate items:
# Create a set
set1 = {"pop", "rock", "soul", "hard rock", "rock", "R&B", "rock", "disco"}
set1
{'R&B', 'disco', 'hard rock', 'pop', 'rock', 'soul'}
Figure 190
Set Operations
Let us go over set operations, as these can be used to change the set. Consider the set A:
# Sample set
A = set(["Thriller", "Back in Black", "AC/DC"])
A
{'AC/DC', 'Back in Black', 'Thriller'}
add
We can add an element to a set using the add() method:
If we add the same element twice, nothing will happen as there can be no duplicates in a set:
remove
We can remove an item from a set using the remove method:
Verify (in)
We can verify if an element is in the set using the in command:
# Sample Sets
album_set1 = set(["Thriller", 'AC/DC', 'Back in Black'])
album_set2 = set([ "AC/DC", "Back in Black", "The Dark Side of the Moon"])
Figure 191
As both sets contain AC/DC and Back in Black we represent these common elements
with the intersection of two circles.
Figure 192
difference
You can find all the elements that are only contained in album_set1 using the difference
method:
You only need to consider elements in album_set1; all the elements in album_set2, including
the intersection, are not included.
Figure 193
album_set2.difference(album_set1)
{'The Dark Side of the Moon'}
Figure 194
Figure 195
union
The union corresponds to all the elements in both sets, which is represented by coloring
both circles:
Figure 196
# Check if superset
set(album_set1).issuperset(album_set2)
False
# Check if subset
set(album_set2).issubset(album_set1)
False
# Check if subset
set({"Back in Black", "AC/DC"}).issubset(album_set1)
True
# Check if superset
album_set1.issuperset({"Back in Black", "AC/DC"})
True
Consider the list A = [1, 2, 2, 1] and set B = set([1, 2, 2, 1]), does sum(A) ==
sum(B)?
Create a new set album_set3 that is the union of album_set1 and album_set2:
Question 2
What method do you use to add an element to a set?
o Insert
o Extend
o Add
o Append
Correct: The add method adds elements to a set.
Question 3
What is the result of the following operation? {'a','b'} & {'a'}
o {}
o {'a','b'}
o {'b'}
o {'a'}
Correct: The intersection operation finds the elements that are in both sets
SETS
Function A function is a block of code, defining a set procedure, which is executed only
when it is called.
Immutable Immutable Objects are of in-built datatypes like int, float, bool, string, Unicode,
and tuple. In simple words, an immutable object can't be changed after it is
created.
Intersection The intersection of two given sets is the largest set, which contains all the
elements that are common to both sets.
Keys The keys () method in Python Dictionary, returns a view object that displays a
list of all the keys in the dictionary in order of insertion using Python.
Lists A list is any list of data items, separated by commas, inside square brackets.
Logic operations In Python, logic operations refer to the use of logical operators such as "and,"
"or," and "not" to perform logical operations on Boolean values (True or False).
Mutable Immutable objects are of in-built datatypes like int, float, bool, string, Unicode,
and tuple. A mutable object can be changed after it is created.
Nesting A nested function is simply a function within another function and is sometimes
called an "inner function".
Ratings in python Ratings in Python typically refer to a numerical or qualitative measure assigned
to something to indicate its quality, performance, or value.
Set operations Set operations in Python refer to mathematical operations performed on sets,
which are unordered collections of unique elements.
Venn diagram A Venn diagram is a graphical representation that uses overlapping circles to
illustrate the relationships and commonalities between sets or groups of items.
Versatile data Versatile data, in a general context, refers to data that can be used in multiple
ways, is adaptable to different applications or purposes, and is not restricted to
a specific use case.
Learning Objectives
In this module you will learn about:
• Classify conditions and branching by identifying structured scenarios with outputs.
• Work with objects and classes.
• Explain objects and classes by identifying data types and creating a class.
• Use exception handling in Python.
• Explain what functions do.
• Build a function using inputs and outputs.
• Explain how for loops and while loops work.
• Work with condition statements in Python, including operators and branching.
• Create and use loop statements in Python.
Figure 197
COMPARISON OPERATORS
Comparison operations compares some value or operand.
Figure 198
Figure 199
Figure 200
We can use the equality operator denoted with two equal signs to determine if two values are
equal. In this case, if seven is equal to six.
Figure 201
Figure 202
Figure 203
Figure 204
Consider the following equality comparison operator: If the value of the left operand, in this case,
the variable i is greater than the value of the right operand, in this case five, the condition
becomes true or else we get a false.
Let's display some values for i on the left. Let's see the value is greater than five in green and
the rest in red. If we set i equal to six, we see that six is larger than five and as a result, we get a
true.
Figure 205
Figure 206
If we set the value of i to two, we would get a false because two is less than five.
Figure 207
We can change the inequality if the value of the left operand, in this case, i is less than the value
of the right operand, in this case, six. Then condition becomes true. Again, we can represent this
with a colored number line. The areas where the inequality is true are marked in green and red
where the inequality is false. If the value for i is set to two, the result is a true. As two is less than
six.
Figure 208
Figure 209
We compare strings as well. Comparing ACDC and Michael Jackson using the equality test, we
get a false, as the strings are not the same. Using the inequality test, we get a true, as the strings
are different.
Figure 210
Figure 211
IF STATEMENT
It's helpful to think of an if statement as a locked room. If this statement is true, you can enter
the room and your program can run some predefined tasks. If the statement is false, your program
will skip the task. For example, consider the blue rectangle representing an ACDC concert. If the
individual is 18 or older, they can enter the ACDC concert. If they are under the age of 18, they
cannot enter the concert. Individual proceeds to the concert their age is 17, therefore, they are
not granted access to the concert and they must move on.
Figure 212
If the individual is 19, the condition is true. They can enter the concert then they can move on.
Figure 213
Figure 214
Therefore, the program will not execute the statement to print, "you will enter". In this case, it
will just print "move on".
Figure 215
For the case where the age is 19, we set the value of the variable age to 19. We check the if
statement. The statement is true. Therefore, the program will execute the statement to print
"you will enter". Then it will just print "move on".
Figure 216
Figure 217
If the individual is 19, the condition is true, they can enter the ACDC concert then they can move
on as before.
Figure 218
The syntax of the else statement is similar. We simply append the statement else. We then add
the expression we would like to execute with an indent.
Figure 219
For the case where the age is 17, we set the value of the variable age to 17. We check the if
statement, the statement is false. Therefore, we progress to the else statement. We run the
Figure 220
For the case where the age is 19, we set the value of the variable age to 19. We check the if
statement, the statement is true. Therefore, the program will execute the statement to print "you
will enter". The program skips the expressions in the else statement and continues to run the
rest of the expressions.
Figure 221
Figure 222
The syntax of the elif statement is similar. We simply add the statement elif with the condition.
We, then add the expression we would like to execute if the statement is true with an indent. Let's
illustrate the code on the left. An 18-year-old enters. They are not older than 18 years of age.
Therefore, the condition is false. So, the condition of the elif statement is checked. The condition
is true. So, then we would print "go see Pink Floyd". Then we would move on as before. If the
variable age was 17, the statement "go see Meat Loaf" would print. Similarly, if the age was
greater than 18, the statement "you will enter" would print.
Figure 223
Figure 224
Logic operations take Boolean values and produce different Boolean values.
Figure 225
Figure 226
Figure 227
Figure 228
Figure 229
OR OPERATOR
Let A and B represent Boolean variables. The OR operator takes in the two values and produces
a new Boolean value.
Figure 231
We can use this table to represent the different values. The first column represents the possible
values of A. The second column represents the possible values of B. The final column represents
the result of applying the OR operation. We see the OR operator only produces a false if all the
Boolean values are false.
Figure 232
Figure 233
Let's see what happens when we set the album year to 1990. The colored number line is green
when the condition is true and red when the condition is false. In this case, the condition is false.
Examining the second condition, we see that 1990 is greater than 1989. So, the condition is true.
We can verify by examining the corresponding second number line. In the final number line, the
green region indicates, where the area is true. This region corresponds to where at least one
statement is true. We see that 1990 falls in the area. Therefore, we execute the statement.
Figure 234
AND OPERATOR
Let A and B represent Boolean variables. The AND operator takes in the two values and
produces a new Boolean value.
Figure 235
We can use this table to represent the different values. The first column represents the possible
values of A. The second column represents the possible values of B. The final column represents
the result of applying the AND operation. We see the AND operator only produces a true if all
the Boolean values are true.
Figure 237
The following lines of code will print out "This album was made in the 80s" if the variable album
year is between 1980 and 1989. Let's see what happens when we set the album year to 1983.
As before, we can use the colored number line to examine where the condition is true. In this
case, 1983 is larger than 1980, so, the condition is true. Examining the second condition, we see
that 1990 is greater than 1983. So, this condition is also true. We can verify by examining the
corresponding second number line. In the final number line, the green region indicates where
the area is true. Similarly, this region corresponds to where both statements are true. We see
that 1983 falls in the area. Therefore, we execute the statement.
Figure 238
TABLE OF CONTENTS
• Condition Statements
o Comparison Operators
o Branching
o Logical operators
• Quiz on Condition Statement
Condition Statements
Comparison Operators
Comparison operations compare some value or operand and based on a condition, produce a
Boolean. When comparing two values you can use these operators:
• equal: ==
• not equal: !=
• greater than: >
• less than: <
• greater than or equal to: >=
• less than or equal to: <=
equal
Let's assign a a value of 5. Use the equality operator denoted with two equal == signs
to determine if two values are equal. The case below compares the variable a with 6.
# Condition Equal a = 5
a == 6
False
Let's display some values for i in the figure. Set the values greater than 5 in green and the rest
in red. The green region represents where the condition is True, the red where the statement is
False. If the value of i is 2, we get False as the 2 falls in the red region. Similarly, if the value for i
is 6 we get a True as the condition falls in the green region.
Figure 239
Not equal
The inequality test uses an exclamation mark preceding the equal sign, if two operands are not
equal then the condition becomes True. For example, the following condition will produce True
as long as the value of i is not equal to 6:
# Inequality Sign i = 2
i != 6
True
# Inequality Sign i = 6
i != 6
False
See the number line below. When the condition is True, the corresponding numbers are marked
in green and for where the condition is False the corresponding number is marked in red. If we
Figure 240
We can apply the same methods on strings. For example, we can use an equality operator
on two different strings. As the strings are not equal, we get a False.
If we use the inequality operator, the output is going to be True as the strings are not equal.
B 66 O 79 b 98 o 111
C 67 P 80 c 99 p 112
D 68 Q 81 d 100 q 113
E 69 R 82 e 101 r 114
F 70 S 83 f 102 s 115
G 71 T 84 g 103 t 116
H 72 U 85 h 104 u 117
I 73 V 86 i 105 v 118
J 74 W 87 j 106 w 119
K 75 X 88 k 107 x 120
L 76 Y 89 l 108 y 121
M 77 Z 90 m 109 z 122
For example, the ASCII code for! is 33, while the ASCII code for + is 43. Therefore + is larger than
! as 43 is greater than 33.
Similarly, from the table above we see that the value for A is 65, and the value for B is 66,
therefore:
# Compare characters
'B' > 'A'
True
Note: Upper Case Letters have different ASCII code than Lower Case Letters, which means the
comparison between the letters in Python is case-sensitive.
Branching
If statement
Branching allows us to run different statements for different inputs. It is helpful to think of an if
statement as a locked room, if the statement is True we can enter the room and your program
will run some predefined tasks, but if the statement is False the program will ignore the task.
For example, consider the blue rectangle representing an ACDC concert. If the individual is older
than 18, they can enter the ACDC concert. If they are 18 or younger, they cannot enter the concert.
# If statement example
age = 19
#age = 18
#expression that can be true or false if age > 18:
#within an indent, we have the expression that is run if the condition is true
print("you can enter" )
#The statements after the if statement will run regardless if the condition is true or
false
print("move on")
you can enter move on
# If statement example
age = 19
age = 18
#expression that can be true or false if age > 18:
#within an indent, we have the expression that is run if the condition is true
print("you can enter" )
#The statements after the if statement will run regardless if the condition is true or
false
print("move on")
move on
It is helpful to use the following diagram to illustrate the process. On the left side, we see
what happens when the condition is True. The person enters the ACDC concert representing the
code in the indent being executed; they then move on. On the right side, we see what happens
when the condition is False; the person is not granted access, and the person moves on. In this
case, the segment of code in the indent does not run, but the rest of the statements are run.
Figure 241
The else statement runs a block of code if none of the conditions are True before this
else statement. Let's use the ACDC concert analogy again. If the user is 17, they cannot go to the
ACDC concert, but they can go to the Meatloaf concert. The syntax of the else statement is similar
as the syntax of the if statement, as else:. Notice that there is no condition statement for else.
Try changing the values of age to see what happens:
The process is demonstrated below, where each of the possibilities is illustrated on each side of
the image. On the left is the case where the age is 17, we set the variable age to 17, and this
corresponds to the individual attending the Meatloaf concert. The right portion shows what
happens when the individual is over 18, in this case 19, and the individual is granted access to
the concert.
Figure 242
elif
The elif statement, short for else if, allows us to check additional conditions if the condition
statements before it is False. If the condition for the elif statement is True, the alternate
expressions will be run. Consider the concert example, where if the individual is 18, they will go
to the Pink Floyd concert instead of attending the ACDC or Meat-loaf concert. A person that is
18 years of age enters the area, and as they are not older than 18 they can not see ACDC, but
since they are 18 years of age, they attend Pink Floyd. After seeing Pink Floyd, they move
on. The syntax of the elif statement is similar in that we merely change the if in the if
statement to elif.
Figure 243
Feel free to change album_year value to other values -- you'll see that the result changes!
Notice that the code in the above indented block will only be executed if the results are True.
As before, we can add an else block to the if block. The code in the else block will only be
executed if the result is False.
Syntax:
if (condition): # do something
else:
# do something else
Figure 244
Feel free to change the album_year value to other values -- you'll see that the result changes
based on it!
Logical operators
Sometimes you want to check more than one condition at once. For example, you might want to
check if one condition and another condition are both True. Logical operators allow you to
combine or modify conditions.
• and
• or
• not
Figure 245
And operator
The and statement is only True when both conditions are true. The or statement is True if one
condition, or both are True. The not statement outputs the opposite truth value.
Let's see how to determine if an album was released after 1979 (1979 is not included) and
before 1990 (1990 is not included). The time periods between 1980 and 1989 satisfy this
condition. This is demonstrated in the figure below. The green on lines a and b represents
periods where the statement is True. The green on line c represents where both conditions
are True, this corresponds to where the green regions overlap.
Figure 246
Figure 247
Not operator
The not statement checks if the statement is false:
1. There are 2 sisters, Annie and Jane, born in 1996 and 1999 respectively. They want to know
who was born in a leap year. Write an if-else statement to determine who was born in a leap
year.
2. In a school canteen, children under the age of 9 are only given milk porridge for breakfast.
Children from 10 to 14 are given a sandwich, and children from 15 to 17 are given a burger.
The canteen master asks the age of the student and gives them breakfast accordingly.
Sam's age is 10. Use if-else statement to determine what the canteen master will offer to
him.
Hint: For each range of age, create an if condition and print what the student will
get according to their age
age = 10
if age <=9:
print ("You will get a bowl of porridge!")
elif age>=10 and age<=14:
print ("You will get a sandwich!")
elif age>=15 and age<=17:
print("You will get a burger!")
You will get a sandwich!
OBJECTIVE:
In this reading, you'll learn about:
• Comparison operators
• Branching
• Logical operators
Comparison operations
Comparison operations are essential in programming. They help compare values and make
decisions based on the results.
Equality operator
The equality operator checks if two values are equal. For example, in Python:
==
age = 25
if age == 25:
print("You are 25 years old.")
Here, the code checks if the variable age is equal to 25 and prints a message accordingly.
Inequality operator
The inequality operator checks if two values are not equal:
if age != 30:
print("You are not 30 years old.")
Here, the code checks if the variable age is not equal to 30 and prints a message accordingly.
if age>= 20:
Print("Yes, the Age is greater than 20")
Here, the code checks if the variable age is greater than or equal to 20 and prints a message
accordingly.
Branching is like making decisions in your program based on conditions. Think of it as real-life
choices.
The IF statement
Consider a real-life scenario of entering a bar. If you're above a certain age, you can enter;
otherwise, you cannot.
age = 20
if age >= 21:
print("You can enter the bar.") else:
print("Sorry, you cannot enter.")
Here, you are using the if statement to make a decision based on the age variable.
Logical operators
has_matching_fingerprint = True
if has_valid_id_card and has_matching_fingerprint:
open_high_security_door()
The OR operator
friend1_likes_comedy = True
friend2_likes_action = False
friend3_likes_drama = False
if friend1_likes_comedy or friend2_likes_action or friend3_likes_drama:
choose a movie()
SUMMARY
In this reading, you delved into the most frequently used operator and the concept of conditional
branching, which encompasses the utilization of if statements and if- else statements.
Question 2
What is the output of the following code segment? i=6
i<5
o False
o True
Correct: 6 is not less than 5.
Question 3
True or False. The equality operator is case-sensitive in the following code segment.
‘a’==‘A’
o True
o False
Correct: The equality operator is case-sensitive.
Question 4
Which of the following best describes the purpose of ‘elif’ statement in a conditional
structure?
o It describes the end of a conditional structure.
o It describes a condition to test for if any one of the conditions has not been met.
o It defines the condition in case the preceding conditions in the if statement are not
fulfilled.
o It describes a condition to test if all other conditions have failed.
Correct! You can use the ‘elif' ‘' statement only when you do not meet any of the prior
conditions.
Figure 248
Before we talk about loops, let's go over the range function. The range function outputs and
ordered sequence as a list I. If the input is a positive integer, the output is a sequence. The
sequence contains the same number of elements as the input but starts at zero.
Figure 249
For example, if the input is three the output is the sequence zero, one, two.
Figure 250
If the range function has two inputs where the first input is smaller than the second input, the
output is a sequence that starts at the first input. Then the sequence iterates up to but not
including the second number.
Figure 251
Figure 252
FOR LOOPS
In this section, we will cover for loops. We will focus on lists, but many of the procedures can be
used on tuples.
Figure 253
Loops perform a task over and over. Consider the group of colored squares. Let's say we would
like to replace each colored square with a white square. Let's give each square a number to make
things a little easier and refer to all the group of squares as squares. If we wanted to tell
someone to replace squares zero with a white square, we would say equals replace square zero
with a white square.
Figure 254
Figure 255
Similarly, for the next square we can say for square one in squares, square one equals white
square.
Figure 256
For the next square we can say for square two in squares, square two equals white square.
Figure 257
Figure 258
Figure 259
If we're going to perform a similar task in Python we cannot use actual squares.
Figure 260
RANGE FUNCTION
So let's use a list to represent the boxes. Each element in the list is a string representing the
color. We want to change the name of the color in each element to white. Each element in the
list has the following index. This is a syntax to perform a loop in Python. Notice the indent.
Figure 261
Figure 262
The value of I is set to zero. Each iteration of the loop starts at the beginning of the indent. We
then run everything in the indent.
Figure 263
Figure 264
Figure 265
The value of I increases by one. We repeat the process for index two.
Figure 266
process continues for the next index, until we've reached the final element.
Figure 267
Figure 268
We then start the second iteration. For the second iteration, the value of square is yellow. We
then start the third iteration.
Figure 269
Figure 270
Figure 271
The variable I is the index and the variable square is the corresponding element in the list.
Figure 272
Let's use the left part of the screen to display the different values of the variable square and I for
the various iterations of the loop.
Figure 273
Figure 274
For the second iteration. The value of the variable square is yellow, and the value of I
corresponds to its index i.e. 1.
Figure 275
WHILE LOOPS
While loops are similar to for loops but instead of executing a statement a set number of times
a while loop will only run if a condition is met.
Figure 276
Figure 277
We don't know the value of the squares beforehand. We would simply continue the process
while the square is orange or see if the square equals orange.
Figure 278
If not, we would stop. For the first example, we would check if the square was orange.
Figure 279
Figure 280
We repeat the process for the second square. The condition is met.
Figure 281
Figure 282
Figure 283
Let's use the figure on the left to represent the code. We will use a list with the names of the
color to represent the different squares.
Figure 284
We create an empty list of new squares. In reality the list is of indeterminate size.
We start the index at zero the while statement will repeatedly execute the statements
within the indent until the condition inside the bracket is false.
Figure 285
Figure 286
We append the value of the second element of the list squares to the list new squares.
Figure 287
We increment the value of I. Now the value in the array squares is purple; therefore, the condition
for the while statement is false and we exit the loop.
Figure 288
Check out the labs for more examples of loop many with real data.
Objectives
What is a Loop?
In programming, a loop is like a magic trick that allows a computer to do something over and
over again. Imagine you are a magician's assistant, and your magician friend asks you to pull a
rabbit out of a hat, but not just once - they want you to keep doing it until they tell you to stop.
That is what loops do for computers - they repeat a set of instructions as many times as needed.
Figure 289
Think about when you need to count from 1 to 10. Doing it manually is easy, but what if you had
to count to a million? Typing all those numbers one by one would be a nightmare! This is where
loops come in handy. They help computers repeat tasks quickly and accurately without getting
tired.
For Loops
For loops are like a superhero's checklist. A for loop in programming is a control structure that
allows the repeated execution of a set of statements for each item in a sequence, such as
elements in a list or numbers in a range, enabling efficient iteration and automation of tasks
In this example, the for loop picks each color from the colors list and prints it on the screen. You
don't have to write the same code for each color - the loop does it automatically!
Sometimes you do not want to paint a rainbow, but you want to count the number of steps to
reach your goal. A range-based for loop is like having a friendly step counter that helps you
reach your target.
Here is how you might use a for loop to count from 1 to 10:
Here, the range(1, 11) generates a sequence from 1 to 10, and the for loop goes through
each number in that sequence, printing it out. It's like taking 10 steps, and you're guided by the
loop!
Range Function
The range function in Python generates an ordered sequence that can be used in loops. It takes
one or two arguments:
• If given one argument (e.g., range(11)), it generates a sequence starting from 0 up to (but
not including) the given number.
• If given two arguments (e.g., range(1, 11)), it generates a sequence starting from the
first argument up to (but not including) the second argument.
With this loop, you not only get the fruit but also its position in the list. It's as if you have a
magical guide pointing out each fruit's location!
For example, here's how you might use a while loop to count from 1 to 10:
count = 1
while count <= 10:
print(count)
count += 1
Both for and while loops have their special moves, but they follow a pattern:
• Initialization: You set up things like a starting point or conditions.
• Condition: You decide when the loop should keep going and when it should stop.
• Execution: You do the task inside the loop.
• Update: You make changes to your starting point or conditions to move forward.
• Repeat: The loop goes back to step 2 until the condition is no longer true.
For Loops: Use for loops when you know the number of iterations in advance and want to
process each element in a sequence. They are best suited for iterating over collections and
sequences where the length is known.
While Loops: Use while loops when you need to perform a task repeatedly as long as a certain
condition holds true. While loops are particularly useful for situations where the number of
iterations is uncertain or where you're waiting for a specific condition to be met.
SUMMARY
In this adventure into coding, we explored loops in Python - special tools that help us do things
over and over again without getting tired. We met two types of loops: "for loops" and
"while loops."
For Loops were like helpers that made us repeat tasks in order. We painted colors, counted
numbers, and even got a helper to tell us where things were in a list. For loops made our job
easier and made our code look cleaner.
While Loops were like detectives that kept doing something as long as a rule was true. They
helped us take steps, guess numbers, and work until we were tired. While loops were like smart
assistants that didn't stop until we said so.
LOOPS IN PYTHON
Welcome! This notebook will teach you about the loops in the Python Programming Language.
By the end of this lab, you'll know how to use the loop statements in Python, including for loop,
and while loop.
TABLE OF CONTENTS
• Loops
o Range
o What is for loop?
o What is while loop?
• Quiz on Loops
Loops
Range
Sometimes, you might want to repeat a given operation many times. Repeated executions like
this are performed by loops. We will look at two types of loops, for loops and while
loops.
Before we discuss loops lets discuss the range object. It is helpful to think of the range object as
an ordered list. For now, let's look at the simplest case. If we would like to generate an object
that contains elements ordered from 0 to 2 we simply use the following command:
Figure 290
NOTE: While in Python 2.x it returned a list as seen in video lessons, in 3.x it
returns a range object.
for i in range(N):
print(dates[i])
1982
1980
1973
The code in the indent is executed N times, each time the value of i is increased by 1 for every
execution. The statement executed is to print out the value in the list at index i as shown here:
Figure 291
Figure 292
Figure 293
Figure 294
Figure 295
# Loop through the list and iterate on both index and element value
squares=['red', 'yellow', 'green', 'purple', 'blue']
for i, square in enumerate(squares):
print(i, square)
red
yellow
green
purple
blue
count = 1
while count <= 5: print(count) count += 1
1
2
3
4
5
while(year != 1973):
print(year)
i = i + 1
year = dates[i]
A while loop iterates merely until the condition in the argument is not met, as shown in the
following figure:
Figure 296
Print the elements of the following list: Genres=[ 'rock', 'R&B', 'Soundtrack',
'R&B', 'soul', 'pop'] Make sure you follow Python conventions.
Write a for loop that prints out the following list: squares=['red', 'yellow', 'green',
'purple', 'blue']
rating = PlayListRatings[i]
10
9.5
10
8
7.5
Write a while loop to copy the strings 'orange' of the list squares to the
list new_squares. Stop and exit the loop if the value on the list is not 'orange':
Hint: Write two for loops. One to print the multiplication table of 6 and the other for
7
print("7*",i,"=",7*i)
Multiplication table of 6:
6* 0 = 0
6* 1 = 6
6* 2 = 12
6* 3 = 18
6* 4 = 24
6* 5 = 30
6* 6 = 36
6* 7 = 42
6* 8 = 48
6* 9 = 54
Multiplication table of 7:
7* 0 = 0
Hint: Use while loop to iterate over the elements of the list. Use if-else statement
inside the for loop to check the length of each element and if the length is 7, add the
element to a new list.
o
1
2
3
o
0
1
2
3
o
0
1
o
0
1
2
Question 2
What is the output of the following:
for x in ['A','B','C']:
print(x+'A')
o AA
BB
CC
o A
B
C
o A
B
C
A
o AA
BA
CA
o A0
B1
C2
o AA
BB
CC
o 0A
1B
2C
o 0
1
2
Figure 297
Figure 298
The function is just a piece of code you can reuse. You can implement your own
function, but in many cases, you use other people’s functions.
Figure 299
Figure 300
Let the orange and yellow squares represent similar blocks of code.
Figure 301
We can run the code using some input and get an output. If we define a function to do the task
we just have to call the function. Let the small squares represent the lines
of code used to call the function.
Figure 302
Figure 303
Figure 304
Figure 305
Figure 306
Figure 307
When we call the function f1, we pass an input to the function. These values are passed
to all those lines of code you wrote.
Figure 308
Figure 309
For example, you can input this value to a new function f2. When we call this new function f2,
the value is passed to another set of lines of code.
Figure 310
Figure 311
Figure 312
You can save these functions and reuse them or use other people’s functions.
Figure 313
Figure 314
LEN
The function len takes in an input of type sequence, such as a string or list, or type collection,
such as a dictionary or set, and returns the length of that sequence or collection.
Figure 315
Figure 316
Figure 317
SUM
The function sum takes in an iterable like a tuple or list and returns the total of all the elements.
Consider the following list. We pass the list into the sum function and assign the result to the
variable S. The function determines the total of all the elements, then returns it, in this case, the
value is 70.
Figure 318
SORTED
There are two ways to sort a list:
1. The first is using the function sorted.
2. We can also use the list method sort. (Methods are similar to functions).
Let's use this as an example to illustrate the difference. The function sorted returns a new sorted
list or tuple.
Consider the list album_ratings. We can apply the function sorted to the list
album_ratings and get a new list sorted_album_rating. The result is a new sorted list.
If we look at the list album_ratings, nothing has changed. Generally, functions take an input,
in this case, a list. They produce a new output, in this instance, a sorted list.
Figure 320
SORT
If we use the method sort, the list album_ratings will change and no new list will be
created. Let's use this diagram to help illustrate the process. In this case, the rectangle
represents the list album_ratings.
Figure 321
Figure 322
Figure 323
MAKING FUNCTIONS
Now that we have gone over how to use functions in Python, let’s see how to build our own
functions.
Figure 324
Figure 325
Figure 326
Figure 327
Figure 328
Figure 329
After we define the function, we can call it. The function will add 1 to 5 and return a
6. We can call the function again; this time assign it to the variable "c" The value for c is 11.
Figure 330
Figure 331
Now the sequences of commands are run, the value of "a" is 5. "b" would be assigned a value of
6. We then return the value of b, in this case, as b was assigned a value of 6, the function returns
a 6.
Figure 332
If we call the function again, the process starts from scratch; we pass in an 8. The subsequent
operations are performed. Everything that happened in the last call will happen again with a
different value of "a" The function returns a value, in this case, 9. Again, this is just a helpful
analogy.
Figure 333
Figure 334
MULTIPLE PARAMETERS
A function can have multiple parameters. The function mult multiplies two numbers; in other
words, it finds their product. If we pass the integers 2 and 3, the result is a new integer. If we
pass the integer 10 and the float 3.14, the result is a float 31.4.
Figure 335
Figure 336
Figure 337
Figure 338
Figure 339
Figure 340
COLLECTING ARGUMENTS
Variadic parameters allow us to input a variable number of elements. Consider the following
function: the function has an asterisk on the parameter names. When we call the function, three
parameters are packed into the tuple names. We then iterate through the loop; the values are
printed out accordingly.
Figure 341
If we call the same function with only two parameters as inputs, the variable names only contain
two elements. The result is only two values are printed out.
Figure 342
The scope of a variable is the part of the program where that variable is accessible. Variables
that are defined outside of any function are said to be within the global scope, meaning they can
be accessed anywhere after they are defined. Here we have a function that adds the string DC to
the parameter x. When we reach the part where the value of x is set to AC, this is within the
global scope, meaning x is accessible anywhere after it is defined. A variable defined in the
global scope is called a global variable. When we call the function, we enter a new scope or the
scope of AddDC. We pass as an argument to the AddDC function, in this case, AC. Within the
scope of the function, the value of x is set to ACDC. The function returns the value and is
assigned to z. Within the global scope, the value z is set to ACDC.
Figure 343
Figure 344
Local variables only exist within the scope of a function. Consider the function Thriller; the local
variable Date is set to 1982. When we call the function, we create a new scope. Within that scope
of the function, the value of the date is set to 1982. The value of date does not exist within the
global scope.
Figure 345
Variables inside the global scope can have the same name as variables in the local scope with no
conflict. Consider the function Thriller; the local variable Date is set to 1982. The global variable
date is set to 2017. When we call the function, we create a new scope. Within that scope, the
value of the date is set to 1982. If we call the function, it returns the value of Date in the local
scope, in this case, 1982. When we print in the global scope, we use the global variable value.
The global value of the variable is 2017. Therefore, the value is set to 2017.
Figure 346
If a variable is not defined within a function, Python will check the global scope. Consider the
function ACDC. The function has the variable Rating, with no value assigned. If we define the
variable Rating in the global scope, then call the function, Python will see there is no value for
the variable Rating. As a result, Python will leave the scope and check if the variable Rating
exists in the global scope. It will use this value of Rating in the global scope within the scope of
ACDC. In the function, will print out a 9. The value of z in the global scope will be 10, as we
added 1. The value of Rating will be unchanged within the global scope.
Figure 347
Consider the function PinkFloyd. If we define the variable ClaimedSales with the keyword
global, the variable will be a global variable. We call the function PinkFloyd. The variable
ClaimedSales is set to the string “45 million” in the global scope. When we print the variable,
we get a value of “45 million.”
Figure 348
There is a lot more you can do with functions. Check out the lab for more examples.
OBJECTIVES:
By the end of this reading, you should be able to:
1. Describe the function concept and the importance of functions in programming
2. Write a function that takes inputs and performs tasks
3. Use built-in functions like len(), sum(), and others effectively
4. Define and use your functions in Python
5. Differentiate between global and local variable scopes
6. Use loops within the function
7. Modify data structures using functions
INTRODUCTION TO FUNCTIONS
A function is a fundamental building block that encapsulates specific actions or computations.
As in mathematics, where functions take inputs and produce outputs, programming functions
perform similarly. They take inputs, execute predefined actions or calculations, and then return
an output.
Purpose of functions
Functions promote code modularity and reusability. Imagine you have a task that needs to be
performed multiple times within a program. Instead of duplicating the same code at various
places, you can define a function once and call it whenever you need that task. This reduces
redundancy and makes the code easier to manage and maintain.
Inputs (Parameters)
Functions operate on data, and they can receive data as input. These inputs are known as
parameters or arguments. Parameters provide functions with the necessary information they
need to perform their tasks. Consider parameters as values you pass to a function, allowing it to
work with specific data.
Performing tasks
Once a function receives its input (parameters), it executes predefined actions or computations.
These actions can include calculations, operations on data, or even more complex tasks. The
purpose of a function determines the tasks it performs. For instance, a function could calculate
the sum of numbers, sort a list, format text, or fetch data from a database.
Producing outputs
After performing its tasks, a function can produce an output. This output is the result of the
operations carried out within the function. It's the value that the function “returns” to the code
that called it. Think of the output as the end product of the function's work. You can use this
output in your code, assign it to variables, pass it to other functions, or even print it out for display.
Example:
Consider a function named calculate_total that takes two numbers as input (parameters), adds
them together, and then produces the sum as the output. Here's how it works:
To use a built-in function, you simply call the function's name followed by parentheses. Any
required arguments or parameters are passed into the function within these parentheses. The
function then performs its predefined task and may return an output you can use in your code.
Len()
len(): Calculates the length of a sequence or collection
sum()
sum(): Adds up the elements in an iterable (list, tuple, and so on)
max()
max(): Returns the maximum value in an iterable
min()
min(): Returns the minimum value in an iterable
Python's built-in functions offer a wide array of functionalities, from basic operations like len()
and sum() to more specialized tasks.
def function_name():
pass
def greet(name):
print("Hello, " + name)
result = greet("Alice")
print(result) # Output: Hello, Alice
Return statement
This line initializes a global variable called global_variable and assigns it the value "I'm
global".
Global variables are accessible throughout the entire program, both inside and
outside functions.
def example_function():
local_variable = "I'm local"
print(global_variable) # Accessing global variable
print(local_variable) # Accessing local variable
example_function()
In this part, you call the example_function() by invoking it. This results in the function's code
being executed.
As a result of this function call, it will print the values of the global and local variables within the
function.
Part 4: Accessing global variable outside the function
After calling the function, you print the value of the global variable global_variable outside
the function. This demonstrates that global variables are accessible inside and outside of
functions.
Part 5: Attempting to access local variable outside the function
Local variables are only visible and accessible within the scope of the function where
they are defined.
def print_numbers(limit):
for i in range(1, limit+1):
print(i)
print_numbers(5) # Output: 1 2 3 4 5
def greet(name):
return "Hello, " + name
for _ in range(3):
print(greet("Alice"))
In this part, you start by creating an empty list named my_list. This empty list serves as the data
structure that you will modify throughout the code.
In this part, you define another function called remove_element. It also takes two parameters:
Here, you use the add_element function to add 3 elements (42, 17, and 99) to the my_list.
These are added one at a time using function calls.
This part simply prints the current state of the my_list to the console, allowing us to see the
elements that have been added so far.
In this part, you use the remove_element function to remove elements from the my_list. First,
you attempt to remove 17 (which is in the list), and then you try to remove 55 (which is not in
Finally, you print the updated my_list to the console. This allows us to observe the
modifications made to the list by adding and removing elements using the defined functions.
CONCLUSION
Congratulations! You've completed the Reading Instruction Lab on Python functions. You've
gained a solid understanding of functions, their significance, and how to create and use them
effectively. These skills will empower you to write more organized, modular, and powerful code
in your Python projects.
Objectives
FUNCTIONS IN PYTHON
Welcome! This notebook will teach you about the functions in the Python Programming
Language. By the end of this lab, you'll know the basic concepts about function, variables, and
how to use functions.
Table of Contents
• Functions
o What is a function?
o Variables
o Functions Make Things Simple
• Pre-defined functions
• Using if/else Statements and Loops in Functions
• Setting default argument values in your custom functions
• Global variables
• Scope of a Variable
• Collections and Functions
• Quiz on Loops
Functions
A function is a reusable block of code which performs operations specified in the function. They
let you break down tasks and allow you to reuse your code in different programs.
There are two types of functions :
• Pre-defined functions
• User defined functions
What is a Function?
You can define functions to provide the required functionality. Here are simple rules to define a
function in Python:
• Functions blocks begin def followed by the function name and parentheses ().
• There are input parameters or arguments that should be placed within these
parentheses.
Figure 349
add(a)
add 1 to a
result = Mult(12,2)
print(result)
24
The same function can be used for different data types. For example, we can multiply two
integers:
Note how the function terminates at the return statement, while passing back a value. This
value can be further assigned to a different variable as desired.
The same function can be used for different data types. For example, we can multiply two
integers:
Two Floats:
Variables
# Function Definition
def square(a):
# Local variable b
b = 1
c = a * a + b
print(a, "if you square + 1", c)
return(c)
Figure 350
x = 3
# Makes function call and return function a y y = square(x)
y
3 if you square + 1 10
10
If there is no return statement, the function returns None. The following two functions are
equivalent:
# Define functions, one with return value None and other without return value
def MJ():
print('Michael Jackson')
def MJ1():
print('Michael Jackson') return(None)
Create a function con that concatenates two strings using the addition operation:
Consider the two lines of code in Block 1 and Block 2: the procedure for each block is identical.
The only thing that is different is the variable names and values.
Block 1:
Block 2:
This function takes two inputs, a and b, then applies several operations to return c. We simply
define the function, replace the instructions with the function, and input the new values of a1,
b1 and a2, b2 as inputs. The entire process is demonstrated in the figure:
Figure 351
Code Blocks 1 and Block 2 can now be replaced with code Block 3 and code Block 4.
Block 3:
a1 = 4
b1 = 5
c1 = Equation(a1, b1) c1
5
Block 4:
a2 = 0
b2 = 0
c2 = Equation(a2, b2)
c2
0
Pre-defined functions
There are many pre-defined functions in Python, so let's start with the simple ones.
Sum()
The sum() function adds all the elements in a list or tuple:
Len()
The len() function returns the length of a list or tuple:
The return() function is particularly useful if you have any IF statements in the function, when
you want your output to be dependent on some condition:
# Function example
def type_of_album(artist, album, year_released):
print(artist, album, year_released)
if year_released > 1980:
return "Modern"
else:
return "Oldie"
We can use a loop in a function. For example, we can print out each element in a list:
The relational operators compare the Unicode values of the characters of the strings from the
zeroth index till the end of the string. It then returns a boolean value according to the operator
used.
# Define a funtion
def check_string(text):
This program uses a user-defined function named compareStrings() to compare two strings.
This function receives both strings as its argument and returns 1 if both strings are equal using
== operator
# Declare two different variables as string1 and string2 and pass string in it
string1 = "Michael Jackson is the best"
string2 = "Michael Jackson is the best"
#step4: Use for loop to iterate words and values to the dictionary
for key in words:
Dict[key] = words.count(key)
You can set a default value for arguments in your function. For example, in the
isGoodRating() function, what if we wanted to create a threshold for what we consider to be a
good rating? Perhaps by default, we should have a default rating of 4:
def isGoodRating(rating=4):
if(rating < 7):
print("this album sucks it's rating is",rating)
else:
print("this album is good its rating is",rating)
isGoodRating()
isGoodRating(10)
this album sucks it's rating is 4
this album is good its rating is 10
So far, we've been creating variables within functions, but we have not discussed variables
outside the function. These are called global variables. Let's try to see what
printer1 returns:
It's because all the variables we create in the function is a local variable, meaning that the
variable assignment does not persist outside the function.
But there is a way to create global variables from within a function as follows:
Scope of a Variable
The scope of a variable is the part of that program where that variable is accessible. Variables
that are declared outside of all function definitions, such as the myFavouriteBand
variable in the code shown here, are accessible from anywhere within the program. As a result,
such variables are said to have global scope, and are known as global variables.
myFavouriteBand is a global variable, so it is accessible from within the getBandRating function,
and we can use it to determine a band's rating. We can also use it outside of the function, such as
when we pass it to the print function to display it:
Take a look at this modified version of our code. Now the myFavouriteBand variable is defined
within the getBandRating function. A variable that is defined within a function is said to be a
SUMMARY VIDEOS LAB 251 of 590
local variable of that function. That means that it is only accessible from within the
function in which it is defined. Our getBandRating function will still work, because
myFavouriteBand is still defined within the function. However, we can no
longer print myFavouriteBand outside our function, because it is a local variable of our
getBandRating function; it is only defined within the getBandRating function:
del myFavouriteBand
def getBandRating(bandname):
myFavouriteBand = "AC/DC"
if bandname == myFavouriteBand:
return 10.0
else:
return 0.0
Finally, take a look at this example. We now have two myFavouriteBand variable definitions.
The first one of these has a global scope, and the second of them is a local variable within the
getBandRating function. Within the getBandRating function, the local variable takes
precedence. Deep Purple will receive a rating of 10.0 when passed to the getBandRating
function. However, outside of the getBandRating function, the getBandRating s local
variable is not defined, so the myFavouriteBand variable we print is the global variable, which
has a value of AC/DC:
# Example of global variable and local variable with the same name
myFavouriteBand = "AC/DC"
def getBandRating(bandname):
myFavouriteBand = "Deep Purple"
if bandname == myFavouriteBand:
return 10.0
else:
return 0.0
When the number of arguments are unknown for a function, They can all be packed into a tuple
as shown:
def printAll(*args): # All the arguments are 'packed' into args which can be treated
like a tuple
print("No of arguments:", len(args))
for argument in args:
print(argument)
#printAll with 3 arguments
printAll('Horsefeather','Adonis','Bone')
#printAll with 4 arguments
printAll('Sidecar','Long Island','Mudslide','Carriage')
No of arguments: 3
Horsefeather
Adonis
Bone
No of arguments: 4
Sidecar
Long Island
Mudslide
Carriage
def printDictionary(**args):
for key in args:
print(key + " : " + args[key])
printDictionary(Country='Canada',Province='Ontario',City='Toronto')
Country : Canada
Province : Ontario
City : Toronto
Functions can be incredibly powerful and versatile. They can accept (and return) data types,
objects and even other functions as arguements. Consider the example below:
def addItems(list):
list.append("Three")
list.append("Four")
myList = ["One","Two"]
addItems(myList)
myList
['One', 'Two', 'Three', 'Four']
Note how the changes made to the list are not limited to the functions scope. This occurs as it is
the lists reference that is passed to the function - Any changes made are on the original instance
of the list. Therefore, one should be cautious when passing mutable objects into functions.
Come up with a function that divides the first input by the second input:
return(a + b)
Can the con function we defined before be used to add two integers or strings?
con(2, 2)
4
Can the con function we defined before be used to concatenate lists or tuples?
Write a function code to find total count of word little in the given string: "Mary had a
little lamb Little lamb, little lamb Mary had a little lamb.Its fleece
was white as snow And everywhere that Mary went Mary went, Mary went
Everywhere that Mary went The lamb was sure to go"**
Question 2
What does the following function return: len([sum([0,0,1])]) ?
o Error
o 0
o 3
o 1
Correct: The function returns the length of the sum of the elements in the list.
Question 3
What is the value of list L after the following code segment
is run?
L=[1,3,2]
sorted(L)
o [3,2,1]
o [0,0,0]
o [1,3,2]
o [1,3,2]
Correct: sorted is a function that returns a new list. It does not change the list L.
def print_function(A):
for a in A:
print(a + '1')
print_function(['a', 'b', 'c'])
o abc1
o a1
b1
c1
o a1
o a
b
c
Question 5
What is the output of the following lines of code?
def Print(A):
for a in A:
print(a+'1')
Print(['a','b','c'])
o a1
o a1 b1 c1
o abc
Correct: Correct, the function concatenates a string.
Figure 352
Figure 353
INTRODUCTION
Have you ever mistakenly entered a number when you were supposed to enter text in an input
field? Most of us have either in error or when testing out a program, but do you know why it gave
an error message instead of completing and terminating the program? In order for the error
message to appear an event was triggered in the background. This event was activated because
the program tried to perform a computation on the name entry and realized the entry contained
numbers and not letters.
By encasing this code in an exception handler, the program knew how to deal with this type of
error and was able to output the error message to continue along with the program.
This is one of many errors that can happen when asking for user input, so let’s see how
exception handling works.
Figure 354
Figure 355
Once it finds the correct exception to handle the error it will then execute that line of code.
Figure 356
For example, let’s say you are writing a program that will open and write a file.
Figure 357
Figure 358
Because of this error the program skipped over the code lines under the “try”
statement and went directly to the exception line.
Figure 359
Since this error fell within the IOError guidelines it printed “Unable to open or read the data in
the file.” to our console.
Figure 360
When writing simple programs we can sometimes get away with only one except statement,
but what happens if another error occurs that is not caught by the IOError?
Figure 361
For example, let’s say our small program was just one section of a much larger program that
was over a thousand lines of code. Our task was to debug the program as it kept throwing an
error causing a disruption for our users.
Figure 362
When investigating the program you found this error kept appearing. Because this error had no
details you ended up spending hours trying to pinpoint and fix the error.
Figure 363
Figure 364
By adding this else statement it will provide us a notification to the console that “The file was
written successfully”.
Figure 365
TRY…EXCEPT…ELSE…FINALLY
Now that we have defined what will happen if our program executes properly, or if an error
occurs there is one last statement to add. For this example since we are opening a file the last
thing we need to do is close the file. By adding a finally statement it will tell the program to close
the file no matter the end result and print “File is now closed” to our console.
Figure 366
Figure 367
Objectives
1. Understanding Exceptions
2. Distinguishing Errors from Exceptions
3. Familiarity with Common Python Exceptions
4. Managing Exceptions Effectively
In the world of programming, errors and unexpected situations are certain. Python, a popular
and versatile programming language, equips developers with a powerful toolset to manage
these unforeseen scenarios through exceptions and error handling.
Exceptions are alerts when something unexpected happens while running a program. It could
be a mistake in the code or a situation that was not planned for. Python can
raise these alerts automatically, but we can also trigger them on purpose using the raise
command. The cool part is that we can prevent our program from crashing by handling exceptions.
Hold on, what is the difference between errors and exceptions? Well, errors are usually big
problems that come from the computer or the system. They often make the program stop
working completely. On the other hand, exceptions are more like issues we can control. They
happen because of something we did in our code and can usually be fixed, so the program keeps
going.
Here is the difference between Errors and exceptions:
Nature Errors are often severe and can Exceptions are generally less severe and
lead to program crashes or can be caught and handled to prevent
abnormal termination. program termination.
Categorization Errors are not classified into Exceptions are categorized into various
categories. classes, such as “ArithmeticError,” “
IOError,” ValueError,” etc., based on
their nature.
Here are a few examples of exceptions we often run into and can handle using this tool:
ZeroDivisionError:
This error arises when an attempt is made to divide a number by zero. Division by zero is
undefined in mathematics, causing an arithmetic error. For example:
result = 10 / 0 print(result)
# Raises ZeroDivisionError
ValueError:
This error occurs when an inappropriate value is used within the code. An example of this is
when trying to convert a non-numeric string to an integer: For example:
num = int("abc")
# Raises ValueError
FileNotFoundError:
This exception is encountered when an attempt is made to access a file that does not exist. For
example:
my_list = [1, 2, 3]
value = my_list[1] # No IndexError, within range missing = my_list[5] # Raises
IndexError
KeyError:
The KeyError arises when an attempt is made to access a non- existent key in a dictionary.
For example:
my_dict = {"name": "Alice", "age": 30}
value = my_dict.get("city") # No KeyError, using .get() method missing = my_dict["city"] #
Raises KeyError
TypeError:
The TypeError occurs when an object is used in an incompatible manner. An example includes
trying to concatenate a string and an integer: For example:
AttributeError:
An AttributeError occurs when an attribute or method is accessed on an object that doesn't
possess that specific attribute or method. For example:
text = "example"
length = len(text) # No AttributeError, correct method usage missing =
text.some_method() # Raises AttributeError
ImportError:
This error is encountered when an attempt is made to import a module that is unavailable. For
example:
import non_existent_module
Note: Please remember, the exceptions you will encounter are not limited to just
these. There are many more in Python. However, there is no need to worry. By using
the technique provided below and following the correct syntax, you will be able to
handle any exceptions that come your way.
NEXT STEP
As we finish up this reading, you are ready to move on to the next part where you will practice
handling errors. For better learning, try out different types of data in the lab. This way, you will
encounter various errors and learn how to deal with them effectively. This knowledge will help
you write stronger and more reliable code in the future.
OBJECTIVES
After completing this lab you will be able to:
• Understand exceptions
• Handle the exceptions
TABLE OF CONTENTS
• What is an Exception?
• Exception Handling
WHAT IS AN EXCEPTION?
In this section you will learn about what an exception is and see examples of them.
Definition
An exception is an error that occurs during the execution of code. This error causes the code to
raise an exception and if not prepared to handle it will halt the execution of the code.
Examples
1/0
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
/tmp/ipykernel_67/2354412189.py in <module>
----> 1 1/0
y = a + 5
NameError Traceback (most recent call last)
/tmp/ipykernel_68/1161414849.py in <module>
----> 1 y = a + 5
NameError: name 'a' is not defined
NameError -- in this case, it means that you tried to use the variable a when it was not defined.
a = [1, 2, 3] a[10]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
/tmp/ipykernel_67/1542938915.py in <module>
1 a = [1, 2, 3]
----> 2 a[10]
Try Except
A try except will allow you to execute code that might raise an exception and in the case of any
exception or a specific one we can handle or catch the exception and execute specific code. This
will allow us to continue the execution of our program even if there is an exception.
Python tries to execute the code in the try block. In this case if there is any exception raised by
the code in the try block, it will be caught and the code block in the except block will be
executed. After that, the code that comes after the try except will be executed.
try:
# code to try to execute except:
# code to execute if there is an exception
a = 1
try:
b = int(input("Please enter a number to divide a"))
a = a/b
print("Success a=",a)
except:
print("There was an error")
Please enter a number to divide a 0
There was an error
A specific try except allows you to catch certain exceptions and also execute certain code
depending on the exception. This is useful if you do not want to deal with some exceptions and
the execution should halt. It can also help you find errors in your code that you might not be
aware of. Furthermore, it can help you differentiate responses to different exceptions. In this
case, the code after the try except might not run depending on the error.
try:
# code to try to execute
except (ZeroDivisionError, NameError):
# code to execute if there is an exception of the given types
# code that will execute if there is no exception or a one that we are handling
File "/tmp/ipykernel_425/2752586831.py", line 5
except (ZeroDivisionError, NameError):
^
IndentationError: expected an indented block
try:
# code to try to execute
except ZeroDivisionError:
# code to execute if there is a ZeroDivisionError
except NameError:
# code to execute if there is a NameError
# code that will execute if there is no exception or a one that we are handling
File "/tmp/ipykernel_425/2664809330.py", line 5
except ZeroDivisionError:
^
IndentationError: expected an indented block
You can also have an empty except at the end to catch an unexpected exception:
Do not run, just to illustrate:
try:
# code to try to execute
except ZeroDivisionError:
# code to execute if there is a ZeroDivisionError
except NameError:
# code to execute if there is a NameError
except:
# code to execute if ther is any exception
# code that will execute if there is no exception or a one that we are handling
a = 1
try:
b = int(input("Please enter a number to divide a"))
a = a/b
print("Success a=",a)
except ZeroDivisionError:
print("The number you provided cant divide 1 because it is 0")
except ValueError:
print("You did not provide a number")
else allows one to check if there was no exception when executing the try block. This is useful
when we want to execute something only if there were no errors.
Do not run, just to illustrate
try:
# code to try to execute
except ZeroDivisionError:
# code to execute if there is a ZeroDivisionError
except NameError:
# code to execute if there is a NameError
except:
# code to execute if ther is any exception
else:
# code to execute if there is no exception
# code that will execute if there is no exception or a one that we are handling
File "/tmp/ipykernel_425/301467801.py", line 5
except ZeroDivisionError:
^
IndentationError: expected an indented block
finally allows us to always execute something even if there is an exception or not. This is
usually used to signify the end of the try except.
try:
# code to try to execute
except ZeroDivisionError:
# code to execute if there is a ZeroDivisionError
except NameError:
# code to execute if there is a NameError
except:
# code to execute if ther is any exception
else:
# code to execute if there is no exception
finally:
# code to execute at the end of the try except no matter what
# code that will execute if there is no exception or a one that we are handling
File "/tmp/ipykernel_425/2829442695.py", line 5
except ZeroDivisionError:
^
IndentationError: expected an indented block
a = 1
try:
b = int(input("Please enter a number to divide a"))
a = a/b
except ZeroDivisionError:
print("The number you provided cant divide 1 because it is 0")
except ValueError:
print("You did not provide a number")
except:
print("Something went wrong")
else:
print("success a=",a)
Please enter a number to divide a 1
success a= 1.0
Now let’s let the user know that we are done processing their answer. Using the finally, let's add
a print.
a = 1
try:
b = int(input("Please enter a number to divide a"))
a = a/b
except ZeroDivisionError:
print("The number you provided cant divide 1 because it is 0")
except ValueError:
print("You did not provide a number")
except:
print("Something went wrong")
else:
print("success a=",a)
finally:
print("Processing Complete")
Please enter a number to divide a 1
success a= 1.0
Processing Complete
Imagine you have two numbers and want to determine what happens when you divide one
number by the other. To do this, you need to create a Python function called safe_divide. You
give this f u n c t i o n 2 numbers, a 'numerator' and a 'denominator'. The 'numerator' is
the number you want to divide, and the 'denominator' is the number you want to divide
by. Use the user input method of Python to take the values.
The function should be able to do the division for you and give you the result. But here's the
catch: if you try to divide by zero (which is not allowed in math), the function should be smart
enough to catch that and tell you that it's not possible to divide by zero. Instead of showing an
error, it should return None, which means 'nothing' or 'no value', and print "Error: Cannot
divide by Zero.
Hint:
Follow these:
• Define a function to perform the division and use two arguments.
• Use the try-except block to handle ZeroDivisionError.
• Return the result of the division if no error occurs.
• Return None if division by zero occurs.
• take user input for numerator and denominator values.
• call and print your function with the user inputs.
def safe_divide(numerator,denominator):
try:
result = numerator / denominator
return result
except ZeroDivisionError:
print("Error: Cannot divide by zero.")
return None
# Test case
numerator=int(input("Enter the numerator value:"))
denominator=int(input("Enter the denominator value:"))
print(safe_divide(numerator,denominator))
Enter the numerator value: 2
Enter the denominator value: 0
Error: Cannot divide by zero.
None
Note: Practice handling exceptions by trying different input types like using integers,
strings, zero, negative values, or other data types.
Imagine you have a number and want to calculate its square root. To do this, you need to
create a Python function. You give this function one number, 'number1'.
The function should generate the square root value if you provide a positive integer or float value
as input. However, the function should be clever enough to detect the mistake if you enter a
negative value. It should kindly inform you with a message saying, 'Invalid input! Please
enter a positive integer or a float value.
Hint:
Follow these:
* Define a function to perform square root of the argument.
* Use try-except block for error handling.
* Use `sqrt()` function from the `math` package to calculate the square root. Catch ValueError and display
the error message."
* Take user input of the value, number1.
* Test with negative numbers to validate error handling.
def perform_calculation(number1):
try:
result = math.sqrt(number1)
print(f"Result: {result}")
except ValueError:
print("Error: Invalid input! Please enter a positive integer or a float
value.")
# Test case
number1=float(input("Enter the number:"))
perform_calculation(number1)
Enter the number: 4
Result: 2.0
Note:- Practice handling exceptions by trying different input types like using integers,
strings, zero, negative values, or other data types.
Imagine you have a number and want to perform a complex mathematical task. The calculation
requires dividing the value of the input argument "num" by the difference between "num" and 5,
and the result has to be stored in a variable called "result".
You have to define a function so that it can perform that complex mathematical task. The function
should handle any potential errors that occur during the calculation. To do this, you can use a
try-except block. If any exception arises during the calculation, it should catch the error using the
generic exception class "Exception" as "e". When an exception occurs, the function should
display "An error occurred during calculation.
Follow these:
* Define a function for the complex calculation and pass any argument.
* Use a try-except block for error handling.
* Perform the calculation and store the result in "result."
* Catch any exceptions using Exception as e.
* Display "An error occurred during calculation." when an exception is caught.
* take user input
* Call the defined function with the user input
Enter a number: 5
An error occurred during calculation.
Note: Practice handling exceptions by trying different input types like using integers,
strings, zero, negative values, or other data types.
a = 1 try:
b = int(input("Please enter a number to divide a")) a = a/b
print("Success a=",a) except:
print("There was an error")
o ZeroDivisionError
o Success a=1/0
o There was an error
o Success a=NaN
Correct: This division will generate an error, leading to the exception part.
Figure 368
Figure 369
Figure 370
OBJECTS: TYPES
Let's do several less abstract examples. Every time we create an integer, we are creating an
instance of type integer, or we are creating an integer object. In this case, we are creating five
instances of type integer or five integer objects.
Figure 371
Similarly, every time we create a list, we are creating an instance of type list, or we are creating
a list object. In this case, we are creating five instances of type list or five list objects.
Figure 372
Figure 373
METHODS
• A class or type's methods are functions that every instance of that class or type provides.
• It's how you interact with the object. We have been using methods all this time, for
example, on lists.
• Sorting is an example of a method that interacts with the data in the object.
Consider the list ratings, the data is a series of numbers contained within the list. The method
sort will change the data within the object. We call the method by adding a period at the end of
the object's name, and the method's name we would like to call with parentheses.
Figure 374
Figure 375
We can call the reverse method on the list, changing the list again. We call the method, reversing
the order of the sequence within the object. In many cases, you don't have to know the inner
workings of the class and its methods, you just have to know how to use them. Next, we will
cover how to construct your own classes.
Figure 376
CLASSES
You can create your own type or class in Python.
Figure 377
Figure 378
Figure 379
Figure 380
For the class rectangle, we changed the name of the class, but the rest is kept the same.
Figure 381
Figure 382
We can create an object that is an instance of type circle. The color data attribute is red, and the
data attribute radius is four. We could also create a second object that is an instance of type
circle. In this case, the color data attribute is green, and the data attribute radius is two.
Figure 383
We can also create an object that is an instance of type rectangle. The color data attribute is
blue, and the data attribute of height and width is two. The second object is also an instance of
type rectangle. In this case, the color data attribute is yellow, and the height is one, and the width
is three.
Figure 384
Figure 385
Let us continue building the circle class in Python. We define our class. We then initialize each
instance of the class with data attributes, radius, and color using the class constructor.
Figure 386
The self parameter refers to the newly created instance of the class. The parameters, radius, and
color can be used in the constructors body to access the values passed to the class constructor
when the class is constructed. We could set the value of the radius and color data attributes to
the values passed to the constructor method.
The function init is a constructor. It's a special function that tells Python you are making a new
class. There are other special functions in Python to make more complex classes. The radius and
color parameters are used to initialize the radius and color data attributes of the class instance.
Figure 387
Figure 388
Figure 389
Figure 390
Figure 391
Figure 392
The arguments passed to the circle constructor are used to initialize the data attributes
of the newly created circle instance.
Figure 393
Figure 394
Typing the object's name followed by a dot and the data attribute name gives us the data
attribute value, for example, radius. In this case, the radius is 10. We can do the same for color.
Figure 395
We can see the relationship between the self parameter and the object.
Figure 396
In Python, we can also set or change the data attribute directly. Typing the object's name
followed by a dot and the data attribute name and set it equal to the corresponding value. We
can verify that the color data attribute has changed. Usually, in order to change the data in an
object, we define methods in the class.
Figure 398
We have seen how data attributes consist of the data defining the objects. Methods are functions
that interact and change the data attributes, changing or using the data attributes of the object.
Figure 399
Let's say we would like to change the size of a circle. This involves changing the radius attribute.
Figure 400
Figure 401
Let's see how this part of the code works when we create an object and call the add_radius
method.
Figure 402
As before, we create an object with the object constructor. We pass two arguments to the
constructor. The radius is set to two and the color is set to red. In the constructor's body, the data
attributes are set.
Figure 403
We can use the box analogy to see the current state of the object. We call the method by adding
a dot followed by the method, name, and parentheses. In this case, the argument of the function
Figure 404
Internally, the method is called with a value of 8, and the proper self object.
Figure 405
The method assigns a new value to self radius. This changes the object, in particular, the
radius data attribute.
Figure 406
Figure 407
Figure 408
In the labs, we also create the method called drawCircle. See the lab for the implementation
of drawCircle.
Figure 409
Figure 410
Similarly, we can create a new object of type circle. We can access the data attribute of radius.
We can access the data attribute color. We can use the method drawCircle to draw the circle.
Figure 411
In summary, (1) we have created an object of class circle called RedCircle with a radius attribute
of 3, and a color attribute of red. (2) We also created an object of class circle called BlueCircle,
with a radius attribute of 10 and a color attribute of blue.
Figure 412
Figure 413
So, we have a class, an object that is a realization or instantiation of that class. For example, we
can create two objects of class Circle, or two objects of class Rectangle.
Figure 414
The dir function is useful for obtaining the list of data attributes and methods associated with a
class. The object you're interested in is passed as an argument. The return value is a list of the
objects data attributes. The attribute surrounded by underscores are for internal use, and you
shouldn't have to worry about them. The regular looking attributes are the ones you should
concern yourself with. These are the objects, methods, and data attributes. There is a lot more
you can do with objects in Python. Check Python.org for more info.
Figure 415
OBJECTIVES
In this reading, you will learn about:
• Fundamental concepts of Python objects and classes.
• Structure of classes and object code.
• Real-world examples related to objects and classes.
State
The attributes or data that describe the object. For your "Car" object, this might include attributes
like "color", "speed", and "fuel level".
Behavior
The actions or methods that the object can perform. In Python, methods are functions that
belong to objects and can change the object's state or perform specific operations.
class ClassName:
• Class attributes are variables shared among all class instances (objects).
• They are defined within the class but outside of any methods.
class ClassName:
# Class attributes (shared by all instances) class_attribute = value
• The init method is a special method known as the constructor. It initializes the
instance attributes (also called instance variables) when an object is created.
• The self parameter is the first parameter of the constructor, referring to the instance
being created.
• attribute1, attribute2, and so on are parameters passed to the constructor when
creating an object.
• Inside the constructor, self.attribute1, self.attribute2, and so on are used to assign
values to instance attributes.
• Instance attributes are variables that store data specific to each class instance.
• They are initialized within the __init__ method using the self keyword followed by
the attribute name.
• These attributes hold unique data for each object created from the class.
class ClassName:
# Class attributes (shared by all instances) class_attribute = value
class ClassName:
# Class attributes (shared by all instances) class_attribute = value
Using the same steps you can define multiple instance methods.
class ClassName:
# Class attributes (shared by all instances) class_attribute = value
• To create objects (instances) of the class, you call the class like a function and provide
arguments the constructor requires.
• Each object is a distinct instance of the class, with its own instance attributes and the
ability to call methods defined in the class.
# Create objects (instances) of the class object1 = ClassName(arg1, arg2, ...) object2
= ClassName(arg1, arg2, ...)
Real-world example
Let's write a python program that simulates a simple car class, allowing you to create car
instances, accelerate them, and display their current speeds
1. Let's start by defining a Car class that includes the following attributes and methods:
o Class attribute max_speed, which is set to 120 km/h.
o Constructor method init that takes parameters for the car's make, model,
color, and an optional speed (defaulting to 0). This method initializes instance
attributes for make, model, color, and speed.
o Method accelerate(self, acceleration) that allows the car to accelerate. If the
acceleration does not exceed the max_speed, update the car's speed attribute.
Otherwise, set the speed to the max_speed.
o Method get_speed(self) that returns the current speed of the car.
class Car:
# Class attribute (shared by all instances) max_speed = 120 # Maximum speed in km/h
# Constructor method (initialize instance attributes) def init (self, make, model,
color, speed=0):
self.make = make self.model = model self.color = color
self.speed = speed # Initial speed is set to 0
# Create objects (instances) of the Car class car1 = Car("Toyota", "Camry", "Blue")
car2 = Car("Honda", "Civic", "Red")
3. Using the accelerate method, you will increase the speed of car1 by 30 km/h and car2 by 20
km/h.
Next steps
OBJECTIVES
After completing this lab you will be able to:
• Work with classes and objects
• Identify and define attributes and methods
TABLE OF CONTENTS
• Introduction to Classes and Objects
• Creating a class
• Instances of a Class: Objects and Attributes
• Methods
• Creating a class
• Creating an instance of a class Circle
• The Rectangle Class
Creating a Class
The first step in creating a class is giving it a name. In this notebook, we will create two classes:
Circle and Rectangle. We need to determine all the data that make up that class, which we call
attributes. Think about this step as creating a blue print that we will use to create objects. In
figure 1 we see two classes, Circle and Rectangle. Each has their attributes, which are variables.
The class Circle has the attribute radius and color, while the Rectangle class has the attribute
height and width. Let’s use the visual examples of these shapes before we get to the code, as this
will help you get accustomed to the vocabulary.
Figure 1: Classes circle and rectangle, and each has their own attributes. The class Circle has the attribute radius and color,
the class Rectangle has the attributes height and width.
An instance of an object is the realization of a class, and in Figure 2 we see three instances of
the class circle. We give each object a name: red circle, yellow circle, and green circle. Each object
has different attributes, so let's focus on the color attribute for each object.
Figure 2: Three instances of the class Circle, or three objects of type Circle.
The color attribute for the red Circle is the color red, for the green Circle object the color attribute
is green, and for the yellow Circle the color attribute is yellow.
Methods
Methods give you a way to change or interact with the object; they are functions that interact
with objects. For example, let’s say we would like to increase the radius of a circle by a specified
amount. We can create a method called add_radius(r) that increases the radius by r. This is
shown in figure 3, where after applying the method to the "orange circle object", the radius of
the object increases accordingly. The “dot” notation means to apply the method to the object,
which is essentially applying a function to the information in the object.
Figure 3: Applying the method “add_radius” to the object orange circle object.
Now we are going to create a class Circle, but first, we are going to import a library to draw the
objects:
1. The first step in creating your own class is to use the class keyword, then the name
of the class as shown in Figure 4. In this course the class parent will always be object:
2. The next step is a special method called a constructor init , which is used to initialize the
object. The inputs are data attributes. The term self contains all the attributes in the set. For
example the self.color gives the value of the attribute color and self.radius will give you the
radius of the object. We also have the method add_radius() with the parameter r, the
method adds the value of r to the attribute radius. To access the radius we use the
syntax self.radius. The labeled syntax is summarized in Figure 5:
The actual object is shown below. We include the method drawCircle to display the image of a
circle. We set the default radius to 3 and the default color to blue:
# Constructor
def __init__(self, radius=3, color='blue'):
self.radius = radius
self.color = color
# Method
def drawCircle(self):
plt.gca().add_patch(plt.Circle((0, 0), radius=self.radius, fc=self.color))
plt.axis('scaled')
plt.show()
We can use the dir command to get a list of the object's methods. Many of them are default
Python methods.
['__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getstate__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'add_radius',
'color',
'drawCircle',
'radius']
Figure 416
We can increase the radius of the circle by applying the method add_radius(). Let's increases the
radius by 2 and then by 5:
print('Radius of object:',RedCircle.radius)
RedCircle.add_radius(2)
print('Radius of object of after applying the method
add_radius(2):',RedCircle.radius)
RedCircle.add_radius(5)
print('Radius of object of after applying the method
add_radius(5):',RedCircle.radius)
Radius of object: 1
Radius of object of after applying the method add_radius(2): 3
Radius of object of after applying the method add_radius(5): 8
Let’s create a blue circle. As the default color is blue, all we have to do is specify what the radius
is:
As before, we can access the attributes of the instance of the class by using the dot notation:
Figure 417
Compare the x and y axis of the figure to the figure for RedCircle; they are different.
Let's create a class rectangle with the attributes of height, width, and color. We will only add the
method to draw the rectangle object:
class Rectangle(object):
# Constructor
def __init__(self, width=2, height=3, color='r'):
self.height = height
self.width = width
self.color = color
# Method
def drawRectangle(self):
plt.gca().add_patch(plt.Rectangle((0, 0), self.width, self.height
,fc=self.color))
plt.axis('scaled')
plt.show()
Let’s create the object SkinnyBlueRectangle of type Rectangle. Its width will be 2 and height will
be 3, and the color will be blue:
As before we can access the attributes of the instance of the class by using the dot notation:
Figure 418
We can access the attributes of the instance of the class by using the dot notation:
FatYellowRectangle.drawRectangle()
Figure 419
Task-2. Update the class with the default color for all vehicles," white".
Task-3. Additionally, you need to create methods in the Vehicle class to assign seating capacity
to a vehicle.
Task-4. Create a method to display all the properties of an object of the class.
def display_properties(self):
print("Properties of the Vehicle:")
print("Color:", self.color)
print("Maximum Speed:", self.max_speed)
print("Mileage:", self.mileage)
print("Seating Capacity:", self.seating_capacity)
Task-5. Additionally, you need to create two objects of the Vehicle class object that should have
a max speed of 200kph and mileage of 50000kmpl with five seating capacities, and another car
object should have a max speed of 180kph and 75000kmpl with four seating capacities.
class Vehicle:
color = "white"
def __init__(self, max_speed, mileage):
def display_properties(self):
print("Properties of the Vehicle:")
print("Color:", self.color)
print("Maximum Speed:", self.max_speed)
print("Mileage:", self.mileage)
print("Seating Capacity:", self.seating_capacity)
Congratulations, you have completed your first lesson and hands-on lab in Python.
class Circle(object):
# Constructor
def __init__(self, radius=3, color='blue'):
self.radius = radius
self.color = color
# Method
def add_radius(self, r):
self.radius = self.radius + r
return self.radius
o C1 = Circle(‘yellow’,4)
o C1.radius = Circle.radius(4)
o C1.color = Circle.color(‘yellow’)
o C1 = Circle()
o C1 = Circle(4, ‘yellow’)
Correct! C1 = Circle(4, ‘yellow’) correctly creates an instance of the Circle class with C1 having
a radius of 4 and its color set to ‘yellow.’
Question 2
Consider the execution of the following lines of code.
What are the values of the radius and color attributes for the CircleObject after their execution?
class Circle(object):
# Constructor
def __init__(self, radius=3, color='blue'):
self.radius = radius
self.color = color
# Method
def add_radius(self, r):
self.radius = self.radius + r
return self.radius
o 3, 'red'
o 10, ‘red’
o 10, 'blue'
o 3, ‘blue’
Correct! The radius attribute is updated to 10 while the color attribute is kept as
default ‘blue.’
class Vehicle:
color = "white"
V1 = Vehicle(150, 25)
Question 4
Which of the following options would create an appropriate object that points to a red, 5-
seater vehicle with a maximum speed of 200kmph and a mileage of 20kmpl?
class Vehicle:
color = "white"
V1 = Vehicle(150, 25)
o Car = Vehicle(200,20)
Car.assign_seating_capacity(5)
o Car = Vehicle(200,20)
Car.color = ‘red’
Car.assign_seating_capacity(5)
o Car = Vehicle(200,20)
Car.color = ‘red’
o Car = Vehicle(200, 20)
Correct! All attributes are correctly assigned here.
class Graph():
def __init__(self, id):
self.id = id
self.id = 80
val = Graph(200)
print(val.id)
o 0
o 200
o Invalid Syntax
o 80
Correct! The value of the attribute is overwritten to 80 every time the object is created,
irrespective of the value of the attribute passed.
OBJECTIVES
After completing this lab, you will be able to use Python commands to perform text analysis. This
includes converting the text to lowercase and then finding and counting the frequency of all
unique words, as well as a specified word.
SETUP
For this lab, we will be using the following data types:
• List
• Strings
• Classes and objects
Let's consider a real-life scenario where you are analyzing customer feedback for a product. You
have a large dataset of customer reviews in the form of strings, and you want to extract useful
information from them using the three identified tasks:
Task 1. String in lower case: You want to Pre-process the customer feedback by converting all
the text to lowercase. This step helps standardize the text. Lower casing the text allows you to
focus on the content rather than the specific letter casing.
Task 2. Frequency of all words in a given string: After converting the text to lowercase, you
want to determine the frequency of each word in the customer feedback. This information will
help you identify which words are used more frequently, indicating the key aspects or topics that
customers are mentioning in their reviews. By analyzing the word frequencies, you can gain
insights into the most common issues raised by customers.
Task 3. Frequency of a specific word: In addition to analyzing the overall word frequencies, you
want to specifically track the frequency of a particular word that is relevant to your analysis. For
example, you might be interested in monitoring how often the word "reliable" appears in the
customer reviews to gauge customer sentiment about the product's reliability. By focusing on
the frequency of a specific word, you can gain a deeper understanding of customer opinions or
preferences related to that particular aspect.
Note:- In Part-A, you won't be getting any output as we are just storing the string and
creating a class.
"Lorem ipsum dolor! diam amet, consetetur Lorem magna. sed diam nonumy eirmod tempor. diam et
labore? et diam magna. et diam amet."
For achieving the tasks mentioned in the scenario, We need to create a class with 3 different
methods.
# Please do not run this code cell as it is incomplete and will produce an error.
1. Inside the constructor, we will convert the text argument to lowercase using the
lower() method.
2. Then, will Remove punctuation marks (periods, exclamation marks, commas,
and question marks) from the text using the replace() method.
3. At last, we will Assign the formatted text to a new attribute called fmtText.
Here we will be Updating the above TextAnalyzer class with points mentioned above.
• In this step, we will Implement the freqAll() method with the below parameters:
1. Split the fmtText attribute into individual words using the split() method.
2. Create an empty dictionary to store the word frequency.
3. Iterate over the list of words and update the frequency dictionary accordingly.
4. Use count method for counting the occurence
5. Return the frequency dictionary.
self.fmtText = formattedText
def freqAll(self):
# split text into words
wordList = self.fmtText.split(' ')
# Create dictionary
freqMap = {}
for word in set(wordList): # use set to remove duplicates in list
freqMap[word] = wordList.count(word)
return freqMap
• In step-5, we have to Implement the freqOf(word) method that takes a word argument:
1. Create method and pass the word that need to be found.
2. Get the freqAll method for look for count and check if that word is in the list.
3. Return the count.
self.fmtText = formattedText
def freqAll(self):
# split text into words
# Create dictionary
freqMap = {}
for word in set(wordList): # use set to remove duplicates in list
freqMap[word] = wordList.count(word)
return freqMap
def freqOf(self,word):
# get frequency map
freqDict = self.freqAll()
if word in freqDict:
return freqDict[word]
else:
return 0
PART-B
In Part B, we will be calling the functions created in Part A, allowing the functions to execute and
generate output.
Step-2 Call the function that converts the data into lowercase
Step-3 Call the function that counts the frequency of all unique words from
the data.
We have successfully calculated the frequency of all unique words in the string.
Step-4 Call the function that counts the frequency of specific word.
Here, we will call the function that counts the frequency of the word "lorem"
Print the output.**
1. marks = 90
2. attendance_percentage = 87
6. else:
behaviors.
Example:
1. class Person:
3. self.name = name
4. self.age = age
when called.
Example:
Example 1:
1. 5 == 5
returns True
Example 2:
1. age = 25 age == 30
returns False
Example 2:
3. print(fruit)
1. greet("Alice")
Example 1:
returns True
Example 2:
2. minimum = 100
returns True
returns True
Example 2:
1. age = 20
2. max_age = 25
returns False
Example:
7. else:
Example:
6. else:
Example:
2. print("You're an adult.")
3. else:
Example 1:
returns True
Example 2:
1. size = 38
2. max_size = 40
returns True
Example 1:
1. 4 < 6
returns True
Example 2:
1. score = 60
2. passing_score = 65
returns True
3. break
6. if # boolean statement
7. continue
Example 1:
2. if num == 3:
3. break
4. print(num)
Example 2:
2. if num == 3:
3. continue
4. print(num)
Syntax:
1. !isLocked
Example:
1. a = 10
2. b = 20
3. a != b
returns True
Example 2:
1. count=0
2. count != 0
returns False
returns True
Example:
of integers from 2 to 9.
integers from 1 to 9.
2. result = add(3, 5)
exception
Example:
1. try:
2. num = int(input("Enter a
number: "))
3. except ValueError:
with Else Block executed if no exception occurs 1. try: # Code that might raise an
in the try block. exception except
exception
Example:
1. try:
2. num = int(input("Enter a
number: "))
3. except ValueError:
5. else:
with Finally executes, regardless of whether 1. try: # Code that might raise an
Block an exception occurred. exception except
exception
Example:
7. 7
1. try:
3. data = file.read()
4. except FileNotFoundError:
6. finally:
7. file.close()
2. print(count) count += 1
Term Definition
corresponding indices.
For loops For loops in Python are used for iterating over a sequence (such
as a
Global variable Global variables in Python are variables defined outside of any
function or block and can be accessed and modified from any part
of the code.
Local variables Local variables in Python are variables defined within a specific
function or block of code and are only accessible within that
function or block.
Logic operators Logic operators in Python are used to perform logical operations
on Boolean values, including operators like and (logical AND), or
(logical
Scope of function The "scope of a function" in Python refers to the region of code
where a variable defined within that function is accessible or
visible.
iteration.
While loops While loops in Python are used to repeatedly execute a block of
code as long as a specified condition is true.
LEARNING OBJECTIVES
In this module you will learn about:
• Explain how Pandas use data frames.
• Use Pandas library for data analysis.
• Read text files using Python libraries including "open" and "with".
• Utilize NumPy to create one-dimensional and two-dimensional arrays.
• Write and save files in Python.
Figure 420
We will use Python's open function to get a file object. We can apply a method to that object to
read data from the file.
Figure 421
Figure 422
Figure 423
Figure 424
Figure 425
At :40 the voice over says "r' for reading but the slide shows "w" for writing.
Figure 426
Figure 427
We can now use the file object to obtain information about the file.
Figure 428
Figure 429
Using a "with" statement to open a file is better practice because it automatically closes the file.
Figure 430
The code will run everything in the indent block, then closes the file. This code reads the file,
Example1.txt. We can use the file object, "File1."
Figure 431
Figure 432
Figure 433
The method "read" stores the values of the file in the variable "file_stuff" as a string.
Figure 434
Figure 435
You can check if the file content is closed, but you cannot read from it outside the indent.
Figure 436
But you can print the file content outside the indent as well.
Figure 437
Figure 438
We can output every line as an element in a list using the method "readlines." The first line
corresponds to the first element in the list. The second line corresponds to the second element
in the list, and so on. We can use the method "readline" to read the first line of the file.
Figure 439
If we run this command, it will store the first line in the variable "file_stuff" then print the first
line.
Figure 440
Figure 441
We can use a loop to print out each line individually as follows.Let's represent every character
in a string as a grid. We can specify the number of characters we would like to read from the
string as an argument to the method "readline."
Figure 442
When we use a four as an argument in the method "readline," we print out the first four
characters in the file.
Figure 443
At 3:06 in the video, output to the function readlines() is wrongly mentioned. The output is relevant to the function read().
Figure 444
Check out the labs for more examples of methods and other file types.
Objectives
Table of Contents
• Download Data
• Reading Text Files
• A Better Way to Open a File
Download Data
## Uncomment these if working locally, else let the following code cell run.
# import urllib.request
# url = 'https://cf-courses-data.s3.us.cloud-object-
storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-
SkillsNetwork/labs/Module%204/data/example1.txt'
# filename = 'Example1.txt'
# urllib.request.urlretrieve(url, filename)
import pandas as pd
filename = "https://cf-courses-data.s3.us.cloud-object-
storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-
SkillsNetwork/labs/Module%204/data/example1.txt"
if response.status == 200:
f.write(await response.bytes())
print("done")
One way to read or write a file in Python is to use the built-in open function. The open function
provides a File object that contains the methods and attributes you need in order to read, save,
and manipulate the file. In this notebook, we will only cover .txt files. The first parameter you
need is the file path and the file name. An example is shown as follow:
The mode argument is optional and the default value is r. In this notebook we only cover two
modes:
• **r**: Read mode for reading files
• **w**: Write mode for writing files
For the next example, we will use the text file Example1.txt. The file is shown as follows:
file1.name
'example1.txt'
file1.mode
'r'
FileContent = file1.read()
FileContent
print(FileContent)
This is line 1
This is line 2
This is line 3
type(FileContent)
str
It is very important that the file is closed in the end. This frees up resources and ensures
consistency across different python versions.
Using the with statement is better practice, it automatically closes the file even if the code
encounters an exception. The code will run everything in the indent block then close the file
object.
This is line 1
This is line 2
This is line 3
The file object is closed, you can verify it by running the following cell:
file1.closed
True
print(FileContent)
This is line 1
This is line 2
This is line 3
Figure 445
We don’t have to read the entire file, for example, we can read the first 4 characters by entering
three as a parameter to the method .read():
This
Once the method.read(4) is called the first 4 characters are called. If we call the method
again, the next 4 characters are called. The output for the following cell will demonstrate the
process for different inputs to the method read():
This
is
line 1
This is line 2
The process is illustrated in the below figure, and each color represents the part of the file read
after the method read() is called:
Figure 446
This
is line 2
We can also read one line of the file at a time using the method readline():
We can also pass an argument to readline() to specify the number of charecters we want to
read. However, unlike read(), readline() can only read one line at most.
This is line 1
This is line 2
This
We can use the method readlines() to save the text file to a list:
FileasList[0]
FileasList[1]
FileasList[2]
Figure 447
We will use Python's open function to get a file object to create a text file.
Figure 448
We can apply method 'write' to write data to that file. As a result, text will be written to the file.
Figure 449
Figure 450
Figure 451
This is made up of the file name – if you have that file in your directory, it will be overwritten –
and the file directory.
Figure 452
Figure 454
Figure 455
Figure 456
Figure 457
Figure 458
Figure 459
(3) We use the method write, to write data into the file.
Figure 460
The argument is the text we would like input into the file.
Figure 461
Figure 462
Figure 463
As before, we use a 'with' command and the open function to create a file.
Figure 464
Figure 465
We use a 'for' loop to read each element of the first lines and pass it to the variable line.
Figure 466
The first iteration of the loop writes the first element of the list to the file Example2.
Figure 467
Figure 468
Figure 469
Figure 470
Figure 471
Figure 472
First, we read the file Example1 and interact with it via the file object, readfile.
Figure 473
Then we create a new file Example3 and use the file object writefile to interact with it.
SUMMARY VIDEOS LAB 354 of 590
The for loop takes a line from the file object readfile and stores it in the file Example3 using the
file object writefile.
Figure 474
Figure 475
Figure 476
Figure 477
Figure 478
OBJECTIVES
After completing this lab you will be able to:
• Write to files using Python libraries
TABLE OF CONTENTS
• Writing Files
• Appending Files
• Additional File modes
• Copy a File
WRITING FILES
We can open a file object using the method write() to save the text file to a list. To write to a file,
the mode argument must be set to w. Let’s write a file Example2.txt with the line: “This is
line A”
# Read file
The method .write() works similar to the method .readline(), except instead of reading a new line
it writes a new line. The process is illustrated in the figure. The different color coding of the grid
represents a new line added to the file after each method call.
Figure 479
Lines = ["This is line A\n", "This is line B\n", "This is line C\n"]
Lines
['This is line A\n', 'This is line B\n', 'This is line C\n']
This is line B
This is line C
We can verify the file is written by reading it and printing out the values:
However, note that setting the mode to w overwrites all the existing data in the file.
APPENDING FILES
We can write to files without losing any of the existing data as follows by setting the mode
argument to append: a. you can append a new line as follows:
ADDITIONAL MODES
It's fairly ineffecient to open the file in a or w and then reopening it in r to read any lines. Luckily
we can access the file in the following modes:
There were no errors but read() also did not output anything. This is because of our location in
the file.
Most of the file methods we've looked at work in a certain location in the file. .write() writes at
a certain location in the file. .read() reads at a certain location in the file and so on. You can think
of this as moving your pointer around in the notepad to make changes at specific location.
Opening the file in w is akin to opening the .txt file, moving your cursor to the beginning of the
text file, writing new text and deleting everything that follows. Whereas opening the file in a is
similiar to opening the .txt file, moving your cursor to the very end and then adding the new
pieces of text.
It is often very useful to know where the 'cursor' is in a file and be able to control it. The following
methods allow us to do precisely this -
data = testwritefile.read()
if (not data): #empty strings return false in python
print('Read nothing')
else:
print(testwritefile.read())
New Location : 0
Overwrite
ne A
This is line B
This is line C
This is line C
This is line D
This is line E
This is line C
This is line D
This is line E
This is line E
Finally, a note on the difference between w+ and r+. Both of these modes allow access to read
and write methods, however, opening a file in w+ overwrites it and deletes all pre-existing data.
To work with a file on existing data, use r+ and a+. While using r+, it can be useful to add a
.truncate() method at the end of your data. This will reduce the file to your data and delete
everything that follows.
In the following code block, Run the code as it is first and then run it with the .truncate().
COPY A FILE
Let's copy the file Example2.txt to the file Example3.txt:
After reading files, we can also write data into files and save them in different file formats like
.txt, .csv, .xls (for excel files) etc. You will come across these in further examples
NOTE: If you wish to open and view the example3.txt file, download this lab here and
run it locally on your machine. Then go to the working directory to ensure the
example3.txt file exists and contains the summary data that we wrote.
memReg = 'members.txt'
exReg = 'inactive.txt'
fee =('yes','no')
def genFiles(current,old):
with open(current,'w+') as writefile:
writefile.write('Membership No Date Joined Active \n')
data = "{:^13} {:<11} {:<6}\n"
genFiles(memReg,exReg)
Now that you've run the prerequisite code cell above, which prepared the files for this exercise,
you are ready to move on to the implementation.
Exercise: Implement the cleanFiles function in the code cell below.
'''
The two arguments for this function are the files:
- currentMem: File containing list of current members
- exMem: File containing list of old members
This function should remove all rows from currentMem containing 'no'
in the 'Active' column and appends them to exMem.
'''
def cleanFiles(currentMem, exMem):
# TODO: Open the currentMem file as in r+ mode
#TODO: Open the exMem file in a+ mode
#TODO: Read each member in the currentMem (1 member per row) file into a
list.
# Hint: Recall that the first line in the file is the header.
#TODO: iterate through the members and create a new list of the innactive
members
The code cell below is to verify your solution. Please do not modify the code and run it to test
your implementation of cleanFiles.
def testMsg(passed):
if passed:
return 'Test Passed'
else :
return 'Test Failed'
testWrite = "testWrite.txt"
testAppend = "testAppend.txt"
passed = True
genFiles(testWrite,testAppend)
try:
cleanFiles(testWrite,testAppend)
except:
print('Error')
Implementation of cleanFiles:
def cleanFiles(currentMem,exMem):
with open(currentMem,'r+') as writeFile:
with open(exMem,'a+') as appendFile:
#get the data
writeFile.seek(0)
members = writeFile.readlines()
#remove header
header = members[0]
members.pop(0)
memReg = 'members.txt'
exReg = 'inactive.txt'
cleanFiles(memReg,exReg)
Congratulations, you have completed this lesson and hands-on lab in Python.
Question 2
Which data attribute retrieves the file's title?
o file1.open()
o file1.close()
o file1.mode
o file1.name
Correct:The name attribute returns the filename.
Question 3
Which command instructs Python to initiate a new line?
o \q
o \b
o \e
o \n
Correct:In Python \n instructs the code to begin a new line.
Question 4
Which attribute facilitates the input of data into a file?
o file1.close()
o file1.read()
o file1.open()
o file1.write()
Correct:The “write” method writes data into a file.
Figure 480
IMPORTING PANDAS
You can import the library or dependency like pandas using the following command.
Start with the import command followed by the name of the library. You now have access to a
large number of pre-built classes and functions. This assumes the library is installed.
Figure 481
In our lab environment, all the necessary libraries are installed. Let us say we would like to load
a CSV file using pandas build in function, read_csv. A CSV is a typical file type used to store data.
Simply type the word pandas, then a dot and the name of the function with all the inputs.
Figure 482
Figure 483
You are not limited to the abbreviation pd. In this case, we use the term banana. However, we
will stick with pd for the rest of the video. Let us examine this code in more detail.
Figure 484
DATAFRAMES
One way pandas allows you to work with data is in a dataframe. Let us review the process of
going from a CSV file to a dataframe.
Figure 485
Figure 486
Figure 487
The result is stored to the variable df. This is short for dataframe. Now that you have the data
in a dataframe, you can work with it.
Figure 488
Figure 489
The process for loading an Excel file is similar. Use the path of the Excel file, the function
read_excel.
Figure 490
Figure 491
Figure 492
Figure 493
Figure 494
Figure 495
You can then cast the dictionary to a dataframe using the function DataFrame.
Figure 496
Figure 497
Figure 498
Figure 499
USING DATAFRAMES
Create a new dataframe consisting of one column.
Figure 500
Figure 501
Figure 502
Figure 503
Figure 504
Figure 505
ILOC
One way to access unique elements is with the iloc method. The first input is an integer
representing the row index, and the second is the integer representing the column index.
Figure 506
Figure 507
You can access the second row and first column as follows.
Figure 508
Figure 509
Figure 510
You can use the name of the row index and the column as well.
Figure 511
LOC
You can access the first row of the column named artist as follows.
Similarly, you can access the second row of the column named artist.
Figure 512
Figure 513
Figure 514
Figure 515
Figure 516
Similarly, you can access the index b or the second row of the column named artist.
Figure 517
Figure 518
Figure 519
Assign the first two rows and the first three columns to the variable z.
Figure 520
Figure 521
Figure 522
The code assigns the first 3 rows and all columns in between the columns named artist and
released.
Figure 523
Figure 524
Figure 525
Figure 526
We can see there are 3 unique colors. Let's say you would like to find out how many unique
elements are in a column of a data frame. This may be much more difficult because instead of
13 elements, you may have millions. Pandas has the method unique to determine the unique
elements in a column of a data frame.
Figure 527
Figure 528
Figure 529
Figure 530
Figure 531
We can look at the column Released for songs made after 1979,
Figure 532
then select the corresponding rows. We can accomplish this within one line of code in Pandas.
But let's break up the steps.
Figure 533
Figure 534
For our case, we simply specify the column Released and the inequality for the albums after
1979.
Figure 535
Figure 536
The result is true when the condition is true and false otherwise.
SUMMARY VIDEOS LAB 384 of 590
Figure 537
Figure 538
We simply use the dataframes names and square brackets we placed the previously mentioned
inequality and assign it to the variable df1.
Figure 539
Figure 540
SAVE AS CSV
We can save the new data frame using the method to_csv.
Figure 541
The argument is the name of the csv file. Make sure you include a .csv extension.
Figure 542
There are other functions to save the data frame in other formats.
OBJECTIVES
After completing this lab you will be able to:
• Use Pandas Library to create DataFrame and Series
• Locate data in the DataFrame using loc() and iloc() functions
• Use slicing
Pandas is a popular library for data analysis built on top of the Python programming language.
Pandas generally provide two data structures for manipulating data, They are:
• DataFrame
• Series
A DataFrame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows
and columns.
• A Pandas DataFrame will be created by loading the datasets from existing storage.
• Storage can be SQL Database, CSV file, an Excel file, etc.
• It can also be created from the lists, dictionary, and from a list of dictionaries.
Series represents a one-dimensional array of indexed data. It has two main components :
1. An array of actual data.
2. An associated array of indexes or data labels.
The index is used to access individual data values. You can also get a column of a dataframe as
a Series. You can think of a Pandas series as a 1-D dataframe.
Once you’ve imported pandas, you can then use the functions built in it to create and analyze
data.
In this practice lab, we will learn how to create a DataFrame out of a dictionary.
Let us consider a dictionary 'x' with keys and values as shown below.
We then create a dataframe from the dictionary using the function pd.DataFrame(dict)
Column Selection:
To select a column in Pandas DataFrame, we can either access the columns by calling them by
their columns name.
Let's Retrieve the data present in the ID column.
ID
0 1
1 2
2 3
3 4
Let's use the type() function and check the type of the variable.
The output shows us that the type of the variable is a DataFrame object.
z = df[['Department','Salary','ID']]
z
Department Salary ID
0 Architect Group 100000 1
1 Software Group 80000 2
2 Design Team 50000 3
3 Infrastructure 60000 4
Figure 543
Marks
0 85
1 72
2 89
3 76
Problem 3: Retrieve the Country and Course columns and assign it to a variable c
Country Course
0 UK Python
1 Canada Data Structure
2 China Machine Learning
3 USA Web Development
x = df['Name']
x
0 Rose
1 John
2 Jane
3 Mary
Name: Name, dtype: object
The output shows us that the type of the variable is a Series object.
loc() is a label-based data selecting method which means that we have to pass the name of the
row or column that we want to select. This method includes the last element of the range passed
in it.
Simple syntax for your understanding:
loc[row_label, column_label]
iloc() is an indexed-based selecting method which means that we have to pass integer index in
the method to select a specific row/column. This method does not include the last element of
the range passed in it.
Simple syntax for your understanding:
iloc[row_index, column_index]
Examples
Let us see some examples on the same.
# Access the value on the first row and the first column
df.iloc[0, 0]
'Rose'
# Access the value on the first row and the third column
df.iloc[0,2]
'Architect Group'
df2=df
df2=df2.set_index("Name")
#To display the first 5 rows of new dataframe
df2.head()
Try it yourself
Use the loc() function,to get the Department of Jane in the newly created dataframe df2.
Use the iloc() function,to get the Salary of Mary in the newly created dataframe df2.
Exercise 3: Slicing
Slicing uses the [] operator to select a set of rows and/or columns from a DataFrame.
To slice out a set of rows, you use this syntax: data[start:stop],
here the start represents the index from where to consider, and stop represents the index one
step BEYOND the row you want to select. You can perform slicing using both the index and the
name of the column.
NOTE: When slicing in pandas, the start bound is included in the output.
So if you want to select rows 0, 1, and 2 your code would look like this: df.iloc[0:3].
It means you are telling Python to start at index 0 and select rows 0, 1, 2 up to but not including
3.
NOTE: Labels must be found in the DataFrame or you will get a KeyError.
Indexing by labels(i.e. using loc()) differs from indexing by integers (i.e. using iloc()). With loc(),
both the start bound and the stop bound are inclusive. When using loc(), integers can be used,
but the integers refer to the index label and not the position.
df.iloc[0:2, 0:3]
Name ID Department
0 Rose 1 Architect Group
1 John 2 Software Group
#let us do the slicing using loc() function on old dataframe df where index column is
having labels as 0,1,2
df.loc[0:2,'ID':'Department']
ID Department
0 1 Architect Group
1 2 Software Group
2 3 Design Team
#let us do the slicing using loc() function on new dataframe df1 where index column
is Name having labels: Rose, John and Jane
df2.loc['Rose':'Jane', 'ID':'Department']
Name ID Department
Rose 1 Architect Group
John 2 Software Group
jane 3 Design Team
Try it yourself ¶
using loc() function, do slicing on old dataframe df to retrieve the Name, ID and department of
index column having labels as 2,3
Name ID Department
jane 3 Design Team
Mary 4 Infrastructure
Congratulations, you have completed this lesson and the practice lab on Pandas.
OBJECTIVES
After completing this lab you will be able to:
• Use Pandas to access and view data
TABLE OF CONTENTS
• About the Dataset
• Introduction of Pandas
• Viewing Data and Accessing Data
• Quiz on DataFrame
Figure 544
import pandas as pd
After the import command, we now have access to a large number of pre-built classes and
functions. This assumes the library is installed; in our lab environment all the necessary libraries
are installed. One way pandas allow you to work with data is a dataframe. Let's go through the
process to go from a comma separated values (.csv) file to a dataframe. This
variable csv_path stores the path of the .csv, which is used as an argument to
the read_csv function. The result is stored in the object df, this is a common short form used for
a variable referring to a Pandas dataframe.
csv_path = 'https://cf-courses-data.s3.us.cloud-object-
storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-
SkillsNetwork/labs/Module%204/data/TopSellingAlbums.csv'
df = pd.read_csv(csv_path)
We can use the method head() to examine the first five rows of a dataframe:
df.head()
Figure 545
# Read data from Excel File and print the first five rows
xlsx_path = 'https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-
data/CognitiveClass/PY0101EN/Chapter%204/Datasets/TopSellingAlbums.xlsx'
df = pd.read_excel(xlsx_path)
df.head()
Figure 546
Figure 547
x = df['Length']
x
0 00:42:19
1 00:42:11
2 00:42:49
3 00:57:44
4 00:46:33
5 00:43:08
6 01:15:54
7 00:40:01
Name: Length, dtype: object
You can also get a column as a dataframe. For example, we can assign the column Artist:
x = df[['Artist']]
type(x)
You can do the same thing for multiple columns; we just put the dataframe name, in this case, df,
and the name of the multiple column headers enclosed in double brackets. The result is a new
dataframe comprised of the specified columns:
y = df[['Artist','Length','Genre']]
y
Figure 548
Figure 549
One way to access unique elements is the iloc method, where you can access the 1st row and
the 1st column as follows:
# Access the value on the first row and the first column
df.iloc[0, 0]
'Michael Jackson'
You can access the 2nd row and the 1st column as follows:
# Access the value on the second row and the first column
df.iloc[1,0]
'AC/DC'
You can access the 1st row and the 3rd column as follows:
# Access the value on the first row and the third column
df.iloc[0,2]
1982
# Access the value on the second row and the third column
df.iloc[1,2]
1980
Figure 550
Figure 551
You can perform slicing using both the index and the name of the column:
Figure 552
df.loc[0:2, 'Artist':'Released']
Figure 554
q = df[['Rating']]
q
Assign the variable q to the dataframe that is made up of the column Released and Artist:
q = df[['Released', 'Artist']]
q
Use the following list to convert the dataframe index df to characters and assign it to df_new;
find the element corresponding to the row index a and column 'Artist'. Then select the
rows a through d for the column 'Artist'
df_new=df
df_new.index=new_index
df_new.loc['a', 'Artist']
df_new.loc['a':'d', 'Artist']
a Michael Jackson
b AC/DC
c Pink Floyd
d Whitney Houston
Name: Artist, dtype: object
Question 2
How would you access the first row and first column in the DataFrame df?
o df.ix[1,1]
o df.ix[1,0]
o df.ix[0,1]
o df.ix[0,0]
Correct: As Python uses zero-based indexing, the output here will be the first row and first
column.
Question 3
What is the proper way to load a CSV file using pandas?
o pandas.import_csv(‘data.csv’)
o pandas.load_csv(‘data.csv’)
o pandas.read_csv(‘data.csv’)
o pandas.from_csv(‘data.csv’)
Correct: pandas.read_csv('data.csv') loads CSV files using Pandas.
Question 4
Assume that you have a data frame containing details of various musical artists, their famous
albums, their genres, and various other parameters. Here, 'Genre' is the fifth column in the
sequence and there is an entry of “Disco” in the 7th row of the data. How would you select the
Genre disco?
o df.iloc[7, ‘Genre’]
o df.iloc[7, 5]
o df.iloc[6, 4]
o df.iloc[6, ‘genre’]
Correct: Correct! df.iloc[6, 4] will return the genre “disco.”
Figure 555
OBJECTIVES
In the video we will be covering
• the basics and array creation;
• indexing and slicing;
• basic operations;
• universal functions.
Figure 556
Figure 557
A Python list is a container that allows you to store and access data. Each element is associated
with an index. We can access each element using a square bracket as follows.
Figure 558
A Numpy array or ND array is similar to a list. It's usually fixed in size and each element is of the
same type, in this case integers.
Figure 559
Figure 560
Figure 561
Figure 562
Figure 563
Figure 564
Figure 565
Figure 566
Figure 567
The attribute size is the number of elements in the array. As there are five elements the result is
five. The next two attributes will make more sense when we get to higher dimensions, but let's
review them. The attribute ndim represents the number of array dimensions or the rank of the
array, in this case one. The attribute shape is a tuple of integers indicating the size of the array
in each dimension.
Figure 568
Figure 569
Figure 570
Figure 571
Figure 572
Figure 573
Figure 574
Figure 575
Figure 576
We can select the elements from one to three and assign it to a new Numpy array d as follows.
Figure 577
Figure 578
Like lists, we do not count the element corresponding to the last index.
Figure 579
Figure 580
Figure 581
Figure 582
Figure 583
Consider the vector u with two elements, the elements are distinguished by the different colors.
Similarly, consider the vector v with two components. In vector addition, we create a new vector
in this case z. The first component of z is the addition of the first component of vectors u and v.
Similarly, the second component is the sum of the second components of u and v. This new
vector z is now a linear combination of the vector u and v.
Figure 584
Figure 585
It's helpful to use the tip to tail method when adding vectors, placing the tail of the vector v on
the tip of vector u.
Figure 586
The new vector z is constructed by connecting the base of the first vector u with the tail of the
second v.
Figure 587
Figure 588
Vector Addition
We can also perform vector addition with one line of Numpy code. It would require multiple lines
to perform vector addition1 on two lists as shown on the right side of the screen. In addition, the
Numpy code will run much faster. This is important if you have lots of data.
Figure 589
Vector Subtraction
We can also perform vector subtraction by changing the addition sign to a subtraction sign. It
would require multiple lines to perform vector subtraction on two lists as shown on the right
side of the screen.
Figure 590
1
At 5:44 in the video the transcript and voice over should be "addition" instead of "subtraction."
Figure 591
Consider the vector y, each component is specified by a different color. We simply multiply the
vector by a scalar value, in this case two. Each component of the vector is multiplied by two, in
this case each component is doubled.
Figure 592
We can use the line segment or arrows to visualize what's going on. The original vector y is in
purple.
Figure 593
Figure 594
Vector multiplication with a scalar only requires one line of code using Numpy. It would require
multiple lines to perform the same task as shown with Python lists as shown on the right side
of the screen. In addition, the operation would also be much slower.
Figure 595
Hadamard Product
Hadamard product is another widely used operation in data science. Consider the following two
vectors, u and v. The Hadamard product of u and v is a new vector z. The first component of z is
the product of the first element of u and v. Similarly, the second component is the product of the
second element of u and v. The resultant vector consists of the entry wise product of u and v.
Figure 596
Figure 597
Dot Product
The dot product is another widely used operation in data science. Consider the vector u and v,
the dot product is a single number given by the following term and represents how similar two
vectors are. We multiply the first component from v and u, we then multiply the second
component and add the result together. The result is a number that represents how similar the
two vectors are.
Figure 598
We can also perform dot product using the Numpy function dot and assign it with the variable
result as follows.
Figure 599
Figure 600
UNIVERSAL FUNCTIONS
A universal function is a function that operates on ND arrays.
Figure 601
We can apply a universal function to a Numpy array. Consider the arrays a, we can calculate the
mean or average value of all the elements in a using the method mean. This corresponds to the
average of all the elements. In this case the result is zero.
Figure 602
There are many other functions. For example, consider the Numpy arrays b. We can find the
maximum value using the method max()2. We see the largest value is five, therefore the method
max returns a five.
Figure 603
We can use Numpy to create functions that map Numpy arrays to new Numpy arrays.
Let's implement some code on the left side of the screen and use the right side of the screen to
demonstrate what's going on. We can access the value of pi in Numpy as follows. We can
create the following Numpy array in radians. This array corresponds to the following vector.
We can apply the function sine to the array x and assign the values to the array y. This applies
the sine function to each element in the array, this corresponds to applying the sine function to
each component of the vector. The result is a new array y, where each value corresponds to a
sine function being applied to each element in the array x.
Figure 604
2
At 8:54 in the video the transcript and voice over should be "method max" instead of "method five."
A useful function for plotting mathematical functions is linspace. Linspace returns evenly spaced
numbers over a specified interval.
Figure 605
Figure 606
Figure 607
Figure 608
Figure 609
If we change the parameter num to 9, we get 9 evenly spaced numbers over the interval from -
2 to 2. The result is the difference between subsequent samples is 0.5 as opposed to 1 as before.
Figure 610
Figure 611
The first input corresponds to the values for the horizontal or x-axis.
Figure 612
The second input corresponds to the values for the vertical or y-axis.
Figure 613
There's a lot more you can do with Numpy. Check out the labs and numpy.org for more.
Thanks for watching the video.
1D Numpy in Python
Objectives
Table of Contents
• What is Numpy?
o Type
o Assign Value
o Slicing
o Assign Value with List
o Other Attributes
• Numpy Array Operations
o Array Addition
o Array Multiplication
o Product of Two Numpy Arrays
o Dot Product
o Adding Constant to a Numpy Array
• Mathematical Functions
• Linspace
What is Numpy?
NumPy is a Python library used for working with arrays, linear algebra, fourier transform, and
matrices. NumPy stands for Numerical Python and it is an open source project. The array object
in NumPy is called ndarray, it provides a lot of supporting functions that make working with
ndarray very easy.
Arrays are very frequently used in data science, where speed and resources are very important.
NumPy is usually imported under the np alias.
It's usually fixed in size and each element is of the same type. We can cast a list to a numpy
array by first importing numpy:
import numpy as np
a = np.array([0, 1, 2, 3, 4])
a
array([0, 1, 2, 3, 4])
Figure 614
print("a[0]:", a[0])
print("a[1]:", a[1])
print("a[2]:", a[2])
print("a[3]:", a[3])
print("a[4]:", a[4])
a[0]: 0
a[1]: 1
a[2]: 2
a[3]: 3
a[4]: 4
print(np.__version__)
1.26.1
Type
If we check the type of the array we get numpy.ndarray:
type(a)
numpy.ndarray
As numpy arrays contain data of the same type, we can use the attribute "dtype" to obtain the
data type of the array’s elements. In this case, it's a 64-bit integer:
a.dtype
dtype('int32')
b.dtype
dtype('float64')
If we examine the attribute dtype we see float 64, as the elements are not integers:
Assign value
We can change the value of the array. Consider the array c:
c = np.array([20, 1, 2, 3, 4])
c
array([20, 1, 2, 3, 4])
c[0] = 100
c
array([100, 1, 2, 3, 4]
c[4] = 0
c
array([100, 1, 2, 3, 0])
Try it yourself
Assign the value 20 for the second element in the given array.
d = c[1:4]
d
array([1, 2, 3])
# Set the fourth element and fifth element to 300 and 400
print(arr[1:5:2])
[2,4]
print(arr[:4])
[1 2 3 4]
print(arr[4:])
[5 6 7]
print(arr[1:5:])
[2 3 4 5]
Try it yourself
Print the even elements in the given array.
We can use the list as an argument in the brackets. The output is the elements corresponding
to the particular indexes:
d = c[select]
d
array([100, 2, 300, 400])
We can assign the specified elements to a new value. For example, we can assign the values to
100 000 as follows:
c[select] = 100000
c
array([100000, 1, 100000, 100000, 100000])
Other Attributes
Let's review some basic array attributes using the array a:
a.size
5
The next two attributes will make more sense when we get to higher dimensions but let's review
them. The attribute ndim represents the number of array dimensions, or the rank of the array. In
this case, one:
a.ndim
1
The attribute shape is a tuple of integers indicating the size of the array in each dimension:
a.shape
(5,)
b.ndim
b.shape
(7,)
mean = a.mean()
mean
0.0
standard_deviation=a.std()
standard_deviation
1.0
b = np.array([-1, 2, 3, 4, 5])
b
array([-1, 2, 3, 4, 5])
max_b = b.max()
max_b
5
min_b = b.min()
min_b
-1
min_c = c.min()
min_c
Array Addition
Consider the numpy array u:
u = np.array([1, 0])
u
array([1, 0])
v = np.array([0, 1])
v
array([0, 1])
z = np.add(u, v)
z
array([1, 1])
# Plotting functions
import time
import sys
import numpy as np
Plotvec1(u, z, v)
Figure 615
Try it yourself
Perform addition operation on the given numpy array arr1 and arr2:
Array Subtraction
Consider the numpy array a:
c = np.subtract(a, b)
print(c)
[ 5 10 15]
Try it yourself
Perform subtraction operation on the given numpy array arr1 and arr2:
Array Multiplication
Consider the vector numpy array y:
x = np.array([1, 2])
x
array([1, 2])
y = np.array([2, 1])
y
array([2, 1])
z = np.multiply(x, y)
z
array([2, 2])
Try it yourself
Perform multiply operation on the given numpy array arr1 and arr2:
c = np.divide(a, b)
c
array([5., 2., 6.])
Try it yourself
Perform division operation on the given numpy array arr1 and arr2:
Dot Product
The dot product of the two numpy arrays u and v is given by:
X = np.array([1, 2])
Y = np.array([3, 2])
#Elements of X
print(X[0])
print(X[1])
1
2
np.dot(X, Y)
7
#Elements of Y
print(Y[0])
print(Y[1])
3
2
Figure 616
Try it yourself
Perform dot operation on the given numpy array ar1 and ar2:
u = np.array([1, 2, 3, -1])
u
array([ 1, 2, 3, -1])
u + 1
Figure 617
Mathematical Functions
We can access the value of pi in numpy as follows :
# The value of pi
np.pi
3.141592653589793
We can apply the function sin to the array x and assign the values to the array y; this applies the
sine function to each element in the array:
y = np.sin(x)
y
array([0.0000000e+00, 1.0000000e+00, 1.2246468e-16])
Linspace
A useful function for plotting mathematical functions is linspace. Linspace returns evenly spaced
numbers over a specified interval.
numpy.linspace(start, stop, num = int value)
start : start of interval range
stop : end of interval range
num : Number of samples to generate.
If we change the parameter num to 9, we get 9 evenly spaced numbers over the interval from -
2 to 2:
We can apply the sine function to each element in the array x and assign it to the array y:
Figure 618
Try it yourself
Make a numpy array within [5, 4] and 6 elements
But if you want to result in the form of the list, then you can use for loop:
for x in arr1:
print(x)
1
2
3
u = np.array([1, 0])
v = np.array([0, 1])
u - v
array([ 1, -1])
z = np.array([2, 4])
-2 * z
array([-4, -8])
3. Consider the list [1, 2, 3, 4, 5] and [1, 0, 1, 0, 1]. Cast both lists to a numpy array
then multiply them together:
a = np.array([1, 2, 3, 4, 5])
b = np.array([1, 0, 1, 0, 1])
a * b
array([1, 0, 3, 0, 5])
import time
import sys
import numpy as np
def Plotvec2(a,b):
ax = plt.axes()# to generate the full window axes
ax.arrow(0, 0, *a, head_width=0.05, color ='r', head_length=0.1)#Add an arrow to
the a Axes with arrow head width 0.05, color red and arrow head length 0.1
plt.text(*(a + 0.1), 'a')
ax.arrow(0, 0, *b, head_width=0.05, color ='b', head_length=0.1)#Add an arrow to
the b Axes with arrow head width 0.05, color blue and arrow head length 0.1
plt.text(*(b + 0.1), 'b')
plt.ylim(-2, 2)#set the ylim to bottom(-2), top(2)
plt.xlim(-2, 2)#set the xlim to left(-2), right(2)
Figure 619
5. Convert the list [1, 0] and [0, 1] to numpy arrays a and b. Then, plot the arrays as vectors
using the function Plotvec2 and find their dot product:
Figure 620
Figure 621
7. Why are the results of the dot product for [-1, 1] and [1, 1] and the dot product for [1,
0] and [0, 1] zero, but not zero for the dot product for [1, 1] and [0, 1]?
Hint: Study the corresponding figures, pay attention to the direction the arrows are pointing to.
Answer: The vectors used for question 4 and 5 are perpendicular. As a result, the dot product is
zero.
8. Convert the list [1, 2, 3] and [8, 9, 10] to numpy arrays arr1 and arr2. Then
perform Addition , Subtraction , Multiplication , Division and Dot Operation on
the arr1 and arr2.
9. Convert the list [1, 2, 3, 4, 5] and [6, 7, 8, 9, 10] to numpy arrays arr1 and arr2.
Then find the even and odd numbers from arr1 and arr2.
Figure 622
TABLE OF CONTENTS
In this video, we will cover
• the basics and array creation in 2D,
• indexing and slicing in 2D,
• and basic operations in 2D.
Figure 623
Figure 624
Figure 625
It is helpful to visualize the numpy array as a rectangular array each nested lists corresponds to
a different row of the matrix.
Figure 626
We can use the attribute ndim to obtain the number of axes or dimensions referred to as the
rank. The term rank does not refer to the number of linearly independent columns like a matrix.
It's useful to think of ndim as the number of nested lists.
Figure 627
Figure 628
This list contains another set of lists. This represents the second dimension or axis. The number
of lists the list contains does not have to do with the dimension but the shape of the list.
Figure 629
Figure 630
Figure 631
The first element in the tuple corresponds to the number of nested lists contained in the original
list or the number of rows in the rectangular representation, in this case 3.
Figure 632
The second element corresponds to the size of each of the nested list or the number of columns
in the rectangular array zero.
Figure 633
Figure 634
Figure 635
We can also use the attribute size to get the size of the array. We see there are three rows and
three columns.
Figure 636
Figure 637
We can use rectangular brackets to access the different elements of the array. The following
image demonstrates the relationship between the indexing conventions for the lists like
representation. The index in the first bracket corresponds to the different nested lists each a
different color. The second bracket corresponds to the index of a particular element within the
nested list.
Figure 638
Figure 639
Figure 640
Figure 641
Figure 642
Figure 643
Figure 644
Figure 645
Figure 646
Figure 647
Figure 648
Figure 649
Figure 650
SLICING
We can also use slicing in numpy arrays.
Figure 651
Figure 652
Figure 653
Figure 654
Figure 655
Figure 656
Add
We can also add arrays, the process is identical to matrix addition. Consider the matrix X, each
element is colored differently. Consider the matrix Y. Similarly, each element is colored
differently. We can add the matrices. This corresponds to adding the elements in the same
position, i.e adding elements contained in the same color boxes together. The result is a new
matrix that has the same size as matrix Y or X. Each element in this new matrix is the sum of the
corresponding elements in X and Y.
Figure 657
To add two arrays in numpy, we define the array in this case X. Then we define the second array
Y, we add the arrays. The result is identical to matrix addition.
Figure 658
Figure 659
Figure 660
Figure 661
Figure 662
Figure 663
Hadarmad Product
Hadamard product corresponds to multiplying each of the elements in the same position i.e
multiplying elements contained in the same color boxes together. The result is a new matrix that
is the same size as matrix Y or X. Each element in this new matrix is the product of the
corresponding elements in X and Y.
Figure 664
Figure 665
We can find the product of two arrays X and Y in one line, and assign it to the variable Z as
follows.
Figure 666
Figure 667
We can also perform matrix multiplication with Numpy arrays. Matrix multiplication is a little
more
complex but let's provide a basic overview. Consider the matrix A where each row is a different
color. Also, consider the matrix B where each column is a different color.
Figure 668
In linear algebra, before we multiply matrix A by matrix B, we must make sure that the number
of columns in matrix A in this case three is equal to the number of rows in matrix B, in this case
three.
Figure 669
From matrix multiplication, to obtain the ith row and jth column of the new matrix, we take the
dot product of the ith row of A (blue arrow) with the jth columns of B (red arrow). For the first
column, first row we take the dot product of the first row of A with the first column of B as
follows. The result is zero.
Figure 670
Figure 671
For the second row and the first column of the new matrix, we take the dot product of the second
row of the matrix A. With the first column of matrix B, the result is zero.
Figure 672
Finally, for the second row and the second column of the new matrix, we take the dot product
of the second row of the matrix A with the second column of matrix B, the result is two.
Figure 673
Figure 674
OBJECTIVES
After completing this lab you will be able to:
• Operate comfortably with numpy
• Perform complex operations with numpy
TABLE OF CONTENTS
• Create a 2D Numpy Array
• Accessing different elements of a Numpy Array
• Basic Operations
Consider the list a, which contains three nested lists each of equal size.
# Create a list
a = [[11, 12, 13], [21, 22, 23], [31, 32, 33]]
a
[[11, 12, 13], [21, 22, 23], [31, 32, 33]]
We can use the attribute ndim to obtain the number of axes or dimensions, referred to as the
rank.
Attribute shape returns a tuple corresponding to the size or number of each dimension.
Figure 675
We can access the 2nd-row, 3rd column as shown in the following figure:
Figure 676
We simply use the square brackets and the indices corresponding to the element we would like:
Figure 677
We can also use slicing in numpy arrays. Consider the following figure. We would like to obtain
the first two columns in the first row
Figure 678
# Access the element on the first row and first and second columns
A[0][0:2]
array([11, 12])
Similarly, we can obtain the first two rows of the 3rd column as follows:
# Access the element on the first and second rows and third column
A[0:2, 2]
array([13, 23])
Figure 679
BASIC OPERATIONS
We can also add arrays. The process is identical to matrix addition. Matrix addition of X and Y is
shown in the following figure:
Figure 680
# Add X and Y
Z = X + Y
Z
array([[3, 1],
[1, 3]])
Figure 681
# Multiply Y with 2
Z = 2 * Y
Z
array([[4, 2],
[2, 4]])
Figure 682
# Multiply X with Y
Z = X * Y
Z
array([[2, 0],
[0, 2]])
We can also perform matrix multiplication with the numpy arrays A and B as follows:
First, we define matrix A and B:
# Create a matrix A
# Create a matrix B
# Create a matrix C
C = np.array([[1,1],[2,2],[3,3]])
C
array([[1, 1],
[2, 2],
[3, 3]])
Access the element on the first row and first and second columns.
IMPORTANCE OF APIS
APIs are essential for any engineer because they provide a way to access data and functionality
from other systems, which can save time and resources. For instance, APIs can be used to
integrate applications into the existing architecture of a server or application, allowing
developers to communicate between various products and services without requiring direct
implementation.
APIs are also important because they enable developers to create new applications by
leveraging existing functionality from other systems. This can help developers throughout the
engineering and development process of apps.
APIs are used in a wide range of applications, from social media platforms to e-commerce
websites. They are also used in mobile applications, web applications, and desktop applications.
APPLICATIONS OF APIS
APIs have a wide range of applications, some of which are:
3. Social media platforms: Social media platforms like Facebook, Twitter, and Instagram use
APIs to allow developers to access their data and functionality. This allows developers to
create applications that can interact with these platforms and provide additional
functionality to users.
4. E-commerce websites: E-commerce websites like Amazon and eBay use APIs to allow
developers to access their product catalogs and other data. This allows developers to create
applications that can interact with these platforms and provide additional functionality to
users.
5. Weather applications: Weather applications like AccuWeather and The Weather Channel
use APIs to access weather data from various sources. This allows developers to create
applications that can provide users with up-to-date weather information.
6. Maps and navigation applications: Maps and navigation applications like Google Maps
and Waze use APIs to access location data and other information. This allows developers
to create applications that can provide users with directions, traffic updates, and other
location-based information.
Question 2
What attribute retrieves the number of elements in a Numpy array?
o a.dtype
o a.ndim
o a.shape
o a.size
Correct: size will return the total number of elements in an array of any Dimension.
Question 3
How would you change the first element to 10 in this array c = np.array([100,1,2,3,0])?
o c[4]=10
o c[2]=10
o c[1]=10
o c[0]=10
Correct: Index 0 references the first element.
Example:
content = file.read()
Pandas
Package/Method Description Syntax and Code Example
.read_csv() Reads data from a `.CSV` Syntax: dataframe_name = pd.read_csv("filename.csv")
file and creates a DataFrame. Example: df = pd.read_csv("data.csv")
.read_excel() Reads data from an Excel file Syntax:
and creates a DataFrame.
dataframe_name = pd.read_excel("filename.xlsx")
Example:
df = pd.read_excel("data.xlsx")
dataframe_name["column_name"].replace(old_value,
new_value, inplace=True)
Example:
df["status"].replace("In Progress", "Active",
inplace=True)
tail() Displays the last n rows of Syntax:
the DataFrame.
dataframe_name.tail(n)
Example:
df.tail(5)
Numpy
Package/Method Description Syntax and Code Example
Importing NumPy Imports the NumPy library. Syntax:
import numpy as np
Example:
import numpy as np
np.array() Creates a one or multi- Syntax:
dimensional array,
array_1d = np.array([list1 values]) # 1D Array
array_2d = np.array([[list1 values], [list2 values]]) #
2D Array
Example:
array_1d = np.array([1, 2, 3]) # 1D Array
array_2d = np.array([[1, 2], [3, 4]]) # 2D Array
Numpy Array Attributes - Calculates the mean of Example:
array elements
- Calculates the sum of array np.mean(array)
elements
- Finds the minimum value in np.sum(array)
the array
- Finds the maximum value np.min(array
in the array
- Computes dot product of np.max(array)
two arrays
np.dot(array_1, array_2)
Term Definition
.csv file A .csv (Comma-Separated Values) file is a plain text file format for storing tabular
data, where each line represents a row and uses commas to separate values in
different columns.
.txt file A .txt (Text) file is a common file format that contains plain text without specific
formatting, making it suitable for storing and editing textual data.
Append To "append" means to add or attach something to the end of an existing object,
typically used in the context of adding data to a file or elements to a data structure
like a list in Python.
Data analysis Data analysis is the process of inspecting, cleaning, transforming, and interpreting
data to discover useful information, draw conclusions, and support decision-
making.
DataFrames A DataFrames in Pandas is a two-dimensional, tabular data structure for storing
and analyzing data, consisting of rows and columns.
Dependencies Dependencies in Pandas are external libraries or modules, such as NumPy, that
Pandas rely on for fundamental data manipulation and analysis functionality.
File attribute File attributes generally refer to properties or metadata associated with files,
which are managed at the operating system level.
File object A "file object" in Python represents an open file, allowing reading from or writing
to the file.
Hadamard Product The Hadamard product is a mathematical operation that involves element-wise
multiplication of two matrices or arrays of the same shape, producing a new
matrix with each element being the product of the corresponding elements in the
input matrices.
Importing pandas To import Pandas in Python, you use the statement: import pandas as pd, which
allows you to access Pandas functions and data structures using the abbreviation
"pd."
Index In Python, an "index" typically refers to a position or identifier used to access
elements within a sequence or data structure, such as a list or string.
NumPy NumPy in Python is a fundamental library for numerical computing that provides
support for large, multi-dimensional arrays and matrices, as well as a variety of
high-level mathematical functions to operate on these arrays.
One dimensional NumPy A one-dimensional NumPy array is a linear data structure that stores elements in
a single sequence, often used for numerical computations and data manipulation.
Open function In Python, the "open" function is used to access and manipulate files, allowing you
to read from or write to a specified file.
Pandas Pandas is a popular Python library for data manipulation and analysis, offering
data structures and tools for working with structured data like tables and time
series.
Pandas library Pandas library in Python refer to the various modules and functions within the
Pandas library, which provides powerful data structures and data analysis tools
for working with structured data.
Plotting Mathematical Functions Plotting mathematical functions in Python involves using libraries like Matplotlib
to create graphical representations of mathematical equations, aiding
visualization, and analysis.
Shape In NumPy, "shape" refers to an array's dimensions (number of rows and columns),
describing its size and structure.
Two dimensional NumPy A two-dimensional NumPy array is a structured data representation with rows
and columns, resembling a matrix or table, ideal for various data manipulation and
analysis tasks.
Universal Functions Universal functions (ufuncs) in NumPy are functions that operate element-wise
on arrays, providing efficient and vectorized operations for a wide range of
mathematical and logical operations.
Vector addition Vector addition in Python involves adding corresponding elements of two or more
vectors, producing a new vector with the sum of their components.
LEARNING OBJECTIVES
• Explain the use of the HTTP protocol using the Requests Library method.
• Describe how the URL Request Response HTTP protocol works.
• Invoke simple, open-source APIs.
• Perform basic web scraping using Python.
• Work with different file formats using Python.
• Explain the difference between APIs and REST APIs.
• Summarize how APIs receive and send information.
Figure 683
Specifically, we will discuss what an API is, API libraries, and REST APIs, including Request and
Response and an example with PyCoinGecko.
Figure 684
An API lets two pieces of software talk to each other. For example, you have your program, you
have some data, you have other software components. You use the API to communicate with
other software via inputs and outputs. Just like a function, you don’t have to know how the API
works, just its inputs and outputs.
Figure 685
Figure 686
Let’s clean up the diagram. When you create a dictionary, and then create a pandas object with
the DataFrame constructor, in API lingo, this is an “instance.” The data in the dictionary is passed
along to the pandas API. You then use the dataframe to communicate with the API. When you
call the method head, the dataframe communicates with the API displaying the first few rows
of the dataframe. When you call the method mean the API will calculate the mean and return
the values.
Figure 687
REST APIs are another popular type of API; they allow you to communicate through the internet
allowing you to take advantage of resources like storage, access more data, artificial intelligence
algorithms, and much more. The RE stands for Representational, the S for State, and T for
Transfer.
Figure 688
Figure 689
Here are some common terms. You or your code can be thought of as a client. The web service
is referred to as a resource. The client finds the service via an endpoint. We will review this more
in the next section. The client sends requests to the resource and the response to the client.
Figure 690
HTTP methods are a way of transmitting data over the internet. We tell the REST APIs what to
do by sending a request. The request is usually communicated via an HTTP message. The HTTP
message usually contains a JSON file. This contains instructions for what operation we would
like the service to perform. This operation is transmitted to the web service via the internet. The
service performs the operation.
Figure 691
In a similar manner, the web service returns a response via an HTTP message, where the
information is usually returned via a JSON file. This information is transmitted back to the client.
Figure 693
Figure 694
Using PyCoinGecko to collect data is simple. All we need is to install and import the library, then
create a client object, and finally use a function to request our data. In this function we are getting
data on bitcoin, in US dollars, for the past 30 days.
In this case our response is a JSON expressed as a Python dictionary of nested lists including
price, market cap, and total volumes, which contain the UNIX timestamp and the price at that
time.
Figure 696
We are only interested in price so that is what we will select using the key price.
Figure 697
Figure 698
with the columns timestamp and price it's difficult to understand the column timestamp.
Figure 699
We will convert it to a more readable format using the pandas function to_datetime.
Figure 700
Figure 701
Figure 702
Figure 703
Figure 704
Finally we will use plotly to create the candlestick chart and plot it.
Figure 705
Now we can view the candlestick chart by opening the HTML file and clicking Trust HTML in the
top left of the tab.
Figure 706
Figure 707
OBJECTIVES
After completing this lab you will be able to:
• Create and Use APIs in Python
INTRODUCTION
An API lets two pieces of software talk to each other. Just like a function, you don’t have to know
how the API works only its inputs and outputs. An essential type of API is a REST API that
allows you to access resources via the internet. In this lab, we will review the Pandas Library in
the context of an API, we will also review a basic REST API
TABLE OF CONTENTS
• Pandas is an API
• REST APIs Basics
• Quiz on Tuples
Pandas is an API
Pandas is actually set of software components , much of which is not even written in Python.
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from plotly.offline import plot
import matplotlib.pyplot as plt
import datetime
from pycoingecko import CoinGeckoAPI
from mplfinance.original_flavor import candlestick2_ohlc
dict_={'a':[11,21,31],'b':[12,22,32]}
When you create a Pandas object with the Dataframe constructor in API lingo, this is an
"instance". The data in the dictionary is passed along to the pandas API. You then use the
dataframe to communicate with the API.
df=pd.DataFrame(dict_)
type(df)
pandas.core.frame.DataFrame
When you call the method head the dataframe communicates with the API displaying the first
few rows of the dataframe.
df.head()
When you call the method mean,the API will calculate the mean and return the value.
df.mean()
a 21.0
b 22.0
dtype: float64
REST APIs
Rest API’s function by sending a request, the request is communicated via HTTP message. The
HTTP message usually contains a JSON file. This contains instructions for what operation we
would like the service or resource to perform. In a similar manner, API returns a response, via an
HTTP message, this response is usually contained within a JSON.
In cryptocurrency a popular method to display the movements of the price of a currency.
Figure 709
Figure 710
In this lab, we will be using the CoinGecko API to create one of these candlestick graphs for
Bitcoin. We will use the API to get the price data for 30 days with 24 observation per day, 1 per
hour. We will find the max, min, open, and close price per day meaning we will have 30
candlesticks and use that to generate the candlestick graph. Although we are using the
CoinGecko API we will use a Python client/wrapper for the API called PyCoinGecko.
PyCoinGecko will make performing the requests easy and it will deal with the enpoint targeting.
Lets start off by getting the data we need. Using the get_coin_market_chart_by_id(id,
vs_currency, days). id is the name of the coin you want, vs_currency is the currency you want
the price in, and days is how many days back from today you want.
cg = CoinGeckoAPI()
type(bitcoin_data )
dict
The response we get is in the form of a JSON which includes the price, market caps, and total
volumes along with timestamps for each observation. We are focused on the prices so we will
select that data.
bitcoin_price_data = bitcoin_data['prices']
bitcoin_price_data[0:5]
[[1714201454889, 62978.37519374921],
[1714205039272, 62927.54516807768],
[1714208491993, 62974.580289400634],
[1714212175314, 62931.11352813524],
[1714215937798, 62751.432064903514]]
Now that we have the DataFrame we will convert the timestamp to datetime and save it as a
column called Date. We will map our unix_to_datetime to each timestamp and convert it to a
readable datetime.
data['date'] = data['TimeStamp'].apply(lambda d:
datetime.date.fromtimestamp(d/1000.0))
Using this modified dataset we can now group by the Date and find the min, max, open, and
close for the candlesticks.
Finally we are now ready to use plotly to create our Candlestick Chart.
fig = go.Figure(data=[go.Candlestick(x=candlestick_data['date'],
open=candlestick_data['Price']['first'],
high=candlestick_data['Price']['max'],
low=candlestick_data['Price']['min'],
close=candlestick_data['Price']['last'])
])
fig.update_layout(xaxis_rangeslider_visible=False)
fig.show()
Figure 711
Question 2
Which data format is commonly found in the HTTP message for API requests?
o YAML
o HTML
o JSON
o XML
Correct: JSON is the most common data format found in HTTP message for API requests.
Question 3
What is the primary purpose of an API?
o To provide security to web applications
o To handle server-side database operations
o To connect and enable communication between software applications
o To design user interfaces for mobile applications
Correct: Primary role of API is to establish a connection and enable communication between
different parts of a software application.
Figure 712
OUTLINE
Specifically, we will discuss:
• Uniform Resource Locator: URL
• Request
• Response
Figure 713
Figure 714
Figure 715
When you, the client, use a web page your browser sends an HTTP request to the server where
the page is hosted. The server tries to find the desired resource by default "index.html".
Figure 716
if your request is successful, the server will send the object to the client in an HTTP response;
this includes information like the type of the resource, the length of the resource, and other
information. The table under the Web server represents a list of resources stored in the web
server. In this case, an HTML file, png image, and txt file.
Figure 717
Figure 718
Figure 719
Uniform resource locator (URL) is the most popular way to find resources on the web. We can
break the URL into three parts:
1. the scheme: this is the protocol, for this lab it will always be http://
2. Internet address or Base URL: this will be used to find the location;
some examples include www.ibm.com and www.gitlab.com
3. route: this is the location on the web server;
for example: /images/IDSNlogo.png
Figure 720
Figure 721
Request Message
The following is an example of the request message for the get request method. There are other
HTTP methods we can use.
Figure 722
In the start line we have the GET method. This is an HTTP method. In this case, it’s requesting
the file index.html
Figure 723
Figure 724
In the GET method the Request header is empty. Some Requests have a body; we will have an
example of a request body later.
Figure 725
Response Message
Figure 726
Figure 727
Figure 728
Finally, we have the response body containing the requested file, in this case
an HTML document.
Figure 729
Let’s look at other status codes. Some status code examples are shown in the table below. The
prefix indicates the class; for example, the 100s are informational responses; 100 indicates that
everything is OK so far. The 200s are Successful responses: For example, 200 The request has
succeeded. Anything in the 400s is bad news. 401 means the request is unauthorized. 500’s
stands for server errors, like 501 for not Implemented.
Figure 730
When an HTTP request is made, an HTTP method is sent. This tells the server what action to
perform. A list of several HTTP methods is shown here.
Figure 731
In the next video, we will use Python to apply the GET method that Retrieves data from the
server and the post method that sends data to the server.
Figure 732
OUTLINE
In this video, we will review Python library requests for Working with the HTTP protocols. We
will provide an overview of Get Requests and Post Requests
Figure 733
Figure 734
Figure 735
You can view the request body in the following line. As there is no body for a GET request,
we get a None.
You can view the HTTP response header using the attribute headers. This returns
a python dictionary of HTTP response headers. We can look at the dictionary values.
Figure 736
Figure 737
Figure 738
GET REQUEST
We send a GET request to the server. Like before, we have the Base URL in the Route; we
append /get. This indicates we would like to preform a GET request. This is demonstrated
in the following table:
Figure 739
Figure 740
The start of the query is a ?, followed by a series of parameter and value pairs, as shown in the
table below. The first parameter name is ”name” and the value is ”Joseph.” The second
parameter name is ”ID” and the Value is ”123.” Each pair, parameter, and value is separated by
an equal sign ”=”. The series of pairs is separated by the ampersand, ”&.”
Figure 741
Figure 742
CONTENT-TYPE
We can view the response as text: We can look at the key 'Content-Type’ to look at the content
type.
Figure 743
As the content 'Content-Type' is in the JSON, we format it using the method json() . It returns a
Python dict: The key 'args' has the name and values for the query string.
Figure 744
Figure 745
POST
In order to send the Post Request in the URL, we change the route to POST: This endpoint will
expect data and it is a convenient way to configure an HTTP request to send data to a server.
We have The Payload dictionary. To make a POST request, we use the post() function. The
variable payload is passed to the parameter data :
Figure 746
Figure 747
Figure 748
OBJECTIVES
After completing this lab you will be able to:
• Understand HTTP
• Handle HTTP Requests
TABLE OF CONTENTS
• Overview of HTTP
o Uniform Resource Locator:URL
o Request
o Response
• Requests in Python
o Get Request with URL Parameters
o Post Requests
OVERVIEW OF HTTP
When you, the client, use a web page your browser sends an HTTP request to the server where
the page is hosted. The server tries to find the desired resource by default "index.html". If
your request is successful, the server will send the object to the client in an HTTP response. This
includes information like the type of the resource, the length of the resource, and other
information.
The figure below represents the process. The circle on the left represents the client, the circle
on the right represents the Web server. The table under the Web server represents a list of
resources stored in the web server. In this case an HTML file, png image, and txt file .
The HTTP protocol allows you to send and receive information through the web including
webpages, images, and other web resources. In this lab, we will provide an overview of the
Requests library for interacting with the HTTP protocol.
Figure 749
REQUEST
The process can be broken into the request and response process. The request using the get
method is partially illustrated below. In the start line we have the GET method, this is an HTTP
method. Also the location of the resource /index.html and the HTTP version. The Request
header passes additional information with an HTTP request:
Figure 750
When an HTTP request is made, an HTTP method is sent, this tells the server what action to
perform. A list of several HTTP methods is shown below. We will go over more examples later.
RESPONSE
The figure below represents the response; the response start line contains the version number
HTTP/1.0, a status code (200) meaning success, followed by a descriptive phrase (OK). The
response header contains useful information. Finally, we have the response body containing the
requested file, an HTML document. It should be noted that some requests have headers.
Figure 751
Figure 752
REQUESTS IN PYTHON
Requests is a Python Library that allows you to send HTTP/1.1 requests easily. We can import
the library as follows:
import requests
import os
from PIL import Image
from IPython.display import IFrame
You can make a GET request via the method get to www.ibm.com:
url='https://www.ibm.com/'
r=requests.get(url)
We have the response object r, this has information about the request, like the status of the
request. We can view the status code using the attribute status_code.
r.status_code
200
print(r.request.headers)
{'User-Agent': 'python-requests/2.29.0', 'Accept-Encoding': 'gzip, deflate, br', 'Accept':
'*/*', 'Connection': 'keep-alive', 'Cookie': '_abck=3F3297824B31FE9752AE4F59D907CDE2~-
1~YAAQZWQwF4UMapmPAQAA1LUuwQtGjdW5OHRIgzySUIVU5o1cNb9XPey1sWycyY6JIpR5zkdzXVjYBH+eCWWleLANtzmULIl6+F
+UTPM4LN2wTZM57jdxUV/nzq4N7LIfCKwRgV6+6BYprYYxKndA5k+eRW+Qiv188fnPhAhci+Lpd8KvrKKXijFpc2aUJx3FpNEkoi
F9XpSLLtvrbLcO3F6ftZ3ur/qtoZWmxFsBgbAQfcqinlDQFmRsHoBpiYiBngkvEa2PwdWb7cJ+JphIAxSRdzoHy30GbArmEO7NMp
CkJDG5MlUgOlHz1gX2m/rhFXnAYCdqHypIKN/lg3BDlLW8CnY0M5mcSlS4OwUgMU/Uc+I2Fho=~-1~-1~-1;
bm_sz=EC27E766150F74485F4FC747DE6F2A29~YAAQZWQwF4YMapmPAQAA1LUuwRf/SvMqO/SXdzTNa9tePvsgiZhlQb5pyOkAu
8t+xDwesI98TKB6lV/UOwE4rIcFJpVT4AWrhMWbHthClECG1OmeQQMz9zAatmu/+z/6JPl1lKivCpOQe0SFCH1KKBoSa+PhGlWKp
MRdk58tT8lDBO4UnSUJ36QpE4r8X2Y47IIwEHPOPzEi5EcLqJmgVsEpx3F4Qxx4Jz6k+6ygqPXEaBedmirI+LSkKnJgsfbOWmel0
LadI2dEtY1Alsm3Gk+vQAXhD6WPVbEs9BdJ038nP25eMPi9E1cof6Hy7WXoP4l3XQPlwlQsy0kdZPFFzvlgRvd0q9SkB6pD~3684
406~3555641'}
You can view the request body, in the following line, as there is no body for a get request we get
a None:
You can view the HTTP response header using the attribute headers. This returns a python
dictionary of HTTP response headers.
header=r.headers
print(r.headers)
{'Content-Security-Policy': 'upgrade-insecure-requests', 'x-frame-options': 'SAMEORIGIN', 'Last-
Modified': 'Tue, 28 May 2024 21:30:04 GMT', 'ETag': '"1aec0-6198a564b16b3-gzip"', 'Accept-Ranges':
'bytes', 'Content-Type': 'text/html;charset=utf-8', 'X-Content-Type-Options': 'nosniff', 'Cache-
Control': 'max-age=20', 'Expires': 'Tue, 28 May 2024 21:50:35 GMT', 'X-Akamai-Transformed': '9 13656
0 pmb=mTOE,2', 'Content-Encoding': 'gzip', 'Date': 'Tue, 28 May 2024 21:50:15 GMT', 'Content-
Length': '13851', 'Connection': 'keep-alive', 'Vary': 'Accept-Encoding', 'Strict-Transport-
Security': 'max-age=31536000'}
We can obtain the date the request was sent using the key Date
header['date']
'Tue, 28 May 2024 21:50:15 GMT'
header['Content-Type']
'text/html;charset=utf-8'
r.encoding
'utf-8'
As the Content-Type is text/html we can use the attribute text to display the HTML in the
body. We can review the first 100 characters:
r.text[0:100]
'\n<!DOCTYPE HTML>\n<html lang="en-us">\n<head>\r\n \r\n \r\n \r\n \r\n \r\n
\r\n <meta charset="'
r=requests.get(url)
print(r.headers)
{'Date': 'Wed, 29 May 2024 03:07:54 GMT', 'X-Clv-Request-Id': 'c4ac306f-4bf8-4eb1-8a52-
d0c85bd4a19a', 'Server': 'Cleversafe', 'X-Clv-S3-Version': '2.5', 'Accept-Ranges': 'bytes', 'x-amz-
request-id': 'c4ac306f-4bf8-4eb1-8a52-d0c85bd4a19a', 'ETag': '"8bb44578fff8fdcc3d2972be9ece0164"',
'Content-Type': 'image/png', 'Last-Modified': 'Wed, 16 Nov 2022 03:32:41 GMT', 'Content-Length':
'78776'}
r.headers['Content-Type']
'image/png'
An image is a response object that contains the image as a bytes-like object. As a result, we must
save it using a file object. First, we specify the file path and name
path=os.path.join(os.getcwd(),'image.png')
path
'/resources/labs/Module 5/image.png'
We save the file, in order to access the body of the response we use the attribute content then
save it using the open function and write method:
with open(path,'wb') as f:
f.write(r.content)
Image.open(path)
Figure 753
In the previous section, we used the wget function to retrieve content from the web server as
shown below. Write the python code to perform the same task. The code should be the same
as the one used to download the image, but the file name should be 'Example1.txt'.
url='https://cf-courses-data.s3.us.cloud-object-
storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-
SkillsNetwork/labs/Module%205/data/Example1.txt'
path=os.path.join(os.getcwd(),'example1.txt')
r=requests.get(url)
with open(path,'wb') as f:
f.write(r.content)
You can use the GET method to modify the results of your query, for example retrieving data
from an API. We send a GET request to the server. Like before we have the Base URL, in the
Route we append /get, this indicates we would like to perform a GET request. This is
demonstrated in the following table:
Figure 754
The Base URL is for http://httpbin.org/ is a simple HTTP Request & Response Service.
The URL in Python is given by:
url_get='http://httpbin.org/get'
A query string is a part of a uniform resource locator (URL), this sends other information to the
web server. The start of the query is a ?, followed by a series of parameter and value pairs, as
shown in the table below. The first parameter name is name and the value is Joseph. The second
parameter name is ID and the Value is 123. Each pair, parameter, and value is separated by an
equals sign, =. The series of pairs is separated by the ampersand &.
Figure 755
payload={"name":"Joseph","ID":"123"}
Then passing the dictionary payload to the params parameter of the get() function:
r=requests.get(url_get,params=payload)
We can print out the URL and see the name and values
r.url
r.json()['args']
Post Requests
Like a GET request, a POST is used to send data to a server, but the POST request sends the data
in a request body. In order to send the Post Request in Python, in the URL we change the route
to POST:
url_post='http://httpbin.org/post'
This endpoint will expect data as a file or as a form. A form is convenient way to configure an
HTTP request to send data to a server.
To make a POST request we use the post() function, the variable payload is passed to the
parameter data :
r_post=requests.post(url_post,data=payload)
Comparing the URL from the response object of the GET and POST request we see
the POST request has no name or value pairs.
We can compare the POST and GET request body, we see only the POST request has a body:
r_post.json()['form']
{'ID': '123', 'name': 'Joseph'}
There is a lot more you can do. Check out Requests for more.
Figure 756
Lots of useful data is available on web pages, such as real estate prices and solutions to coding
questions. The website Wikipedia is a repository of the world's information.
Figure 757
OUTLINE
If you have an understanding of HTML, you can use Python to extract this information. In this
video, you will:
• review the HTML of a basic web page;
• understand the Composition of an HTML Tag;
• understand HTML Trees;
• and understand HTML Tables.
Figure 758
Let’s say you were asked to find the name and salary of players in a National Basketball League
from the following web page.
Figure 759
The web page is comprised of HTML. It consists of text surrounded by a series of blue text
elements enclosed in angle brackets called tags.
Figure 760
HTML Composition
The tags tells the browser how to display the content. The data we require is in this text.
Figure 761
Figure 762
Next, we have the body, this is what's displayed on the web page. This is usually the data we
are interested in.
Figure 763
HTML Elements
We see the elements with an “h3”, this means type 3 heading, makes the text larger and bold.
These tags have the names of the players, notice the data is enclosed in the elements. It starts
with a h3 in brackets and ends in a slash h3 in brackets.
Figure 764
Figure 765
Figure 766
Figure 767
Figure 768
Figure 769
Hyperlink Content
These tags contain the content, in this case what’s displayed on the web page.
Figure 770
Figure 771
Figure 772
Figure 773
Figure 774
The result will give you the ability to inspect the HTML. There is also other types of content such
as CSS and JavaScript that we will not go over in this course.
Figure 775
Figure 776
Figure 777
Document Tree
Let's go over a simple example. Tags may contain strings and other tags. These elements are
the tag’s children. We can represent this as a family tree. Each nested tag is a level in the tree.
Figure 778
The tag HTML tag contains the head and body tag. The Head and body tag are the descendants
of the html tag. In particular they are the children of the HTML tag.HTML tag is their parent.
Figure 779
Figure 780
Title tag is the child of the head tag and its parent is the head tag.
Figure 781
The title tag is a descendant of the HTML tag but not its child.
Figure 782
Figure 783
The bold tag is a child of the heading tag, the content of the tag is also part of the tree but this
can get unwieldy to draw.
Figure 784
HTML Tables
Next, let’s review HTML tables.
Figure 785
Figure 786
Each table row is defined with a <tr> tag, you can also use a table header tag for the first row.
Figure 787
The table row cell contains a set of <td> tags, each defines a table cell.
Figure 788
Figure 789
Figure 790
and so on.
Figure 791
Figure 792
Figure 793
Figure 794
Figure 795
We now have some basic knowledge of HTML. Now let's try and extract some data from a web
page.
Figure 796
Figure 797
Figure 798
Would you start manually copying and pasting information from different websites into a
spreadsheet?
Figure 799
Spending hours trying to find the right data, and eventually giving up because the task was too
overwhelming? That’s where web scraping can help.
Figure 800
Figure 801
To get started, we just need a little Python code and the help of two modules named Requests
and BeautifulSoup.
Figure 802
Figure 803
Figure 804
Figure 805
Figure 806
Figure 807
TAG OBJECT
The tag object corresponds to an HTML tag in the original document. For example, the tag
“title.”
Figure 808
Consider the tag h3. If there is more than one tag with the same name, the first element with
that tag is selected. In this case with Lebron James, we see the name is Enclosed in the bold
attribute "b". To extract it, use the Tree representation.
Figure 809
Figure 810
We can access the child of the tag or navigate down the branch as follows:
Figure 811
PARENT ATTRIBUTE
You can navigate up the tree by using the parent attribute. The variable tag child is located here.
We can access the parent.
Figure 812
Figure 813
NEXT-SIBLING ATTRIBUTE
We simply use the next-sibling attribute. We can find the sibling of sibling one. We simply use
the next sibling attribute.
Figure 814
NAVIGABLE STRING
Consider the tag child object.
Figure 815
Figure 816
You can return the content as a Navigable string, this is like a Python string that supports
Beautiful Soup functionality.
Figure 817
FIND_ALL()
Let's review the method find_all. This is a filter, you can use filters to filter based on a tag’s name,
its attributes, the text of a string, or on some combination of these.
Figure 818
Figure 819
Like before, create a BeautifulSoup object. But this time, name it table.
Figure 820
PYTHON ITERABLE
The find_all () method looks through a tag’s descendants and retrieves all descendants that
match your filters. Apply it to the table with the tag tr.
Figure 821
Figure 822
Figure 823
This corresponds to each row in the list, including the table header.
Figure 824
Figure 826
TAG OBJECT
Each element is a tag object. Consider the first row.
Figure 827
Figure 828
VARIABLE ROW
We can also iterate through each table cell.
Figure 829
First, we iterate through the list “table rows,” via the variable row.
Figure 830
Figure 831
We can apply the method find_all to find all the table cells,
Figure 832
then we can iterate through the variable cells for each row.
Figure 833
Figure 834
We continue to iterate through each element and repeat the process for each row.
Figure 835
Figure 836
Figure 838
Figure 839
Figure 840
Use the get method from the requests library to download the webpage. The input is the URL.
Use the text attribute to get the text and assign it to the variable page.
Figure 841
Then, create a BeautifulSoup object ‘soup’ from the variable page. It will allow you to parse
through the HTML page. You can now scrape the Page.
Figure 842
OBJECTIVES
After completing this lab you will be:
• Familiar with the basics of the BeautifulSoup Python library
• Be able to scrape webpages for data and filter the data
TABLE OF CONTENTS
• Beautiful Soup Object
o Tag
o Children, Parents, and Siblings
o HTML Attributes
o Navigable String
• Filter
o find All
o find
o HTML Attributes
o Navigable String
• Downloading And Scraping The Contents Of A Web
For this lab, we are going to be using Python and several Python libraries. Some of these libraries
might be installed in your lab environment or in SN Labs. Others may need to be installed by
you. The cells below will install these libraries when executed.
%%html
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h3>
<b id='boldest'>Lebron James</b>
</h3>
<p> Salary: $ 92,000,000 </p>
<h3> Stephen Curry</h3>
<p> Salary: $85,000, 000 </p>
<h3> Kevin Durant </h3>
<p> Salary: $73,200, 000</p>
</body>
</html>
Lebron James
Salary: $ 92,000,000
Stephen Curry
Salary: $85,000, 000
Kevin Durant
Salary: $73,200, 000
To parse a document, pass it into the BeautifulSoup constructor. The BeautifulSoup object
represents the document as a nested data structure:
First, the document is converted to Unicode (similar to ASCII) and HTML entities are converted
to Unicode characters. BeautifulSoup transforms a complex HTML document into a complex tree
of Python objects. The BeautifulSoup object can create other types of objects. In this lab, we will
cover BeautifulSoup and Tag objects, that for the purposes of this lab are identical. Finally, we
will look at NavigableString objects.
We can use the method prettify() to display the HTML in the nested structure:
print(soup.prettify())
tag_object=soup.title
print("tag object:",tag_object)
If there is more than one Tag with the same name, the first element with that Tag name is called.
This corresponds to the most paid player:
tag_object=soup.h3
tag_object
Enclosed in the bold attribute b, it helps to use the tree representation. We can navigate down
the tree using the child attribute to get the name.
As stated above, the Tag object is a tree of objects. We can access the child of the tag or navigate
down the branch as follows:
tag_child =tag_object.b
tag_child
parent_tag=tag_child.parent
parent_tag
tag_object
tag_object.parent
sibling_1=tag_object.next_sibling
sibling_1
sibling_2 is the header element, which is also a sibling of both sibling_1 and tag_object
sibling_2=sibling_1.next_sibling
sibling_2
Use the object sibling_2 and the method next_sibling to find the salary of Stephen Curry:
sibling_2.next_sibling
HTML Attributes
If the tag has attributes, the tag id="boldest" has an attribute id whose value is boldest. You
can access a tag’s attributes by treating the tag like a dictionary:
tag_child['id']
tag_child.attrs
You can also work with Multi-valued attributes. Check out BeautifulSoup Documentation for
more.
We can also obtain the content of the attribute of the tag using the Python get() method.
tag_child.get('id')
Navigable String
A string corresponds to a bit of text or content within a tag. Beautiful Soup uses the
NavigableString class to contain this text. In our HTML we can obtain the name of the first player
by extracting the string of the Tag object tag_child as follows:
tag_string=tag_child.string
tag_string
type(tag_string)
A NavigableString is similar to a Python string or Unicode string. To be more precise, the main
difference is that it also supports some BeautifulSoup features. We can convert it to string object
in Python:
unicode_string = str(tag_string)
unicode_string
FILTER
Filters allow you to find complex patterns, the simplest filter is a string. In this section we will
pass a string to a different filter method and Beautiful Soup will perform a match against that
exact string. Consider the following HTML of rocket launches:
%%html
<table>
<tr>
<td id='flight' >Flight No</td>
<td>Launch site</td>
<td>Payload mass</td>
</tr>
1 Florida 300 kg
2 Texas 94 kg
3 Florida 80 kg
FIND ALL
The find_all() method looks through a tag’s descendants and retrieves all descendants that
match your filters.
Name
When we set the name parameter to a tag name, the method will extract all the tags with that
name and its children.
table_rows=table_bs.find_all('tr')
table_rows
first_row =table_rows[0]
first_row
print(type(first_row))
first_row.td
If we iterate through the list, each element corresponds to a row in the table:
As row is a cell object, we can apply the method find_all to it and extract table cells in the
object cells using the tag td, this is all the children with the name td. The result is a list, each
element corresponds to a cell and is a Tag object, we can iterate through this list as well. We
can extract the content using the string attribute.
ATTRIBUTES
If the argument is not recognized it will be turned into a filter on the tag’s attributes. For example
with the id argument, Beautiful Soup will filter against each tag’s id attribute. For example, the
first td elements have a value of id of flight, therefore we can filter based on that id value.
table_bs.find_all(id="flight")
We can find all the elements that have links to the Florida Wikipedia page:
list_input=table_bs.find_all(href="https://en.wikipedia.org/wiki/Florida")
list_input
If we set the href attribute to True, regardless of what the value is, the code finds all tags
with href value:
table_bs.find_all(href=True)
There are other methods for dealing with attributes and other related methods. Check out the
following link
Using the logic above, find all the elements without href value
table_bs.find_all(href=False)
Using the soup object soup, find the element with the id attribute content set to "boldest".
soup.find_all(id="boldest")
string
With string you can search for strings instead of tags, where we find all the elements with
Florida:
table_bs.find_all(string="Florida")
FIND
The find_all() method scans the entire document looking for results. It’s useful if you are looking
for one element, as you can use the find() method to find the first element in the document.
Consider the following two tables:
%%html
<h3>Rocket Launch </h3>
<p>
<table class='rocket'>
<tr>
<td>Flight No</td>
<td>Launch site</td>
<td>Payload mass</td>
</tr>
<tr>
<td>1</td>
<td>Florida</td>
<td>300 kg</td>
</tr>
<tr>
<td>2</td>
<td>Texas</td>
<td>94 kg</td>
</tr>
<tr>
<td>3</td>
<td>Florida </td>
<td>80 kg</td>
</tr>
</table>
</p>
<p>
<table class='pizza'>
<tr>
<td>Pizza Place</td>
<td>Orders</td>
<td>Slices </td>
</tr>
<tr>
<td>Domino's Pizza</td>
Figure 843
We can find the first table using the tag name table
two_tables_bs.find("table")
We can filter on the class attribute to find the second table, but because class is a keyword in
Python, we add an underscore to differentiate them.
two_tables_bs.find("table",class_='pizza')
url = "http://www.ibm.com"
We use get to download the contents of the webpage in text format and store in a variable
called data:
data = requests.get(url).text
print(link.get('href'))
#The below url contains an html table with data about colors and color codes.
url = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-
DA0321EN-SkillsNetwork/labs/datasets/HTMLColorCodes.html"
# get the contents of the webpage in text format and store in a variable called data
data = requests.get(url).text
soup = BeautifulSoup(data,"html5lib")
Figure 844
Figure 845
INTRODUCTION
When collecting data you will find there are many different file formats that need to be collected
or read in order to complete a data driven story or analysis. When gathering the data Python can
make the process simpler with its predefined libraries, but before we explore Python let’s first
check out some of the various file formats.
Figure 846
Figure 847
Figure 848
USING DATAFRAMES
Now that we have learned how to read and output the data from a “csv” file let’s make it look a
little more organized. From the last example we were able to print out the data but because the
file had no headers it printed the first line of data as a header.
Figure 850
We easily solve this by adding a dataframe attribute. We use the variable “df” to call the file and
then add the “columns” attribute.
Figure 851
Figure 852
Figure 853
Figure 854
Figure 855
RECAP
In this video, you learned:
• How to recognize different file types
• How to use Python libraries to extract data
• How to use dataframes when collecting data
Figure 856
DATA ENGINEERING
Data engineering is one of the most critical and foundational skills in any data scientist’s toolkit.
File Format
A file format is a standard way in which information is encoded for storage in a file. First, the file
format specifies whether the file is a binary or ASCII file. Second, it shows how the information
is organized. For example, the comma-separated values (CSV) file format stores tabular data in
plain text.
To identify a file format, you can usually look at the file extension to get an idea. For example, a
file saved with name "Data" in "CSV" format will appear as Data.csv. By noticing the .csv
extension, we can clearly identify that it is a CSV file and the data is stored in a tabular format.
There are various formats for a dataset, .csv, .json, .xlsx etc. The dataset can be stored in different
places, on your local machine or sometimes online.
In this section, you will learn how to load a dataset into our Jupyter Notebook.
Now, we will look at some file formats and how to read them in Python:
The Pandas Library is a useful tool that enables us to read various datasets into a Pandas data
frame.
Let us look at how to read a CSV file in Pandas Library.
We use pandas.read_csv() function to read the csv file. In the parentheses, we put the file path
along with a quotation mark as an argument, so that pandas will read the file into a data frame
from that address. The file path can be either a URL or your local file address.
import piplite
await piplite.install(['seaborn', 'lxml', 'openpyxl'])
import pandas as pd
filename = "https://cf-courses-data.s3.us.cloud-object-
storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-
SkillsNetwork/labs/Module%205/data/addresses.csv"
df = pd.read_csv("addresses.csv", header=None)
df
Figure 857
df
Figure 858
df[‘First Name’]
0 John
1 Jack
2 John "Da Man"
3 Stephen
4 NaN
5 Joan "the bone", Anne
Name: First Name, dtype: object
Figure 859
loc() : loc() is label based data selecting method which means that we have to pass the
name of the row or column which we want to select.
# To select the 0th,1st and 2nd row of "First Name" column only
df.loc[[0,1,2], "First Name" ]
0 John
1 Jack
2 John "Da Man"
Name: First Name, dtype: object
Now, let's see how to use .iloc for selecting rows from our DataFrame.
iloc() : iloc() is a indexed based selecting method which means that we have to pass
integer index in the method to select specific row/column.
# To select the 0th,1st and 2nd row of "First Name" column only
df.iloc[[0,1,2], 0]
0 John
1 Jack
2 John "Da Man"
Name: 0, dtype: object
#import library
import pandas as pd
import numpy as np
#creating a dataframe
df=pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), columns=['a', 'b', 'c'])
df
a b c
0 1 2 3
1 4 5 6
2 7 8 9
a b c
0 11 12 13
1 14 15 16
2 17 18 19
Now we will use DataFrame.transform() function to find the square root to each element of the
dataframe.
a b c
sqrt sqrt sqrt
For more information about the transform() function please read the documentation.
import json
import json
person = {
'first_name' : 'Mark',
'last_name' : 'abc',
'age' : 27,
'address': {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
}
}
# Serializing json
json_object = json.dumps(person, indent = 4)
# Writing to sample.json
with open("sample.json", "w") as outfile:
outfile.write(json_object)
print(json_object)
{
"first_name": "Mark",
"last_name": "abc",
"age": 27,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
}
}
Using json.load()
The JSON package has json.load() function that loads the json content from a json file into a
dictionary.
It takes one parameter:
File pointer: A file pointer that points to a JSON file.
import json
print(json_object)
print(type(json_object))
{'first_name': 'Mark', 'last_name': 'abc', 'age': 27, 'address': {'streetAddress': '21 2nd
Street', 'city': 'New York', 'state': 'NY', 'postalCode': '10021-3100'}}
<class 'dict'>
import pandas as pd
filename = "https://cf-courses-data.s3.us.cloud-object-
storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-
SkillsNetwork/labs/Module%205/data/file_example_XLSX_10.xlsx"
df
Figure 860
import xml.etree.ElementTree as ET
filename = "https://cf-courses-data.s3.us.cloud-object-
storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-
SkillsNetwork/labs/Module%205/data/Sample-employee-XML-file.xml"
You would need to firstly parse an XML file and create a list of columns for data frame, then
extract useful information from the XML file and add to a pandas data frame.
Here is a sample code that you can use.:
tree = etree.parse("Sample-employee-XML-file.xml")
root = tree.getroot()
columns = ["firstname", "lastname", "title", "division", "building","room"]
firstname = node.find("firstname").text
lastname = node.find("lastname").text
title = node.find("title").text
division = node.find("division").text
building = node.find("building").text
room = node.find("room").text
dataframe
Figure 861
# Herein xpath we mention the set of xml nodes to be considered for migrating to the
dataframe which in this case is details node under employees.
df=pd.read_xml("Sample-employee-XML-file.xml", xpath="/employees/details")
Save Data
Correspondingly, Pandas enables us to save the dataset to csv by using the dataframe.to_csv()
method, you can add the file path and name along with quotation marks in the parentheses.
For example, if you would save the dataframe df as employee.csv to your local machine, you
may use the syntax below:
datatframe.to_csv("employee.csv", index=False)
We can also read and save other file formats, we can use similar functions to pd.read_csv() and
df.to_csv() for other data formats. The functions are listed in the following table:
Figure 862
# importing PIL
from PIL import Image
filename = "https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/dog-puppy-
on-garden-royalty-free-image-1586966191.jpg"
# Read image
img = Image.open('dog.jpg')
# Output Images
display(img)
Data Analysis
In this section, you will learn how to approach data acquisition in various ways and obtain
necessary insights from a dataset. By the end of this lab, you will successfully load the data into
Jupyter Notebook and gain some fundamental insights via the Pandas Library.
In our case, the Diabetes Dataset is an online source and it is in CSV (comma separated value)
format. Let's use this dataset as an example to practice data reading.
filename = "https://cf-courses-data.s3.us.cloud-object-
storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-
SkillsNetwork/labs/Module%205/data/diabetes.csv"
After reading the dataset, we can use the dataframe.head(n) method to check the top n rows of
the dataframe, where n is an integer. Contrary to dataframe.head(n), dataframe.tail(n) will show
you the bottom n rows of the dataframe.
df.shape
(768, 9)
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 768 entries, 0 to 767
Data columns (total 9 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Pregnancies 768 non-null int64
1 Glucose 768 non-null int64
2 BloodPressure 768 non-null int64
3 SkinThickness 768 non-null int64
4 Insulin 768 non-null int64
5 BMI 768 non-null float64
6 DiabetesPedigreeFunction 768 non-null float64
7 Age 768 non-null int64
8 Outcome 768 non-null int64
dtypes: float64(2), int64(7)
memory usage: 54.1 KB
df.describe()
Figure 863
Pandas describe() is used to view some basic statistical details like percentile, mean, standard
deviation, etc. of a data frame or a series of numeric values. When this method is applied to a
series of strings, it returns a different output
missing_data = df.isnull()
missing_data.head(5)
Figure 864
"True" stands for missing value, while "False" stands for not missing value.
Glucose
False 768
Name: Glucose, dtype: int64
BloodPressure
False 768
Name: BloodPressure, dtype: int64
SkinThickness
False 768
Name: SkinThickness, dtype: int64
Insulin
False 768
Name: Insulin, dtype: int64
BMI
False 768
Name: BMI, dtype: int64
DiabetesPedigreeFunction
False 768
Name: DiabetesPedigreeFunction, dtype: int64
Age
False 768
Name: Age, dtype: int64
Outcome
False 768
Name: Outcome, dtype: int64
df.dtypes
Pregnancies int64
Glucose int64
BloodPressure int64
SkinThickness int64
Insulin int64
BMI float64
DiabetesPedigreeFunction float64
Age int64
Outcome int64
dtype: object
As we can see before, All columns have the correct data type.
Visualization is one of the best way to get insights from the dataset. Seaborn and Matplotlib
are two of Python's most powerful visualization libraries.
# import libraries
import matplotlib.pyplot as plt
import seaborn as sns
Figure 865
As you can see above, 65.10% females are Diabetic and 34.90% are Not Diabetic.
Introduction
In this practice project, you will put the skills acquired through the course to use. You will extract
data from a website using webscraping and request APIs process it using Pandas and Numpy
libraries.
Project Scenario:
An international firm that is looking to expand its business in different countries across the world
has recruited you. You have been hired as a junior Data Engineer and are tasked with creating a
script that can extract the list of the top 10 largest economies of the world in descending order
of their GDPs in Billion USD (rounded to 2 decimal places), as logged by the International
Monetary Fund (IMF).
The required data seems to be available on the URL mentioned below:
URL: click in here
Objectives
After completing this lab you will be able to:
• Use Webscraping to extract required information from a website.
• Use Pandas to load and process the tabular data as a dataframe.
• Use Numpy to manipulate the information contatined in the dataframe.
• Load the updated dataframe to CSV file.
Figure 866
This does not affect the execution of your codes in any way and can be simply ignored.
Setup
import numpy as np
import pandas as pd
# You can also use this section to suppress warnings generated by your code:
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
warnings.filterwarnings('ignore')
Exercise 1
Extract the required GDP data from the given URL using Web Scraping.
URL="https://web.archive.org/web/20230902185326/https://en.wikipedia.org/wiki/List_of
_countries_by_GDP_%28nominal%29"
You can use Pandas library to extract the required table directly as a DataFrame. Note that the
required table is the third one on the website, as shown in the image below.
Figure 867
# Rename the column header from 'GDP (Million USD)' to 'GDP (Billion USD)'
df.rename(columns = {'GDP (Million USD)' : 'GDP (Billion USD)'})
Exercise 2
Modify the GDP column of the DataFrame, converting the value available in Million USD to
Billion USD. Use the round() method of Numpy library to round the value to 2 decimal places.
Modify the header of the DataFrame to GDP (Billion USD).
# Change the data type of the 'GDP (Million USD)' column to integer. Use astype()
method.
# Convert the GDP value in Million USD to Billion USD
# Use numpy.round() method to round the value to 2 decimal places.
# Rename the column header from 'GDP (Million USD)' to 'GDP (Billion USD)'
# Change the data type of the 'GDP (Million USD)' column to integer. Use astype()
method.
df['GDP (Million USD)'] = df['GDP (Million USD)'].astype(int)
# Rename the column header from 'GDP (Million USD)' to 'GDP (Billion USD)'
df.rename(columns = {'GDP (Million USD)' : 'GDP (Billion USD)'})
Exercise 3
Load the DataFrame to the CSV file named "Largest_economies.csv"
Example:
1. child_elements = parent_div.findChildren()
Example:
1. next_sibling =
current_element.find_next_sibling()
parent Access the parent element in Syntax:
the Document Object Model 1. parent = element.parent
(DOM). Example:
1. parent_div = paragraph.parent
select() Syntax:
1. element = soup.select(selector)
Example:
1. title_text = title_element.text
Term Definition
API Key An API key in Python is a secure access token or code used to authenticate and
authorize access to an API or web service, enabling the user to make
authenticated requests.
APIs APIs (Application Programming Interfaces) are a set of rules and protocols that
enable different software applications to communicate and interact, facilitating
the exchange of data and functionality.
Audio file An audio file is a digital recording or representation of sound, often stored in
formats like MP3, WAV, or FLAC, allowing playback and storage of audio
content.
Authorize In Python, "authorize" often means granting permission or access to a user or
system to perform specific actions or access particular resources, often related
to authentication and authorization mechanisms.
Beautiful Soup Objects Beautiful Soup objects in Python are representations of parsed HTML or XML
documents, allowing easy navigation, searching, and manipulation of the
document’s elements and data.
Bitcoin currency Bitcoin is a decentralized digital currency that operates without a central
authority, allowing peer-to-peer transactions on a blockchain network.
Browser A browser is a software application that enables users to access and interact
with web content, displaying websites and web applications.
Candlestick plot A candlestick plot in Python visually represents stock price movements over
time, using rectangles to illustrate the open, close, high, and low prices for a
given period.
Client/Wrapper A client or wrapper in Python is a software component that simplifies
interaction with external services or APIs, encapsulating communication and
providing higher-level functionality for developers.
CoinGecko API The CoinGecko API is a web service that provides cryptocurrency market data
and information, allowing developers to access real-time and historical data for
various cryptocurrencies.
DELETE Method The DELETE method in Python is an HTTP request method used to request the
removal or deletion of a resource on a web server.
Endpoint In Python, an "endpoint" refers to a specific URL or URI that a web service or
API exposes to perform a particular function or access a resource.
File extension A file extension is a suffix added to a filename to indicate the file's format or
type, often used by operating systems and applications to determine how to
handle the file.
HTML HTML (Hypertext Markup Language) is the standard language for creating and
structuring content on web pages, using tags to define the structure and
presentation of documents.
HTML Anchor tags HTML anchor tags in Python are used to create hyperlinks within web pages,
linking to other web pages or resources using the <a> element with the href
attribute.
HTML Tables HTML tables in Python are used to organize and display data in a structured
grid format on a web page, constructed with <table>, <tr>, <th>, and <td>
elements.
HTML Tag An HTML tag in Python is a specific code enclosed in angle brackets used to
define elements within an HTML document, specifying how content should be
presented or structured.
HTML Trees HTML trees in Python refer to the hierarchical structure created when parsing
an HTML document, representing its elements and their relationships, typically
used for manipulation or extraction of data.
Identify In Python, "identify" usually means determining if two variables or objects refer
to the same memory location, which can be checked using the is operator.
Mean value The mean value in Python is the average of a set of numerical values,
calculated by adding all values and dividing by the total number of values.
Navigable string In Python, a Navigable String is a Beautiful Soup object representing a string
within an HTML or XML document, allowing for navigation and manipulation
of the text content.
Plotly Plotly is a Python library for creating interactive and visually appealing web-
based data visualizations and dashboards.
PNG file A PNG (Portable Network Graphics) file is a lossless image format in Python
that is commonly used for high-quality graphics with support for transparency
and compression.
Python iterable A Python iterable is an object that can be looped over, typically used in for
loops, and includes data structures like lists, tuples, and dictionaries.
Query string A query string in Python is a part of a URL that contains data or parameters to
be sent to a web server, typically used in HTTP GET requests to retrieve specific
information.
rb mode In Python, "rb" mode is used when opening a file to read it in binary mode,
allowing you to read and manipulate non-text files like images or binary data.
Service instance In Python, a "service instance" typically refers to an instantiated object or entity
representing a service, enabling interaction with that service in a program or
application.
Transcribe "Transcribe" typically means converting spoken language or audio into written
text, often using automatic speech recognition (ASR) technology.
Unix timestamp A UNIX timestamp is a numerical value representing the number of seconds
that have elapsed since January 1, 1970, 00:00:00 UTC, used for time-keeping
in Unix-based systems and programming.
url (Uniform Resource In Python, a URL (Uniform Resource Locator) is a web address that specifies the
Locator) location of a resource on the internet, typically consisting of a protocol,
domain, and path.
urllib The "urllib" library in Python is used for working with URLs and making HTTP
requests, including functions for fetching web content, handling cookies, and
more.
Web service Web services in Python are software components that allow applications to
communicate over the internet by sending and receiving data in a
standardized format, typically using protocols like HTTP or XML.
xml XML (Extensible Markup Language) is a text-based format for storing and
structuring data using tags, often used for data interchange and configuration
files.
INSTRUCTIONS
Exam Instructions
1. This exam is worth 50% of your entire grade for the course.
2. There is no pass/fail for the exam itself, but the grade you get will affect your overall
passing grade for the course.
3. Time allowed: 1 hour
4. Attempts per question:
o One attempt - For True/False questions
o Two attempts - For any question other than True/False
5. Clicking the "Final Check" button when it appears, means your submission is FINAL. You will
NOT be able to resubmit your answer for that question ever again.
6. Check your grades in the course at any time by clicking on the "Progress" tab.
IMPORTANT: Do not let the time run out and expect the system to grade you
automatically. You must explicitly submit your answers, otherwise they would be
marked as incomplete.
FINAL EXAM
Note: Successful completion of this course is a prerequisite for these Python Project
courses.
If you are looking to start a career in Data Science, Data Engineering or AI & Application
Development, note that this course is part of the following Professional Certificates which are
designed to empower you with the skills to become job-ready in these fields.
• IBM Data Analyst Professional Certificate
• IBM Data Science Professional Certificate
• IBM Data Engineering Professional Certificate
• IBM Full Stack Developer Professional Certificate
• DevOps and Software Engineering Professional Certificate
• Applied Data Science with R Professional Certificate
We encourage you to leave your feedback and rate this course.
Good luck!