redd 發表於 2016-10-12 15:38:56

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

redd 發表於 2016-10-12 15:42:57

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

redd 發表於 2016-10-12 15:47:01

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"

redd 發表於 2016-10-12 15:53:48

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

redd 發表於 2016-10-12 15:58:44

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)

redd 發表於 2016-10-12 16:01:51

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

redd 發表於 2016-10-12 16:06:37

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

redd 發表於 2016-10-12 16:09:51

my_list =

for number in my_list:
    print 2 * number

redd 發表於 2016-10-12 16:38:52

start_list =
square_list = []

for x in start_list:
    square_list.append(x ** 2)
square_list.sort()
print square_list

redd 發表於 2016-10-13 01:16:42

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

redd 發表於 2016-10-18 12:16:57

a =
for x in a:
    if x % 2 == 0:
      print x
print 偶數

redd 發表於 2017-3-6 14:10:12

for letter in "Redd":
    print letter
   
# Empty lines to make the output pretty
print
print

word = "Programming is fun!"

for letter in word:
    # Only print out the letter i
    if letter == "i":
      print letter
頁: [1]
查看完整版本: Lists