This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This function uses global variable s | |
# s is only used for printing --> no problem happens | |
def f(): | |
print(s) | |
# Global scope | |
s = "Erika Siregar" | |
f() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Modified from Coursera | |
import pandas as pd | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from random import shuffle | |
origins = ['China', 'Brazil', 'India', 'USA', 'Canada', 'UK', 'Germany', 'Iraq', 'Chile', 'Mexico'] | |
shuffle(origins) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Modified from Coursera.org | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import pandas as pd | |
plt.figure() | |
data = np.random.rand(10) | |
plt.plot(data, '-o') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Matplotlib Animation Example | |
author: Jake Vanderplas | |
email: [email protected] | |
website: http://jakevdp.github.com | |
license: BSD | |
Please feel free to use and modify this, but keep the above information. Thanks! | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mystr = "erika" | |
for s in mystr: | |
print(s, end=" ") | |
it = iter(mystr) | |
print("\nThis is how iterator looks like:", list(it)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mystr = "erika" | |
for s in mystr: | |
print(s, end=" ") | |
it = iter(mystr) | |
print("\n", it) | |
print(next(it)) # the next() function returns the next value and advances the state | |
print(next(it)) # the next() function returns the next value and advances the state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Modified from Coursera | |
# how to do an animation in matplotlib: | |
# step 1: import the animation module | |
import matplotlib.animation as animation | |
import numpy as np | |
import matplotlib.pyplot as plt | |
# another way to import: | |
# from matplotlib animation import FuncAnimation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# modified from: http://jmduke.com/posts/a-gentle-introduction-to-itertools/ | |
import itertools | |
letters = ['a', 'b', 'c', 'd', 'e', 'f'] | |
booleans = [1, 0, 1, 0, 0, 1] | |
numbers = [23, 20, 44, 32, 7, 12] | |
decimals = [0.1, 0.7, 0.4, 0.4, 0.5] | |
print("before cast to a list: " + str(itertools.chain(letters, booleans, decimals))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = np.random.rand() #return a single Python float | |
b = np.random.rand(2, 3) #return a 2*3 arrays. | |
print(" a = {} \n b = {}".format(a, b)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# modified from Coursera.org | |
plt.figure() | |
Y = np.random.normal(loc=0.0, scale=1.0, size=10000) | |
X = np.random.random(size=10000) | |
plt.hist2d(X, Y, bins=25) | |
plt.colorbar() |