The following is partially taken from the offical documentation:
Notebook documents (or “notebooks”, all lower case) are documents produced by the Jupyter Notebook App, which contain both computer code (e.g. python) and rich text elements (paragraph, equations, figures, links, etc...). Notebook documents are both human-readable documents containing the analysis description and the results (figures, tables, etc..) as well as executable documents which can be run to perform data analysis.
In [ ]:
# press Shit+Enter to execute this cell
print('This is a cell containing python code')
In [ ]:
#we can also make figures
import matplotlib.pyplot as plt
import numpy as np
% matplotlib inline
x = np.linspace(-np.pi, np.pi, 100)
plt.plot(x, np.sin(x))
# Use `Tab` for completion and `Shift-Tab` for code info
This is an equation formatted in LaTeX $y = \sin(x)$
double-click this cell to edit it
The Jupyter Notebook App is a server-client application that allows editing and running notebook documents via a web browser.
A notebook kernel is a “computational engine” that executes the code contained in a Notebook document. The ipython kernel executes python code. Kernels for many other languages exist (official kernels).
The Notebook Dashboard is the component which is shown first when you launch Jupyter Notebook App. The Notebook Dashboard is mainly used to open notebook documents, and to manage the running kernels (visualize and shutdown).
In addition to "standard" data types such as integers, floats and strings, Python knows a number of compound data types, used to group together other values. We will briefly see Lists and Dictionaries
https://docs.python.org/3/tutorial/introduction.html#lists
In [ ]:
# define a new list
zoo_animals = ["pangolin", "cassowary", "sloth", "tiger"]
if len(zoo_animals) > 3:
print("The first animal at the zoo is the " + zoo_animals[0])
print("The second animal at the zoo is the " + zoo_animals[1])
print("The third animal at the zoo is the " + zoo_animals[2])
print("The fourth animal at the zoo is the " + zoo_animals[3])
In [ ]:
# empty list
suitcase = []
suitcase.append("sunglasses")
# Your code here!
suitcase.append("doll")
suitcase.append("ball")
suitcase.append("comb")
list_length = len(suitcase) # Set this to the length of suitcase
print("There are %d items in the suitcase." % (list_length))
print(suitcase)
# we can also append an other list to a list
# using the "extend" method
numbers = [42,7,12]
suitcase.extend(numbers)
print(suitcase)
In [ ]:
suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]
first = suitcase[0:2] # The first and second items (index zero and one)
middle = suitcase[2:4] # Third and fourth items (index two and three)
last = suitcase[4:6] # The last two items (index four and five)
print(last)
In [ ]:
my_list = [1,9,3,8,5,7]
for number in my_list:
print(2*number)
# Your code here
https://docs.python.org/3/tutorial/datastructures.html#dictionaries
Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be strings or numbers.
It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary).
In [ ]:
# Assigning a dictionary with three key-value pairs to residents:
residents = {'Puffin' : 104, 'Sloth' : 105, 'Burmese Python' : 106}
print(residents['Puffin']) # Prints Puffin's room number
print(residents['Sloth'])
print(residents['Burmese Python'])
In [ ]:
menu = {} # Empty dictionary
menu['Raclette'] = 14.50 # Adding new key-value pair
print(menu['Raclette'])
menu['Cheese Fondue'] = 10.50
menu['Muesli'] = 13.50
menu['Quiche'] = 19.50
menu['Cervela'] = 17.50 # Your code here: Add some dish-price pairs to menu!
print("There are " + str(len(menu)) + " items on the menu.")
print(menu)
In [ ]:
del menu['Muesli']
print(menu)
In [ ]:
for key, value in menu.items():
print(key, value)
In [ ]:
raclette = menu.get('Raclette')
print(raclette)
#accessing a non-existing key
burger = menu.get('Burger')
print(burger)
In [ ]:
#listing keys
menu.keys()
In [ ]:
#listing values
menu.values()
NumPy is the fundamental package for scientific computing with Python (http://www.numpy.org/). It is part of SciPy (https://www.scipy.org/).
The numpy package provides functionalities that makes Python similar to MATLAB.
In [ ]:
# first import the package
import numpy as np
# numpy works with array similarly to MATLAB
x = np.array([1,2,4,5,6,8,10,4,3,5,6])
# but indexing start at 0!!
print(x[0])
# arrays have useful methods
print(x.size)
print(x.mean())
# we can also use numpy functions
amax = np.max(x)
print(amax)
And you can do much more with NumPy! see https://docs.scipy.org/doc/numpy-dev/user/quickstart.html
Matplotlib is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms (https://matplotlib.org/).
In [ ]:
# import matplotlib
import matplotlib.pyplot as plt
# make matplotlib create figures inline
%matplotlib inline
x = np.linspace(0,2,100)
y1 = np.exp(x)
y2 = np.sqrt(x)
plt.plot(x,y1, 'r-', label='y = exp(x)')
plt.plot(x,y2, 'b--', label='y = sqrt(x)')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
In [ ]:
# press Shift-Tab to see information about a function
norm = np.random.randn(200)
n, bins, patches = plt.hist(norm, normed=True, bins=20)