Skip to content

Instantly share code, notes, and snippets.

@justinmeiners
Last active September 15, 2018 05:23
Show Gist options
  • Save justinmeiners/e5fe5aace29c37cdad549bec073b09f3 to your computer and use it in GitHub Desktop.
Save justinmeiners/e5fe5aace29c37cdad549bec073b09f3 to your computer and use it in GitHub Desktop.
Examples from python lessons I taught.
# Created By: Justin Meiners (2017)
# simple calculations
# -------------------------------
import math
def volume_cone(radius, height):
volume = math.pi * (radius**2.0) * (height / 3.0)
return volume
radius = 30.0
height = 20.0
print(type(math.pi))
volume = volume_cone(radius, height)
print(volume)
# integration
# -------------------------------
import math
def integral(f_x, min, max, iterations):
width = (max - min) / iterations
area_total = 0.0
for i in range(1, iterations + 1):
xi = width * i
yi = f_x(xi)
area_total = area_total + width * yi
return area_total
print(integral(math.sin, 0.0, 5.0, 2000))
# processing some CSV data
# -------------------------------
import csv
import math
with open('data.csv', 'r') as csvfile:
data_reader = csv.reader(csvfile)
headers = None
for row in data_reader:
if not headers:
headers = row
print("%s\t%s\tmean" % (headers[0], headers[1]) )
continue
sample_id = row[0]
analyte_name = row[1]
conc1 = float(row[2])
conc2 = float(row[3])
conc3 = float(row[4])
total = conc1 + conc2 + conc3
mean = total / 3.0
print("%s\t%s\t%f" % (sample_id, analyte_name, mean) )
# functions, state and global variables
# -------------------------------
import random
apple_count = 10
yogurt_count = 8
def needs_groceries():
if apple_count < 3 or yogurt_count < 2:
return True
else:
return False
def hannah_eat():
global yogurt_count
global apple_count
if yogurt_count >= 1:
yogurt_count = yogurt_count - 1
if apple_count >= 2:
apple_count = apple_count - 2
def justin_eat():
global yogurt_count
global apple_count
if yogurt_count >= 2:
yogurt_count = yogurt_count - 2
if apple_count >= 4:
apple_count = apple_count - 4
def meal():
if needs_groceries():
print("Get to WinCo!")
return
dice_roll = random.sample([False, True], 1)
if dice_roll[0]:
print("Justin")
justin_eat()
else:
print("Hannah")
hannah_eat()
print(apple_count)
print(yogurt_count)
meal()
meal()
meal()
meal()
# physical simulations
# -------------------------------
import math
class Ball:
def __init__(self, x, y, angle, speed):
self.x = x
self.y = y
self.angle_rad = math.radians(angle)
self.vx = math.cos(self.angle_rad) * speed
self.vy = math.sin(self.angle_rad) * speed
def integrate(ball, dt):
gravity = -9.81
ball.vy += gravity * dt
ball.x += ball.vx * dt
ball.y += ball.vy * dt
def simulate(ball):
while ball.y > 0.0:
dt = 1.0 / 30.0
integrate(ball, dt)
coordinate = str(ball.x) + "\t" + str(ball.y)
print(coordinate)
myBall = Ball(0.0, 1.0, 45.0, 19.0)
myBall2 = Ball(0.0, 2.0, 60.0, 19.0)
simulate(myBall)
print('---------------------')
simulate(myBall2)
# Tic Tac Toe
# -------------------------------
from __future__ import print_function
# 0 = nothing, 1 = x, 2 = 0
BLANK = 0
X = 1
O = 2
board = [BLANK] * 9
def print_board(board_to_print):
for i in range(0, 9):
cell = board_to_print[i]
character = '_'
if cell == X:
character = 'x'
elif cell == O:
character = 'o'
print(character, end=' ')
if i == 2:
print('\n')
elif i == 5:
print('\n')
elif i == 8:
print('\n')
def get_player_move(board_to_edit, turn):
if turn == X:
player_name = "X's"
elif turn == O:
player_name = "O's"
message = player_name + " where would you like to play? "
cell = int(input(message)) - 1
if cell > 9 or cell < 1 or board_to_edit[cell] != BLANK:
return False
else:
board_to_edit[cell] = turn
return True
current_turn = X
while True:
valid = get_player_move(board, current_turn)
if not valid:
print("Can't move there!")
continue
print(' \n')
print_board(board)
if current_turn == X:
current_turn = O
elif current_turn == O:
current_turn = X
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment