0% found this document useful (0 votes)
961 views1 page

Lists Dictionaries

This document provides examples and exercises for using lists and dictionaries in Python. It includes examples of creating lists to store favorite hobbies, foods, and sandwich ingredients. It also provides an example of a dictionary to store pet name and type pairs. The exercises ask the reader to create these examples of lists and dictionaries, with one exercise using a for loop to print out statements combining the dictionary keys and values.

Uploaded by

api-291204383
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
961 views1 page

Lists Dictionaries

This document provides examples and exercises for using lists and dictionaries in Python. It includes examples of creating lists to store favorite hobbies, foods, and sandwich ingredients. It also provides an example of a dictionary to store pet name and type pairs. The exercises ask the reader to create these examples of lists and dictionaries, with one exercise using a for loop to print out statements combining the dictionary keys and values.

Uploaded by

api-291204383
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
You are on page 1/ 1

LESSON: LISTS & DICTIONARIES

PROGRAMMING EXERCISES
(Requires IDLE 2.7.10)
Lists - adatatypeyou can use to store a collection of different pieces of
information as a sequence under a single variable name. For example,
#1 Favorites
my_list = ["orange", "blue"]
Make a list of your favorite hobbies and give the list the variable name games. Now
make a list of your favorite foods and name the variable foods. Join the two lists and
Dictionary is similar to a list, but you access values by looking up a key name the result favorites. Finally, print the variable favorites.
instead of an index. A key can be any string or number. For example, d =
{'Key 1' : 1 , 'Key 2' : 2 , 'Key 3':3}
#2: My Five Favorite Ingredients
Create a list containing five different sandwich ingredients, such as the following:
Index - refers to a position within an ordered list
ingredients = ['snails', 'leeches', 'gorilla belly-button lint', 'caterpillar eyebrows',
'centipede toes'] . Now create a loop that prints out the list (including the numbers)
Slicing - When you want to extract part of a string, or some part of a
list, you use a slice. Other languages use the term substring for a string
#3: Pet Names
slice. For example, slicedList = aList[beginIndex:endIndex]
Create a dictionary to hold information about pets. Each key is an animal's name,
and each value is the kind of animal.
For example, 'ziggy': 'canary'
Put at least 3 key-value pairs in your dictionary.
PROGRAMMING CHALLENGE # 3
Use a for loop to print out a series of statements such as "Willie is a dog."
First Reverse
VOCABULARY

Using the Python language, have the function FirstReverse(str) take the
str parameter being passed and return the string in reversed order.
def FirstReverse(str):
# code goes here
return
# keep this function call here
print FirstReverse(raw_input())
Examples of Output:
Input = Cup Output = puC
Input =I Love Code Output = edoC evoL I
Hint: for, Range, len

#4: Polling Friends


Think of a question you could ask your friends. Create a dictionary where each key is
a person's name, and each value is that person's response to your question.
Store at least three responses in your dictionary.
Use a for loop to print out a series of statements listing each person's name, and
their response.

You might also like