I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
Numeric data types
One of the simplest ways to use the Python interpretter is as a fancy
calculator. We'll illustrate this below and use this as an opportunity to
introduce the core numeric data types that Python supports.
Can you write Python code for it?

# this is a comment, the interpretter ignores it
# you can use comments to add short notes or explanation

2 + 10 # add two integers (whole numbers)

2.0 + 10.0 # add two floating point numbers (real (decimal) numbers)

2 + 10.0 # operations that mix integers and floats return floats

2 * 10 # multiplication of integers

2.0 * 10.0 # multiplication of floats

1.0/5.0 # division

2/5 # in Python 2 this used to default to integer division
    # in Python 3 division always returns a float

10 % 3 # The % (modulo) operator yields the remainder after division

2**10  # exponentiation -- 2 raised to the power 10

2**0.5  # exponentiation with fractional powers
        # **0.5 = square root, **(1/3.) = cube root

(10+2)/(4-5)  # numerical operators differ in their precedence
              # contrast the output of this line with the line below

(10+2)/4-5   # it is a good habit to use parentheses to disambiguate
             # potentially confusing calculations

(1 + 1j)  # complex numbers; we won't use these in the course
          # but you might occasionally find the need for them
          # in biological research

(1 + 1j) + (3 + 2j) # adding complex numbers

(0 + 1j) * (1j)  # complex multiplication


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
Numpy includes most of the functions defined in the math module so we didn't
really need to add the math. prefix.
Can you write Python code for it?

exp(1) # e^1

log(e) # natural logarithm of e

log10(100) # log base 10 of 100


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
Slicing lists
Python lists support the notion of ‘slices’ - a continuous sublist of a larger
list. The following code illustrates this concept.
Can you write Python code for it?

c = ['a','b','c','d','e','f']

c[0:3]  # get the elements of from index 0 up to
        # but not including the element at index 3

c[:3]  # same as above, first index implied

c[2:5]  # from element 2 up to 5

c[3:]  # from index three to end (last index implied)

c[-1:0]  # how come this returned an empty list?


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
Strings
Strings aren't numerical data, but working with strings comes up often
enough in numerical computing that it's worth mentioning them here.
Strings represent textual information, data or input.  String are an interesting
data type because they share properties with data structures like lists (data
structures will be introduced in the next handout).
Can you write Python code for it?

# strings can be enclosed in double quotes
s1 = "Beware the Jabberwock, my son!"
print(s1)

type(s1)  # what type are you, s1?

# OR in single quotes
s2 = 'The jaws that bite, the claws that catch!'
print(s2)

# If the string you want to write has a quote character
# you need to wrap it in the other type of quote

# note the single quote at the beginning of 'Twas
s3 = "'Twas brillig, and the slithy toves"
print(s3)

# Concatenating (adding) string
s4 = "abc"
s5 = "def"
print(s4 + s5)


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
The last example above shows that the lengths of the two arrays have to be the same in order to do element-wise operations.
By default,most operations on arrays work element-wise. However there are a variety of functions for doing array-wise operations such as matrix multiplication or matrix inversion. Here are a few examples of using NumPy arrays to represent matrices:
Can you write Python code for it?

m = np.array([[1,2],
              [3,4]])

m

m.transpose()

linalg.inv(m)


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
There are many other built-in functions, and we'll see more examples of these
below.  See the Python documentation on "Built-in
Functions" for more
details.
Defining functions
You can write your own functions. The general form of a function definition in Python is:
def func_name(arg1, arg2, ...):
    body of function
    return result
Note:
 * Python is white space sensitive, body of a function must be indented
(idiomatic style is to indent by 4 spaces NOT tabs)
 * Use a Python aware editor/environment to help get indenting correct. Jupyter
will help you get the indentation correct
Can you write Python code for it?

# a function that carries out a simple mathematical calculation

def area_of_circle(radius):
    """radius of circle --> area of circle"""
    return 3.141592654 * radius**2

area_of_circle(1)

area_of_circle(8)


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
Variable assignment
A value or the result of a calculation can be given a name, and then reused in a
different context by referring to that name. This is called variable assignment.
Can you write Python code for it?

pi = 3.141592654
radius = 4.0
area_circ = pi * radius**2
# notice that you don't get any output from this code cell

# however, once you evaluate this code cell you will see
# the results of your calculation
area_circ


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
You can also do some fancy tricks on lists to generate repeating patterns.
Can you write Python code for it?


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
You can append and delete list elements as well as concatenate two lists
Can you write Python code for it?


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
You can use negative indexing to get elements from the end of a list.
Can you write Python code for it?

z[-1] # last element

z[-2] # second to last element


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
D'abord les index positifs
Can you write Python code for it?

chaine[0], chaine[1], chaine[len(chaine) - 1]


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Des chaînes de caractères aux listes et réciproquement

La fonction split découpe une chaîne de caractères selon le caractère de séparation sep avec la syntaxe chaine.split(sep).
Can you write Python code for it?

chaine = 'Frédéric,Junier,enseignant,mathématiques'
champs = chaine.split(',')
print(champs)


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Et maintenant les index négatifs
Can you write Python code for it?


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Exercice 12
Expliquer ce qui ne va pas dans la fonction ci-dessous.
~~~python
def addition(x, y, z):
    return x + y + z
    print('la réponse est', x + y + z)
~~~
Réponse à l'exercice 12 :
Compléter cette cellule de texte
Premiers compléments sur les listes et les chaînes de caractères
Opérateurs et fonctions communs aux chaînes de caractère et aux listes

L'opérateur in d'appartenance
Can you write Python code for it?

chaine = 'Bonjour'

'b' in chaine

'B' in chaine

liste = [0, 7]

8 in liste

0 in liste


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Exercice 13
En informatique la couleur associée à un pixel de l'écran  est souvent représentée sous la forme d'un triplet d'entiers comme (R, G, B). La première composante R représente la quantité de Rouge, la deuxième G celle de vert et la troisième B celle de jaune. Par addition des couleurs primaires Rouge, Vert et Bleu on peut représenter n'importe quelle couleur du spectre visible. Les couleurs de pixels sont en général codées sur 3 octets ce qui donne 8 bits par composante c'est-à-dire $2^8=256$ valeurs : de 0 pour l'absence de couleur à 255 pour l'intensité  maximale.

La variable C de type liste contient une couleur, mais laquelle ?

~~~python
C = [255, 255, 255]
~~~

Comment peut-on modifier la variable C pour qu'elle code la couleur jaune ? la couleur noire ?
  On pourra s'aider de l'outil sur  la page https://www.w3schools.com/colors/colors_picker.asp.
Can you write Python code for it?

# Cellule de code pour répondre à l'exercice 13


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Exercice 14
Les fichiers au format postcript sont des fichier textes contenant des instructions permettant de représenter une image dans un format vectoriel à l'aide des fonctions mathématiques du langage de programmation postscript. Ces fichiers peuvent être convertis en fichiers de format binaire comme le format png à l'aide de programme comme convert de la bibliothèque ImageMagick.

En informatique, quelle est la différence entre un fichier  texte et un fichier  binaire ?

Quelle est la différence entre une image de format vectoriel et une image de format binaire ? Citez des formats vectoriels ou bitmap connus.

Compléter la fonction conversion_nom(source, ext) où le paramètre   source est un nom de fichier de la forme nombase.extension,  pour qu'elle retourne le nom associé après remplacement de l'extension source par  l'extension ext passée en paramètre.

~~~python
def conversion_nom(source, ext):
   # à compléter
   # return
~~~
Can you write Python code for it?

# Cellule de code pour répondre à l'exercice 14


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Exercice 16

Écrire un programme en Python avec le module turtle qui dessine une spirale comme celle ci:
Can you write Python code for it?

# Cellule de code pour répondre à l'exercice 16


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Exercice 20

On lance successivement un dé cubique équilibré à 6 faces numérotées de 1 à 6 jusqu'à obtenir un 6.
  Recopier et compléter la fonction premier6() ci-dessous pour qu'elle retourne le rang du premier 6.

~~~python
from random import randint
def premier6():
    de = randint(1, 6)
    rang = 1
    # while à compléter
        # bloc du while
    return rang
~~~

On répète un grand nombre n de fois l'expérience qui consiste à lancer un dé cubique jusqu'à obtenir un premier 6, compléter le programme précédent avec une fonction rangMoyen(n) qui retourne le rang moyen du premier 6 sur cet échantillon de taille n.

~~~python
def rangMoyen(n):
    sommeRang = 0
    for k in range(n):
        # à compléter
    return sommeRang / n
~~~
Can you write Python code for it?

# Cellule de code pour répondre à l'exercice 20


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Fonctions  spécifiques aux chaînes de caractères

La fonction  lower permet de  convertir tous les caractères en minuscules.

De même la fonction upper permet de convertir tous les caractères en majuscules
Il s'agit de méthodes internes qu'on applique avec la syntaxe chaine.lower() ou chaine.upper().
Can you write Python code for it?

chaine = 'Bonjour'

chaine.lower()

chaine.upper()


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Fonctions de bibliothèques et le module turtle
Définition 6
Dans un langage de programmation une  bibliothèque est un ensemble de fonctions qui constitue une trousse à outils autour d'un même thème.
Si cette bibliothèque est installée sur sa machine on peut l'importer dans un programme pour utiliser ses fonctions.
Une bibliothèque écrite en Python s'appelle un module. Il existe des milliers de modules et l'un des plus populaires dans l'informatique éducative est turtle  qui implémente en Python les fonctions du  légendaire langage graphique Logo
Pour importer toutes les fonctions du module turtle dans son programme on écrit :
~~~python
from turtle import *
~~~
Pour utiliser la fonction penup() du module on écrira alors penup().
On peut également créer juste un point d'accès au module turtle avec :
~~~python
import turtle
~~~
Pour utiliser la fonction penup() du module on écrira alors turtle.penup().
Exercice 15

Pour découvrir quelques fonctions de turtle, lire  puis exécuter le programme ci-dessous dans la cellule juste après celle-ci.

Si vous avez un message d'erreur contenant le terme Terminator, exécutez la cellule de nouveau.

Pour aller plus loin, vous pouvez consulter la documentation sur https://docs.python.org/3/library/turtle.html#module-turtle
Can you write Python code for it?

from turtle import *

