Skip to content

Commit 900d15f

Browse files
committed
session 1 materials
1 parent a36a0c9 commit 900d15f

9 files changed

+300
-0
lines changed

session-1/0-namesInRandomOrder.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# this is the silly script I wrote at the beginning of class
2+
# to randomize the order of names
3+
# but here I have saved it with fake names
4+
5+
# import our friend the random Library
6+
# we will use it to randomize the order of the names
7+
import random
8+
9+
# i just copy/pasted the names from a spreadsheet so they came in this big ugly text block
10+
# i used three quotes """ to designate a multi-line string
11+
theNames = """Fidela Laws
12+
Angelica Courtney
13+
Jess Bouck
14+
Carley Vallone
15+
Cameron Balmer
16+
Newton Panella
17+
Ardith Eggleston
18+
Genaro Hilson
19+
Vanetta Cockrill
20+
Valentine Mccleary
21+
Buddy Choiniere"""
22+
23+
# to convert these names into a proper python list, we will split the string every time we encounter a newline ('\n')
24+
namesList = theNames.split('\n')
25+
26+
# now that we have a list, we can put its contents in a random order using the shuffle() function, which lives in the random library
27+
random.shuffle(namesList)
28+
29+
# now that the list is randomized
30+
# we can loop through it and print each result in the random order
31+
# for each name in our list, we will assign it to the variable theName
32+
for theName in namesList:
33+
# i only want to print first names
34+
# so I will split the name by tabs, and grab the first item from that list
35+
firstName = theName.split('\t')[0]
36+
# ok let’s print the first name
37+
print(firstName)

session-1/1-helloWorld.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# use the print function to output the string "hello world"
2+
print('hello world')
3+
4+
# also works with double quotes
5+
print("hello world")

