<jupyter_start><jupyter_text>Welcome to Notebooks!There are a lot of useful keyboard shortcuts you can use -- Check out the Help >> Keyboard Shortcuts menu.. We can designate cells as markdown -- which lets us do some cools stuff...  A few quick and useful things.[Note: Examples Taken from Adam-p Markdown Cheat Sheet](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)First let's look at some headings.. H1 H2 H3 H4 H5 H6---We can also section break things...  Next is how to add emphsis to some text..Emphasis, aka italics, with *asterisks* or _underscores_.Strong emphasis, aka bold, with **asterisks** or __underscores__.Combined emphasis with **asterisks and _underscores_**.Strikethrough uses two tildes. ~~Scratch this.~~  Let's also see how to do lists and alignment...1. First ordered list item2. Another item * Unordered sub-list. 1. Actual numbers don't matter, just that it's a number 1. Ordered sub-list4. And another item.   You can have properly indented paragraphs within list items. Notice the blank line above, and the leading spaces (you need three spaces).* Unordered list can use asterisks- Or minuses+ Or pluses  Links are really important ... code blocks...[I'm an inline-style link](https://www.google.com)URLs and URLs in angle brackets will automatically get turned into links. http://www.example.com or  and sometimes example.com (but not on Github, for example).Inline `code has backticks` around it.```You can also do blocks of code.```You can also tell markdown what type of code you are using...```javascriptvar s = "JavaScript syntax highlighting";alert(s);``` ```pythons = "Python syntax highlighting"print s``` ```No language indicated, so no syntax highlighting. But let's throw in a tag.```  Finally, tables are a bit cumbersome...Markdown | Less | Pretty--- | --- | ---*Still* | `renders` | **nicely**1 | 2 | 3--- Back to the Powerpoint---  Let's do some Code!The cell below loads up a few libraries and does some initialization.<jupyter_code>### Standard Magic and startup initializers.

# Load Numpy
import numpy as np
# Load MatPlotLib
import matplotlib
import matplotlib.pyplot as plt
# Load Pandas
import pandas as pd

# This lets us show plots inline and also save PDF plots if we want them
%matplotlib inline
from matplotlib.backends.backend_pdf import PdfPages
matplotlib.style.use('fivethirtyeight')
# Seaborn is a plotting package for Pandas that we'll try out...
import seaborn as sns

# These two things are for Pandas, it widens the notebook and lets us display data easily.
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:95% !important; }</style>"))<jupyter_output><empty_output><jupyter_text>A quick note about the Data8 Textbook.The [Data8 Textbook](https://www.inferentialthinking.com/chapters/intro) uses a package called `DataScience` which is a wrapper around Pandas.  When you do stuff in that textbook please remember that the Pandas calls are a bit different.  First let's go over the examples from the slides here and make sure we understand them.<jupyter_code># Define a simple function.

def my_func(x, y):
    if x > y:
        return x
    else:
        return y
my_func(1,2)
def my_func(x, y):
    return (x-1, y+2)<jupyter_output><empty_output><jupyter_text>Be careful with notebooks... we can have scope problems if we run cells out of order!<jupyter_code># What is in scope here? 
(a, b) = my_func(1, 2)
print(a)
print(b)<jupyter_output><empty_output><jupyter_text>Let's look at some simple lists and data structures.<jupyter_code>a = [1, 2, 4, 'a']
a
# len: returns the number of items of an enumerable object
len(['c', 'm', 's', 'c', 3, 2, 0])

# range: returns an iterable object
list(range(10))

# enumerate: returns iterable tuple (index, element) of a list
a = enumerate( ['311', '320', '330'] )
print(a)
# Recall here that Python3 does lazy evaluation for these iterators.  We have to manually expand it.
list(a)
a = ['311', '320', '330']
for i,j in enumerate(a):
    print(i,j)
## Map and Filter

# map: apply a function to a sequence or iterable

arr = [1, 2, 3, 4, 5]
out = map(lambda x: x**2, arr)
print(list(out))
print(arr)
new_arr = [x**2 for x in arr if x <= 3]
new_arr
# What happened here??
arr = [1, 2, 3, 4, 5]
out = map(lambda x: x**2, arr)
print(out)


# Remember again lazy evaluation!
# filter: returns a list of elements for which a predicate is true

arr = [1, 2, 3, 4, 5, 6, 7]
out = filter(lambda x: x % 2 == 0, arr)
print(out)

# Remember again that we have to explicitly evaluate the iterator.
print(list(out))
# A more pythonic way: list comprehension...
x = [i for i in arr if i % 2 == 0]
x
# List comprehensions are the best!
P = [ 2**x for x in range(17) ]
P
# Can also do dictionaries...
D = {x:['no'] for x in range(10)}
D<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1099334995843724, 'fraction_numerical': 0.01683291770573566, 'mean_word_length': 4.277412280701754, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 22, 'https://': 3, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '218630', 'license': 'MIT', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1474, 'n_words': 662, 'path': '_notebooks/02-Tools.ipynb', 'repo_name': 'sbroth/cmps3160', 'size': '4812'}