0% found this document useful (0 votes)
2 views8 pages

23BBS0100 Py Da2

This document contains a series of Python programming exercises aimed at practicing various concepts such as list manipulation, tuple handling, dictionary operations, and set operations. Each exercise includes a brief description followed by a sample code implementation. The tasks range from calculating sums and finding elements to merging lists and working with sets.

Uploaded by

ashutoshdash.p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

23BBS0100 Py Da2

This document contains a series of Python programming exercises aimed at practicing various concepts such as list manipulation, tuple handling, dictionary operations, and set operations. Each exercise includes a brief description followed by a sample code implementation. The tasks range from calculating sums and finding elements to merging lists and working with sets.

Uploaded by

ashutoshdash.p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Programming in Python

Lab Assignment-2
Ashutosh Dash
23BBS0100

1) Write a Python program to print sum of numbers presents inside list.

l=eval(input("Enter List: "))


sum=0
for i in range(0,len(l)):
sum=sum+l[i]
print("Sum of Elements: ",sum)

2) Write a Python program to find the second largest number in a list.

l=eval(input("Enter List: "))


sum=0
for i in range(0,len(l)):
for j in range(0,i):
if l[i]>l[j]:
temp=l[j]
l[j]=l[i]
l[i]=temp
print("Second Largest Element is ",l[1])

3) Write a Python program to access the index of a list.

l=eval(input("Enter List: "))


ele=eval(input("Enter Element to Search: "))
if ele in l:
print("Element Found")
print("Found at Index ",l.index(ele))
4) Write a Python program to append a list to the second list.

l1=eval(input("Enter List 1: "))


l2=eval(inputWrite a Python program to access the index of a list.Enter List 2: "))
l1.append(l2)
print("Appended List: ",l1)

5) Write a Python program to access the index of a list.

l=eval(input("Enter List: "))


ele=eval(input("Enter Element to Search: "))
if ele in l:
print("Element Found")
print("Found at Index ",l.index(ele))

6) Write a Python program to calculate the difference between the two lists.

l1=eval(input("Enter List 1: "))


l2=eval(input("Enter List 2: "))
l3=[]
for i in range (0,len(l1)):
if l1[i] not in l2:
if l1[i] not in l3:
l3.append(l1[i])
for m in range (0,len(l2)):
if l2[m] not in l1:
if l2[m] not in l3:
l3.append(l2[m])
for j in range (0,len(l2)):
if l2[j] in l1:
if l2[j] not in l3:
l3.append(l2[j])
print("Different Elements are ",l3)
7) Python Program to Count the Occurrence of an Item in a List

l=eval(input("Enter List: "))


ele=eval(input("Enter Element to Search: "))
ctr=0
if ele in l:
print("Element Found")
for i in range(0,len(l)):
if l[i]==ele:
ctr=ctr+1
print(ele," Occured ",ctr," Times")

8) Write a Python program to find common items in two lists

l1=eval(input("Enter List 1: "))


l2=eval(input("Enter List 2: "))
l3=[]
for i in range (0,len(l1)):
if l1[i] in l2:
if l1[i] not in l3:
l3.append(l1[i])
print("Common element: ",l3)

9) In a supermarket there are two sections S1 and S2. The sales details of item1 to item n of
section1 and item1 to item p of section2 are maintained in a sorted order. Write a program
to merge the elements of the two sorted lists to form the consolidated list.

S1=eval(input("Enter List 1: "))


S2=eval(input("Enter List 2: "))
for i in range(0,len(S1)):
for j in range(0,i):
if S1[i]>S1[j]:
temp=S1[j]
S1[j]=S1[i]
S1[i]=temp
for k in range(0,len(S2)):
for l in range(0,k):
if S2[k]>S2[l]:
temp=S2[l]
S2[l]=S2[k]
S2[k]=temp
s3=[]
s3.extend(S1)
s3.extend(S2)
print("The Consolidated Sorted list is ",s3)