speed(1)        #parametrage de la vitesse de 1 lent à 10 rapide, 0 étant la vitesse la plus rapide
shape("turtle") #choix de la forme de la tortue
pencolor("red")  #choix de la couleur du crayon
pensize(4)      #épaisseur du crayon
penup()         #lever le crayon
goto(0,0)      # aller à la position (0,0)
setheading(0)  #orientation de la tortue vers l'Est / 90  Nord / 180 Ouest / 270 Sud
pendown()       #poser le crayon
for k in range(4):
    forward(100)  #avancer de 100 pixels
    left(90)           #tourner de 90 degrés
mainloop()       #gestionnaire d’evenement, indispensable pour l’affichage et l’interactivite


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
L'opérateur []  permet d'accéder à l'élément en position k dans le conteneur (chaîne ou liste) avec la syntaxe conteneur[k].

Attention les positions sont indexées de gauche à droite de 0 à len(conteneur)-1 et de droite à gauche de $-1$ à -len(conteneur).
Can you write Python code for it?


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
La fonction input

La fonction input récupère un texte un saisi par l'utilisateur après un prompt et retourne la saisie sous forme de chaîne de caractères. La fonction prend comme argument optionnel le prompt qui doit être une chaîne de caractères.

Exercice 4

Testez le programme ci-dessous.

Comment expliquez-vous la réponse de l'interpréteur ?

Demandez au professeur comment on peut corriger ce  programme.
Can you write Python code for it?

date = input('Entrez votre date de naissance : ')
print('Vous avez  ', 2017 - date, 'ans')


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
La fonction len permet d'obtenir la longueur du conteneur avec len(conteneur).
Can you write Python code for it?


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Le test
Définition 3
Un test est composé d’une instruction de contrôle puis d'un bloc d'instructions.
En Python, l'instruction de contrôle commence par le mot clef if suivi d’une
condition à valeur booléenne (True ou False) et se termine par le symbole :.
Le bloc d’instructions qui suit s'exécute si et seulement si la condition de l'instruction de contrôle a pour valeur True . Il correspond à un embranchement dans le flux d'instructions.
En Python, un bloc d'instructions commence à la première ligne suivant le symbole : et son imbrication dans le reste du programme est caractérisée  par son niveau d'indentation. Toutes les instructions d'un même bloc doivent avoir le même niveau d'indentation. Le flux principal d'instructions est collé contre la marge puis chaque niveau d'indentation est décalé de quatre espaces ou une tabulation vers la droite.
~~~python
flux parent
if conditionA:
    #bloc d'instructions exécuté si la valeur de conditionA est True
    if conditionB:
        #bloc d'instructions imbriqué, exécuté si conditionA  est à True
        # et si conditionB est à True
retour au flux parent
~~~
Si la condition du test n'est pas vérifiée, on peut prévoir l'exécution d'un bloc d'instructions alternatif après un else :.
~~~python
flux parent
if condition:
    #bloc d'instructions exécuté si la valeur de condition est True
else:
    #bloc d'instructions exécuté si la valeur de condition est False
retour au  flux parent
~~~
On peut aussi tester d'autres conditions mutuellement exclusives pour modéliser un choix entre plus de deux alternatives. Chaque condition  testée dans un elif condition : commande un bloc d'instructions et un bloc d'instructions  par défaut peut se trouver après un else :.
~~~python
flux parent
if condition1:
    #bloc d'instructions exécuté si la valeur de condition1 est à True
elif condition2:
    #bloc d'instructions  si  condition2 est True (et condition1 est à False)
elif condition3:
    #bloc d'instructions si  condition3 est True (et condition1 et condition2 sont à False)
else...
Can you write Python code for it?

# Cellule pour exécuter le code de l'exercice 6


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Le type bool pour les booléens (deux valeurs logiques possibles True ou False)
Can you write Python code for it?

c = (1 < 3)

c

print(type(c))

d =  ( 1 == 2)

d

print(type(d))


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Le type str pour les chaînes de caractères
Can you write Python code for it?

e = 'Hello World'
print(e)

print(type(e))


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Les  instructions élémentaires
Un petit nombre d'insctructions élémentaires permettent de traduire dans un langage de programmation tout type d'algorithme exécutable par un ordinateur.
L'affectation et le type d'une variable
Définition 2
Une variable est l'association d'un espace de la mémoire de l'ordinateur, accessible par son nom, et  d'une valeur que l'on y stocke. En Python on définit une variable par une instruction d'affectation, par exemple pour affecter la valeur 12 à la variable de nom a on écrit :
~~~python
a = 12
~~~
Un langage de programmation fixe des contraintes pour les nomes de variables. En Python les seuls caractères autorisés sont les caractères non accentués, les chiffres (sauf en première position) et le tiret bas (mais pas le tiret haut).
~~~python
1var = 13     #nom incorrect
var1 = 13     #nom correct
var-1 = 14     #nom incorrect
var_1 = 14     #nom correct
~~~
Un langage de programmation n'accepte par défaut qu'un nombre fini de types de valeurs, ainsi une variable est caractérisée par son type accessible par la fonction type et qu'on peut afficher avec la fonction print:
~~~python
type(a)
~~~
Exercice 2

Recopier et compléter le programme suivant pour qu'il échange les entiers  des variables x et y.
 Le programme doit fonctionner quels que soient les entiers contenus dans x et y.

~~~python
x = 501
y = 504
à compléter
~~~
Can you write Python code for it?

# Cellule de code pour répondre à l'exercice 2


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Les fonctions
Définition 5
Lorsqu'on a besoin d'utiliser plusieurs fois un même bloc d'instructions, on peut l'encapsuler dans une fonction. On peut ainsi étendre le  langage avec une nouvelle instruction.
Dans la définition d'une fonction, on précise son nom et en général des paramètres formels d'entrée et une valeur de retour. En Python on définit une fonction ainsi :
~~~python
def mafonction(parametre1, parametre2):
    #bloc d'instructions (optionnel)
    return valeur
~~~
Par exemple une fonction maximum qui prend en paramètres deux nombres x et y et qui retourne leur maximum,  s'écrira  :
~~~python
def maximum(x, y):
    if x < y:
        return y
    else:
        return x
~~~
En fait, l'exécution d'une instruction  return correspond à une sortie de la  fonction, toutes les instructions suivantes ne seront pas exécutées. Ainsi la fonction précédente peut s'écrire plus simplement :
~~~python
def maximum2(x, y):
    if x < y:
        return y
    return x
~~~
Nous venons de voir la définition d'une fonction. On exécute la fonction en substituant aux  paramètres formels  des valeurs particulières appelées arguments. Cette instruction se nomme un appel de fonction :
~~~python
note1 = int(input('Entrez une note'))   #récupération d'un argument
note2 = int(input('Entrez une note'))   #récupération d'un autre argument
maxi = maximum2(note1, note2)           #affectation de la valeur retournée par un appel de fonction
~~~
Il existe aussi des fonctions sans paramètres d'entrée ou sans valeur de retour.
Par exemple, une fonction qui affiche le nom complet d'un acronyme peut s'écrire :
~~~python
def icn():
    print("Informatique et Création Numérique")
~~~
Attention, return valeur  retourne valeur qu'on peut capturer dans une variable alors que  print(valeur) affiche valeur sur la sortie standard (l'écran par défaut) mais valeur ne peut alors être capturée dans une variable.
Exercice 11

On donne ci-dessous un programme qui permet de remplir un tableau de valeurs de la  fonction  mathématique $f : x \mapsto...
Can you write Python code for it?

# Répondre à l'exercice 11 dans cette cellule de code.


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Les instructions d'entrée / sortie
La fonction print

On utilise la fonction print pour un affichage sur la sortie standard qui par défaut est l'écran.
Can you write Python code for it?


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Les opérateurs arithmétiques suivent des règles de précédence (priorité de l'exponentiation sur la multiplication et de la multiplication sur l'addition). Ces priorités peuvent être changées dans un calcul à l'aide de  parenthèses.
Can you write Python code for it?


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Les principaux types de Python

Le type int pour les entiers
Can you write Python code for it?


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
On peut aussi afficher la valeur d'une expression obtenue par évaluation d'une combinaison de littéraux  (valeurs correspondant aux types de base du langage), d'opérateurs et de variables
Can you write Python code for it?


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Pour afficher du texte, celui-ci doit être contenu dans une chaîne de caratère délimitée par des apostrophes comme 'La mer' ou des guillemets comme "L'océan".
Can you write Python code for it?


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Pour afficher l'aide sur une fonction en python, on peut utiliser la documentation en ligne sur https://docs.python.org/3/ ou la documentation intégrée avec la fonction help.
Can you write Python code for it?


I am working on the file "Intro-Python-ICN-2017.ipynb".
The first task is:
Voici ce qui se produit lorsqu'on essaie d'accéder à un index hors de la plage
Can you write Python code for it?


I am working on the file "Intro.ipynb".
The first task is:
2 cost minions
Can you write Python code for it?


I am working on the file "Intro.ipynb".
The first task is:
2-cost minions
Let's see what is the expected minion stats from a Piloted Shredder deathrattle.
Can you write Python code for it?

two_cost_minions_df = all_collectible_cards_df[(all_collectible_cards_df['type'] == 'Minion') &
                                               (all_collectible_cards_df['cost'] == 2)]
print('2-cost minion mean attack: {:.2f}'.format(two_cost_minions_df.attack.mean()))
print('2-cost minion mean health: {:.2f}'.format(two_cost_minions_df.health.mean()))


I am working on the file "Intro.ipynb".
The first task is:
3 cost minions
Can you write Python code for it?


I am working on the file "Intro.ipynb".
The first task is:
4 cost minions
Can you write Python code for it?


I am working on the file "Intro.ipynb".
The first task is:
6 cost minions
Can you write Python code for it?


I am working on the file "Intro.ipynb".
The first task is:
Card attributes
Can you write Python code for it?

print('Card attributes:', ', '.join(set(sum((list(x.keys()) for x in all_collectible_cards), list()))))


I am working on the file "Intro.ipynb".
The first task is:
Card mechanics
Can you write Python code for it?

print('Card mechanics:', ', '.join(
        set(sum((x['mechanics'] for x in all_collectible_cards if 'mechanics' in x), list()))))


I am working on the file "Intro.ipynb".
The first task is:
Cards data
Load the collectible cards inside JSON data
Let's start loading the game cards data from Hearthstone JSON and loading it to python using the json library.
Can you write Python code for it?

all_sets_filename = os.path.join('data', 'AllSets.json')

# Uncomment the following lines to update the date file
#import urllib
#urllib.urlretrieve ('http://hearthstonejson.com/json/AllSets.json', all_sets_filename)

