Skip to content

Instantly share code, notes, and snippets.

View erikaris's full-sized avatar

Erika Siregar erikaris

  • Old Dominion University
  • Norfolk, Virginia
View GitHub Profile
# 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()
# 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)
# 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')
"""
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!
"""
mystr = "erika"
for s in mystr:
print(s, end=" ")
it = iter(mystr)
print("\nThis is how iterator looks like:", list(it))
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
# 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
# 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)))
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))
# 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()