The Jupyter Notebook is a web application that allows you to create and share documents that contain live code, equations, visualizations and explanatory text. The following links may be useful in gaining a basic understanding of Jupyter and how to work in Jupyter notebook:
If this is your first time using Jupyter, start with taking a tour of the user interface:
Here are some other helpful tips and shortcuts:
Cells are designated as Code, Markdown, or Raw NBConvert (Notebook Conversion). To switch from one type to another, click on Cell -> Cell Type.
Throughout our tutorials, we will mainly be working in code cells to learn how to use Python. However, each of the tutorials include Markdown cells to provide introductions and learning objectives for each tutorial, background information, and instructions throughout. We encourage you to open these markdown cells to become familiar with the syntax, and add markdown notes as you see fit. Learning to use Markdown and Notebook Conversion cells will become a useful skill for Python scripts and notebooks you will use in your future research. Throughout this data institute, you will learn to use the full set of Jupyter Notebook tools for conducting reproducible (and publishable) research.
Code cells are where we will write and execute our Python scripts and functions.
Markdown cells are mainly used to neatly document the code that is written in Jupyter notebooks. While this course does not focus on Markdown, we encourage you to explore the Markdown capabilities, and add Markdown cells to your notebooks as you see fit. Some useful resources for learning Markdown Syntax include:
The NBConvert tool allows you to convert a Jupyter .ipynb noteook document file into another format. The nbconvert documentation contains a complete description of this tool's capabilities: https://media.readthedocs.org/pdf/nbconvert/latest/nbconvert.pdf. The description below, copied from the document, gives a brief overview of the tool:
Using nbconvert enables:
Overall, notebook conversion and the nbconvert tool give scientists and researchers the flexibility to deliver information in a timely way across different formats.
Primarily, the nbconvert tool allows you to convert a Jupyter .ipynb notebook document file into another static format including HTML, LaTeX, PDF, Markdown, reStructuredText, and more. nbconvert can also add productivity to your workflow when used to execute notebooks programmatically.
If used as a Python library (import nbconvert), nbconvert adds notebook conversion within a project. For example, nbconvert is used to implement the “Download as” feature within the Jupyter Notebook web application. When used as a command line tool (invoked as jupyter nbconvert ...), users can conveniently convert just one or a batch of notebook files to another format.
In this tutorial, we will learn some useful Python commands and tips, including how to:
This link has some useful tips that may come in handy in your research: https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/
In [2]:
import this
In [3]:
#Basic commands: print, !pwd, !ls
#Python does not automatically display results unless you use a print statement
#You can also use a print statement as a debugging tool:
#Print working directory - the ! executes bash commands from the Python platform
print('Current Working Directory:')
!pwd
print('\nFiles in Working Directory:') #\n enters to a new line
!ls ./
In [4]:
#Integers
x = 1;
print('x type:',type(x))
#Floating Point Numbers
x_float = float(x) #use the command float to change a number to type float
print('x_float type:',type(x_float))
#Strings
example_string = 'This is a string'
print('example_string type:',type(example_string))
#To change a variable to type string use the command str(argument), for example:
x_string = str(x)
print('x_string type:',type(x_string))
In [5]:
#Import Numpy and make an example array
import numpy as np
#to run a numpy command, use the syntax np.command_name
example_array = np.array([[1,2,3],[4,5,6]])
print('example_array:\n',example_array)
print('example_array type:',type(example_array))
In [6]:
#extract a single value from the array:
#indexing starts at 0, so [0,0] extracts the 1st row and 1st column
#negative indices will count backwards from the end
print(example_array[0,0])
print(example_array[-1,-1])
Since the Jupyter notebook doesn't have a separate window to display the variables that have been generated, we can use the commands %who, %who_ls, or %whos to show a complete list of imported modules, functions, variables, and other outputs (eg. figures). The command %who lists the variable names in row format, %who_ls lists the variable names in column format, and %whos is the most comprehensive, displaying variable name, type, and data/info. Remember this tool throughout the following tutorials if you need to check whether a module or function was already imported, a variable created, etc.
In [7]:
#Display variables & imported modules
%whos