Lists Dictionaries
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