jpj251@nyu.edu
¶[Part 1: Python]
[Part 2: Not Python]
print("hi")
hi
float("infinity")
inf
type(float("infinity"))
float
my_list = [1,2,3,4,5]
my_list.sum()
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-95-ed142f2087a1> in <module> ----> 1 my_list.sum() AttributeError: 'list' object has no attribute 'sum'
noooooooooo
import numpy as np
my_array = np.array(my_list)
my_array.sum()
15
yayyyyyyyyyy
def how_many_letters():
return len("abcdefghijklmnopqrstuvwxyz")
...No output?
how_many_letters()
26
What does this function return?
fn_result = how_many_letters()
print(fn_result)
26
print(type(fn_result))
<class 'int'>
def say_hi():
print("hi")
say_hi()
hi
What does this function return?
hi_result = say_hi()
hi
print(hi_result)
None
print(type(hi_result))
<class 'NoneType'>
Instead...
def return_hi():
return "hi"
return_hi()
'hi'
What does this function return?
hi_result_2 = return_hi()
print(hi_result_2)
hi
print(type(hi_result_2))
<class 'str'>
I know this is a ten year old meme and that teachers using memes is one of the lamest things in the universe but... i had to do it
def triple(the_number):
return 3 * the_number
triple(5)
15
triple(0)
0
triple(triple(5))
45
Can do more than one!
def combine_names(first_name, second_name, third_name):
return first_name + ", " + second_name + ", and " + third_name
combine_names("Alice", "Bob", "Craig")
'Alice, Bob, and Craig'
if
, if-else
, if-elif-else
(really if-elif-...-elif-else
)if
Statements¶if condition:
expression(s)
(Lecture 12.3, Slide 6)
if-elif-...-elif-else
¶if condition_1:
expression_1
elif condition_2:
expression_2
...
elif condition_10:
expression_10
else:
expression_11
(Lecture 12.3, Slide 8)
while
Loops¶while condition:
expression
(Lecture 12.2, Slide 12)
(tl;dr: While $X$ is true, do $Y$)
i = 0
while i < 10:
print(i)
i = i + 1
0 1 2 3 4 5 6 7 8 9
my_list = ["Afghanistan", "Albania", "Algeria", "Yemen", "Zambia", "Zimbabwe"]
list_index = 0
while list_index < len(my_list):
current_thing = my_list[list_index]
print("Item #" + str(list_index) + " is " + str(current_thing))
list_index = list_index + 1
Item #0 is Afghanistan Item #1 is Albania Item #2 is Algeria Item #3 is Yemen Item #4 is Zambia Item #5 is Zimbabwe
That's a lot of gross-looking code...
(tl;dr: For each thing in $X$, do $Y$)
for current_thing in my_list:
print(current_thing)
Afghanistan Albania Algeria Yemen Zambia Zimbabwe
for list_index, current_thing in enumerate(my_list):
print("Element #" + str(list_index) + " is " + str(current_thing))
Element #0 is Afghanistan Element #1 is Albania Element #2 is Algeria Element #3 is Yemen Element #4 is Zambia Element #5 is Zimbabwe
import matplotlib.pyplot as plt
def plot_distribution():
die_faces = [1,2,3,4,5,6]
probabilities = [1/6,1/6,1/6,1/6,1/6,1/6]
plt.bar(die_faces, probabilities)
plt.show()
plot_distribution()
1/6
0.16666666666666666
(from https://alphaarchitect.com/2014/01/04/the-law-of-large-numbers-and-casino-earnings/)
import numpy as np
# N = 10
rolls = np.random.randint(1, 7, 10)
print(rolls)
more_rolls = np.random.randint(1, 7, 10)
print(more_rolls)
even_more_rolls = np.random.randint(1, 7, 10)
print(even_more_rolls)
[2 2 4 3 1 6 2 1 2 6] [2 6 5 5 4 5 3 4 6 6] [5 2 3 3 6 6 3 3 2 5]
plt.hist(rolls, bins=range(1,8))
plt.show()
plt.hist(more_rolls, bins=range(1,8))
plt.show()
plt.hist(even_more_rolls, bins=range(1,8))
plt.show()
rolls_100 = np.random.randint(1, 7, 100)
print(rolls_100)
more_rolls_100 = np.random.randint(1, 7, 100)
print(more_rolls_100)
even_more_rolls_100 = np.random.randint(1, 7, 100)
print(even_more_rolls_100)
[1 4 5 6 3 2 3 3 6 4 1 6 5 5 1 1 3 5 5 4 4 2 1 3 4 6 5 6 4 4 1 2 3 4 2 1 6 6 6 2 1 1 3 2 3 5 3 4 2 1 1 3 5 5 3 6 1 2 6 5 1 4 6 3 4 3 1 6 3 2 6 3 1 3 6 1 4 2 3 6 3 6 5 5 3 1 3 6 4 4 1 1 3 2 5 6 6 5 4 4] [3 4 5 5 3 6 1 4 1 1 5 4 1 6 5 3 1 4 5 4 1 3 2 1 2 4 3 6 4 2 4 4 3 4 6 5 5 2 5 1 5 5 1 5 4 6 3 2 2 6 6 3 2 2 3 5 4 1 3 1 1 6 2 6 2 6 5 1 4 5 2 6 1 3 2 1 1 4 6 6 3 5 5 1 2 4 4 1 5 2 2 3 5 6 6 4 5 2 5 4] [2 1 3 2 1 5 5 5 5 3 3 4 4 6 6 5 6 5 3 6 3 2 2 6 4 2 1 6 6 4 2 6 5 1 3 2 1 4 1 6 3 6 5 1 5 3 4 6 4 4 6 1 3 3 4 1 1 6 1 1 1 5 1 2 1 5 6 6 3 4 3 3 1 6 6 3 5 1 1 4 6 6 4 6 4 5 4 4 5 4 5 4 4 2 5 2 3 2 3 4]
plt.hist(rolls_100, bins=range(1,8))
plt.show()
plt.hist(more_rolls_100, bins=range(1,8))
plt.show()
Hmmmm... it's getting really tedious to make 2, 3, 4 rolls
variables each time... did we learn something that could help us here?
i = 0
while i < 3:
current_roll_100 = np.random.randint(1, 7, 100)
plt.hist(current_roll_100, bins=range(1,8))
plt.show()
i = i + 1
print("Done!")
Done!
<Figure size 432x288 with 0 Axes>
plt.hist(np.random.randint(1, 7, 1000), bins=range(1,8))
plt.show()
Again, getting super tired of writing this same basic code over and over again... wonder if there's ANOTHER thing we learned that could help us here?
def plot_dice_rolls(N):
rolls_N = np.random.randint(1, 7, N)
plt.hist(rolls_N, bins=range(1,8))
plt.show()
plot_dice_rolls(10000)
plot_dice_rolls(100000)
plot_dice_rolls(1000000)
plot_distribution()