session-1/2-math.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# let’s do some math
2+
3+
print('addition')
4+
print(1 + 1)
5+
6+
print('subtraction')
7+
print(2 - 1)
8+
9+
print('multiplication')
10+
print(2 * 2)
11+
12+
print('division')
13+
# ooh tricky, the result is a floating number, not an integer anymore!
14+
print(5 / 2)
15+
16+
print("floor division")
17+
# how many times does B go into A
18+
print(5 // 2)
19+
# and let’s grab the remainder
20+
print(5 % 2)
21+
# 2 goes into 5 two times with 1 left over
22+
23+
print('order of operations')
24+
# please = parentheses
25+
# excuse = exponents
26+
# my = multiplication
27+
# dear = division
28+
# aunt = addition
29+
# sally = subtraction
30+
print(5*5+2) # this is really 25+2 = 27
31+
print(5*(5+2)) # this is really 5*7 = 35
32+

session-1/3-variables.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
print('let’s do some math with numbers')
2+
print(16 // 5)
3+
print(16 % 5)
4+
# but we had to type 16 twice and 5 twice, what a pain to change!
5+
6+
print('now let’s do the same thing with variables')
7+
8+
# we only type these numbers once
9+
bigNum = 16
10+
littleNum = 5
11+
12+
# and now we just reference them
13+
howMany = bigNum // littleNum
14+
remainder = bigNum % littleNum
15+
print( howMany )
16+
print( remainder )

session-1/4-simpleGrid.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# define the width and height of the new page
2+
# w h
3+
newPage(1000, 1000)
4+
5+
# define x and y as where we start? (0, 0) is in the bottom left
6+
x = 0
7+
y = 0
8+
9+
# define how many rows and columns
10+
rows = 10
11+
cols = 10
12+
13+
# define the width and height of each unit of the grid
14+
gridWidth = 100
15+
gridHeight = 100
16+
17+
# define the width and height of the shape we will draw in that grid
18+
shapeWidth = 100
19+
shapeHeight = 100
20+
21+
# ok it’s time to loop
22+
# first we will loop through each row
23+
# and then WITHIN that loop we will loop through each column
24+
# for theThing in theRangeOfThings
25+
for row in range(rows):
26+
# everything indented this much runs once for each row
27+
for col in range(cols):
28+
# everything indented this much
29+
# runs once for each column
30+
# within each row (one grid unit)
31+
# x y w h
32+
oval(x, y, shapeWidth, shapeHeight)
33+
x += gridWidth
34+
# we are OUTDENTED, which means we are back
35+
# to code that only runs once per row
36+
# now that we’ve reached the end of the row
37+
# we want to advance y to move us up to the next row
38+
y += gridHeight
39+
# and we want to return x to its original position
40+
# (a carriage return, essentially)
41+
x -= cols*gridWidth
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# import our special functions from the random library
2+
from random import randrange, random
3+
4+
# define the width and height of the new page
5+
# w h
6+
newPage(1000, 1000)
7+
8+
# define x and y as where we start? (0, 0) is in the bottom left
9+
x = 0
10+
y = 0
11+
12+
# define how many rows and columns
13+
rows = 10
14+
cols = 10
15+
16+
# define the width and height of each unit of the grid
17+
gridWidth = 100
18+
gridHeight = 100
19+
20+
# define the width and height of the shape we will draw in that grid
21+
shapeWidth = 100
22+
shapeHeight = 100
23+
24+
# ok it’s time to loop
25+
# first we will loop through each row
26+
# and then WITHIN that loop we will loop through each column
27+
# for theThing in theRangeOfThings
28+
29+
for row in range(rows):
30+
# everything indented this much runs once for each row
31+
for col in range(cols):
32+
# everything indented this much runs once for each grid unit
33+
34+
# before we draw our shape, let’s pick a color for it
35+
# we can create 3 variables
36+
r = random() # red
37+
g = random() # green
38+
b = random() # blue
39+
a = 1 # alpha (0 = transparent, 1 = opaque)
40+
fill(r, g, b, a)
41+
42+
# okay, now we finally draw our shape
43+
# we can make a conditional
44+
# if this condition is true:
45+
# then run this code
46+
# if not:
47+
# then run this code
48+
49+
if random() > .5:
50+
rect(x, y, shapeWidth, shapeHeight+randrange(-10, 10))
51+
else:
52+
oval(x, y, shapeWidth, shapeHeight+randrange(-10, 10))
53+
54+
# at the end of the column, advance X to the next column
55+
x += gridWidth
56+
# we are OUTDENTED, which means we are back
57+
# to code that only runs once per row
58+
# now that we’ve reached the end of the row
59+
# we want to advance y to move us up to the next row
60+
y += gridHeight
61+
# and we want to return x to its original position
62+
# (a carriage return, essentially)
63+
#
64+
x -= cols*gridWidth

session-1/6-spoilerAlert!.py

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# THIS SCRIPT GOES BEYOND WHAT WE COVERED IN CLASS
2+
# we will cover this next time, but I wanted to give you more to play with
3+
# it’s a multipage document that we will save
4+
5+
# import our special functions from the random library
6+
from random import randrange, random
7+
8+
# page dimensions
9+
pageWidth = 1000
10+
pageHeight = 1000
11+
12+
# define how many pages
13+
numPages = 10
14+
15+
# define how many rows and columns
16+
rows = 50
17+
cols = 50
18+
19+
# define the width and height of each unit of the grid
20+
gridWidth = pageWidth/rows
21+
gridHeight = pageHeight/cols
22+
23+
# define the width and height of the shape we will draw in that grid
24+
shapeWidth = pageWidth/rows
25+
shapeHeight = pageHeight/cols
26+
27+
# ok it’s time to loop
28+
# first we will loop through each row
29+
# and then WITHIN that loop we will loop through each column
30+
# for theThing in theRangeOfThings
31+
32+
# a loop for each page
33+
for page in range(numPages):
34+
newPage(pageWidth, pageHeight)
35+
fill(0)
36+
rect(0, 0, width(), height())
37+
38+
# define the width and height of the shape we will draw in that grid
39+
# make it a little smaller than the grid this time
40+
shapeWidth = pageWidth/rows - 10
41+
shapeHeight = pageHeight/cols - 10
42+
43+
# define x and y as where we start? (0, 0) is in the bottom left
44+
# for each page we want to reset these
45+
46+
# we are going to draw the shapes from the center this time,
47+
# so we want our X and Y coordinates to be at the center of each grid
48+
x = gridWidth / 2
49+
y = gridHeight / 2
50+
51+
for row in range(rows):
52+
# everything indented this much runs once for each row
53+
for col in range(cols):
54+
# everything indented this much runs once for each grid unit
55+
56+
# before we draw our shape, let’s pick a color for it
57+
# we can create 3 variables
58+
r = random() # red
59+
g = random() # green
60+
b = random() # blue
61+
a = .8 # alpha (0 = transparent, 1 = opaque)
62+
fill(r, g, b, a)
63+
64+
# okay, now we finally draw our shape
65+
66+
# let’s define specific shape dimensions for
67+
# JUST THIS ONE shape
68+
# by using our basic dimensions and adding some randomness
69+
thisShapeWidth = shapeWidth + randrange(-5, 5)
70+
thisShapeHeight = shapeHeight + randrange(-5, 5)
71+
72+
# we can make a conditional
73+
# if this condition is true:
74+
# then run this code
75+
# if not:
76+
# then run this code
77+
if random() > .5:
78+
# to draw from the center, we move the
79+
# x position to the left half of its width
80+
# and move it down half its height
81+
rect(x-thisShapeWidth/2, y-thisShapeHeight/2, thisShapeWidth, thisShapeHeight)
82+
else:
83+
oval(x-thisShapeWidth/2, y-thisShapeHeight/2, thisShapeWidth, thisShapeHeight)
84+
# at the end of the column, advance X to the next column
85+
x += gridWidth
86+
# let’s also make the shape a smidge bigger each time
87+
shapeWidth += .01
88+
shapeHeight += .01
89+
# we are OUTDENTED, which means we are back
90+
# to code that only runs once per row
91+
# now that we’ve reached the end of the row
92+
# we want to advance y to move us up to the next row
93+
y += gridHeight
94+
# and we want to return x to its original position
95+
# (a carriage return, essentially)
96+
#
97+
x -= cols*gridWidth
98+
99+
# now let’s save this image to the desktop
100+
# first as a PDF
101+
saveImage('~/desktop/myFancyGrid.pdf')
102+
# then as a GIF
103+
saveImage('~/desktop/myFancyGrid.gif')

session-1/7-randrange.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from random import randrange
2+
print(randrange(0, 100))

session-1/slides/concepts.pdf

12.8 MB
Binary file not shown.

0 commit comments

Comments
 (0)