Jupyter notebooks (formerly IPython Notebooks) are frameworks to write reports, prototype code, and make presentations. Jupyter can run different kernels (i.e. programming languages) like C++, Julia, R, etc. For the rest of this tutorial, we focus on using Python 3.6.*
#from IPython.display import Video
from IPython.display import HTML
HTML("""
<video width="640" height="480" controls>
<source src="howtocode.mp4" type="video/mp4">
</video>
""")
1) For coding environments, provide detailed comments for interpretability and reproducibility. This will also help the future you when you come back to past assignments/code.
2) Read the error message when your code does not work. They contain important information on how to debug your code.
3) If you do something more than twice, automate that action.
4) Write code that works first, then optimize!
5) Notebooks can be split if they get too long. You want to be able to come back to a notebook without too much trouble and searching.
Python is a dynamic, interpreted language. Variables do not need to be declared and the code does not need to be compiled. In short, the Python interpreter will attempt to make sense of your code and will raise an error if something goes wrong.
In Python, comments in code start with a #. In Jupyter notebooks, you also have the option of making it cell a Markdown cell.
Practice! Below, try making
I can start typing
# this is my first comment. I'm going to add some numbers
3 + 4
7
Markdown cells also support $\LaTeX$ input with either $ or \begin{equation}
Practice! Create a Markdown cell with $\LaTeX$!
For example, $f(x) = x^2$.
type(2)
int
type(42.0)
float
type('hello world')
str
These values belong to different types: 2 is an integer, 42.0 is a floating-point number, and 'Hello, World!' is a string, so-called because the letters it contains are strung together.
Some random/built-in functions are
help(abs)
Help on built-in function abs in module builtins: abs(x, /) Return the absolute value of the argument.
Practice! What happens if you try $\texttt{ print(abs(-100)) + 100}$?
type(print(abs(-100)))
100
NoneType
type(100 + 2.0)
float
# import ... imports packages and follows the structure
# import [package name]
import sys
sys.version
'3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)]'
# Jupyter notebooks also support tab completion
sys.v
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-10-bdeb1f1b5d8e> in <module> 1 # Jupyter notebooks also support tab completion ----> 2 sys.v AttributeError: module 'sys' has no attribute 'v'
Variables, just as in mathematics, are placeholders for values assigned to them. Functions, on the other hand, take input (like variables) and output the result of applying certain rules to the input.
# Define two variables and add them
# Using variables as input to a function
help(sum)
Help on built-in function sum in module builtins: sum(iterable, /, start=0) Return the sum of a 'start' value (default: 0) plus an iterable of numbers When the iterable is empty, return the start value. This function is intended specifically for use with numeric values and may reject non-numeric types.
# Multiply two variables, and save as another variable
a = 2
b = 3
a * b
6
c
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-14-2b66fd261ee5> in <module> ----> 1 c NameError: name 'c' is not defined
# Raise one variable to the power of another variable
# pow(2,3)
2 ** 3
8
# Compare two variables
a > b
a < b
a == b
False
# division
print(4 / 3) # / performs division
# remainders # % outputs remainder when dividing by second number
print(4 % 3)
1.3333333333333333 1
Practice! Create three variables and practice adding, multiplying, dividing, etc.
type(2)
int
type(.1)
float
.1 + .3000000000000001
0.40000000000000013
We now shift our focus to other data structures, lists and strings. Strings, lists, and tuples are all sequence types are ordered collection of objects. Lists are enclosed in square brackets ([ and ]) and tuples in parentheses (( and )).
Lists are mutable (can be changed) and tuples are immutable (cannot be changed) .
** NOTE: Python is 0-indexed, so keep this in mind when writing loops, accessing elements, etc.
a = 'mystring'
print(a)
mystring
list_num = [1,2,3,4,5] # this is a list
tup_num = (1,2,3,4,5) # this is a tuple
list_num * 4
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
# let's make a list of fruits
fruits = ['apples', 'figs', 'oranges']
fruits[1]
'figs'
fruits.append('mango')
print(fruits)
['apples', 'figs', 'oranges', 'mango']
fruits.insert(1,'cherry')
fruits
['apples', 'cherry', 'figs', 'oranges', 'mango']
# Functions have the following structure
# def myfun(input):
# input operations
# return output
def sumfun(a,b):
return a+b
def sumfun2(a,b):
out = a + b
return out
sumfun(3,-1)
sumfun2(3,-1)
2
def myname():
print('Hello Mario!')
myname()
Hello Mario!
Try the following exercises on your own:
1) Write a function that takes two inputs (a and b) and outputs a^b + b.
2) Write a function that takes no inputs and prints "Your code has finished!"
def mypow(a,b):
first_term = pow(a,b)
mysum = first_term + b
return mysum
In python, you check conditions with an if statement
x = 5
if x > 10: # the first condition checks if x is greater than 10
print(x)
elif x < 3:
print(x+4)
else:
print('x does not satisfy the conditions.')
x does not satisfy the conditions.
Recall that the Fibonacci numbers are of the form $$F_n = F_{n-1} + F_{n-2},$$ where $F_0 = 0, F_1 = 1$.
The first few numbers in the sequence are $$ 0,1,1,2,3,5,8,13,21,34,55 $$
x = 8
def myfun(x):
if x > 10: # the first condition checks if x is greater than 10
print(x)
elif x < 3:
print(x+4)
else:
x = x + myfun(x)
print(x)
#print('x does not satisfy the conditions.')
myfun(x)
--------------------------------------------------------------------------- RecursionError Traceback (most recent call last) <ipython-input-38-366cd113e535> in <module> ----> 1 myfun(x) <ipython-input-37-a8190f3474c1> in myfun(x) 7 print(x+4) 8 else: ----> 9 x = x + myfun(x) 10 print(x) 11 #print('x does not satisfy the conditions.') ... last 1 frames repeated, from the frame below ... <ipython-input-37-a8190f3474c1> in myfun(x) 7 print(x+4) 8 else: ----> 9 x = x + myfun(x) 10 print(x) 11 #print('x does not satisfy the conditions.') RecursionError: maximum recursion depth exceeded in comparison