Python Programs on Lists
Write a python program to perform following operations on a list of integers.
- Print the total number of items in the list.
- Print the last item in the list.
- Print the list in reverse order.
- Print Yes if the list contains a 5 and No otherwise.
- Print the number of occurrences of a element in the list.
- Remove the first and last items from the list and sort the remaining items.
- Print how many integers in the list is less than a given value?
- Print the average of the elements in the list.
- Print the largest and smallest value in the list.
Logic:
- The len() function returns the number of characters in a string, including spaces and special characters.
- list[-1]: Access the last element using the index -1.
- Use list slicing with a step of -1 to reverse the list.
list[::-1] - Use the in operator to check if 5 is present in the list.
- Use the count() method to count occurrences of a specified element.
- Slice the list to exclude the first and last elements list[1:-1], then use the sort() method to sort the remaining elements.
- Use a list comprehension or loop to count elements less than the given value (e.g., 7).
- Compute the sum of elements using sum() and divide by the number of elements.
- Use the max() and min() functions to find the largest and smallest values.
Program:
########## Lists #################
# Sample list of integers
numbers = [10, 3, 5, 8, 5, 12, 1]
# (b) Print the total number of items in the list
print("Total number of items in the list:", len(numbers))
# (c) Print the last item in the list
print("Last item in the list:", numbers[-1])
# (d) Print the list in reverse order
print("List in reverse order:", numbers[::-1])
# (e) Print Yes if the list contains a 5, No otherwise
if 5 in numbers:
print("Yes")
else:
print("No")
# (f) Print the number of occurrences of an element (e.g., 5)
element = 5
print("Number of occurrences of {element}:", numbers.count(element))
# (g) Remove the first and last items, then sort the remaining items
modified_list = numbers[1:-1]
modified_list.sort()
print("List after removing first and last items and sorting:", modified_list)
# (h) Print how many integers are less than a given value (e.g., 7)
given_value = 7
count_less_than = sum(1 for num in numbers if num < given_value)
print("Number of integers less than {given_value}:", count_less_than)
# (i) Print the average of the elements in the list
average = sum(numbers) / len(numbers)
print("Average of the elements:", average)
# (j) Print the largest and smallest value in the list
print("Largest value:", max(numbers))
print("Smallest value:", min(numbers))
Output:
Total number of items in the list: 7
Last item in the list: 1
List in reverse order: [1, 12, 5, 8, 5, 3, 10]
Yes
Number of occurrences of 5: 2
List after removing first and last items and sorting: [3, 5, 5, 8]
Number of integers less than 7: 4
Average of the elements: 6.285714285714286
Largest value: 12
Smallest value: 1
Last item in the list: 1
List in reverse order: [1, 12, 5, 8, 5, 3, 10]
Yes
Number of occurrences of 5: 2
List after removing first and last items and sorting: [3, 5, 5, 8]
Number of integers less than 7: 4
Average of the elements: 6.285714285714286
Largest value: 12
Smallest value: 1