<jupyter_start><jupyter_text>Week 1 - Getting Started<jupyter_code>import numpy as np

print("Numpy:", np.__version__)<jupyter_output>Numpy: 1.10.4<jupyter_text>Python SummaryFurther informationMore information is usually available with the `help` function. Using ? brings up the same information in ipython. Using the `dir` function lists all the options available from a variable. help(np)np?dir(np) VariablesA variable is simply a name for something. One of the simplest tasks is printing the value of a variable.Printing can be customized using the format method on strings.<jupyter_code>location = 'Bethesda'
zip_code = 20892
elevation = 71.9

print("We're in", location, "zip code", zip_code, ", ", elevation, "m above sea level")
print("We're in " + location + " zip code " + str(zip_code) + ", " + str(elevation) + "m above sea level")
print("We're in {0} zip code {1}, {2}m above sea level".format(location, zip_code, elevation))
print("We're in {0} zip code {1}, {2:.2e}m above sea level".format(location, zip_code, elevation))<jupyter_output>We're in Bethesda zip code 20892 ,  71.9 m above sea level
We're in Bethesda zip code 20892, 71.9m above sea level
We're in Bethesda zip code 20892, 71.9m above sea level
We're in Bethesda zip code 20892, 7.19e+01m above sea level<jupyter_text>TypesA number of different types are available as part of the standard library. The following links to the documentation provide a summary.* https://docs.python.org/3.5/library/stdtypes.html* https://docs.python.org/3.5/tutorial/datastructures.htmlOther types are available from other packages and can be created to support special situations.A variety of different methods are available depending on the type.<jupyter_code># Sequences

# Lists
l = [1,2,3,4,4]
print("List:", l, len(l), 1 in l)

# Tuples
t = (1,2,3,4,4)
print("Tuple:", t, len(t), 1 in t)

# Sets
s = set([1,2,3,4,4])
print("Set:", s, len(s), 1 in s)

# Dictionaries
# Dictionaries map hashable values to arbitrary objects
d = {'a': 1, 'b': 2, 3: 's', 2.5: 't'}
print("Dictionary:", d, len(d), 'a' in d)<jupyter_output>List: [1, 2, 3, 4, 4] 5 True
Tuple: (1, 2, 3, 4, 4) 5 True
Set: {1, 2, 3, 4} 4 True
Dictionary: {3: 's', 2.5: 't', 'b': 2, 'a': 1} 4 True<jupyter_text>Conditionalshttps://docs.python.org/3.5/tutorial/controlflow.html<jupyter_code>import random

if random.random() < 0.5:
    print("Should be printed 50% of the time")
elif random.random() < 0.5:
    print("Should be primted 25% of the time")
else:
    print("Should be printed 25% of the time")<jupyter_output>Should be printed 50% of the time<jupyter_text>Loopshttps://docs.python.org/3.5/tutorial/controlflow.html<jupyter_code>for i in ['a', 'b', 'c', 'd']:
    print(i)
else:
    print('Else')
    
for i in ['a', 'b', 'c', 'd']:
    if i == 'b':
        continue
    elif i == 'd':
        break
    print(i)
else:
    print('Else')<jupyter_output>a
b
c
d
Else
a
c<jupyter_text>Functionshttps://docs.python.org/3.5/tutorial/controlflow.html<jupyter_code>def is_even(n):
    return not n % 2

print(is_even(1), is_even(2))
def first_n_squared_numbers(n=5):
    return [i**2 for i in range(1,n+1)]

print(first_n_squared_numbers())
def next_fibonacci(status=[]):
    if len(status) < 2:
        status.append(1)
        return 1
    status.append(status[-2] + status[-1])
    return status[-1]

print(next_fibonacci(), next_fibonacci(), next_fibonacci(), next_fibonacci(), next_fibonacci(), next_fibonacci())
def accepts_anything(*args, **kwargs):
    for a in args:
        print(a)
    for k in kwargs:
        print(k, kwargs[k])
        
accepts_anything(1,2,3,4, z=11, b=2, c=3, d=55)
# For quick and simple functions a lambda expression can be a useful approach. 
# Standard functions are always a valid alternative and often make code clearer.
f = lambda x: x**2
print(f(5))