all_collectible_cards = hearthpricer.load_json(all_sets_filename)
print('# of collectible cards:', len(all_collectible_cards))


I am working on the file "Intro.ipynb".
The first task is:
Eight schools example
https://pystan.readthedocs.org/en/latest/getting_started.html
Can you write Python code for it?

schools_code = """
data {
    int<lower=0> J; // number of schools
    real y[J]; // estimated treatment effects
    real<lower=0> sigma[J]; // s.e. of effect estimates
}
parameters {
    real mu;
    real<lower=0> tau;
    real eta[J];
}
transformed parameters {
    real theta[J];
    for (j in 1:J)
    theta[j] <- mu + tau * eta[j];
}
model {
    eta ~ normal(0, 1);
    y ~ normal(theta, sigma);
}
"""

schools_dat = {'J': 8,
               'y': [28,  8, -3,  7, -1,  1, 18, 12],
               'sigma': [15, 10, 16, 11,  9, 11, 10, 18]}

fit = pystan.stan(model_code=schools_code, data=schools_dat,
                  iter=1000, chains=4)

fit

la = fit.extract(permuted=True)
la.keys()

for k,v in la.iteritems():
    print k,v.shape

fit.plot()
plt.gcf().set_size_inches(18.5, 10.5);


I am working on the file "Intro.ipynb".
The first task is:
Results
0 cost minions
Can you write Python code for it?


I am working on the file "Intro.ipynb".
The first task is:
Some statistics
Deathrattle minions
Let's see what is the expected damage from a Scarlet Purifier.
Can you write Python code for it?

minions_df = all_collectible_cards_df[(all_collectible_cards_df['type'] == 'Minion')]
deathrattle_minions_df = minions_df[minions_df.apply(
        lambda x: x['mechanics'] is not numpy.nan and ('Deathrattle' in x['mechanics']), axis=1)]

print('Deathrattle minions population: {} ({:.2%})'.format(
        len(deathrattle_minions_df), 1.0 * len(deathrattle_minions_df) / len(minions_df)))


I am working on the file "Intro.ipynb".
The first task is:
The model
Card analysis will be done based on the following base model equation:
$$cost = \sum (attribute_i \cdot coeff_i) + intrinsic$$
The intrinsic value represents the cost of having that card in your deck and also can be viewed as the slot_cost.
Modelling the cards
With the previous model, let's create a matrix with all the information to work with.
Can you write Python code for it?

all_collectible_cards_df = pandas.DataFrame(all_collectible_cards)
all_collectible_cards_df.info()


I am working on the file "Intro.ipynb".
The first task is:
These results can be bad to anyone with some experience, because Wisp is listed in the first place with a great distance to the second and River Crocolisk (a good vanilla) is on the low end. But this was only a example of how the model works. More complex examples below.
Adding simple mechanics
To enrich the model, let's add simple mechanics to a minion-only matrix.
Processing the cards mechanics
Cards have to be processed to extract the card mechanics (Charge, Stealth, Windfury, Taunt, Divine Shield) from the text, adding the text_mechanics attribute with the complex mechanics. All cards with unknown mechanics are discarded when processed.
Can you write Python code for it?

all_mechanics_cards = hearthpricer.process_mechanics(all_collectible_cards)
print('# of processed cards: {} ({:.2%})'.format(
        len(all_mechanics_cards), 1.0 * len(all_mechanics_cards) / len(all_collectible_cards)))


I am working on the file "Intro.ipynb".
The first task is:
Vanilla minions modelling
To test the model, let's extract the coefficients for attack and health with only the minions with no text (vanilla minions).
Can you write Python code for it?

vanilla_minions_df = pandas.DataFrame(
    all_collectible_cards_df[(all_collectible_cards_df['type'] == 'Minion') &
                             (all_collectible_cards_df['text'].isnull())])
vanilla_minions_df.info()


I am working on the file "Intro.ipynb".
The first task is:
With these coeffs, we can define a ratio attribute with the ratio between the real price and cost:
$$ratio = \frac{(price - intrinsic) - (cost - intrinsic)}{cost - intrinsic} = \frac{price - cost}{cost - intrinsic}$$
Then sort the results from the best to the worst in terms of ratio.
Can you write Python code for it?

intrinsic = vanilla_coeffs[0][0]
vanilla_minions_df['ratio'] = (vanilla_minions_df['price'] -  vanilla_minions_df['cost']) / \
                              (vanilla_minions_df['cost'] - intrinsic)
vanilla_minions_df[['name', 'cost', 'price', 'ratio']].sort('ratio', ascending=False)


I am working on the file "Intro/Intro.ipynb".
The first task is:
Alternatively, we can store data in a dictionary instead of in lists. A dictionary keeps a mapping of keys to a set of values, and each key is unique. Using our top 10 movies example, we could create a dictionary that contains ratings a key, and movie titles as another key.
Can you write Python code for it?

top_10_movies_dict = {"Rating" : [9.2, 9.2, 9., 8.9, 8.9, 8.9, 8.9, 8.9, 8.9, 8.8],
                      "Movie" : ['The Shawshank Redemption (1994)',
                                'The Godfather (1972)',
                                'The Godfather: Part II (1974)',
                                'Pulp Fiction (1994)',
                                "Schindler's List (1993)",
                                'The Lord of the Rings: The Return of the King (2003)',
                                '12 Angry Men (1957)',
                                'The Dark Knight (2008)',
                                'Il buono, il brutto, il cattivo (1966)',
                                'The Lord of the Rings: The Fellowship of the Ring (2001)']}


I am working on the file "Intro/Intro.ipynb".
The first task is:
Alternatively, you can also get rid of columns you dont need using .drop()
Can you write Python code for it?


I am working on the file "Intro/Intro.ipynb".
The first task is:
And this one:
Can you write Python code for it?

print("\N{WAVING HAND SIGN}, \N{EARTH GLOBE ASIA-AUSTRALIA}!")


I am working on the file "Intro/Intro.ipynb".
The first task is:
And to find the average value of a column, use mean().
Can you write Python code for it?


I am working on the file "Intro/Intro.ipynb".
The first task is:
But wait! The table looks like it's sorted in increasing order. This is because sort_values defaults to ordering the column in ascending order. To correct this, add in the extra optional parameter
Can you write Python code for it?


I am working on the file "Intro/Intro.ipynb".
The first task is:
C. Lists
The next topic is particularly useful in the kind of data manipulation that you will see throughout 101B. The following few cells will introduce the concept of lists (and their counterpart, numpy arrays). Read through the following cell to understand the basic structure of a list.
A list is an ordered collection of objects. They allow us to store and access groups of variables and other objects for easy access and analysis. Check out this documentation for an in-depth look at the capabilities of lists.
To initialize a list, you use brackets. Putting objects separated by commas in between the brackets will add them to the list.
Can you write Python code for it?

# an empty list
lst = []
print(lst)

# reassigning our empty list to a new list
lst = [1, 3, 6, 'lists', 'are' 'fun', 4]
print(lst)


I am working on the file "Intro/Intro.ipynb".
The first task is:
Creating dataframes
The rows and columns of a pandas dataframe are essentially a collection of lists stacked on top/next to each other. For example, if I wanted to store the top 10 movies and their ratings in a datatable, I could create 10 lists that each contain a rating and a corresponding title, and these lists would be the rows of the table:
Can you write Python code for it?

top_10_movies = pd.DataFrame(data=np.array(
            [[9.2, 'The Shawshank Redemption (1994)'],
            [9.2, 'The Godfather (1972)'],
            [9., 'The Godfather: Part II (1974)'],
            [8.9, 'Pulp Fiction (1994)'],
            [8.9, "Schindler's List (1993)"],
            [8.9, 'The Lord of the Rings: The Return of the King (2003)'],
            [8.9, '12 Angry Men (1957)'],
            [8.9, 'The Dark Knight (2008)'],
            [8.9, 'Il buono, il brutto, il cattivo (1966)'],
            [8.8, 'The Lord of the Rings: The Fellowship of the Ring (2001)']]), columns=["Rating", "Movie"])
top_10_movies


I am working on the file "Intro/Intro.ipynb".
The first task is:
D. Numpy Arrays
Closely related to the concept of a list is the array, a nested sequence of elements that is structurally identical to a list. Arrays, however, can be operated on arithmetically with much more versatility than regular lists. For the purpose of later data manipulation, we'll access arrays through Numpy, which will require an import statement.
Now run the next cell to import the numpy library into your notebook, and examine how numpy arrays can be used.
Can you write Python code for it?

import numpy as np

# Initialize an array of integers 0 through 9.
example_array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

# This can also be accomplished using np.arange
example_array_2 = np.arange(10)
print('Undoubled Array:')
print(example_array_2)

# Double the values in example_array and print the new array.
double_array = example_array*2
print('Doubled Array:')
print(double_array)


I am working on the file "Intro/Intro.ipynb".
The first task is:
Easy enough, right? Let's look at a function that takes two parameters, compares them somehow, and then returns a boolean value (True or False) depending on the comparison. The is_multiple function below takes as parameters an integer m and an integer n, checks if m is a multiple of n, and returns True if it is. Otherwise, it returns False.
if statements, just like while loops, are dependent on boolean expressions. If the conditional is True, then the following indented code block will be executed. If the conditional evaluates to False, then the code block will be skipped over. Read more about if statements here.
Can you write Python code for it?

def is_multiple(m, n):
    if (m % n == 0):
        return True
    else:
        return False

is_multiple(12, 4)

is_multiple(12, 7)


I am working on the file "Intro/Intro.ipynb".
The first task is:
Finally, you can use square bracket notation to index rows by their indices with a single set of brackets. You must specify a range of values for which you want to index. For example, if I wanted the 20th to 30th rows of accounts:
Can you write Python code for it?


I am working on the file "Intro/Intro.ipynb".
The first task is:
For loops can also iterate over ranges of numerical values. If I wanted to alter example_array without copying it over to a new list, I would use a numerical iterator to access list indices rather than the elements themselves. This iterator, called i, would range from 0, the value of the first index, to 9, the value of the last. I can make sure of this by using the built-in range and len functions.
Can you write Python code for it?

for i in range(len(example_array)):
    example_array[i] = example_array[i] + 5

example_array


I am working on the file "Intro/Intro.ipynb".
The first task is:
Lists can also be operated on with a few built-in analysis functions. These include min and max, among others. Lists can also be concatenated together. Find some examples below.
Can you write Python code for it?

