Codecademy Python Answers Lists And Dictionaries

Also Read: How To Chat With Your Friend Using Command Prompt

Lists and Dictionaries 1/14 – Introduction to Lists

zoo_animals = ["pangolin", "cassowary", "sloth","cow" ];
# One animal is missing!

if len(zoo_animals) > 3:
	print "The first animal at the zoo is the " + zoo_animals[]
	print "The second animal at the zoo is the " + zoo_animals[1]
	print "The third animal at the zoo is the " + zoo_animals[2]
	print "The fourth animal at the zoo is the " + zoo_animals[3]

Lists and Dictionaries 2/14 – Access By Index

numbers = [5, 6, 7, 8]

print "Adding the numbers at indices 0 and 2..."
print numbers[] + numbers[2]
print "Adding the numbers at indices 1 and 3..."
print numbers[1] + numbers[3]

Lists and Dictionaries 3/14 – New Neighbors

zoo_animals = ["pangolin", "cassowary", "sloth", "tiger"]
# Last night our zoo's sloth brutally attacked 
#the poor tiger and ate it whole.

# The ferocious sloth has been replaced by a friendly hyena.
zoo_animals[2] = "hyena"

# What shall fill the void left by our dear departed tiger?
# Your code here!
zoo_animals[3] = "lion"
print zoo_animals

Lists and Dictionaries 4/14 – Late Arrivals and List Length

suitcase = [] 
suitcase.append("sunglasses")
suitcase.append("nenu")
suitcase.append("na")
suitcase.append("rakshashi")

# Your code here!




list_length = len(suitcase)# Set this to the length of suitcase

print "There are %d items in the suitcase." % (list_length)
print suitcase

Lists and Dictionaries 5/14 – List Slicing

suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]

first  = suitcase[:2]  # The first and second items (index zero and one)
middle = suitcase[2:4]            # Third and fourth items (index two and three)
last   = suitcase[4:6]            # The last two items (index four and five)

Lists and Dictionaries 6/14 – Slicing Lists and Strings

animals = "catdogfrog"
cat  = animals[:3]   # The first three characters of animals
dog  = animals[3:6]              # The fourth through sixth characters
frog = animals[6:10]            # From the seventh character to the end

Lists and Dictionaries 7/14 -Maintaing Order

animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
duck_index =   animals.index("duck") # Use index() to find "duck"

# Y!our code here
animals.insert(duck_index,"cobra")


print animals # Observe what prints after the insert operation

Lists and Dictionaries 8/14 – For One And All

my_list = [1,9,3,8,5,7]

for number in my_list:
   
    print number*2

Lists and Dictionaries 9/14 – More With ‘For’

start_list = [5, 3, 1, 2, 4]
square_list = []

for number in start_list:
    prathap = number**2
    square_list.append(prathap)
    square_list.sort()
   

print square_list

Lists and Dictionaries 10/14 – This Next Part is Key

# Assigning a dictionary with three key-value pairs to residents:
residents = {'Puffin' : 104, 'Sloth' : 105, 'Burmese Python' : 106}

print residents['Puffin'] # Prints Puffin's room number

# Your code here!
print residents['Sloth']
print residents['Burmese Python']

Lists and Dictionaries 11/14 – New Entries

menu = {} # Empty dictionary
menu['Chicken Alfredo'] = 14.50 # Adding new key-value pair
menu['ken Alfredo'] = 14.50
menu['Chin Alfredo'] = 14.50
menu['Chicken Aldo'] = 14.50


print menu['Chicken Alfredo']

# Your code here: Add some dish-price pairs to menu!




print "There are " + str(len(menu)) + " items on the menu."
print menu

Lists and Dictionaries 12/14 – Changing Your Mind

# key - animal_name : value - location 
zoo_animals = { 'Unicorn' : 'Cotton Candy House',
'Sloth' : 'Rainforest Exhibit',
'Bengal Tiger' : 'Jungle House',
'Atlantic Puffin' : 'Arctic Exhibit',
'Rockhopper Penguin' : 'Guntur'}
# A dictionary (or list) declaration may break across multiple lines

# Removing the 'Unicorn' entry. (Unicorns are incredibly expensive.)
del zoo_animals['Unicorn']
del zoo_animals['Sloth']
del zoo_animals['Bengal Tiger']

# Your code here!




print zoo_animals

Lists and Dictionaries 13/14 – Remove a Few Things

backpack = ['xylophone', 'dagger', 'tent', 'bread loaf']
backpack.remove('dagger')

Lists and Dictionaries 14/14 – Its Dangerous to go Alone! Take This

inventory = {
    'gold' : 500,
    'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key
    'backpack' : ['xylophone','dagger', 'bedroll','bread loaf'],
    "pocket" : ['seashell','strange berry','lint']
}
# Adding a key 'burlap bag' and assigning a list to it
inventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']

# Sorting the list found under the key 'pouch'
inventory['pouch'].sort() 
inventory['backpack'].sort()
inventory['backpack'].remove('dagger')
# Your code here
inventory['gold'] += 50
Also Read :   Codecademy Python Answers Loops

Leave a Reply

Your email address will not be published. Required fields are marked *