Name :- Odedara Bhavesh P.
Roll_no:- 35
Sem :- 5
Div :- A
Email :- [email protected]
Assignment-1
1. Python program to interchange first and last
elements in a list.
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
def interchnage(lst):
if len(lst) < 2:
return lst
lst[0], lst[-1] = lst[-1], lst[0]
return lst
original_list = [10, 20, 30, 40, 50]
print("Original List:", original_list)
interchnage_lst = interchnage(original_list)
print("Interchange List :", interchnage_lst)
2. Python program to swap two elements in a list
def swapPositions(list, pos1, pos2):
list[pos1], list[pos2] = list[pos2], list[pos1]
return list
List = [22, 44, 66, 88]
pos1, pos2 = 1, 3
2
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
print(swapPositions(List, pos1-1, pos2-1))
3. Python | Ways to find length of list.
my_list = [1, 2, 3, 4, 5]
length = len(my_list)
print("Length of the list:", length)
4. Python | Ways to check if element exists in list.
my_list = [1, 2, 3, 4, 5, 6, 7]
i = 4
if i in my_list:
print("Exists")
else:
print("Not Exists")
3
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
5. Different ways to clear a list in Python.
my_list = [1, 2, 3, 4, 5, 6]
print("Before :", my_list)
my_list.clear()
print("After :", my_list)
6. Python | Reversing a List.
my_list = [1, 2, 3, 4, 5]
my_list.reverse()
print(my_list)
4
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
7. Python program to find sum of elements in list.
my_list = [1, 21, 13, 14, 15]
result = sum(my_list)
print("Sum of elements in the list:", result)
8. Python | Multiply all numbers in the list.
def multilist(mylist):
result = 1
for x in mylist:
result = result*x
return result
5
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
print("Sum of List1 :", multilist(list1))
print("Sum of List2 :", multilist(list2))
9. Python program to find smallest number in a list.
def find_smallest_number(lst):
smallest = min(lst)
return smallest
my_list = [5, 2, -1, 1, 7, 3]
result = find_smallest_number(my_list)
print("Smallest Element is :", result)
6
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
10. Python program to find largest number in a list.
def find_largest_number(lst):
if not lst:
return None
largest_number = max(lst)
return largest_number
my_list = [12, 45, 7, 23, 98, 56]
largest = find_largest_number(my_list)
print("largest Element is :", largest)
11. Python program to find second largest number
in a list.
def second_largest_number(lst):
largest = max(lst)
lst.remove(largest)
second_largest = max(lst)
return second_largest
my_list = [3, 6, 7, 23, 75, 46, 86]
result = second_largest_number(my_list)
7
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
print("Second largest number is ", result)
12. Python program to find N largest elements
from a list.
def N_number_max(lst, n):
sort_list = sorted(lst, reverse=True)
largest_elements = sort_list[:n]
return largest_elements
my_list = [10, 20, 53, 643, 74, 5]
n = 4
result = N_number_max(my_list, n)
print("{n} largest elements is the list ", result)
8
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
13. Python program to print even numbers in a list.
def even_numbers(lst):
for num in lst:
if num % 2 == 0:
print(num)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("Even numbers in the list:")
even_numbers(my_list)
14. Python program to print odd numbers in a List.
def odd_numbers(lst):
for num in lst:
if num % 2 != 0:
print(num)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("Even numbers in the list:")
odd_numbers(my_list)
9
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
15. Python program to print all even numbers in a
range.
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
for num in range(start, end + 1):
if num % 2 == 0:
print(num, end=" ")
16. Python program to print all odd numbers in a
range.
start = int(input("Enter the start of range: "))
10
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
end = int(input("Enter the end of range: "))
for num in range(start, end + 1):
if num % 2 != 0:
print(num, end=" ")
17. Python program to print positive numbers in a
list.
def positive_numbers(lst):
for num in lst:
if num > 0:
print(num)
my_list = [1, 3, -4, -6, 7, 9, -10]
print("Positive numbers in the list:")
positive_numbers(my_list)
11
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
18. Python program to print negative numbers in a
list.
def negative_numbers(lst):
for num in lst:
if num < 0:
print(num)
my_list = [1, 3, -4, -6, 7, 9, -10]
print("Negative numbers in the list:")
negative_numbers(my_list)
12
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
19. Python program to print all positive numbers in
a range.
start, end = -4, 19
for num in range(start, end + 1):
if num >= 0:
print(num, end=" ")
20. Python program to print all negative numbers
in a range.
start, end = -7, 10
for num in range(start, end + 1):
if num < 0:
print(num, end=" ")
13
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
21. Remove multiple elements from a list in
Python.
def remove_elements(lst, elements_remove):
for elem in elements_remove:
while elem in lst:
lst.remove(elem)
my_list = [1, 2, 3, 4, 5]
elements_remove = [2, 4]
remove_elements(my_list, elements_remove)
print("List removing elements:", elements_remove)
print("List after removing elements:", my_list)
22. Python – Remove empty List from List.
def rmv_empty_lists(lst):
new_list = [sublist for sublist in lst if sublist]
return new_list
my_list = [1, 2, [], 3, [], [], 4, []]
result = rmv_empty_lists(my_list)
print("List after removing empty lists:", result)
14
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
23. Python | Cloning or Copying a list.
# Python program to copy or clone a list
# Using the Slice Operator
def Cloning(li1):
li_copy = li1[:]
return li_copy
li1 = [4, 8, 2, 1, 5, 8]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)
15
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
24. Python | Count occurrences of an element in a
list.
def elements_count(lst, element):
return lst.count(element)
my_list = [1, 2, 3, 3, 4, 5, 4, 3, 2, 1, 3, 4, 3, 2]
el_count = 3
result = elements_count(my_list, el_count)
print(f"The element {el_count} occurs {result} times in the list.")
25. Python | Remove empty tuples from a list.
def rmv_empty_tuples(lst):
new_list = [tup for tup in lst if tup]
return new_list
my_list = [(), (1, 2), (), (), (3, 4), ('yah', '29')]
result = rmv_empty_tuples(my_list)
print("List after removing empty tuples:", result)
16
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
26. Python | Program to print duplicates from a list
of integers.
lis = [1, 2, 3, 4, 5, 6, 5, 4, 2, 4, 3, 2, 4, 3, 3, 5, 3, 3, 2, 4]
uniList = []
dupList = []
for i in lis:
if i not in uniList:
uniList.append(i)
elif i not in dupList:
dupList.append(i)
print(dupList)
17
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
27. Python program to find Cumulative sum of a
list.
def cumulative_sum(lst):
cum_sum = []
total_sum = 0
for num in lst:
total_sum += num
cum_sum.append(total_sum)
return cum_sum
my_list = [1, 2, 3, 4, 5]
result = cumulative_sum(my_list)
print("Cumulative sum of the list:", result)
28. Python | Sum of number digits in List.
test_list = [12, 63, 16, 00]
print("The original list is : " + str(test_list))
18
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
res = []
for ele in test_list:
sum = 0
for digit in str(ele):
sum += int(digit)
res.append(sum)
print("List Integer Summation : " + str(res))
29. Break a list into chunks of size N in Python.
my_list = [1, 2, 3, 4, 5,
6, 7, 8, 9, 10]
start = 0
end = len(my_list)
step = 3
for i in range(start, end, step):
x = i
print(my_list[x:x+step])
19
Name:- Odedara Bhavesh p.
Roll_no :- 35
Div :- A
30. Python | Sort the values of first list using
second list.
list1 = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
list2 = [0, 1, 1, 0, 1, 2, 2, 0, 1]
a = list(set(list2))
a.sort()
res = []
for i in a:
for j in range(0, len(list2)):
if (list2[j] == i):
res.append(list1[j])
print(res)
20