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".

In [1]:
# 1
print('Hello, Stefan and Damon!')
Hello, Stefan and Damon!
In [2]:
# 2
for i in range(4):
    print("Hello Yourself. We're here to drink blood.")
Hello Yourself. We're here to drink blood.
Hello Yourself. We're here to drink blood.
Hello Yourself. We're here to drink blood.
Hello Yourself. We're here to drink blood.
In [140]:
# 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)
['Crabbe', 'Dolohov', 'Lestrange']
In [141]:
# 4
for g in good_people:
    for deathEater in death_eaters:
        print(f'{g} hates {deathEater}')
Harry hates Crabbe
Harry hates Dolohov
Harry hates Lestrange
Ron hates Crabbe
Ron hates Dolohov
Ron hates Lestrange
Hermy hates Crabbe
Hermy hates Dolohov
Hermy hates Lestrange
In [142]:
# 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')
The world isn't split between good people and death eaters
In [143]:
# 6
print((False and False))
print(not (False and False))
print(None)
print(not None)
False
True
None
True
In [144]:
# 6

for variable in ['u', "hitchhiker", 42, [1.618,'asdf',3.141592,4], 'galaxy', True, 1., None, not None]:
    print(type(variable))
<class 'str'>
<class 'str'>
<class 'int'>
<class 'list'>
<class 'str'>
<class 'bool'>
<class 'float'>
<class 'NoneType'>
<class 'bool'>
In [145]:
# 7 

print("the fault is" + " in our face")
# y u no print me?
the fault is in our face
In [146]:
# 8

53 + (-11 // 1)
Out[146]:
42
In [147]:
# 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"
In [148]:
# 10

villains = ['Garou', 'Boros', 'Marugori']
villains.append('Garou')
for v in villains:
    print(one_punch_man(v))
Serious Punch.
Consecutive Normal Punches.
punch
Serious Punch.
In [149]:
# 11

bbt_gfs = {
    'sheldon': 'amy',
    'leonard': 'penny',
    'kurt': 'penny',
    'zack': 'penny',
    'leonard': 'priya',
    'raj': None
}

print(type(bbt_gfs))
<class 'dict'>
In [150]:
# 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))
amy
priya
5
bernadette
6
In [151]:
# 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
Carmela
['Livia', 'AJ']
AJ
6
['Carmela', 'Junior', 'Livia', 'AJ', 'Janice']
[]
In [152]:
# 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
1 is NOT a prime
2 is a prime
3 is a prime
4 is 2 times 2
5 is a prime
6 is 2 times 3
7 is a prime
8 is 2 times 4
9 is 3 times 3
10 is 2 times 5
11 is a prime
12 is 2 times 6
In [154]:
# 15 

dany_kids = ['Drogon', 'Rhaegal', 'Tyrion']

dany_kids[2] = 'Viserion'

print(dany_kids)
['Drogon', 'Rhaegal', 'Viserion']
In [155]:
# 16

squares = [x**2 for x in range(10)]
print(squares)
print(squares[5])
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
25
In [156]:
# 17

t = 'heisenberg', 'skinny pete', 'badger', 'krazy-8', 'JP'
print(type(t))
print(t[3])

#t[2] = 'skyler'

# cover benefits of tuples over lists
<class 'tuple'>
krazy-8
In [157]:
# 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
In [158]:
# 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)
In [159]:
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)))
Francis
Francis
In [184]:
# 20 

#! cat sherlock.txt
In [2]:
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)
there
2587
In [3]:
print('Thank You.')

# cover classes in a separate lecture
# implementation of term project template from scratch
Thank You.
In [1]:
print('PS: Sorry for it being "the one where we missed your favorite show"')
PS: Sorry for it being "the one where we missed your favorite show"
In [ ]: