Skip main navigation

New offer! Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. New subscribers only T&Cs apply

Find out more

Interactive Python notebook walkthrough

Learners will write code using interactive notebooks during the course. In this video, Jeremy Singer introduces how to work with an online notebook.
JEREMY: Hello. In this tutorial video, I want to show you how to interact with your first Python notebook. Look. Here’s our example down here, my first Python notebook. So an interactive notebook consists of different panes, and you can see here a sequence of white and black panes containing different kinds of text. The highlighted pane, which is in focus at the moment, has a blue margin to the left of it. See? There’s the next one highlighted when I click on it and so on as I scroll down. The white panes just contain explanatory text. So here we go.
We see that this is an example notebook for our data science course, and the scenario in this notebook is an ice cream shop. [LAUGHS] So we are going to pretend we have an ice cream shop, and we want to do some data science on the ice creams. Let’s scroll down and click on this black pane here now. This is Python source code. So this is the first part of the data science program that we want to run to analyse our data. In the black pane, the lines are all numbered. You see there are 19 lines of text here, 19 lines of Python source code. Some of them are blank.
Some of them start with this hash sign here, or pound sign. Those lines are comments, which again aren’t really part of the program. They’re more just like explanatory text for the reader. So the first interesting line– I’m going to click on it here so it’s highlighted– is this print instruction here. Print, brackets, quotes, “Welcome to the ice cream shop.” So when we run the program– I’ll do it in a minute– the output from this line here, line five, ought to be the text– “Welcome to the ice cream shop.” OK. So I’m going to scroll further down, and I see, after the next comment on lines seven and eight, these three lines here, nine to 11, which define variables.
These represent the number of strawberry ice creams that are sold, 10 of them, number of chocolate, 15, number of vanilla ice creams, 25. So these names of variables are going to be associated with these integer values. So the next question is– here’s the comment– how many ice creams did we sell altogether? Let’s compute the total here on line 15. So the total number of ice cream sold is the sum of– that’s what these brackets mean here– this list of values, and the list of values is inside a pair of square brackets– num strawberry, num chocolate, num vanilla. So when I execute this line here, it should sum this list of values for us.
And the integer total will be stored in the total variable. Finally, we’ve got three print statements– sequence of three print statements here– print “Today we sold a total of” total ice creams. The first print statement will print exactly the text in green inside the quotes. The second one won’t print the word “total.” Instead it will print the value which is stored in the total variable. Do you see that? There are no quotes around this total here. So this prints out the value total and then “ice creams”. Let’s run it and see what happens. So I’m going to go up to the toolbar at the top and click on the run button. Ta da! “Welcome to the ice cream shop.
Today we sold a total of 50 ice creams.” I could play around the things a little bit. Let’s suppose I sold 20 strawberry ice creams. And now I run it again. And we see the total of our ice creams has gone up to 60. Suppose I sold 35 vanilla ice creams, and the total has gone up to 70. There we go. OK. And I could change the name of the ice cream shop. “The amazing Glasgow University ice cream shop.” And run the program again. “Welcome to the amazing Glasgow University ice cream shop. “ One thing I might say is, “Welcome to Jeremy’s ice cream shop.” And you just need to watch a little gotcha here.
Because “Jeremy’s” with an apostrophe, the apostrophe is the same character that is used to begin and end the string. So I need to say this isn’t the end of the string. This is just an apostrophe in the middle. To do that, I need to put a back quote– no, sorry, that’s a backslash character– backslash apostrophe. Say this isn’t the end of the string. You see the rest of the string has gone green again. Now I run it, and it says “Welcome to Jeremy’s ice cream shop.” Brilliant. Let’s carry on down. So you’ve got another white pane. You might need to play around with this Python code. Oh, we just did that. OK.
Finally, for our first test notebook, let’s try drawing a pie chart. Pie charts, invented by Scotsman William Playfair in the 1800s. And we want a pie chart to show how many ice creams of each flavour we’ve sold. So let’s go and look at this next black panel here, which has got the second fragment of Python source code in it. We need to import a library here. This unlocks some existing Python functionality for us so we can make use of it, and the particular functionality we want is plt.pie, which is going to draw the pie chart. There are one, two, three values to draw.
These are the integer values we computed earlier for num strawberry, num chocolate, and num vanilla ice creams. We put a label to correspond to each. That one corresponds there, there, and there. And we also specify the colours for each sector of the pie chart as well, colours that will correspond to the flavours of our ice creams. Finally, we say in order to draw the pie chart. And when I click “run now” and scroll down, look! There’s my pie chart with my different flavours of ice cream and their relative proportions. I could go back to the first panel and change– let’s say we sold a great many more strawberry ice creams, and then I need to run that again.
And then I go back to my second panel. It doesn’t automatically update. But I can run this one again, and strawberry should now be huge. There we go. OK. I could change this if I wanted to. I could turn strawberry into a different flavour. There we go. Mint. And I could turn chocolate into a different flavour. Let’s say bubblegum. I can run it again. You see the labels have changed. I might want to change the colours, mightn’t I? Mint is probably going to be green, and bubblegum is probably going to be blue, I suppose. So let’s try that.
And there we have it. Wonderful. OK. So this is just a simple example to show us how the programs work and how the code is executed in this interactive notebook. This is the end of the first Python notebook and the end of this example. Let’s save our code so it’s there when we come back to it later. Brilliant.

Data science is very much a practical, experience-based activity. Throughout this course, we want to give you the opportunity to experiment with data, using the popular Python programming language.

We have developed an online interactive notebook facility, to allow you to write Python code in your web browser and run it on our servers at the University of Glasgow. This video works through how you might use an interactive notebook. The actual notebook shown in the video is available in the next step, so you can have a go for yourself.

Each fragment of Python code works, but you should feel free to change the code and see what happens. Don’t worry if you make mistakes or break it – that’s normal for coding! If you want to restore the notebook back to its initial state, click the ‘reset’ button in the toolbar at the top.

You can move between panes in the notebook with Shift + Cursor up or Shift + Cursor down. Alternatively you can click on a pane to highlight it. You can run the code in a Python pane with Shift + Enter. Alternatively you click the ‘run’ button in the toolbar at the top.

There are other interactive notebooks throughout the rest of the course, so please ensure you understand how to work with them at this stage.

For advanced users: Although we support browser-based execution of simple notebooks on our server, more adventurous learners may want to download and install local software on your own machines – so you can do data science at home. We recommend the Jupyter Notebook system. This is quite complex to install and configure, but it is the industry standard for basic data science. All of the notebooks we will use in this online course are available for download, and will work with Jupyter Notebook.

If you get stuck with any of these activities, please let us know in the comments section and we will do our best to advise you.

This article is from the free online

Getting Started with Teaching Data Science in Schools

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now