people = [{'name': 'Alice', 'age': 30}, 
          {'name': 'Bob', 'age': 35}, 
          {'name': 'Charlie', 'age': 35}, 
          {'name': 'Dennis', 'age': 25}]
print(people)
people.sort(key=lambda x: x['age'])
print(people)<jupyter_output>25
[{'age': 30, 'name': 'Alice'}, {'age': 35, 'name': 'Bob'}, {'age': 35, 'name': 'Charlie'}, {'age': 25, 'name': 'Dennis'}]
[{'age': 25, 'name': 'Dennis'}, {'age': 30, 'name': 'Alice'}, {'age': 35, 'name': 'Bob'}, {'age': 35, 'name': 'Charlie'}]<jupyter_text>Numpyhttp://docs.scipy.org/doc/numpy/reference/<jupyter_code>a = np.array([[1,2,3], [4,5,6], [7,8,9]])
print(a)
print(a[1:,1:])
a = a + 2
print(a)
a = a + np.array([1,2,3])
print(a)
a = a + np.array([[10],[20],[30]])
print(a)
print(a.mean(), a.mean(axis=0), a.mean(axis=1))
import matplotlib.pyplot as plt

%matplotlib inline
x = np.linspace(0, 3*2*np.pi, 500)

plt.plot(x, np.sin(x))
plt.show()<jupyter_output><empty_output><jupyter_text>Exercises<jupyter_code>a = "The quick brown fox jumps over the lazy dog"
b = 1234567890.0<jupyter_output><empty_output><jupyter_text>* Print the variable `a` in all uppercase* Print the variable `a` with every other letter in uppercase* Print the variable `a` in reverse, i.e. god yzal ...* Print the variable `a` with the words reversed, i.e. ehT kciuq ...* Print the variable `b` in scientific notation with 4 decimal places<jupyter_code>print(a.upper())
# prints each letter lower or upper case
for i in range(len(a)):
    if i % 2 == 0:
        print(a[i].lower(),end="")
    else:
        print(a[i].upper(),end="")
# prints in reverse
print("\n")
for i in range(1,len(a)+1):
    print(a[-i],end="")
# prints each word in reverse by first splitting string
# into words then reversing individual words
print("\n")
for w in a.split():
    for i in range(1,len(w)+1):
        print(w[-i],end="")
    print(" ",end="")    
# print b in scientific notation
people = [{'name': 'Bob', 'age': 35}, 
          {'name': 'Alice', 'age': 30}, 
          {'name': 'Eve', 'age': 20},
          {'name': 'Gail', 'age': 30},
          {'name': 'Dennis', 'age': 25},
          {'name': 'Charlie', 'age': 35}, 
          {'name': 'Fred', 'age': 25},]<jupyter_output><empty_output><jupyter_text>* Print the items in `people` as comma seperated values* Sort `people` so that they are ordered by age, and print* Sort `people` so that they are ordered by age first, and then their names, i.e. Bob and Charlie should be next to each other due to their ages with Bob first due to his name. * Write a function that returns the first n prime numbers* Given a list of coordinates calculate the distance using the (Euclidean distance)[https://en.wikipedia.org/wiki/Euclidean_distance]* Given a list of coordinates arrange them in such a way that the distance traveled is minimized (the itertools module may be useful).<jupyter_code>coords = [(0,0), (10,5), (10,10), (5,10), (3,3), (3,7), (12,3), (10,11)]<jupyter_output><empty_output><jupyter_text>* Print the standard deviation of each row in a numpy array* Print only the values greater than 90 in a numpy array* From a numpy array display the values in each row in a seperate plot (the subplots method may be useful)<jupyter_code>np.random.seed(0)
a = np.random.randint(0, 100, size=(10,20))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16127671541057367, 'fraction_numerical': 0.03768278965129359, 'mean_word_length': 4.421493902439025, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 39, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '32884', 'license': 'MIT', 'n_tokens_mistral': 2732, 'n_tokens_neox': 2505, 'n_words': 831, 'path': 'Wk01-overview/Wk01-Overview_wl.ipynb', 'repo_name': 'beyondvalence/biof509_wtl', 'size': '7112'}