1.
Write a python program to sum all the items in a
list.
# Define a list of numbers
numbers = [1, 2, 3, 4, 5]
# Calculate the sum of all the items in the list
total_sum = sum(numbers)
# Print the result
print("numbers = [1, 2, 3, 4, 5]")
print("The sum of all items in the list is:", total_sum)
2. Write a python program to multiply all the items in a list.
# Define a list of numbers
numbers = [1, 2, 3, 4, 5]
# Calculate the product of all the items in the list
total_product = 1
for number in numbers:
total_product *= number
# Print the result
print("numbers = [1, 2, 3, 4, 5]")
print("The product of all items in the list is:", total_product)
3. Write a python program to get the largest
number from a list.
# Define a list of numbers
numbers = [10, 45, 22, 7, 99, 33]
# Get the largest number using the max() function
largest_number = max(numbers)
# Print the result
print("numbers = [10, 45, 22, 7, 99, 33]")
print("The largest number in the list is:", largest_number)
4. write a python program to get the smallest number
from a list.
# Define a list of numbers
numbers = [10, 45, 22, 7, 99, 33]
# Get the smallest number using the min() function
smallest_number = min(numbers)
# Print the result
print("numbers = [10, 45, 22, 7, 99, 33]")
print("The smallest number in the list is:", smallest_number)
5. write a python program to count the number of
strings where the string length is 2 or more and the first
and last character are same from a given list of strings.
Sample list:[‘abc’, ‘xyz’, ‘aba’, ‘1221’]
# Define the sample list of strings
sample_list = ['abc', 'xyz', 'aba', '1221']
# Initialize a counter to count the valid strings
count = 0
# Loop through each string in the list
for string in sample_list:
# Check if the length is 2 or more and the first and last characters are the same
if len(string) >= 2 and string[0] == string[-1]:
count += 1
# Print the result
print("sample_list = ['abc', 'xyz', 'aba', '1221']")
print("Number of strings where the first and last characters are the same and
length is 2 or more:", count)
6. Write a python program to get a list sorted in
increasing order by the last element in each tuple from
a given list of no empty tuples. Sample
list:[(2,5),(1,2),(4,4),(2,3),(2,1)] expected result:
[(2,1),(1,2),(2,3),(4,4),(2,5)]
# Define the sample list of tuples
sample_list = [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
# Sort the list by the last element in each tuple using a lambda function
sorted_list = sorted(sample_list, key=lambda x: x[-1])
# Print the sorted list
print("sample_list = [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]")
print("Sorted list by the last element in each tuple:", sorted_list)
7. write a python program to remove duplicates from a
list.
# Define the list with duplicates
sample_list = [1, 2, 3, 4, 2, 5, 3, 6, 4]
# Remove duplicates by converting the list to a set, then back to a list
unique_list = list(set(sample_list))
# Print the result
print("sample list =[1,2,3,4,2,5,3,6,4]")
print("List after removing duplicates:", unique_list)
8. write a python program to check a list is empty or not.
# Define a list
sample_list = []
# Check if the list is empty
if not sample_list:
print("sample list =[]")
print("The list is empty.")
else:
print("samle list =[]")
print("The list is not empty.")
9. write a python program to clone or copy a list.
# Define the original list
original_list = [1, 2, 3, 4, 5]
# Clone the list using the copy() method
cloned_list = original_list.copy()
# Print the original and cloned list
print("Original List:", original_list)
print("Cloned List:", cloned_list)
10. write a python program to find the list of words that
are longer than n from a given list of words.
# Define the list of words
words = ['apple', 'banana', 'kiwi', 'cherry', 'strawberry', 'pear']
# Define the value of n (the length threshold)
n=5
# Use a list comprehension to find words longer than n characters
longer_words = [word for word in words if len(word) > n]
# Print the result
print("words = ['apple', 'banana', 'kiwi', 'cherry', 'strawberry', 'pear']")
print(f"Words longer than {n} characters:", longer_words)