Coding

The Big Picture

Text Editor, Command Line, Machine Code

# python comment

Hello World

  1. text editor:
    print( "hi there!" )

    • save as "hello.py"
  2. command line:
    $ cd /Desktop/workshop
    $ python hello.py

now your turn

Someone has already done that...

importing modules
(even your own)

import banana

Names from a hat



#Choose a Name from a Hat
import random

fruit = ['apple', 'banana', 'orange']

random.choice(fruit)
                    

now your turn

What did we learn?
$ python hello.py

import random

#hashtag?

Why Dictionaries?

when lists are so wonderful...

and dictionaries are UNORDERED

  • word: definition (close associations)
  • speed (constant time)
  • memory (plays nicely with json)

{ word1: definition }

{ word1: definition,     word2: definition }

Writing a Dictionary

so many emails to remember ;(


#contacts
emails = {'mark': '[email protected]', 
          'tom': '[email protected]', 
          'brendan': '[email protected]', 
          'zev': '[email protected]'}
                    

#live code:

#look up zev's email

#add a new email

#change mark's email

now your turn

Playing with Dictionaries

len()

empty_dict = {}

len(empty_dict)                  
#show number of entries       
.get

empty_dict['mark'] 
#uh oh, nothing's there!

empty_dict.get('mark', 'cool')          
#get to the rescue! 
.update

#two dictionaries --> one!

empty_dict.update(existing_dict)

now your turn
(as a grocery store owner)

  1. create inventory of fruit
    • 2 bananas, 1 apple, 5 oranges, 3 grapefruits
  2. Tom buys 3 oranges
    • decrease the number of oranges to 2
  3. Expand store to veggies
    • add three veggies of your choice

Module 3: displaying answers

    First

  1. create a dictionary with questions and answers
  2. display the answer to the first question

  3. Next

  4. assign a variable "question" to a random question
    hint: remember import random
  5. display answer based on variable

  6. Finally

  7. create a function called "show_answer"
    show_answer should take a question and display the answer

Caught in a loop

iterating through lists, dictionaries




 names = ['mark', 'tom', 'zev', 'brendan']    
                    

                names = ['mark', 'tom', 'zev', 'brendan']
                    


for name in names

Keeping track of your steps

enumerate #beauty of python vs. C


for i, name in enumerate( names ):
print( i, name )

Step, break and wiggle with it

break

names = ['mark', 'tom', 'zev', 'brendan']      

#print up to zev
for name in names:
    print name
    if name == "zev":
        break
continue

#print all names except tom's
for name in names:
    if name == 'tom':
        continue
    else:
        print name

now your turn
(print names, skipping every other name)

Module 4: keeping score

    First

  1. display each team name
  2. a loop is handy here!

    Next

  3. create an empty dictionary of scores
  4. enter each team into the dictionary with a score of 0
  5. remember to loop!

    Finally

    Create a Function to

  6. ask the user for each team's score
  7. update the score in the dictionary