# A list containing six integers.
a_list = [1, 6, 4, 8, 13, 2]

# Another list containing six integers.
b_list = [4, 5, 2, 14, 9, 11]

print('Max of a_list:', max(a_list))
print('Min of b_list:', min(a_list))

# Concatenate a_list and b_list:
c_list = a_list + b_list
print('Concatenated:', c_list)


I am working on the file "Intro/Intro.ipynb".
The first task is:
Many different conditions can be included to filter, and you can use & and | operators to connect them together. Make sure to include parantheses for each condition!
Another way to reorganize data to make it more convenient is to sort the data by the values in a specific column. For example, if we wanted to find the the tallest person in our data set, we could sort the table by height:
Can you write Python code for it?


I am working on the file "Intro/Intro.ipynb".
The first task is:
Notice how both ways return the same table! However, the list method created the table by essentially taking the lists and making up the rows of the table, while the dictionary method took the keys from the dictionary to make up the columns of the table. In this way, dataframes can be viewed as a collection of basic data structures, either through collecting rows or columns.
Reading in Dataframes
Luckily for you, most datatables in this course will be premade and given to you in a form that is easily read into a pandas method, which creates the table for you. A common file type that is used for economic data is a Comma-Separated Values (.csv) file, which stores tabular data. It is not necessary for you to know exactly how .csv files store data, but you should know how to read a file in as a pandas dataframe.
We will read in a .csv file that contains data from Spring 2017 about VOT and closure.
Can you write Python code for it?

# Run this cell to read in the table
vot = pd.read_csv("data/vots.csv")


I am working on the file "Intro/Intro.ipynb".
The first task is:
Notice how the above cell returns an array of all the closure values in their original order.
Now, if you wanted to get the first closure value from this array, you could index it with another pair of square brackets:
Can you write Python code for it?


I am working on the file "Intro/Intro.ipynb".
The first task is:
Notice that instead of multiplying each of the elements by two, multiplying a list and a number returns that many copies of that list. This is the reason that we will sometimes use Numpy over lists. Other mathematical operations have interesting behaviors with lists that you should explore on your own.
E. Looping
Loops are often useful in manipulating, iterating over, or transforming large lists and arrays. The first type we will discuss is the for loop. For loops are helpful in traversing a list and performing an action at each element. For example, the following code moves through every element in example_array, adds it to the previous element in example_array, and copies this sum to a new array.
Can you write Python code for it?

new_list = []

for element in example_array:
    new_element = element + 5
    new_list.append(new_element)

new_list


I am working on the file "Intro/Intro.ipynb".
The first task is:
Now we can clearly see that the tallest person was 185.42 cm tall.
Useful Functions for Numeric Data
Here are a few useful functions when dealing with numeric data columns.
To find the minimum value in a column, call min() on a column of the table.
Can you write Python code for it?


I am working on the file "Intro/Intro.ipynb".
The first task is:
Now, we can use this dictionary to create a table with columns Rating and Movie
Can you write Python code for it?

top_10_movies_2 = pd.DataFrame(data=top_10_movies_dict, columns=["Rating", "Movie"])
top_10_movies_2


I am working on the file "Intro/Intro.ipynb".
The first task is:
One of the advantages of pandas is its built-in plotting methods. We can simply call .plot() on a dataframe to plot columns against one another. All that we have to do is specify which column to plot on which axis. We include the extra variable kind='scatter' to override the default lineplot.
Can you write Python code for it?


I am working on the file "Intro/Intro.ipynb".
The first task is:
Pandas columns have many of the same properties as numpy arrays. Keep in mind that pandas dataframes, as well as many other data structures, are zero-indexed, meaning indexes start at 0 and end at the number of elements minus one.
If you wanted to create a new datatable with select columns from the original table, you can index with double brackets.
Can you write Python code for it?

## Note: .head() returns the first five rows of the table
vot[['pclo', 'tclo', 'kclo', 'pvot', 'tvot', 'kvot']].head()


I am working on the file "Intro/Intro.ipynb".
The first task is:
Question 1: Variables
See if you can write a series of expressions that creates two new variables called x and y and assigns them values of 10.5 and 7.2. Then assign their product to the variable combo and print it.
Can you write Python code for it?

# Fill in the missing lines to complete the expressions.
x = ...
...
...
print(...)


I am working on the file "Intro/Intro.ipynb".
The first task is:
Question 2: Lists
Build a list of length 10 containing whatever elements you'd like. Then, slice it into a new list of length five using a index slicing. Finally, assign the last element in your sliced list to the given variable and print it.
Can you write Python code for it?

### Fill in the ellipses to complete the question.
my_list = ...

my_list_sliced = my_list[...]

last_of_sliced = ...

print(...)

ok.grade('q02')


I am working on the file "Intro/Intro.ipynb".
The first task is:
Question 4: Writing functions
In the following cell, complete a function that will take as its parameters a list and two integers x and y, iterate through the list, and replace any number in the list that is a multiple of x with y.

Hint: use the is_multiple() function to streamline your code.
Can you write Python code for it?

def replace_with_y(lst, x, y):
    for i in range(...):
        if(...):
            ...
    return lst

ok.grade('q04')


I am working on the file "Intro/Intro.ipynb".
The first task is:
Running the cell below will give you some feed back on your responses. Though the OK tests are not always comprehensive (passing all of the tests does not guarantee full credit for questions), they give you a pretty good indication as to whether or not you're on track.
Can you write Python code for it?


I am working on the file "Intro/Intro.ipynb".
The first task is:
Sidenote: Another way to write is_multiple is below, think about why it works.
def is_multiple(m, n):
    return m % n == 0

Since functions are so easily replicable, we can include them in loops if we want. For instance, our is_multiple function can be used to check if a number is prime! See for yourself by testing some possible prime numbers in the cell below.
Can you write Python code for it?

# Change possible_prime to any integer to test its primality
# NOTE: If you happen to stumble across a large (> 8 digits) prime number, the cell could take a very, very long time
# to run and will likely crash your kernel. Just click kernel>interrupt if it looks like it's caught.

possible_prime = 9999991

for i in range(2, possible_prime):
    if (is_multiple(possible_prime, i)):
        print(possible_prime, 'is not prime')
        break
    if (i >= possible_prime/2):
        print(possible_prime, 'is prime')
        break


I am working on the file "Intro/Intro.ipynb".
The first task is:
Slicing lists
As you can see from above, lists do not have to be made up of elements of the same kind. Indices do not have to be taken one at a time, either. Instead, we can take a slice of indices and return the elements at those indices as a separate list.
Can you write Python code for it?

### This line will store the first (inclusive) through fourth (exclusive) elements of lst as a new list called lst_2:
lst_2 = lst[1:4]

lst_2


I am working on the file "Intro/Intro.ipynb".
The first task is:
The most important line in the above cell is the "for element in..." line. This statement sets the structure of our  loop, instructing the machine to stop at every number in example_array, perform the indicated operations, and then move on. Once Python has stopped at every element in example_array, the loop is completed and the final line, which outputs new_list, is executed. It's important to note that "element" is an arbitrary variable name used to represent whichever index value the loop is currently operating on. We can change the variable name to whatever we want and achieve the same result, as long as we stay consistent. For example:
Can you write Python code for it?

newer_list = []

for completely_arbitrary_name in example_array:
    newer_element = completely_arbitrary_name + 5
    newer_list.append(newer_element)

newer_list


I am working on the file "Intro/Intro.ipynb".
The first task is:
The pd.read_csv function expects a path to a .csv file as its input, and will return a data table created from the data contained in the csv.
We have provided vots.csv in the data directory, which is all contained in the current working directory (aka the folder this assignment is contained in). For this reason, we must specify to the read_csv function that it should look for the csv in the data directory, and the / indicates that vots.csv can be found there.
Here is a sample of some of the rows in this datatable:
Can you write Python code for it?


I am working on the file "Intro/Intro.ipynb".
The first task is:
The vot table is being indexed by the condition vot["height"] >= 170, which returns a table where only rows that have a "height" greater than $170$ is returned. We then index this table with the double bracket notation from the previous section to only get the closure and vot columns.
Suppose now we wanted a table with data from English speakers, and where the t VOT was less than .01 or k VOT is greater than .08.
Can you write Python code for it?


I am working on the file "Intro/Intro.ipynb".
The first task is:
This behavior differs from that of a list. See below what happens if you multiply a list.
Can you write Python code for it?


I am working on the file "Intro/Intro.ipynb".
The first task is:
Though matplotlib is sometimes considered an "ugly" plotting tool, it is powerful. It is highly customizable and is the foundation for most Python plotting libraries. Check out the documentation to get a sense of all of the things you can do with it, which extend far beyond scatter and line plots. An arguably more attractive package is seaborn, which we will go over in future notebooks.
Question 5: Plotting
Try plotting two of the closure columns against one another.
Can you write Python code for it?

pclo = ...
kclo = ...

plt.scatter(pclo, kclo)
plt.xlabel(...)
plt.ylabel(...)

# note: plt.show() is the equivalent of print, but for graphs
plt.show()

ok.grade('q05')


I am working on the file "Intro/Intro.ipynb".
The first task is:
To access a value in the list, put the index of the item you wish to access in brackets following the variable that stores the list. Lists in Python are zero-indexed, so the indicies for lst are 0, 1, 2, 3, 4, 5, and 6.
Can you write Python code for it?

# Elements are selected like this:
example = lst[2]

# The above line selects the 3rd element of lst (list indices are 0-offset) and sets it to a variable named example.
print(example)


I am working on the file "Intro/Intro.ipynb".
The first task is:
To find the maximum value, call max().
Can you write Python code for it?


I am working on the file "Intro/Intro.ipynb".
The first task is:
Understanding Check 2 Change the cell above so that it prints out:
First this line,
then the whole 🌏,
and then this one.

Writing Jupyter notebooks
You can use Jupyter notebooks for your own projects or documents.  When you make your own notebook, you'll need to create your own cells for text and code.
To add a cell, click the + button in the menu bar.  It'll start out as a text cell.  You can change it to a code cell by clicking inside it so it's highlighted, clicking the drop-down box next to the restart (⟳) button in the menu bar, and choosing "Code".
Understanding Check 3 Add a code cell below this one.  Write code in it that prints out:
A whole new cell! ♪🌏♪

(That musical note symbol is like the Earth symbol.  Its long-form name is \N{EIGHTH NOTE}.)
Run your cell to verify that it works.
Errors
Python is a language, and like natural human languages, it has rules.  It differs from natural language in two important ways:
1. The rules are simple.  You can learn most of them in a few weeks and gain reasonable proficiency with the language in a semester.
2. The rules are rigid.  If you're proficient in a natural language, you can understand a non-proficient speaker, glossing over small mistakes.  A computer running Python code is not smart enough to do that.
Whenever you write code, you'll make mistakes.  When you run a code cell that has errors, Python will sometimes produce error messages to tell you what you did wrong.
Errors are okay; even experienced programmers make many errors.  When you make an error, you just have to find the source of the problem, fix it, and move on.
We have made an error in the next cell.  Run it and see what happens.
Can you write Python code for it?


I am working on the file "Intro/Intro.ipynb".
The first task is:
You should see something like this (minus our annotations):

The last line of the error output attempts to tell you what went wrong.  The syntax of a language is its structure, and this SyntaxError tells you that you have created an illegal structure.  "EOF" means "end of file," so the message is saying Python expected you to write something more (in this case, a right parenthesis) before finishing the cell.
There's a lot of terminology in programming languages, but you don't need to know it all in order to program effectively. If you see a cryptic message like this, you can often get by without deciphering it.  (Of course, if you're frustrated, feel free to ask a friend or post on the class Piazza.)
Understanding Check 4 Try to fix the code above so that you can run the cell and see the intended message instead of an error.
Introduction to Programming Concepts
Run the cell below, which will allow you to check if your solutions to some of the coming exercises are correct.
Can you write Python code for it?

from client.api.notebook import Notebook
ok = Notebook('Intro.ok')


I am working on the file "Intro/Intro.ipynb".
The first task is:
You will notice that only the last line in a cell gets printed out. If you want to see the values of previous expressions, you need to call print on that expression. Try adding print statements to some of the above expressions to get them to display.
B. Variables
In the example below, a and b are Python objects known as variables. We are giving an object (in this case, an integer and a float, two Python data types) a name that we can store for later use. To use that value, we can simply type the name that we stored the value as. Variables are stored within the notebook's environment, meaning stored variable values carry over from cell to cell.
Can you write Python code for it?


I am working on the file "IntroDBWorkshop.ipynb".
The first task is:
A note on the syntax above - since I like to write clean, well-indented SQL; I used a string concatentation method in Python, by opening up a parentheses and adding strings without commas on new lines between them. If you print sql you'll see it's just one long string with spaces inside of it. You could also use docstrings with the three quotes """ to write a multiline string, or even read in the SQL from a file.
Now write and execute the contacts table create statement.
Can you write Python code for it?


I am working on the file "IntroDBWorkshop.ipynb".
The first task is:
Consider the following questions while you're working with the database:

What errors have you been receiving? What would you do to prevent them (e.g. create guarentees)?
What happens if you pass in a name that doesn't exist in the database to our company select?
What happens if you run the insert statements twice in a row with the same values?
How would you ensure that company name, and email are unique? What other fields should be unique?

Workshop
At this point, you should be able to create a small Python application that does the following:

Inserts contacts into the database by gathering their name, email, and company name
Prints out a list of companies and the number of contacts associated with each

Using the following incomplete code as a template to help you design your project:
Can you write Python code for it?

import os
import sqlite3

def create_tables(conn):
    """
    Write your CREATE TABLE statements in this function and execute
    them with the passed in connection.
    """
    # TODO: fill in.
    pass

def connect(path="people.db", syncdb=False):
    """
    Connects to the database and ensures there are tables.
    """

    # Check if the SQLite file exists, if not create it.
    if not os.path.exists(path):
        syncdb=True

    # Connect to the sqlite database
    conn = sqlite3.connect(path)
    if syncdb:
        create_tables(conn)

    return conn

def insert(name, email, company, conn=None):
    if not conn: conn = connect()

    # Attempt to select company by name first.

    # If not exists, insert and select new id.

    # Insert contact

if __name__ == "__main__":
    name    = raw_input("Enter name: ")
    email   = raw_input("Enter email: ")
    company = raw_input("Enter company: ")

    conn = connect()
    insert(name, email, company, conn)

    # Change below to count contacts per company!
    contacts = conn.execute("SELECT count(id) FROM contacts").fetchone()
    print "There are now {} contacts".format(*contacts)

    conn.close()


I am working on the file "IntroDBWorkshop.ipynb".
The first task is:
Here we've created essentially a SQL template for inserting the names of companies into the table. Note that we don't have to assign an id, since it will be automatically assigned using the AUTOINCREMENT property of that field.
The ? is a parameter to the query, and can be used as a placeholder for any user input. Values for the parameters are then passed to the second argument of the execute method as a tuple. You should not use string formatting methods like:
python
sql = "INSERT INTO companies (name) VALUES ({})".format("Georgetown University")
This is potentially unsafe behavior, and the ? parameters do a lot of work on your behalf to make sure things work correctly and securely.
Let's go ahead and insert another record using the same sql statement.
Can you write Python code for it?

cursor.execute(sql, ("US Department of Commerce",))
conn.commit()


I am working on the file "IntroDBWorkshop.ipynb".
The first task is:
The last thing we should mention is the commit call. Nothing will be written to the database until commit is called. This gives us an interesting ability to do transactions - a series of SQL queries that when completed together succesfully, we commit them. However if something goes wrong during execution, we don't commit and therefore "rollback".
Selecting Records
Before we go on to insert contact information, we need to know the id of the company of the contact we're inserting. However, because we inserted the data using the auto increment feature, we don't know what the company's ids are. To read them, we'll have to fetch them as follows:
Can you write Python code for it?

cursor.execute("SELECT id FROM companies WHERE name=?", ("Georgetown University",))
print cursor.fetchone()


I am working on the file "IntroDBWorkshop.ipynb".
The first task is:
Using the same parameter statement in our predicate clause and passing in the tuple containing "Georgetown University" as an argument to execute, we can select the id that we need, which is returned as a Row. Rows present themselves as tuples, and since we only fetched the ID, it is the first element in the record. Note that we can use SELECT * to select all fields in a record if so desired.
The fetchone statement goes and gets the first record it finds. Note that the name of companies are not constrained uniqueness, therefore there could be multiple "Georgetown University" records fetched from this query. If you wanted all of them, you would use fetchall.
Now to insert a person who works for Georgetown University you would write a statement similar to:
python
sql = "INSERT INTO contacts (name, email, company_id) VALUES (?,?,?)"
cursor.execute(sql, ("Benjamin Bengfort", "bb830@georgetown.edu", 1))
conn.commit()
And at this point you should start inserting some contacts and companies.
Can you write Python code for it?

# Insert some contacts and companies using the methods described above.


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
Apply
You can also pass a function along an axis with the apply method.
This method is very efficient to iterate along the axis, much faster than a for loop for example.
Can you write Python code for it?


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
Create a dataframe
A dataframe is a two-dimensional labeled array.
 There is various way to create a dataframe. for more information, you can have a look at: http://pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe
from a dictionnary of list
Can you write Python code for it?


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
Get some rows
Can you write Python code for it?


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
Lets have a look at the amazing capabilities of the "read_csv" method!
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html
This method provide so many options that you will probably never need to perform extra programing in order to read your data! (This is really timesaving for data analysis tasks)
Can you write Python code for it?


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
Merge data structures
Pandas provide different methods to merge Series, Dataframe and Paneis:

Merge
join
concat
append

Let's merge our dataframe with the observed temperature from external file.
Can you write Python code for it?

Path = "./data/"
filename = "temperature.txt"

df_temp = pd.read_csv(Path+filename, index_col =0, parse_dates=True)


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
Pivot Table
Create pivot table spread-sheet like with multi-index.

data: A DataFrame object
values: a column or a list of columns to aggregate
index: a column, Grouper, array which has the same length as data, or list of them. Keys to group by on the pivot table index. If an array is passed, it is being used as the same manner as column values.
columns: a column, Grouper, array which has the same length as data, or list of them. Keys to group by on the pivot table column. If an array is passed, it is being used as the same manner as column values.
aggfunc: function to use for aggregation, defaulting to numpy.mean

In the following example we want to restructure the dataframe to see the average volume and precipitation by month and year.
Can you write Python code for it?

df['month'] = df.index.month
df['year']= df.index.year

pt = pd.pivot_table(df, values=['volume (%)', "rain (mm/dia)"], index=['month','year'])


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
Read (real) data from file.
Let's explore hydrometeorological data observed in the region of the Cantareira sistema (The main reservoir of the São Paulo megacity), more precisely the accumulated rainfall (mm/day) and the volume of water in (%) (100% correspond to the maximum capacity).
Note: In this example, the index of our dataframe is going to be a datetime object, as time series are recurrently used in atmospheric sciences. However, the index could be whatever labeled object, for example the ID of the stars for applications in astronomy.
Can you write Python code for it?

Path = './data/'
filename = "DataCantareira.csv"


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
Resample
df temp does not have the same time frequency. Therfore, It is necessary to resample this dataframe by day prior to merge.
Can you write Python code for it?

df_temp_day = df_temp.resample('D', how='mean')


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
Selection
You can select items in your Pandas data structure in the same style than numpy array.
However, Pandas has his own optimized methods for data access:

.at
.iat
.loc
.iloc
.ix

Opa Numpy style
Get a column from the dataframe
Can you write Python code for it?


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
Selection by Position
Select via the position of the passed integers
Can you write Python code for it?


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
Setting
Adding a new column
Can you write Python code for it?

# df['rain (m/dia)'] = df['rain (mm/dia)']/1000


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
The new datastructure has a multi-index along the axis 0. Pandas object can handle multiple index on multiple axis.
Can you write Python code for it?


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
Time Series / Date functionality
Pandas provide powerful tools to work with time series based upon numpy datetime64 and timedelta64 dtypes.
Can you write Python code for it?


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
To create a temporal serie
Can you write Python code for it?


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
To fill NaN with a value
Can you write Python code for it?


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
To interpolate where their is missing data
Can you write Python code for it?


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
Visualising the data structure
Methods to quickly give a look at your data:

head
tail
columns
index
values
describe
Can you write Python code for it?

# df.describe()

# df.columns

# df.index


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
We can verify that the index is a datetime index with the folowing code
Can you write Python code for it?


I am working on the file "IntroPandas/Intro-Pandas-PBCII.ipynb".
The first task is:
Writing data structure to file
Can you write Python code for it?


I am working on the file "IntroToPython.ipynb".
The first task is:
Array Math
Just as with spreadsheets, you can do math on entire arrays using operators (+,-,*, /, and **) and numpy functions.
Try doing some math on your arrays x1, x2, and x3.
Can you write Python code for it?


I am working on the file "IntroToPython.ipynb".
The first task is:
Astronomy-specific Example
Now we get into a really specific case. Astropy is a collection of several packages that are very useful for astronomers. It is actively developed and has new stuff all the time. Here, we show how you can use the cosmology calculator to compute the age of the Universe given a redshift.
Can you write Python code for it?

# The astropy package has all kinds of astronomy related routines.
# Here, we define a cosmology that allows to compute things like
# the age of the universe or Hubble constant at different redshifts

cosmo = FlatLambdaCDM(H0=70., Om0=0.3)
redshift=0.
print(cosmo.age(redshift))

redshift=[0,1,2,3]
print(cosmo.age(redshift))


I am working on the file "IntroToPython.ipynb".
The first task is:
Beware, though, that unlike for loops, you can have a never-ending loop if the condition is never false. Your computer is happy to keep grinding away forever. How could you safeguard against this?
If blocks
if statements act as a way to carry out a task only if a certain condition is met. As with the for loops above, everything that is indented after the if statement is part of the block of code that is executed if the condition is met. Likewise for the else block.
Can you write Python code for it?

x=5
if x==5:
    print('Yes! x is 5')

# The two equal signs evaluate whether x is equal to 5.  One can also use >, >=, <, <=, != (not equal to)


I am working on the file "IntroToPython.ipynb".
The first task is:
Binning Data
Another common statistical task is binning data. This is often done on noisy spectra to make "real" features more visible. In the following example, we are going to bin galaxies based on their redshift and compute the mean stellar mass for each bin.
Can you write Python code for it?

# This is an example of binning data and computing a particular value for the data in each bin.
# The scipy package is used to carry out this task.

# Lets make some fake data of galaxies spanning random redshifts between 0<z<3:
z=npr.rand(10000)*3.
# And these galaxies have random stellar masses between 9<log(M/Msun)<12:
m=npr.rand(10000)*3.+9.

# Now we want to compute the median stellar mass for galaxies at 0<z<1, 1<z<2, and 2<z<3:
# So lets declalre the bin edges
bins=[0.,1.,2.,3.]

m_med,xbins,btemp = stats.binned_statistic(z,m,statistic='median',bins=bins)
print(bins)
print(m_med)


I am working on the file "IntroToPython.ipynb".
The first task is:
Exercise
Try this. Use a while loop and an if-else block to generate the Collatz sequence. Start the list with any positive integer. To get the next element of the list, check if the current element is even or odd. If even, divide it by 2. If odd, multiply by 3 and add 1. The sequence ends if you get to 1. Print out the length of the list. The Collatz conjecture states that the sequence will always convert to 1 eventually regardless of the starting integer. The proof of this conjecture is one of the great unsolved problems in mathematics.
Functions
Functions allow you to make a bundle of python statements that are executed whenever the function is called. Each function has arguments you pass in and value(s) that are returned. For example, you've been using the print function.  There are also some functions above that you have been using. Now we will make our own.
Can you write Python code for it?

# the function 'myfunc' takes two numbers, x and y, adds them together and returns the results
def myfunc(x,y):
    z=x+y
    return z

# to call the function, we simply invoke the name and feed it the requisite inputs:
g=myfunc(2,3.)
print(g)

# you can set input parameters to have a default values
def myfunc2(x,y=5.):
    z=x+y
    return z

g=myfunc2(2.)
print(g)

g=myfunc2(2.,4.)
print(g)


I am working on the file "IntroToPython.ipynb".
The first task is:
File access with open
In order to access the contents of an existing file, or write contents into a new file, you use the open function. This will return a file object that you can use to read or write data. Here are some common tasks. First, let's write data to a file:
Can you write Python code for it?

# Open a file for writing (note the 'w')
f = open('a_test_file', 'w')
# write a header, always a good idea!
f.write("# This is a test data file. It contains 3 columns\n")
for i in range(10):
    f.write("%d %d %d\n" % (i, 2*i, 3*i))
# We need to "close" the file to make sure it is written to disk
f.close()


I am working on the file "IntroToPython.ipynb".
The first task is:
Importing Python packages: examples
One of the major advantages of python is a wealth of specialized packages for doing common scientific tasks. Sure, you could write your own least-squares fitter using what we've shown you so far, but before you attempt anything like that, take a little time to "google that" and see if a solution exists already.
You will have to import Python modules/packages to carry out many of the tasks you will need for your research.  As already discussed, numpy is probably the most useful.  scipy and astropy are other popular packages.  Lets play around with a few of these to give you an idea of how useful they can be.
Random Numbers
Can you write Python code for it?

# I like to declare all of my imported packages at the top of my script so that I know what is available.
# Also note that there are many ways to import packages.

import numpy.random as npr                   # Random number generator
from scipy import stats                      # statistics functions
import scipy.interpolate as si               # interpolation functions
from astropy.cosmology import FlatLambdaCDM  # Cosmology in flat \Lambda-CDM universe

# Random numbers are useful for many tasks.

# draw 5 random numbers from a uniform distribution between 0 and 1:
x1=npr.uniform(0, 1, size=5)
print(x1)

# draw 5 random numbers from a normal distribution with mean 10 and standard
#  deviation 0.5:
x2=npr.normal(10, 0.5, size=5)
print(x2)

# draw 10 random integers between 0 and 5(exclusive)
x3=npr.randint(0,5,10)
print(x3)


I am working on the file "IntroToPython.ipynb".
The first task is:
Interpolation
In science, we measure discrete values of data. Sometimes you need to interpolate between two (or more) points. A common example is drawing a smooth line through the data when making a graph. You could do this by hand using numpy, but the module scipy has an entire interpolation package that offers an easy solution.
Can you write Python code for it?

# Interpolating between data points is another common task.  We'll again use scipy to do some interpolating:

x=np.arange(5.)
y=x**2

print(x)
print(y)

# Linear interpolation
f=si.interp1d(x,y,kind='linear')
# si.interp1d returns a function, f, which can be used to feed values to.
# For example, lets evaluate f(x)
print(f(x))
# And now a different value
print(f(0.5))

# We can employ a higher order interpolation scheme to get more precise results (assuming a smoothly varying function)
f=si.interp1d(x,y,kind='quadratic')
print(f(x))
print(f(0.5))


I am working on the file "IntroToPython.ipynb".
The first task is:
Lists can also contain a mixture of different types (even lists). Basically, anything can be an element of a list. Try making a list of strings, floats, and the list x1 above. Print it out to see what it looks like. Can you guess how to refer to an element of a list that's in another list?
Numpy arrays allow for more functionality than lists.  While they may also contain a mix of object types, you will primarily be working with numpy arrays that are comprised of numbers: either integers or floats.  For example, you will at some point read in a table of data into a numpy array and do things to it, like add, multiply, etc.
Above, we imported the numpy module as np.  We will use this to create arrays.
Can you write Python code for it?


I am working on the file "IntroToPython.ipynb".
The first task is:
Notice that after the line containing the for statement, the subsequent lines are indented to indicate that they are all part of the same block. Every line that shares the same indenting will be repeated 5 times.
You can use a for loop to build up a list of elements by appending to an existing list (using its append() member function). For example, to create the first N elements of the Fibonacci sequence:
Can you write Python code for it?

fib = [1,1]
N = 100
for i in range(N-2):
    fib.append(fib[-2]+fib[-1])
print (fib)


I am working on the file "IntroToPython.ipynb".
The first task is:
Now you can later use this value. Try printing out x using the print() function. You can also modify the variable. Try adding or subtracting from x and print it out its value again.
Can you write Python code for it?


I am working on the file "IntroToPython.ipynb".
The first task is:
Now you try. Generate a random array of numbers drawn from a Poisson distribution with expectation value 10. Check that the mean of the array is approximately 10 and the standard deviation is approximately sqrt(10).
Here is a practical example of using random numbers. Often in statistics, we have to compute the mean of some population based on a limited sample. For example, a survey may ask car drivers their age and make/model of car. A marketing team may want to know the average age of drivers of Ford Mustangs so they can target their audience. Calculating a mean is easy, but what about the uncertainty in that mean? You could compute the population standard deviation, but that pre-supposes the underlying distribution is Gaussian. Another method, that does not make any assumptions about the distribution is bootstrapping. Randomly remove values from the data and replace them with copies of other values. Compute a new mean. Do that N times and compute the standard deviation of these bootstrapped mean values.
Below is an example of bootstrapping a sample to determine the uncertainty on a measurement.  In this case, we will compute the mean of a sample of ages, and the uncertainty on the mean.
Can you write Python code for it?

# x below represents a measurement of the ages of N people, where N=x.size
x=np.array([19.,20.,22.,19.,21.,24.,35.,22.,21.])
# This is the mean age:
print(np.mean(x))

# Now we "bootstrap" to determine the error on this measurement:
ntrials=10000 # number of times we will draw a random sample of N ages
x_arr=np.zeros(ntrials) # store the mean of each random sample in this array

for ii in range(ntrials):
    # draw N random integers, where N equals the number of samples in x
    ix=npr.randint(0,x.size,x.size)
    # subscript the original array with these random indices to get a new sample and compute the mean
    x_arr[ii]=np.mean(x[ix])

# Finally, compute the standard deviation of the array of mean values to get the uncertainty on the *mean* age
print(np.std(x_arr))


I am working on the file "IntroToPython.ipynb".
The first task is:
One can also have a series of conditions in the form of an elif block:
Can you write Python code for it?

x=5

if x==2:
    print('Yes! x is 2')
elif x==3:
    print('Yes! x is 3')
elif x==4:
    print('Yes! x is 4')
else:
    print('x is '+str(x))


I am working on the file "IntroToPython.ipynb".
The first task is:
Running code
There are many environments in which one can run Python code:
- iPython notebooks like this one are good for running quick snippets of code.
- Spyder (provided with Anaconda) provides a space for writing scripts, executing them, and also for easily looking up definitions of different functions. Very similar to the IDL graphical IDE.
- One can also write code in a plain text editor, like Emacs/Aquamacs.  Then execute the code in a terminal running Python or iPython.
Running code from the command-line
This is the most common and agnostic way to run your code. If you send your code to someone else, assume they will run it from the command line. If you are running your code on an HPC cluster, it needs to be run from the command-line. Lastly, writing code that runs with minimal user-interaction makes it more repeatable.
There are two aspects of writing command-line code that you should be familiar with: 1) getting arguments from the command line; and 2) working with files. The first is done through the sys.argv variable, the second is done with os.path package. Here we look at each briefly.
sys.argv
Quite simply, this is a list of the command-line arguments. You've seen several unix commands. Suppose we wanted to write the equivalent of the cp command, but using a python script. Usually, you run the command like this from the command-line:
cp file1 file2

That would copy file1 to file2. So our python script will need to get both the source and destination file name. Here is how I would write a simple script to do the same thing as cp:
import sys
f1 = sys.argv[1]
f2 = sys.argv[2]
print ("copying %s to %s" % (f1,f2))

# Here would be the code to actually copy one file to the other

Note: you can try to print out sys.argv on the next command-block. It will show you how this ipython notebook was actually run. But it is not very useful for doing anything practical.
With more complicated code, your command-line arguments may also get rather complicated (you may have optional arguments, switches, etc). There...
Can you write Python code for it?


I am working on the file "IntroToPython.ipynb".
The first task is:
Take a guess at how to create a 5-element array of ones.
Now suppose you want all the elements to be equal to np.pi. How could you do that as a one-liner. Hint:  vectorize.
We can create a sequence of numbers using np.arange(start,stop,step), where you specify a number to start (inclusive), when to stop (non-inclusive), and what step size to have between each element. Make an array called x1 using arange that goes from 0 to 4 inclusive.
Now make an array called x2 that goes from 0 to 10 in steps of 2.
Another handy function is np.linspace(start,stop,N), which gives you a specified number N of elements equally spaced between start and stop. The stop value in this case is inclusive (will be part of the sequence). Try making an array called x3 that goes from 0 to 8 and has 5 elements.
One can also create N-dimensional numpy arrays.  For example, images that you read into Python will typically be stored into 2D numpy arrays.
Can you write Python code for it?


I am working on the file "IntroToPython.ipynb".
The first task is:
The os module gives you functions relating to the operating system, sys gives you information on the python interpreter and your script, and numpy is the mathematical powerhorse of python, allowing you to manipulate arrays of numbers.
Once you import a module, all the functions and data for that module live in its namespace. So if I wanted to use the getcwd function from the os module, I would have to refer to it as os.getcwd. Try running getcwd all by itself; you'll get an error. Then do it the correct way:
As you can probably guess, os.getcwd gets the current working directory. Try some commands for yourself. The numpy module has many mathematical functions. Try computing the square root (sqrt) of 2. You can also try computing $\sin(\pi)$ (numpy has a built-in value numpy.pi) and even $e^{i\pi}$ (python uses the engineering convention of 1j as $\sqrt{-1}$).
There can also be namespaces inside namespaces. For example, to test if a file exists, you would want to use the isfile() function within the path module, which is itself in the os module. Give it a try:
Here is a non-comprehensive list of modules you may find useful. Documentation for all of them can be found with a quick google search.

os, sys: As mentioned above, these give you access to the operating system, files, and environment.
numpy: Gives you arrays (vectors, matrices) and the ability to do math on them.
scipy: Think of this as "Numerical Recipes for Python". Root-finding, fitting functions, integration, special                mathematical functions, etc.
pandas: Primarily used for reading/writing data tables. Useful for data wrangling.
astropy: Astronomy-related functions, including FITS image reading, astrometry, and cosmological calculations.

Getting Help
Almost everything in python has documentation. Just use the help() function on anything in python to get information. Try running help() on a function you used previously. Try as many others as you like.
Variables and Types
A variable is like a box that you put stuff (values) in for later...
Can you write Python code for it?


I am working on the file "IntroToPython.ipynb".
The first task is:
The other common matrix tasks can also be done with either numpy functions, numpy.linalg, or member functions of the object itself
Can you write Python code for it?

# Taking the transpose
x_t = x.T
print(x_t)

# Computing the inverse
x_i = np.linalg.inv(x)

# Matrix Multiplication
I = np.dot(x_i,x)
print (I)


I am working on the file "IntroToPython.ipynb".
The first task is:
The reverse of reshape is ravel, which flattens a multi-dimensional array into a 1D array.  Try this on x.
Control blocks
So far, we've been running individual sets of commands and looking at their results immediately. Later, we will write a complete program, which is really just bundling up instructions into a recipe for solving a given task. But as the tasks we want to perform become more complicated, we need control blocks. These allow us to:

Repeat tasks again and again (loops)
Perform tasks only if certain conditions are met (if-else blocks)
Group instructions into a single logical task (user-defined functions)

python is rather unique in that it uses indenting to indicate the beginning and end of a logical block. This actually forces you to write readable code, which is a really good thing!
For loops
for loops are useful for repeating a series of operations a given number of times. In python, you loop over elements of a list or array. So if you want to loop over a sequence of integers (say, the indices of an array), then you would use the range() function to generate the list of integers. You might also use the len() function if you need the length of the array.
Can you write Python code for it?

# range(n) creates a list of n elements
print(range(5))

# We can use it to iterate over a for loop
for ii in range(5):
    print(ii)


I am working on the file "IntroToPython.ipynb".
The first task is:
Try making N in the above code block bigger and see how the execution time goes up. Now do the exact same thing in the next code block, but use numpy functions without a loop. See how the execution time improves.
While loops
Similar to a for-loop, a while-loop executes the same code block repeatedly until a condition is no longer true. These are handy if you don't know ahead of time how long a loop will take, but you know you have to stop when a condition is true (or false). As an example, we can estimate the smallest floating point number (called machine-$\epsilon$) by continually dividing by 2 until we get zero.
Can you write Python code for it?


I am working on the file "IntroToPython.ipynb".
The first task is:
Use print() to have a look at the data we read in. Note that the rows will include the newline character (\n). You can use the string.strip() function to get rid of it.
In the next tutorial (visualization), we'll show you a better way to read in standard format data files like this. But sometimes you'll be faced with data that these more automatic functions can't handle, so it's good to know how to read it in by hand.
Debugging
You almost never write a script without introducing bugs (the term comes from when computers were mechanical machines and insects literallly interfered with the running of the code). Luckily, python gives a very nice "traceback" report when it encounters a problem with what you've written. let's just generate a mistake on purpose and see what happens.
Can you write Python code for it?

x1=np.arange(5)
x2=np.arange(3)
print(x1)
print(x2)
print(x1+x2)


I am working on the file "IntroToPython.ipynb".
The first task is:
We can also "slice" N-dimensional arrays. Note that we have used reshape to transform a 1D array into a 2D array with the same total number of elements. This is another handy way to create N-dimensional arrays.
Can you write Python code for it?

x=np.arange(8)
print(x)
x=x.reshape((4,2))
print(x)

print(x[0,:])
print(x[:,0])


I am working on the file "IntroToPython.ipynb".
The first task is:
We can use the len() function to determine the length of the string. Try this out below.
Strings are very similar to lists in python (we'll see that below). Each alpha-numeric character is an element in the string, and you can refer to individual elements (characters) using an index enclosed in square brackets ([]). You can also specify a range of indices by separating them with a colon (:). Python indexes from 0 (not 1), so the first element is index [0], the second [1] and so on. Negative indices count from the end of the string. Try printing out the 2nd character of your string s, then the whole string except for the first and last characters.
Specifying a range of indices (as well as more complicated indexing we'll see later) is called slicing. There is also a string module that contains many functions for manipulating strings.
Formatting strings
Sometimes you'll need your integers and floats to be converted into strings and written out into a table with specific formats (e.g., number of significant figures). This involves a syntax that's almost a separate language itself (though if you've used C or C++ it will be very familiar). Here is a good reference: https://pyformat.info/
We'll cover the most important. First, if you just print out a regular floating point number, you get some arbitrary number of significant figures. The same is true if you just try to convert the float to a string using str(), which takes any type of variable and tries to turn it into a string. Try printing the string value of np.pi.
If you only want two significant figures, or you want the whole number of span 15 spaces (to make nicely lined-up colums for a table), you need to use a format string. A format string is just like a regular string, but has special place-holders for your numbers. These are enclosed in curly brackets ({}) and have special codes to specify how to format the variable. Without any other information, a simple {} will be replaced with whatever str() produces for the variable. For more control over numerical...
Can you write Python code for it?

fmt = "This is a float with two decimals:  {:.2f}"
print (fmt)


I am working on the file "IntroToPython.ipynb".
The first task is:
What happens if you try to add x1 to the matrix x you created above. Give it a try:
We also have access to most mathematical functions you'll need. Try raising an array to 3rd power using np.power. Take the base-10 log of an array and make sure it gives you what you expect.  A shorthand for raising to a power is **, for example, 2**3=8.
Matrix Math
numpy can treat 2D arrays as matrices and leverage special compiled libraries so that you can do linear algebra routines easily (and very fast). Here, we construct a rotation matrix and use dot do do the dot-product. Try changing theta to different angles (in radians!).
Can you write Python code for it?

# CCW rotation by 180 degrees
theta=np.pi/4
# Rotation matrix
x=np.array([[np.cos(theta),-np.sin(theta)],
            [np.sin(theta),np.cos(theta)]])
print(x)

# Lets rotate (1,1) about the origin
y=np.array([1.,1.])
z=np.dot(x,y)

print(y)
print(z)


I am working on the file "IntroToPython.ipynb".
The first task is:
You can also have multiple conditions that are evaluated:
Can you write Python code for it?

x=5

if x > 2 and x*2==10:
    print('x is 5')

if x > 7 or x*2 == 10:
    print('x is 5')


I am working on the file "IntroToPython.ipynb".
The first task is:
You may notice (if N is large enough) that you get numbers that have an L at the end. python has a special type called a long integer which allows for abitrarily large numbers.
Here is an example of what not to do with for loops (if you can help it). For loops are more computationally expensive in python than using the numpy functions do do the math. Always try to cast the problem in terms of numpy math and functions. It will make your code faster. Try making N in the following example larger and larger and you'll see the difference.
Can you write Python code for it?

# A slightly more complex example of a for loop:
import time
N = 100
x=np.arange(N)
y=np.zeros(N)

t1 = time.time()   # start time
for ii in range(x.size):
    y[ii]=x[ii]**2
t2 = time.time()   # end time

print ("for loop took "+str(t2-t1)+" seconds")


I am working on the file "IntroToPython.ipynb".
The first task is:
You may want to make a format string with more than one number or string. You can do this by specifying multiple format codes and inject an equal number of arguments to the .format() function.
Can you write Python code for it?

fmt = "Here is a float: '{:.2f}', and another '{:8.4f}', an integer {:d}, and a string {}"
print (fmt.format(x, np.pi, 1000, 'look ma, no quotes!'))


I am working on the file "IntroToPython.ipynb".
The first task is:
You will find that python is full of nerdy humour like this. By the way, python is named after Monty Python's Flying Circus, not the reptile. Let's go ahead and import some useful modules you will need. Use this next command cell to import the modules named os, sys, and numpy. You can import them one-by-one or all at once, separated by commas.
Can you write Python code for it?

# Use this command box to run your own commands.
# By the way, Python ignores anything after a hash (#) symbol, so good for comments


I am working on the file "Intro_2.ipynb".
The first task is:
An extremely useful pre-defined function is "range":
Can you write Python code for it?


I am working on the file "Intro_2.ipynb".
The first task is:
Indexing is pretty flexible
Can you write Python code for it?

print myarray[3:5]  #3 to 5, 5 excluded
print myarray[3:]   #3 onwards
print myarray[:4]    #Until 4, i.e. 0,1,2,3
print myarray[:-2]   #Until two from the end, i.e. exclude the last two

indexing_list=[3,4,5]
print myarray[indexing_list]

print np.ones(4)

boolean_indexing=np.ones_like(myarray)
print boolean_indexing

boolean_indexing[[4,5,6]]=False
print boolean_indexing.astype('bool')

print np.arange(myarray.shape[0])[boolean_indexing.astype('bool')]

print np.ones((3,4))

y,x=np.mgrid[:4,:5]
print x

print y

print x**2+y**2


I am working on the file "Intro_2.ipynb".
The first task is:
Lists in python
Lists are some of the most useful data structures in basic python. You can put anything you want in a list, you can change individual cells, elements can be lists themselves too.
Can you write Python code for it?

mylist=[1,2,3,'a',True,[5,6]]

print mylist
print mylist[5]


I am working on the file "Intro_2.ipynb".
The first task is:
Numpy array and indexing
You can create a numpy array a couple of different ways. One of them is to use np.arange, the equivalent of range() we saw for lists, but also creates floats
The BEST thing about numpy arrays, is that you can do math on them treating them as single numbers, and it will do the operations to EACH element by default!
Can you write Python code for it?

print np.arange(10)
print np.arange(5,10)

print np.arange(1.5,4,.5)  #start, end (excluded) and step
print np.arange(3,4,.1)

print 1/np.arange(0,10.5,.5)


I am working on the file "Intro_4.ipynb".
The first task is:
After you've enabled pdb, type help to show  available commands. Some commands are e.g. step, quit, restart.
If you have set pdb on before an exception is triggered, (I)python can call the interactive pdb debugger after the traceback printout.
If you want to activate the debugger AFTER an exception is caught/fired, without having to rerun your code, you can use the %debug magic (or debug in ipython).
If you are running some python scripts, where instead of running code line by line you want to run a large chunk of code before checking the variables or stepping through the code line-by-line, it's useful to use import pdb; pdb.set_trace().
Go to ipython or terminal and execute pdb1.py to see how it is used in practice inside python scripts.
If you know where you want to exit the code a priori, you can use sys.exit().
Can you write Python code for it?

import sys

a = [1,2,3]
print a
sys.exit()

b = 'hahaha'
print b


I am working on the file "Intro_4.ipynb".
The first task is:
As we know, 5/10 - 10 = -9.5 and not -10, so something must be wrong inside the function. In this simple example, it may be super obvious that we are dividing an integer with an integer, and will get back an integer. (Division between integers is defined as returning the integer part of the result, throwing away the remainder. The same division operator does real division when operating with floats, very confusing, right?).
But this is good enough to show why simple print statements will suffice in some cases.
Ok, assuming that we didn't know what the problem was, we will use print to find out what went wrong.
Can you write Python code for it?

def blah(a, b):
    c = 10
    print "a: ", a
    print "b: ", b
    print "c: ", c
    print "a/b = %d/%d = %f" %(a,b,a/b)
    print "output:", a/b - c
    return a/b - c

blah(aa, bb)


I am working on the file "Intro_4.ipynb".
The first task is:
Catching Errors
Some common types of errors:

NameError:
undefined variables

Logic error:
harder to debug
usually associate with the equation missing something

IOError
TypeError
Can you write Python code for it?

# Way to handle errors inside scripts

try:
    # what we want the code to do
except: # when the above lines generate errors, will immediately jump to exception handler here, not finishing all the lines in try
    # Do something else

# Some Example usage of try…except:
# use default behavior if encounter IOError
try:
     import astropy
except ImportError:
     print("Astropy not installed...")

# Slightly more complex:
# Try, raise, except, else, finally
try:
     print ('blah')
     raise ValueError()            # throws an error
except ValueError, Err:            # only catches 0 division errors
     print ("We caught an error! ")
else:
     print  ("here, if it didn't go through except...no errors are caught")
finally:
     print ("literally, finally... Useful for cleaning files, or closing files.")

# If we didn't have an error...
#
try:
     print ('blah')
#      raise ValueError()            # throws an error
except ValueError, Err:              # only catches 0 division errors
     print ("We caught an error! ")
else:
     print  ("here, if it didn't go through except... no errors are caught")
finally:
     print ("literally, finally... Useful for cleaning files, or closing files.")


I am working on the file "Intro_4.ipynb".
The first task is:
From this, it's clear that a/b is the problem since a/b = 1/2 and not 0. And you can quickly go an fix that step. For example by using float(b), or by multiplying by 1. , the dot makes it a float.
But using print statements may be inconvenient if the code takes a long time to run,  and also you may want to chcek the values of other variables to diagnose the problem. If you use crtl+C at this point, you will lose the values stored inside all variables. Perhaps you would want to go back and put another print statement inside the code and run it again to check another variable. But this goes on... and you may have to go back many times!
Alternatively, you can use the pdb module, which is an interactive source debugger. The variables are presevered at breakpoint, and can interactively step through each line of your code.
(see more at https://pymotw.com/2/pdb/)
To use, you can enable pdb either before an Exception is caught, or after. In Jupyter notebook, pdb can be enabled with the magic command %pdb, %pdb on, %pdb 1, disabled with %pdb, %pdb off or %pdb 0.
Can you write Python code for it?


I am working on the file "Intro_IPython.ipynb".
The first task is:
In the menu we can find useful drop-down menus to perform simple tasks to edit our notebook. The best way to know them is to experiment
Most important, the kernel menu, where we can kill the execution if we run intro trouble.
The use of the notebook allows to develop code faster in an exploratory way, writing small pieces successively, thus concentrating in the implementation of the algorithm, and not in the details of programming language.
Running long codes from the notebook is however not advisable.
We sould never thus forget to refactor our code into a standalone python program, encapsulating the different pieces of the algorithms in function or modules, to allow for reusability.
Markdown essentials for documenting an Ipython notebook
Here are some examples of formatting with markdown (click in the cell to see the markdown source):
Headers:
Header 1
Header 2
Header 3
Header 6
Lists:

Red
Green
Blue

Nested lists:

Red
Strawberry, cherry
Green
Apple, pear

Blue
None
As far as I know

Ordered lists:

Bird
McHale
Parish

Code blocks:
for i in xrange(1000):
    print i

Inline code:
Use the printf() function.
Horizontal rules:

HTML links:
This is an example inline link.
Emphasis:
italics
italics
bold face
bold face
Inserting $\LaTeX$ symbols
One of the great improvements of ipython over simple markdown is that it allows to include latex expressions. This operation is performed by using MathJax to render LaTeX inside markdown.
MathJax is installed by default in the anaconda distribution. It can handle regularly complex expressions, but take into account that it is not a full full latex intepreter.
Let us see some examples:
$$c = \sqrt{a^2 + b^2}$$
$$F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx$$
\begin{eqnarray}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}}...
Can you write Python code for it?


I am working on the file "Intro_Ipython_Numpy_Scipy_solution.ipynb".
The first task is:
Bonus : Calcul symbolique avec Sympy
Can you write Python code for it?

from sympy import *
init_printing() # formatte les sorties avec Latex

X = Symbol('X')
x, y, z, t, i = symbols('x y z t i')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)

expand((X+1)*(X+2))

factor(x**2+3*x+2)

(1/cos(x)).series(x, 0, 10) # développement en série limitée autour de 0 à l'ordre 10

summation(1/2**i, (i, 0, oo)) # somme infinie de 1/2^n

integrate(exp(-y**2)*erf(y), y) # intégration

f = Function('f')
f(x).diff(x, x) + f(x)

dsolve(f(x).diff(x, x) + f(x), f(x))   # résolution d'équations différentielles

solve([x + 5*y - 2, -3*x + 6*y - 15], [x, y]) # résolution d'équation linéaire

import IPython
import sympy
from statsmodels import version as sm_version
print "Sympy version   \t:\t %s"%sympy.__version__
print "IPython version \t:\t %s"%IPython.__version__
print "numpy version   \t:\t %s"%np.__version__
print "statsmodels version \t:\t %s"%sm_version.full_version
print "matplotlib version \t:\t %s"%plt.__version__


I am working on the file "Intro_Ipython_Numpy_Scipy_solution.ipynb".
The first task is:
Création de 100 points de 0 à $2\pi$ dans le vecteur $x_4$, puis calcul de
$$y_4 = \sin(x4)$$
Can you write Python code for it?

x4 = np.linspace(0,5*np.pi,100)
y4 = np.sin(x4)

fig = plt.figure() # création d'une fenetre de graph
ax1 = fig.add_subplot(121) # graphe de gauche sur 2 graphes répartis horizontalement
ax2 = fig.add_subplot(122) # graphe de droite sur 2 graphes répartis horizontalement
ax1.plot(x4,y4)
ax2.plot(y4,x4)
ax1.set_title(r'$sin(x)$')
fig.savefig('test.png',dpi=150)


I am working on the file "Intro_Ipython_Numpy_Scipy_solution.ipynb".
The first task is:
Définition du vecteur $b$
Can you write Python code for it?

def rhs(Ti,Tg,Td,Fo):
    """
    rhs : right hand side of the equation

    Calcul le vecteur b tel que Ax = b

    Ti vecteur de solution à l'instant i
    Tg condition limite à gauche en température (scalaire)
    Td condition limite à droite en température (scalaire)
    Fo : Fourier de maille
    """
    b = copy(Ti)
    b[0]  += Fo*Tg
    b[-1] += Fo*Td
    return b