PyCon intro tutorial/Python scripts2: Difference between revisions
Content added Content deleted
imported>Jesstess No edit summary |
imported>Jesstess No edit summary |
||
Line 7: | Line 7: | ||
# How do you create a new dictionary? |
# How do you create a new dictionary? |
||
# How do you replace items in a dictionary? |
# How do you replace items in a dictionary? |
||
# If you have a dictionary <code>flavors = {"Adam": "vanilla", "Jessica": "chocolate"}</code> and execute the code <code>flavors["Adam"] = "rum raisin"</code> what does <code>flavors</code> now look like? |
|||
# What are 3 examples of when using dictionaries would be a good idea? |
|||
== Step # |
== Step #2: learn about modules == |
||
Time to practice looking things up! Use your favorite search engine to learn about modules. Answer these questions: |
|||
# What does <code>import random</code> do? |
|||
# After reviewing https://docs.python.org/2/library/random.html, how would you select a random item from a list? |
|||
== Step #3: Review the state capitals quizzer == |
== Step #3: Review the state capitals quizzer == |
Revision as of 15:52, 9 April 2014
Step #1: learn about dictionaries
Time to practice looking things up! Use your favorite search engine to learn about another useful Python container type called a dictionary. Answer these questions:
- How do you create a new dictionary?
- How do you replace items in a dictionary?
- If you have a dictionary
flavors = {"Adam": "vanilla", "Jessica": "chocolate"}
and execute the codeflavors["Adam"] = "rum raisin"
what doesflavors
now look like? - What are 3 examples of when using dictionaries would be a good idea?
Step #2: learn about modules
Time to practice looking things up! Use your favorite search engine to learn about modules. Answer these questions:
- What does
import random
do? - After reviewing https://docs.python.org/2/library/random.html, how would you select a random item from a list?
Step #3: Review the state capitals quizzer
Review a useful script synthesizes a number of useful concepts that we've covered.
- Download the script state_capitals.py and save it to your Desktop. Be sure to save it as a
.py
file. - Open the script in your text editor.
- Read through the script. Think about these questions:
- Why do we use a dictionary to store the states and capitals?
- What is the purpose of the variable i in the for loop?
- What is the purpose of the input function?
- Think about one way that you could extend the script to make it more useful. How would you make that change in code?