NOTE: This is the first notebook from a 3-days python workshop that I curated.
Getting Started with Jupyter Notebooks! 👋¶
⏱️ 15 min
This course teaches you all of the tools you need automate repetitive tasks -- starting from the basics: Jupyter, Python and Pandas.
The course focuses on building Python skills that will be helpful to you in practice, leaving behind a lot of the theoretical explanations you might find in other courses.
The goal of this course is to demistify the Python basics so you have the confidence to start applying Python automations in your day-to-day work.
Things to know before we start:
- 💡 the lightbulb emoji means thers an important information.
- 🧑💻 the Person Behind Computer emoji means thers an task waiting for you.
- This is not an exam, it’s an opportunity to learn something new. You will get stuck at some point -- don’t hesitate to ask questions to the your tutor, your colleagues, or Stack Overflow.
- Most imporantly: Enjoy the joureny!
Notebook Basics¶
Throughout the course you will encounter a lot of these documents ending in .ipynb
.
It’s the Jupyter notebook file format, and where we will write all of our Python code!
It’s like a python script, but interactive.
Let’s run the first program, which prints “hello world”
💡 Press Shift+Enter to run the code cell.
🧑💻 First task: Run the below code cell
print("hello world")
Hurray, we just ran our first bit of Python code in a Juptyer notebook.
Notice that there is the notebook code cell, followed by the notebook cell output.
And what you’re reading right now is a third element called a markdown cell.
Markdown cells¶
Markdown cells are useful for documenting your work!
💡 You can convert a notebook code cell to markdown cell by pressing “m”
💡 You can convert a markdown cell to notebook code cell by pressing “y”
🧑💻 Now, convert the below cell to a markdown cell.
Hello World
💡 Adding a # in front of a line in markdown will convert it into a header.
🧑💻 Now, add the title # Hi! to the above markdown cell.
💡 Each time you run a cell, the next cell becomes active. You can run several cells in a row by continuing to press Shift + Enter.
🧑💻 Run the following 2 cells.
i = 5
i = i+1
print(i)
🧑💻 Execute the previous cell 5 times
You should see that i increases everytime! That’s because Python remembers what you’ve previously executed.
That’s important to keep in mind, ecspecially when your Python scripts become more complex.
It’s considered be best practice when the final notebook can run from top to bottom.
But when iterating and tweaking your analysis workflow, you will often find yourself executing cells in a non-cronolgical order.
While executing cells, you will notice a little execution number left to of each cell. That number will increment each time you run a cell.
💡 Having a very long execution history might lead to unused variables or even bugs, so it’s recommended to restart the kernel from time to time.
🧑💻 Find the “Restart the kernel” button in the above toolbar. Restart the kernel, execute the above cell again and see how the value is reset.
Now you know about Cell execution, Inputs, Outputs, Restating Kernel and Double Cell Execution.
What comes next are widgets.
Widgets¶
💡 These widgets are interactive elements
🧑💻 Set new min and max values to the below slider and slide it left and right with your mouse
from ipywidgets import FloatSlider
FloatSlider(min=0, max=10)
💡 If you don’t need the cell output anymore is also a button to clear all cell output. Pressing that won’t restart the kernel.
Notebook shortcuts will boost your productivity!
💡 Use the “Up” and “Down” Keys to select the prev/next cell.
💡 Use the “a” key to insert a cell above.
💡 Use the “b” key to insert a cell below.
💡 Double tap the “d” to delete a cell.
🧑💻 Try, some of these shortcuts below.
Well done! You just learned the basics of Jupyter notebooks. Head over to the next notebook where we’ll start to learn some Python fundamentals.