During my fist day in my Intermediate Python program class, I learned about List, the various kinds of list.
"""
Learning Python
Lesson 6 : Lists & Dictionaries
List Functions
Simple programming exercises from Codecademy
"""
# Exercise 0 : Appending & List Length
"""
Append 3 more items to the suitcase list.
Then, set list_length equal to the length of suitcase.
"""
suitcase = []
suitcase.append("sunglasses")
# Append 3 more items to the suitcase
suitcase.append("hat")
suitcase.append("shorts")
suitcase.append("shoes")
suitcase.append("beachwear")
# Create a variable called list_length and assign it to the length of suitcase
list_length = len(suitcase)
print "There are %d items in the suitcase." % list_length
print suitcase
# Exercise 1 : List Slicing
"""
Use list slicing to slice suitcase into 3 parts.
"""
suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]
# Create a variable called first and give it the first 2 items in the suitcase
first = suitcase[0:2]
# Create a variable called middle and give it the next 2 items in the suitcase
middle = suitcase[2:4]
# Create a variable called last and give it the last 2 items in the suitcase
last = suitcase[4:]
# Print your variables: first, middle and last to check your code
print first
print middle
print last
# Exercise 2 : Slicing Lists and Strings
"""
Assign each variable a slice of animals that
spells out that variable's name.
"""
animals = "catdogfrog"
# Create a variable called cat and slice the list to get the first three characters
cat= animals[0:3]
# Create a variable called dog and slice the list to get the next three characters
dog= animals[3:6]
# Create a variable called frog and slice the list to get the last four characters
frog= animals[6:]
# Print your variables: cat, dog and frog to check your code
print cat
print dog
print frog
# Exercise 3 : Maintaining Order
"""
Create a variable called duck_index.
Use the index() function to assign duck_index to the index of "duck".
Then insert the string "cobra" at that index.
"""
animals = ["aardvark", "badger", "duck", "emu", "fox"]
# Assign duck_index to the index of "duck". Use the index() function
duck_index= animals.index("duck")
# Now insert "cobra" into the list of animals at the duck_index
animals.insert(duck_index, "cobra")
# Print the list of animals and see what happens
print animals
# Exercise 4 : For One and All
"""
Write a statement in the indented part of the for loop
that prints a number equal to 2 * number for every list item.
"""
my_list = [1,9,3,8,5,7]
for number in my_list:
# Your code here
print number * 2
# Exercise 5 : For One and All
"""
Write a for loop that appends values to square_list
with items that are the square (x ** 2) of
each item in start_list. Then sort square_list!
"""
start_list = [5, 3, 1, 2, 4]
square_list = []
# Write a for loop that goes through each number in the start_list
# Append the square of each number to square_list
for number in start_list:
square_list.append(number ** 2)
print "Adding the square of %d" % number
print square_list
# Sort square_list
square_list.sort()
# Print square_list
print square_list
"""
# ************************************************************
# TESTS : DO NOT MODIFY THESE
# ************************************************************
print("")
if list_length == 4 : print("Exercise 0 : Test Passed!")
if first == suitcase[0:2] and middle == suitcase[2:4] and last == suitcase[4:6]: print("Exercise 1 : Test Passed!")
if cat == "cat" and dog == "dog" and frog == "frog" : print("Exercise 2 : Test Passed!")
if animals[duck_index] == "cobra" : print("Exercise 3 : Test Passed!")
if square_list == [1, 4, 9, 16, 25]: print("Exercise 5 : Test Passed!")
"""
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====
>>>
Traceback (most recent call last):
File "/tmp/guest-A6n3zJ/Downloads/Lesson6-ListFunctions.py", line 27, in <module>
print "There are %d items in the suitcase." % list_length
NameError: name 'list_length' is not defined
>>>
There are 5 items in the suitcase.
['sunglasses', 'hat', 'shorts', 'shoes', 'beachwear']
['sunglasses', 'hat']
['passport', 'laptop']
['suit', 'shoes']
>>>
There are 5 items in the suitcase.
['sunglasses', 'hat', 'shorts', 'shoes', 'beachwear']
['sunglasses', 'hat']
['passport', 'laptop']
['suit', 'shoes']
cat
dog
frog
>>>
There are 5 items in the suitcase.
['sunglasses', 'hat', 'shorts', 'shoes', 'beachwear']
['sunglasses', 'hat']
['passport', 'laptop']
['suit', 'shoes']
cat
dog
frog
['aardvark', 'badger', 'cobra', 'duck', 'emu', 'fox']
>>>
There are 5 items in the suitcase.
['sunglasses', 'hat', 'shorts', 'shoes', 'beachwear']
['sunglasses', 'hat']
['passport', 'laptop']
['suit', 'shoes']
cat
dog
frog
['aardvark', 'badger', 'cobra', 'duck', 'emu', 'fox']
2
18
6
16
10
14
>>>
There are 5 items in the suitcase.
['sunglasses', 'hat', 'shorts', 'shoes', 'beachwear']
['sunglasses', 'hat']
['passport', 'laptop']
['suit', 'shoes']
cat
dog
frog
['aardvark', 'badger', 'cobra', 'duck', 'emu', 'fox']
2
18
6
16
10
14
>>>
There are 5 items in the suitcase.
['sunglasses', 'hat', 'shorts', 'shoes', 'beachwear']
['sunglasses', 'hat']
['passport', 'laptop']
['suit', 'shoes']
cat
dog
frog
['aardvark', 'badger', 'cobra', 'duck', 'emu', 'fox']
2
18
6
16
10
14
[1, 4, 9, 16, 25]
>>>
There are 5 items in the suitcase.
['sunglasses', 'hat', 'shorts', 'shoes', 'beachwear']
['sunglasses', 'hat']
['passport', 'laptop']
['suit', 'shoes']
cat
dog
frog
['aardvark', 'badger', 'cobra', 'duck', 'emu', 'fox']
2
18
6
16
10
14
Adding the square of 5
[25]
Adding the square of 3
[25, 9]
Adding the square of 1
[25, 9, 1]
Adding the square of 2
[25, 9, 1, 4]
Adding the square of 4
[25, 9, 1, 4, 16]
[1, 4, 9, 16, 25]
>>>
No comments:
Post a Comment