0% found this document useful (0 votes)
37 views4 pages

PWP 6

The document contains examples of Python programs to perform common list operations like summing items, multiplying items, finding the largest/smallest item, reversing a list, finding common items between lists, and selecting even items from a list. Each example includes the program code and sample output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views4 pages

PWP 6

The document contains examples of Python programs to perform common list operations like summing items, multiplying items, finding the largest/smallest item, reversing a list, finding common items between lists, and selecting even items from a list. Each example includes the program code and sample output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

PRACTICAL NO.

XI] EXERCISE:
1) Write a Python Program to sum all the items in a list

 Program Code:

# Function to calculate the sum of items in a list


def sum_of_list(lst):
total = 0
for item in lst:
total += item
return total

# Example list
numbers = [10, 20, 30, 40, 50]

# Calculate and print the sum of items in the list


result = sum_of_list(numbers)
print(f"The sum of items in the list is: {result}")

OUTPUT

2) Write a Python Program to multiplies all items in the list

 Program Code:

def product_of_list(lst):
result = 1
for item in lst:
result *= item
return result
# Example list
numbers = [2, 3, 4, 5]

# Calculate and print the product of items in the list


result = product_of_list(numbers)
print(f"The product of items in the list is: {result}")

OUTPUT
3) Write a python program to get the largest number from a list

 Program Code:

# Function to find the largest number in a list


def find_largest(lst):
if not lst:
return None # Return None for an empty list

largest = lst[0]
for number in lst[1:]:
if number > largest:
largest = number
return largest

# Example list
numbers = [15, 7, 22, 35, 10]

# Find and print the largest number in the list


largest_number = find_largest(numbers)

if largest_number is not None:


print(f"The largest number in the list is: {largest_number}")
else:
print("The list is empty.")

OUTPUT

4) Write a python program to get the smallest from the list

 Program Code:

# Function to find the smallest number in a list


def find_smallest(lst):
if not lst:
return None # Return None for an empty list

smallest = lst[0]
for number in lst[1:]:
if number < smallest:
smallest = number
return smallest
# Example list
numbers = [15, 7, 22, 35, 10]
# Find and print the smallest number in the list
smallest_number = find_smallest(numbers)

if smallest_number is not None:


print(f"The smallest number in the list is: {smallest_number}")
else:
print("The list is empty.")
OUTPUT
5) Write a python program to reverse a list

Program Code:

def reverse_list(lst):
return lst[::-1]

# Example list
original_list = [1, 2, 3, 4, 5]
# Reverse the list and print the result
reversed_list = reverse_list(original_list)
print(f"Original List: {original_list}")
print(f"Reversed List: {reversed_list}")

OUTPUT

6) Write a python program to find common items from two lists

 Program Code:
# Function to find common items between two lists
def find_common_items(list1, list2):
common_items = set(list1) & set(list2)
return list(common_items)

# Example lists
list1 = [1, 2, 3, 4, 5]
list2 = [3, 4, 5, 6, 7]

# Find and print the common items between the two lists
common_items = find_common_items(list1, list2)
print(f"List 1: {list1}")
print(f"List 2: {list2}")
print(f"Common Items: {common_items}")

OUTPUT
7) Write a python program to select the even items from a list

 Program Code:

def select_even_items(lst):
even_items = [item for item in lst if item % 2 == 0]
return even_items
original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_items = select_even_items(original_list)
print(f"Original List: {original_list}")
print(f"Even Items: {even_items}")

OUTPUT

You might also like