Difference between revisions of "Python List Exercises"
From WLCS
Line 10: | Line 10: | ||
Create a file name "ch9_yourLastName.py". In your file, complete the following exercises: | Create a file name "ch9_yourLastName.py". In your file, complete the following exercises: | ||
− | + | '''List Basics:''' | |
# Create a list named wordList that contains the following words: "washington", "lee", "generals", "arlington", "virginia" | # Create a list named wordList that contains the following words: "washington", "lee", "generals", "arlington", "virginia" | ||
# Print out the first element in wordList | # Print out the first element in wordList | ||
Line 17: | Line 17: | ||
# Print out the word "generals" in the list | # Print out the word "generals" in the list | ||
− | + | '''Lists with loops:''' | |
# Create a list named numList with the following numbers 3, 1, 7, 6, 4, 1, 1, 5, 4, 7, 9, 0, 9, 7, 7, 43, 2, 6, 87, 67, 4, 2, 532 | # Create a list named numList with the following numbers 3, 1, 7, 6, 4, 1, 1, 5, 4, 7, 9, 0, 9, 7, 7, 43, 2, 6, 87, 67, 4, 2, 532 | ||
# Print out the last element in numList using '''len(numList)-1''' | # Print out the last element in numList using '''len(numList)-1''' | ||
Line 25: | Line 25: | ||
# Write a loop that prints out all the numbers in numList that are greater than 10 | # Write a loop that prints out all the numbers in numList that are greater than 10 | ||
# Write a loop that prints out all the numbers in numList that are even (HINT: if numList[x]%2 == 0) | # Write a loop that prints out all the numbers in numList that are even (HINT: if numList[x]%2 == 0) | ||
+ | |||
+ | '''Adding and removing elements:''' | ||
+ | # Create an empty list [] named myList | ||
+ | # Add the following words to myList by using '''myList.append( WORD )''' | ||
+ | #* "The" | ||
+ | #* "quick" | ||
+ | #* "brown" | ||
+ | #* "fox" | ||
+ | #* "jumps" | ||
+ | #* "over" | ||
+ | #* "the" | ||
+ | #* "lazy" | ||
+ | #* "dog" | ||
+ | # Print myList to see if all the words were added | ||
+ | # Delete the word "brown" from the list by using '''del myList[ LOCATION ]''' | ||
+ | # Now delete the word "over" | ||
+ | # Print myList again to see if the words were deleted |
Revision as of 08:12, 17 May 2010
Objectives
- You will learn to create lists
- You will learn to access elements (things) in a list
- You will learn to traverse (walk through) a list using a loop
Resources
Directions
Create a file name "ch9_yourLastName.py". In your file, complete the following exercises:
List Basics:
- Create a list named wordList that contains the following words: "washington", "lee", "generals", "arlington", "virginia"
- Print out the first element in wordList
- Print out the length of the list by using len(wordList)
- Print out the last element in wordList by using wordList[len(wordList)-1]
- Print out the word "generals" in the list
Lists with loops:
- Create a list named numList with the following numbers 3, 1, 7, 6, 4, 1, 1, 5, 4, 7, 9, 0, 9, 7, 7, 43, 2, 6, 87, 67, 4, 2, 532
- Print out the last element in numList using len(numList)-1
- Print out the middle element in numList using len(numList) / 2
- Write a loop that goes from 0 to len(numList)-1 that prints out ALL the elements in numList
- Write a loop that goes from len(numList)-1 down through 0 and prints numList in reverse order
- Write a loop that prints out all the numbers in numList that are greater than 10
- Write a loop that prints out all the numbers in numList that are even (HINT: if numList[x]%2 == 0)
Adding and removing elements:
- Create an empty list [] named myList
- Add the following words to myList by using myList.append( WORD )
- "The"
- "quick"
- "brown"
- "fox"
- "jumps"
- "over"
- "the"
- "lazy"
- "dog"
- Print myList to see if all the words were added
- Delete the word "brown" from the list by using del myList[ LOCATION ]
- Now delete the word "over"
- Print myList again to see if the words were deleted