-- Worksheet 1 --
[ List ]
(Fill-in-the-blanks and short questions)
Fill-in-the-blanks
Part A: Basics of Lists
1. A list refers to a __________ of data which are normally
__________.
2. Instead of storing related data as separate __________,
we can store them as a __________.
3. When printed as output, lists are ordered __________
4. When declaring a list, square __________ are used.
5. Values in a list are separated by a __________.
Part B: Declaring Lists
6. The syntax for declaring a list is
`listName =
__________`
7. An example of a list that stores five ages is:
`userAge =
__________________________`
8. To declare an empty list, we write:
`listName =
__________`
9. To add values to an empty list, we use the __________()
method.
Part C: Accessing Values
10. List indexes always start from __________.
11. The first item in a list has an index of __________.
12. The last item in a list can be accessed using the index
__________.
13. In the list `userAge = [21, 22, 23, 24, 25]`, the value
accessed by `userAge[1]` is __________.
14. In the same list, the value accessed by `userAge[-2]` is
__________.
Part D: Assigning and Slicing
15. If we write `userAge2 = userAge`, then `userAge2`
becomes __________.
16. The notation `2:4` is known as a __________.
17. In slice notation, the start index is always __________
and the end index is always __________.
18. The slice `userAge[2:4]` results in the list __________.
19. The third number in a slice is called the __________.
20. The slice `userAge[1:5:2]` results in the list
__________.
21. The default starting index for slicing is __________.
22. The default ending index for slicing is __________.
Part E: Modifying Lists
23. To modify an item in a list, we use the syntax:
`listName[__________] = __________`
24. If `userAge = [21, 22, 23, 24, 25]` and we write
`userAge[1] = 5`, the new list is __________.
Part F: Adding and Removing Items
25. The method used to add an item to the end of a list is
__________().
26. If we write `userAge.append(99)`, the value 99 is added
to the __________ of the list.
27. To remove an item from a list, we use the __________
statement.
28. If we write `del userAge[2]`, the __________ item is
removed.
Short questions
1. What is a list in Python, and why is it useful?
_________________________________________
_________________________________________
2. Write the correct syntax to create a list called
`numbers` with values 1, 2, and 3.
_________________________________________
3. Which brackets are used to declare a list?
_________________________________________
4. What is the index of the first item in a list?
_________________________________________
5. Given `userAge = [21, 22, 23, 24, 25]`, what value does
`userAge[2]` return?
_________________________________________
6. Using the same list, what value does `userAge[-1]`
return?
_________________________________________
7. How do you declare an empty list called `items`?
_________________________________________
8. What does the slice `userAge[1:4]` return?
_________________________________________
9. Explain what the slice notation `2:4` means.
_________________________________________
10. What is the result of `userAge[1:5:2]`?
_________________________________________
11. How do you change the value `22` in `userAge` to `5`?
_________________________________________
12. What do the following two commands do:
a)
`userAge.append(99)`
b) `del
userAge[2]`
_________________________________________
_________________________________________
13. Define the term index.
_________________________________________
No comments:
Post a Comment