Jupyter notebook atms391geodata/Week 1/Week 1 Exercise 1 scripts_examples_notebook_tour.ipynb
A brief tour of the jupyter notebook
This document will give you a brief tour of the capabilities of the IPython notebook.
You can view its contents by scrolling around, or execute each cell by typing Shift-Enter
. After you conclude this brief high-level tour, you should read the accompanying notebook titled 01_notebook_introduction
, which takes a more step-by-step approach to the features of the system.
The rest of the notebooks in this directory illustrate various other aspects and capabilities of the IPython notebook; some of them may require additional libraries to be executed.
NOTE: This notebook must be run from its own directory, so you must cd
to this directory and then start the notebook, but do not use the --notebook-dir
option to run it from another location.
You can run shell aliases and magic commands:
DUE_DATE.txt
DUE_DATE.txt~
Homework 1: Basic python notebook.ipynb
Homework 1: Basic python notebook.ipynb~
nyan-cat.png
python-logo-generic.svg
python-logo-large.png
Week 1 Exercise 1 scripts_examples_notebook_tour.html
Week 1 Exercise 1 scripts_examples_notebook_tour.ipynb
Week 1 Exercise 1 scripts_examples_notebook_tour.ipynb~
Week 1 Exercise 2 notebook_introduction.ipynb
Week 1 Exercise 3 variables_strings_numbers.ipynb
Week 1 Exercise 4 introducing_functions.ipynb
The IPython notebook is great!
Plots with matplotlib: do not execute the next below if you do not have matplotlib installed or didn't call the %matplolib
magic, as the code will not work.
You can paste blocks of input with prompt markers, such as those from the official Python tutorial
##Python variable types
####Strings Strings in Python are identified as a contiguous set of characters in between quotation marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken using the slice operator ( and [ : ] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.
The plus ( + ) sign is the string concatenation operator and the asterisk ( * ) is the repetition operator. For example:
####Numbers Number data types store numeric values. They are immutable data types which means that changing the value of a number data type results in a newly allocated object.
####Lists
Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type.
The values stored in a list can be accessed using the slice operator ( and [ : ] ) with indexes starting at 0 in the beginning of the list and working their way to end -1. The plus ( + ) sign is the list concatenation operator, and the asterisk ( * ) is the repetition operator. For example:
####Tuples A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.
The main differences between lists and tuples are: Lists are enclosed in brackets ( ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists. For example:
Following is invalid with tuple, because we attempted to update a tuple, which is not allowed. Similar case is possible with lists:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-9dccc7681e60> in <module>()
1 tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
2 list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
----> 3 tuple[2] = 1000 # Invalid syntax with tuple
4 list[2] = 1000 # Valid syntax with list
TypeError: 'tuple' object does not support item assignment
####Dictionaries Python's dictionaries are kind of hash table type. They work like structures in MATLAB or IDL, and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.
Dictionaries have no concept of order among elements. It is incorrect to say that the elements are "out of order"; they are simply unordered.
Dictionaries are enclosed by curly braces ( { } ) and values can be assigned and accessed using square braces ( [] ). For example:
####Converting between types
Function | Description |
---|---|
int(x [,base]) | Converts x to an integer. base specifies the base if x is a string. |
long(x [,base]) | Converts x to a long integer. base specifies the base if x is a string. |
float(x) | Converts x to a floating-point number. |
complex(real [,imag]) | Creates a complex number. |
str(x) | Converts object x to a string representation. |
repr(x) | Converts object x to an expression string. |
eval(str) | Evaluates a string and returns an object. |
tuple(s) | Converts s to a tuple. |
list(s) | Converts s to a list. |
set(s) | Converts s to a set. |
dict(d) | Creates a dictionary. d must be a sequence of (key,value) tuples. |
frozenset(s) | Converts s to a frozen set. |
chr(x) | Converts an integer to a character. |
unichr(x) | Converts an integer to a Unicode character. |
ord(x) | Converts a single character to its integer value. |
hex(x) | Converts an integer to a hexadecimal string. |
oct(x) | Converts an integer to an octal string. |
###Errors Errors are shown in informative ways:
File "<ipython-input-10-ed740ad8bd14>", line 1
run non_existent_file
^
SyntaxError: invalid syntax
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-11-dc39888fd1d2> in <module>()
1 x = 1
2 y = 4
----> 3 z = y/(1-x)
ZeroDivisionError: integer division or modulo by zero
When IPython needs to display additional information (such as providing details on an object via x?
it will automatically invoke a pager at the bottom of the screen:
Non-blocking output of kernel
If you execute the next cell, you will see the output arriving as it is generated, not all at the end.
Clean crash and restart
We call the low-level system libc.time routine with the wrong argument via ctypes to segfault the Python interpreter:
Markdown cells can contain formatted text and code
You can italicize, boldface
build
lists
and embed code meant for illustration instead of execution in Python:
or other languages:
Courtesy of MathJax, you can include mathematical expressions both inline: and displayed:
Rich displays: include anyting a browser can show
Note that we have an actual protocol for this, see the display_protocol
notebook for further details.
Images
An image can also be displayed from raw data or a url
SVG images are also supported out of the box (since modern browsers do a good job of rendering them):
Video
And more exotic objects can also be displayed, as long as their representation supports the IPython display protocol.
For example, videos hosted externally on YouTube are easy to load (and writing a similar wrapper for other hosted content is trivial):
Mathematics
And we also support the display of mathematical expressions typeset in LaTeX, which is rendered in the browser thanks to the MathJax library.
Note that this is different from the above examples. Above we were typing mathematical expressions in Markdown cells (along with normal text) and letting the browser render them; now we are displaying the output of a Python computation as a LaTeX expression wrapped by the Math()
object so the browser renders it:
Loading external codes
Drag and drop a
.py
in the dashboardUse
%loadpy
with any local or remote url: the Matplotlib Gallery!
In this notebook we've kept the output saved so you can see the result, but you should run the next cell yourself (with an active internet connection).
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-e181747b0e6d> in <module>()
19 ix = arange(a, b, 0.01)
20 iy = func(ix)
---> 21 verts = [(a,0)] + list(zip(ix,iy)) + [(b,0)]
22 poly = Polygon(verts, facecolor='0.8', edgecolor='k')
23 ax.add_patch(poly)
TypeError: 'list' object is not callable
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-766c9cf4d8d7> in <module>()
18 ix = arange(a, b, 0.01)
19 iy = func(ix)
---> 20 verts = [(a,0)] + list(zip(ix,iy)) + [(b,0)]
21 poly = Polygon(verts, facecolor='0.8', edgecolor='k')
22 ax.add_patch(poly)
TypeError: 'list' object is not callable