DESIGNED FOR MY STUDENTS

Thursday, March 6, 2025

LIST - Python lab programs with a detailed explanation

 Certainly! Below are some sample Python lab programs that focus on list operations along with a detailed explanation of the code execution. These programs help in understanding how lists work and how you can manipulate them in Python.


1. Sum of All Elements in a List

Objective: Calculate the sum of all elements in a list.

def sum_of_list(lst):
    return sum(lst)

numbers = [10, 20, 30, 40, 50]
print(f"The sum of the list is: {sum_of_list(numbers)}")

Explanation:

  • sum_of_list(lst): This function takes a list (lst) as an argument and uses the built-in sum() function to calculate the sum of all elements in the list.
  • numbers = [10, 20, 30, 40, 50]: A list numbers is created with some integer values.
  • sum(lst): The sum() function adds up all elements in the list. It is an efficient way to find the sum without needing to iterate through the list manually.
  • Output: The sum of all elements is calculated and printed. The expected output is 150.

2. Find the Maximum and Minimum Element in a List

Objective: Find the largest and smallest elements in a list.

def find_max_min(lst):
    return max(lst), min(lst)

numbers = [1, 2, 3, 4, 5, 100, 99]
maximum, minimum = find_max_min(numbers)
print(f"The maximum element is {maximum}")
print(f"The minimum element is {minimum}")

Explanation:

  • find_max_min(lst): This function takes a list lst as input and returns two values: the maximum and minimum elements of the list using Python's built-in max() and min() functions.
  • numbers = [1, 2, 3, 4, 5, 100, 99]: A list numbers is created.
  • max(lst): Finds the largest element in the list.
  • min(lst): Finds the smallest element in the list.
  • Output: The maximum element (100) and minimum element (1) are printed.

3. Reverse a List

Objective: Reverse a list without using built-in methods.

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

numbers = [1, 2, 3, 4, 5]
print(f"Reversed list: {reverse_list(numbers)}")

Explanation:

  • reverse_list(lst): This function takes a list lst and uses slicing ([::-1]) to reverse the list.
    • The slice [::-1] tells Python to take the entire list but with a step of -1, which effectively reverses the list.
  • numbers = [1, 2, 3, 4, 5]: A list numbers is created.
  • lst[::-1]: This reverses the order of the elements in the list.
  • Output: The reversed list is printed as [5, 4, 3, 2, 1].

4. Remove Duplicates from a List

Objective: Remove duplicate elements from a list.

def remove_duplicates(lst):
    return list(set(lst))

numbers = [1, 2, 2, 3, 4, 4, 5]
print(f"List after removing duplicates: {remove_duplicates(numbers)}")

Explanation:

  • remove_duplicates(lst): This function converts the list lst to a set using set(lst). A set automatically removes duplicate values because sets do not allow duplicates.
    • After converting to a set, we convert it back to a list using list().
  • numbers = [1, 2, 2, 3, 4, 4, 5]: A list with duplicate values is created.
  • set(lst): This removes the duplicates by converting the list to a set, where only unique values remain.
  • Output: The list after removing duplicates is [1, 2, 3, 4, 5] (order is not guaranteed because sets do not maintain the original order of elements).

5. Find Even and Odd Numbers in a List

Objective: Separate even and odd numbers into two different lists.

def separate_even_odd(lst):
    even = [num for num in lst if num % 2 == 0]
    odd = [num for num in lst if num % 2 != 0]
    return even, odd

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even, odd = separate_even_odd(numbers)
print(f"Even numbers: {even}")
print(f"Odd numbers: {odd}")

Explanation:

  • separate_even_odd(lst): This function uses list comprehensions to separate even and odd numbers.
    • even = [num for num in lst if num % 2 == 0]: Creates a list of numbers that are divisible by 2 (even numbers).
    • odd = [num for num in lst if num % 2 != 0]: Creates a list of numbers that are not divisible by 2 (odd numbers).
  • numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: A list numbers containing a mix of even and odd numbers is created.
  • List comprehensions:
    • The even list is created by iterating through the original list and selecting numbers divisible by 2.
    • The odd list is created by selecting numbers that aren't divisible by 2.
  • Output: The even numbers [2, 4, 6, 8, 10] and the odd numbers [1, 3, 5, 7, 9] are printed.

6. Merge Two Lists

Objective: Merge two lists into one.

def merge_lists(lst1, lst2):
    return lst1 + lst2

list1 = [1, 2, 3]
list2 = [4, 5, 6]
print(f"Merged list: {merge_lists(list1, list2)}")

Explanation:

  • merge_lists(lst1, lst2): This function takes two lists (lst1 and lst2) and merges them using the + operator. The + operator concatenates the two lists into one.
  • list1 = [1, 2, 3] and list2 = [4, 5, 6]: Two lists are created.
  • lst1 + lst2: The + operator combines the two lists into a single list.
  • Output: The merged list is [1, 2, 3, 4, 5, 6].

7. Sort a List in Ascending Order

Objective: Sort a list in ascending order using a custom sorting algorithm (Bubble Sort).

def bubble_sort(lst):
    n = len(lst)
    for i in range(n):
        for j in range(0, n-i-1):
            if lst[j] > lst[j+1]:
                lst[j], lst[j+1] = lst[j+1], lst[j]

numbers = [5, 3, 8, 6, 7, 2]
bubble_sort(numbers)
print(f"Sorted list: {numbers}")

Explanation:

  • bubble_sort(lst): This function implements the Bubble Sort algorithm, which repeatedly compares adjacent elements and swaps them if they are in the wrong order.
    • The outer loop (for i in range(n)) iterates through the list n times, where n is the length of the list.
    • The inner loop (for j in range(0, n-i-1)) compares each pair of adjacent elements and swaps them if the left element is larger than the right element.
  • numbers = [5, 3, 8, 6, 7, 2]: A list numbers is created with unordered elements.
  • Bubble Sort Process: The list is progressively sorted by repeatedly comparing adjacent elements and swapping them until the list is fully sorted.
  • Output: The sorted list is [2, 3, 5, 6, 7, 8].

0 comments:

Post a Comment