This lecture is best experienced together. In 90 minutes, I'll give you 20 python codes, and you need to explain why they do what they do. I'll help you out by saying "yes" or "no".
# 1
print('Hello, Stefan and Damon!')
# 2
for i in range(4):
print("Hello Yourself. We're here to drink blood.")
# 3
good_people = ['Harry', 'Ron', 'Hermy']
death_eaters = ['Crabbe', 'Dolohov', 'Lestrange']
the_world = ['Harry', 'Ron', 'Hermy', 'Crabbe', 'Dolohov', 'Lestrange', 'Snape', 'Dumbledore', 'Fred']
print(death_eaters)
# 4
for g in good_people:
for deathEater in death_eaters:
print(f'{g} hates {deathEater}')
# 5
if the_world != good_people + death_eaters:
print("The world isn't split between good people and death eaters")
else:
print('It is easy to see that nearly six years of magical education have not been wasted on you')
# 6
print((False and False))
print(not (False and False))
print(None)
print(not None)
# 6
for variable in ['u', "hitchhiker", 42, [1.618,'asdf',3.141592,4], 'galaxy', True, 1., None, not None]:
print(type(variable))
# 7
print("the fault is" + " in our face")
# y u no print me?
# 8
53 + (-11 // 1)
# 9
def one_punch_man(enemy):
'''
- OPMs reactions to various enemies
- input: enemy (string)
- output: reaction (string)
'''
if enemy == 'Garou':
return "Serious Punch."
elif enemy == 'Boros':
return "Consecutive Normal Punches."
else:
return "punch"
# 10
villains = ['Garou', 'Boros', 'Marugori']
villains.append('Garou')
for v in villains:
print(one_punch_man(v))
# 11
bbt_gfs = {
'sheldon': 'amy',
'leonard': 'penny',
'kurt': 'penny',
'zack': 'penny',
'leonard': 'priya',
'raj': None
}
print(type(bbt_gfs))
# 12
print(bbt_gfs['sheldon'])
print(bbt_gfs['leonard'])
print(len(bbt_gfs))
bbt_gfs['howard'] = 'bernadette'
print(bbt_gfs['howard'])
print(len(bbt_gfs))
# 13
sopranos = ['Tony', 'Carmela', 'Junior', 'Livia', 'AJ']
print(sopranos[1])
print(sopranos[3:])
print(sopranos[-1])
sopranos = sopranos + ['Janice']
print(len(sopranos))
del sopranos[0]
print(sopranos)
sopranos.clear()
print(sopranos)
# look later: extend, insert, remove, pop, index, count, reverse, copy, etc from docs
# 14
# tells if all numbers till n are primes, or gives their factorization
def primes(n):
print('1 is NOT a prime')
for i in range(2,n):
for j in range(2,i):
if i % j == 0:
print(i, 'is', j, 'times', i//j)
break
else:
print(i, 'is a prime')
primes(13)
# cover pep8 in a later lecture
# 15
dany_kids = ['Drogon', 'Rhaegal', 'Tyrion']
dany_kids[2] = 'Viserion'
print(dany_kids)
# 16
squares = [x**2 for x in range(10)]
print(squares)
print(squares[5])
# 17
t = 'heisenberg', 'skinny pete', 'badger', 'krazy-8', 'JP'
print(type(t))
print(t[3])
#t[2] = 'skyler'
# cover benefits of tuples over lists
# 18
import random
candidates = ['Francis', 'Conway', 'Claire', 'Brockhart']
num_voters = 42
# the bad python way
def bad_voting(num, candidates):
votes = []
num_candidates = len(candidates)
for i in range(num):
temp = random.randint(0, num_candidates-1)
votes.append(candidates[temp])
return votes
def bad_winner(votes):
uniq_cands = []
for cand in votes:
if cand not in uniq_cands:
uniq_cands.append(cand)
freq_cands = {}
for cand in uniq_cands:
freq_cands[cand] = 0
for cand in votes:
freq_cands[cand] += 1
winner = None
winner_votes = 0
for cand in uniq_cands:
if freq_cands[cand] > winner_votes:
winner_votes = freq_cands[cand]
winner = cand
return winner
# 19
# the pythonic way
def voting(num, candidates):
return [random.choice(candidates) for _ in range(num)]
def winner(votes):
return max(votes, key=votes.count)
seed_value = 6
random.seed(seed_value)
print(winner(voting(num_voters, candidates)))
random.seed(seed_value)
print(bad_winner(bad_voting(num_voters, candidates)))
# 20
#! cat sherlock.txt
pattern = input()
f = open('sherlock.txt', 'r')
count = 0
for line in f:
for word in line.split():
if word.lower() == pattern:
count += 1
f.close()
print(count)
print('Thank You.')
# cover classes in a separate lecture
# implementation of term project template from scratch
print('PS: Sorry for it being "the one where we missed your favorite show"')