Python Lists and Dictionaries
More with 'for'
If your list is a jumbled mess, you may need to
animals = ["cat", "ant", "bat"] animals.sort() for animal in animals: print animal
- First, we create a list called
animalswith three strings. The strings are not in alphabetical order.
- Then, we sort
animalsinto alphabetical order. Note that
.sort()modifies the list rather than returning a new list.
- Then, for each item in
animals, we print that item out as
"ant", "bat", "cat"on their own line each.
for-loop that iterates over
.append()s each number squared (
x ** 2) to