Lab 1: Expressions
Welcome to Data Science 8: Foundations of Data Science! Each week you will complete a lab assignment like
this one. You can't learn technical subjects without hands-on practice, so labs are an important part of the
course.
Before we get started, there are some administrative details.
Labs are required. There are two ways to receive credit for each lab.
1. (The normal way) Come to your lab section and work on the lab as directed. Before you leave, you need to
submit the lab and have a staff member check you off to confirm that you came to lab section and
attempted to make progress while you were there. You do not need to finish the lab or answer every
question correctly to receive full credit. However, you do need to try; that's why we check you off. Expect to
discuss your progress briefly with a staff member before they will check you off. The lab must be submitted
by Friday night, even if it is incomplete, to get credit for attending.
1. (The hard way) Lab assignments will be posted Sunday night. If you complete the entire lab so that all tests
pass and submit it by 9:59 AM on Wednesday, then you don't need to physically attend lab for the rest of
the week.
Collaborating on labs is more than okay -- it's encouraged! You should rarely be stuck for more than a few
minutes on questions in labs, so ask a neighbor or an instructor for help. (Explaining things is beneficial, too --
the best way to solidify your knowledge of a subject is to explain it.) Please don't just share answers, though.
You can read more about course policies (http://data8.org/sp18/policies.html) on the course website
(http://data8.org/sp18).
Today's lab
In today's lab, you'll learn how to:
1. navigate Jupyter notebooks (like this one);
2. write and evaluate some basic expressions in Python, the computer language of the course;
3. call functions to use code other people have written; and
4. break down Python code into smaller parts to understand it.
This lab covers parts of Chapter 3 (http://www.inferentialthinking.com/chapters/03/programming-in-
python.html) of the online textbook. You should read the book, but not right now. Instead, let's get started!
,1. Jupyter notebooks
This webpage is called a Jupyter notebook. A notebook is a place to write programs and view their results.
1.1. Text cells
In a notebook, each rectangle containing text or code is called a cell.
Text cells (like this one) can be edited by double-clicking on them. They're written in a simple format called
Markdown (http://daringfireball.net/projects/markdown/syntax) to add formatting and section headings. You
don't need to learn Markdown, but you might want to.
After you edit a text cell, click the "run cell" button at the top that looks like ►| to confirm any changes. (Try not
to delete the instructions of the lab.)
Question 1.1.1. This paragraph is in its own text cell. Try editing it so that this sentence is the last sentence in
the paragraph, and then click the "run cell" ►| button . This sentence, for example, should be deleted. So
should this one.
1.2. Code cells
Other cells contain code in the Python 3 language. Running a code cell will execute all of the code it contains.
To run the code in a code cell, first click on that cell to activate it. It'll be highlighted with a little green or blue
rectangle. Next, either press ►| or hold down the shift key and press return or enter .
Try running this cell:
In [1]: print("Hello, World!")
Hello, World!
And this one:
In [2]: print("\N{WAVING HAND SIGN}, \N{EARTH GLOBE ASIA-AUSTRALIA}!")
, !
The fundamental building block of Python code is an expression. Cells can contain multiple lines with multiple
expressions. When you run a cell, the lines of code are executed in the order in which they appear. Every
print expression prints a line. Run the next cell and notice the order of the output.
, In [3]: print("First this line is printed,")
print("and then this one.")
First this line is printed,
and then this one.
Question 1.2.1. Change the cell above so that it prints out:
First this line,
then the whole ,
and then this one.
Hint: If you're stuck on the Earth symbol for more than a few minutes, try talking to a neighbor or a TA. That's a
good idea for any lab problem.
1.3. Writing Jupyter notebooks
You can use Jupyter notebooks for your own projects or documents. When you make your own notebook, you'll
need to create your own cells for text and code.
To add a cell, click the + button in the menu bar. It'll start out as a text cell. You can change it to a code cell by
clicking inside it so it's highlighted, clicking the drop-down box next to the restart (⟳) button in the menu bar,
and choosing "Code".
Question 1.3.1. Add a code cell below this one. Write code in it that prints out:
A whole new cell! ♪ ♪
(That musical note symbol is like the Earth symbol. Its long-form name is \N{EIGHTH NOTE} .)
Run your cell to verify that it works.
Welcome to Data Science 8: Foundations of Data Science! Each week you will complete a lab assignment like
this one. You can't learn technical subjects without hands-on practice, so labs are an important part of the
course.
Before we get started, there are some administrative details.
Labs are required. There are two ways to receive credit for each lab.
1. (The normal way) Come to your lab section and work on the lab as directed. Before you leave, you need to
submit the lab and have a staff member check you off to confirm that you came to lab section and
attempted to make progress while you were there. You do not need to finish the lab or answer every
question correctly to receive full credit. However, you do need to try; that's why we check you off. Expect to
discuss your progress briefly with a staff member before they will check you off. The lab must be submitted
by Friday night, even if it is incomplete, to get credit for attending.
1. (The hard way) Lab assignments will be posted Sunday night. If you complete the entire lab so that all tests
pass and submit it by 9:59 AM on Wednesday, then you don't need to physically attend lab for the rest of
the week.
Collaborating on labs is more than okay -- it's encouraged! You should rarely be stuck for more than a few
minutes on questions in labs, so ask a neighbor or an instructor for help. (Explaining things is beneficial, too --
the best way to solidify your knowledge of a subject is to explain it.) Please don't just share answers, though.
You can read more about course policies (http://data8.org/sp18/policies.html) on the course website
(http://data8.org/sp18).
Today's lab
In today's lab, you'll learn how to:
1. navigate Jupyter notebooks (like this one);
2. write and evaluate some basic expressions in Python, the computer language of the course;
3. call functions to use code other people have written; and
4. break down Python code into smaller parts to understand it.
This lab covers parts of Chapter 3 (http://www.inferentialthinking.com/chapters/03/programming-in-
python.html) of the online textbook. You should read the book, but not right now. Instead, let's get started!
,1. Jupyter notebooks
This webpage is called a Jupyter notebook. A notebook is a place to write programs and view their results.
1.1. Text cells
In a notebook, each rectangle containing text or code is called a cell.
Text cells (like this one) can be edited by double-clicking on them. They're written in a simple format called
Markdown (http://daringfireball.net/projects/markdown/syntax) to add formatting and section headings. You
don't need to learn Markdown, but you might want to.
After you edit a text cell, click the "run cell" button at the top that looks like ►| to confirm any changes. (Try not
to delete the instructions of the lab.)
Question 1.1.1. This paragraph is in its own text cell. Try editing it so that this sentence is the last sentence in
the paragraph, and then click the "run cell" ►| button . This sentence, for example, should be deleted. So
should this one.
1.2. Code cells
Other cells contain code in the Python 3 language. Running a code cell will execute all of the code it contains.
To run the code in a code cell, first click on that cell to activate it. It'll be highlighted with a little green or blue
rectangle. Next, either press ►| or hold down the shift key and press return or enter .
Try running this cell:
In [1]: print("Hello, World!")
Hello, World!
And this one:
In [2]: print("\N{WAVING HAND SIGN}, \N{EARTH GLOBE ASIA-AUSTRALIA}!")
, !
The fundamental building block of Python code is an expression. Cells can contain multiple lines with multiple
expressions. When you run a cell, the lines of code are executed in the order in which they appear. Every
print expression prints a line. Run the next cell and notice the order of the output.
, In [3]: print("First this line is printed,")
print("and then this one.")
First this line is printed,
and then this one.
Question 1.2.1. Change the cell above so that it prints out:
First this line,
then the whole ,
and then this one.
Hint: If you're stuck on the Earth symbol for more than a few minutes, try talking to a neighbor or a TA. That's a
good idea for any lab problem.
1.3. Writing Jupyter notebooks
You can use Jupyter notebooks for your own projects or documents. When you make your own notebook, you'll
need to create your own cells for text and code.
To add a cell, click the + button in the menu bar. It'll start out as a text cell. You can change it to a code cell by
clicking inside it so it's highlighted, clicking the drop-down box next to the restart (⟳) button in the menu bar,
and choosing "Code".
Question 1.3.1. Add a code cell below this one. Write code in it that prints out:
A whole new cell! ♪ ♪
(That musical note symbol is like the Earth symbol. Its long-form name is \N{EIGHTH NOTE} .)
Run your cell to verify that it works.