
Some simple Python exercisesđ
1. Sum of List Elements
Problem Description
Sum of List Elements
Write a Python function that calculates the sum of all elements in a given list of integers.
Parameters:
numbers
 (List of integers): The input list containing integers.
Returns:
- An integer representing the sum of all elements in the input list.
Example:
- Input:Â
numbers = [1, 2, 3, 4, 5]
Output:Â15
- Input:Â
numbers = [10, -5, 7, 8, -2]
Output:Â18
def sum_list(numbers):
sum=0
for n in numbers:
sum=sum+n
return sum
2. Largest Element in a List
Find the Largest Element in a List
Write a Python function that finds and returns the largest element in a given list of integers.
Parameters:
numbers
 (List of integers): The input list containing integers.
Returns:
- An integer representing the largest element in the input list.
Example:
- Input:Â
numbers = [3, 8, 2, 10, 5]
Output:Â10
- Input:Â
numbers = [-5, -10, -2, -1, -7]
Output:Â-1
def find_largest(numbers):
max=numbers[0]
for n in numbers:
if(n>max):
max=n
return max
3. Remove Duplicate in a List
Remove Duplicates from a List
You are given a list of integers. Write a Python program that removes any duplicate elements from the list and returns a new list with only unique elements. The order of elements in the list should be maintained.
Parameters:
lst
 (List of integers): The list of integers from which duplicates should be removed.
Returns:
- A list of integers where all duplicates have been removed, preserving the original order.
Example:
- Input:Â
lst = [1, 2, 2, 3, 4, 4, 5]
Output:Â[1, 2, 3, 4, 5]
- Input:Â
lst = [4, 5, 5, 4, 6, 7]
Output:Â[4, 5, 6, 7]
def remove_duplicates(lst):
set_from_list=set(lst)
my_list=list(set_from_list)
return my_list
4. Check if all elements in a List are Unique
Check if All Elements in a List are Unique
You are given a list of integers. Write a Python program that checks if all elements in the list are unique. If all elements are unique, return True
; otherwise, return False
.
Parameters:
lst
 (List of integers): The list of integers to check for uniqueness.
Returns:
- A boolean valueÂ
True
 if all elements in the list are unique,ÂFalse
 otherwise.
Example:
- Input:Â
lst = [1, 2, 3, 4, 5]
Output:ÂTrue
- Input:Â
lst = [1, 2, 3, 3, 4, 5]
Output:ÂFalse
def check_unique(lst):
total_elements=len(lst)
new_set=set(lst)
total_el_in_set=len(new_set)
return total_el_in_set == total_elements
5. Reverse a List
Reverse a List (Non-Slicing Approach)
You are given a list of integers. Write a Python program that reverses the list without using slicing (lst[::-1]
). The program should return the reversed list.
Parameters:
lst
 (List of integers): The list of integers to be reversed.
Returns:
- A list of integers where the order of elements is reversed from the input list.
Example:
- Input:Â
lst = [1, 2, 3, 4, 5]
Output:Â[5, 4, 3, 2, 1]
def reverse_list(lst):
return [lst[i] for i in range(len(lst) - 1, -1, -1)]
6. Number of Odd and Even Elements in a list
Count Even and Odd Numbers in a List
You are given a list of integers. Write a Python program that counts and returns the number of even and odd numbers in the list.
Parameters:
lst
 (List of integers): The list of integers where you will count the even and odd numbers.
Returns:
- A tupleÂ
(even_count, odd_count)
 whereÂeven_count
 is the number of even numbers andÂodd_count
 is the number of odd numbers.
Example:
- Input:Â
lst = [1, 2, 3, 4, 5]
Output:Â(2, 3)
- There areÂ
2
 even numbers: 2, 4 - There areÂ
3
 odd numbers: 1, 3, 5
- There areÂ
def count_even_odd(lst):
odNum=0
evenNum=0
for num in lst:
if num%2==0:
evenNum+=1
else:
odNum+=1
result=(evenNum,odNum)
return result
7. Check if List is Subset of Another List
Check if a List is a Subset of Another List
You are given two lists of integers. Write a Python program that checks whether the first list is a subset of the second list, without using the in
 keyword. A list is considered a subset if all elements of the first list are present in the second list.
Parameters:
lst1
 (List of integers): The first list, which is being checked as a subset.lst2
 (List of integers): The second list, which is the list to compare against.
Returns:
- A boolean valueÂ
True
 ifÂlst1
 is a subset ofÂlst2
, otherwiseÂFalse
.
Example:
- Input:Â
lst1 = [1, 2, 3]
,Âlst2 = [1, 2, 3, 4, 5]
Output:ÂTrue
- All elements inÂ
lst1
 are present inÂlst2
.
- All elements inÂ
- Input:Â
lst1 = [1, 6]
,Âlst2 = [1, 2, 3, 4, 5]
Output:ÂFalse
- The elementÂ
6
 is not present inÂlst2
.
- The elementÂ
def is_subset(lst1, lst2):
return set(lst1).issubset(lst2)
8. Maximum Difference between 2 consecutive elements in a List
Find Maximum Difference Between Two Consecutive Elements
You are given a list of integers. Write a Python program to find the maximum difference between two consecutive elements in the list. The difference is defined as the absolute value of the difference between two consecutive elements.
Parameters:
lst
 (List of integers): A list of integers.
Returns:
- An integer representing the maximum difference between two consecutive elements.
Example:
- Input:Â
lst = [1, 7, 3, 10, 5]
Output:Â7
- The maximum difference is betweenÂ
3
 andÂ10
 (i.e.,Â|3 - 10| = 7
).
- The maximum difference is betweenÂ
- Input:Â
lst = [10, 11, 15, 3]
Output:Â12
- The maximum difference is betweenÂ
15
 andÂ3
 (i.e.,Â|15 - 3| = 12
).
- The maximum difference is betweenÂ
def max_consecutive_difference(lst):
maxValue = 0
for index, item in enumerate(lst):
if abs(item-lst[index-1])>maxValue:
maxValue= abs(item-lst[index-1])
return maxValue
9. Merge two sorted List
Merge Two Sorted Lists
You are given two sorted lists of integers. Write a Python function to merge these two sorted lists into one sorted list. The resulting list should also be in non-decreasing order.
Parameters:
list1
 (List of integers): The first sorted list.list2
 (List of integers): The second sorted list.
Returns:
- A single list of integers, containing all elements fromÂ
list1
 andÂlist2
, sorted in non-decreasing order.
Example:
- Input:Â
list1 = [1, 3, 5]
,Âlist2 = [2, 4, 6]
Output:Â[1, 2, 3, 4, 5, 6]
- Input:Â
list1 = [1, 4, 7]
,Âlist2 = [2, 3, 5, 8]
Output:Â[1, 2, 3, 4, 5, 7, 8]
def merge_two_sorted_lists(list1, list2):
merged_list= list2+list1
merged_list.sort()
return merged_list
10. Rotate a List
Rotate a List (Without Slicing)
You are given a list of integers and an integer k
. Write a Python function to rotate the list to the right by k
positions without using slicing. A rotation shifts elements from the end of the list to the front.
Parameters:
lst
 (List of integers): The list to be rotated.k
 (Integer): The number of positions to rotate the list.
Returns:
- A list of integers rotated byÂ
k
 positions.
Example:
- Input:Â
lst = [1, 2, 3, 4, 5]
,Âk = 2
Output:Â[4, 5, 1, 2, 3]
- Input:Â
lst = [10, 20, 30, 40, 50]
,Âk = 3
Output:Â[30, 40, 50, 10, 20]
def rotate_list(lst, k):
if not lst:
return []
n = len(lst)
k = k % n
return lst[n - k:] + lst[:n - k]