Lists
zoo_animals = ["pangolin", "cassowary", "sloth", "pig"];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
print "The third animal at the zoo is the " + zoo_animals
print "The fourth animal at the zoo is the " + zoo_animals
numbers =
print "Adding the numbers at indices 0 and 2..."
print numbers + numbers
print "Adding the numbers at indices 1 and 3..."
print numbers + numbers 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 = "hyena"
# What shall fill the void left by our dear departed tiger?
# Your code here!
zoo_animals = "pig" suitcase = []
suitcase.append("sunglasses")
suitcase.append("hat")
suitcase.append("shoes")
suitcase.append("underwear")
list_length = len(suitcase) # Set this to the length of suitcase
print "There are %d items in the suitcase." % (list_length)
print suitcase suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]
first= suitcase# The first and second items (index zero and one)
middle = suitcase# Third and fourth items (index two and three)
last = suitcase# The last two items (index four and five) animals = "catdogfrog"
cat= animals[:3] # The first three characters of animals
dog= animals # The fourth through sixth characters
frog = animals # From the seventh character to the end animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
duck_index = animals.index("duck") # Use index() to find "duck"
animals.insert(duck_index, "cobra")
print animals # Observe what prints after the insert operation my_list =
for number in my_list:
print 2 * number start_list =
square_list = []
for x in start_list:
square_list.append(x ** 2)
square_list.sort()
print square_list backpack = ['xylophone', 'dagger', 'tent', 'bread loaf']
backpack.remove("dagger") a =
for x in a:
if x % 2 == 0:
print x
print 偶數 for letter in "Redd":
print letter
# Empty lines to make the output pretty
word = "Programming is fun!"
for letter in word:
# Only print out the letter i
if letter == "i":
print letter
頁:
[1]