10) Write a program to demonstrate working with tuples in python.

t1=(1,2,3,4,5,6)
l1=list(t1)
print(l1)

11) Write a Python program to convert a list to a tuple.

l1=[1,2,3,4,5,6]
t1=tuple(l1)
print(t1)

12) Python program to find the size of a Tuple

t1=eval(input("Enter Tuple: "))


n=len(t1)
print("Size of Tuple is ",n)
13) Write a Python program to get the 3rd element from the last element of a tuple

t=eval(input("Enter Tuple: "))


l=list(t)
ele=l[-3]
print("3rd Last element is ",ele)

14) Write a Python program to replace the last value of tuples in a list.
Sample list: [(10, 20, 40), (40, 50, 60), (70, 80, 90)]
Expected Output: [(10, 20, 100), (40, 50, 100), (70, 80, 100)]

s1=[(10, 20, 40), (40, 50, 60), (70, 80, 90)]


for i in range(0,len(s1)):
l1=list(s1[i])
l1[-1]=100
s1[i]=tuple(l1)
print("New List: ",s1)

15) Write a python program to find the sum of all items in a dictionary.

d={'A':1,'B':2,'C':3,'D':4}
sum=0
l=d.keys()
for i in d:
sum=sum+d[i]
print("Sum of Values is ",sum)
16) Given two Python sets, write a Python program to update the first set with items that exist
only in the first set and not in the second set. DO NOT USE ANY BUILT-IN FUNCTIONS

a={1,2,3,4,5,6}
b={3,4,5,6,7,8}
s=set()
for i in a:
if i not in b:
s.add(i)
a.clear()
a.update(s)
print("New Set A is ",a)

17) Write a Python script to concatenate the following dictionaries to create a new one.
Sample Dictionary :
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50, 6:60}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}

dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50, 6:60}
final_dic={}
final_dic.update(dic1)
final_dic.update(dic2)
final_dic.update(dic3)
print("New Dictionary: ",final_dic)

18) Write a Python program to find the highest 3 values of corresponding keys in a dictionary

my_dict = {'a': 30, 'b': 50, 'c': 20, 'd': 70, 'e': 40}
items_list = list(my_dict.items())
for i in range(len(items_list)):
for j in range(i + 1, len(items_list)):
if items_list[i][1] < items_list[j][1]:
items_list[i], items_list[j] = items_list[j], items_list[i]

top_3_items = items_list[:3]
result_dict = dict(top_3_items)
print("Top 3 values of corresponding keys:", result_dict)
19) Write a Python program to create a set.

my_set = {1, 2, 3, 4, 5}
print("Created Set:", my_set)

20) Write a Python program to add and remove a member(s) to a set

my_set = {1, 2, 3, 4, 5}
my_set.add(6)
print("Set after adding 6:", my_set)
my_set.remove(3)
print("Set after removing 3:", my_set)

21) Remove items from set1 that are not common to both set1 and set2 without using any built-in
functions.

set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
common_items = set1.intersection(set2)
set1 = set1.intersection(common_items)
print("Set1 after removing non-common items:", set1)

22) Write a Python program to create a union of sets.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print("Union of sets:", union_set)
23) Write a Python program to check if a set is a subset of another set.set1 = {1, 2, 3}

set2 = {1, 2, 3, 4, 5}
is_subset = set1.issubset(set2)
print("Is set1 a subset of set2?", is_subset)

24) Write a Python program that uses frozensets.

set1 = {1, 2, 3}
frozen_set = frozenset(set1)
print("Original set:", set1)
print("Frozen set:", frozen_set)

25) Write a Python program to find the maximum and minimum values in a set.

my_set = {5, 2, 8, 1, 10}


max_value = max(my_set)
min_value = min(my_set)
print("Maximum value in the set:", max_value)
print("Minimum value in the set:", min_value)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy