Skip to content

Instantly share code, notes, and snippets.

@thomd
Last active December 17, 2021 23:55
Show Gist options
  • Save thomd/3dd7b667e5c91672b9285e32a35bc157 to your computer and use it in GitHub Desktop.
Save thomd/3dd7b667e5c91672b9285e32a35bc157 to your computer and use it in GitHub Desktop.
Jupyter Notebooks Introduction #jupyter #python #markdown
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Working with code cells\n",
"\n",
"In this notebook you'll get some experience working with code cells.\n",
"\n",
"First, run the cell below. As I mentioned before, you can run the cell by selecting it the click the \"run cell\" button above. However, it's easier to run it by pressing **Shift + Enter** so you don't have to take your hands away from the keyboard."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Select the cell, then press Shift + Enter\n",
"3**2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Shift + Enter runs the cell then selects the next cell or creates a new one if necessary. You can run a cell without changing the selected cell by pressing **Control + Enter**.\n",
"\n",
"The output shows up below the cell. It's printing out the result just like in a normal Python shell. Only the very last result in a cell will be printed though. Otherwise, you'll need to use `print()` print out any variables. \n",
"\n",
"> **Exercise:** Run the next two cells to test this out. Think about what you expect to happen, then try it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"3**2\n",
"4**2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(3**2)\n",
"4**2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now try assigning a value to a variable."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"mindset = 'growth'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There is no output, `'growth'` has been assigned to the variable `mindset`. All variables, functions, and classes created in a cell are available in every other cell in the notebook.\n",
"\n",
"What do you think the output will be when you run the next cell? Feel free to play around with this a bit to get used to how it works."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mindset[:4]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code completion\n",
"\n",
"When you're writing code, you'll often be using a variable or function often and can save time by using code completion. That is, you only need to type part of the name, then press **tab**.\n",
"\n",
"> **Exercise:** Place the cursor at the end of `mind` in the next cell and press **tab**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"mind"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here, completing `mind` writes out the full variable name `mindset`. If there are multiple names that start the same, you'll get a menu, see below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Run this cell\n",
"mindful = True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Complete the name here again, choose one from the menu\n",
"mind"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Remember that variables assigned in one cell are available in all cells. This includes cells that you've previously run and cells that are above where the variable was assigned. Try doing the code completion on the cell third up from here.\n",
"\n",
"Code completion also comes in handy if you're using a module but don't quite remember which function you're looking for or what the available functions are. I'll show you how this works with the [random](https://docs.python.org/3/library/random.html) module. This module provides functions for generating random numbers, often useful for making fake data or picking random items from lists."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Run this\n",
"import random"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> **Exercise:** In the cell below, place the cursor after `random.` then press **tab** to bring up the code completion menu for the module. Choose `random.randint` from the list, you can move through the menu with the up and down arrow keys."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"random."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Above you should have seen all the functions available from the random module. Maybe you're looking to draw random numbers from a [Gaussian distribution](https://en.wikipedia.org/wiki/Normal_distribution), also known as the normal distribution or the \"bell curve\". \n",
"\n",
"## Tooltips\n",
"\n",
"You see there is the function `random.gauss` but how do you use it? You could check out the [documentation](https://docs.python.org/3/library/random.html), or just look up the documentation in the notebook itself.\n",
"\n",
"> **Exercise:** In the cell below, place the cursor after `random.gauss` the press **shift + tab** to bring up the tooltip."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"random.gauss"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You should have seen some simple documentation like this:\n",
"\n",
" Signature: random.gauss(mu, sigma)\n",
" Docstring:\n",
" Gaussian distribution.\n",
" \n",
"The function takes two arguments, `mu` and `sigma`. These are the standard symbols for the mean and the standard deviation, respectively, of the Gaussian distribution. Maybe you're not familiar with this though, and you need to know what the parameters actually mean. This will happen often, you'll find some function, but you need more information. You can show more information by pressing **shift + tab** twice.\n",
"\n",
"> **Exercise:** In the cell below, show the full help documentation by pressing **shift + tab** twice."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"random.gauss"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You should see more help text like this:\n",
"\n",
" mu is the mean, and sigma is the standard deviation. This is\n",
" slightly faster than the normalvariate() function."
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment