Lists Basic
Lists Basic
Programming Questions
1. Write a Python program to create a list numbers containing the numbers 1, 2, 3, 4, and 5. Then,
print the sum of all the numbers in the list. (2 marks)
Answer:
Python
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
2. Write a Python program to create a list fruits containing the names of five fruits. Then, print the
name of the fruit at index 2. (2 marks)
Answer:
Python
3. Write a Python program to create a list numbers containing the numbers 1, 2, 3, 4, and 5. Then,
use a for loop to print each number in the list. (2 marks)
Answer:
Python
numbers = [1, 2, 3, 4, 5]
print(num)
4. Write a Python program to create a list words containing five words. Then, use the sort()
method to sort the words in alphabetical order and print the sorted list. (2 marks)
Answer:
Python
5. Write a Python program to create a list numbers containing the numbers 1, 2, 3, 4, and 5. Then,
use the reverse() method to reverse the order of the numbers in the list and print the reversed
list. (2 marks)
Answer:
Python
numbers = [1, 2, 3, 4, 5]
numbers.reverse()
6. Write a Python program to create a list fruits containing the names of five fruits. Then, use the
index() method to find the index of the fruit "banana" in the list and print the index. (2 marks)
Answer:
Python
index = fruits.index("banana")
7. Write a Python program to create a list numbers containing the numbers 1, 2, 3, 4, and 5. Then,
use the count() method to count the number of occurrences of the number 3 in the list and print
the count. (2 marks)
Answer:
Python
numbers = [1, 2, 3, 4, 5]
count = numbers.count(3)
8. Write a Python program to create a list words containing five words. Then, use the join()
method to concatenate the words in the list into a single string and print the concatenated
string. (2 marks)
Answer:
Python
9. Write a Python program to create a list numbers containing the numbers 1, 2, 3, 4, and 5. Then,
use a for loop to print the square of each number in the list. (2 marks)
Answer:
Python
numbers = [1, 2, 3, 4, 5]
10. Write a Python program to create a list fruits containing the names of five fruits. Then, use the
sort() method to sort the fruits in alphabetical order and print the sorted list. Finally, use the
reverse() method to reverse the order of the fruits in the list and print the reversed list. (2
marks)
Answer:
Python
fruits.sort()
fruits.reverse()