Divyanshiexp 4 Py
Divyanshiexp 4 Py
In [1]: # Read and display list (using loop) with sum all the items in a list
lst=[]
s=0
n=int(input("Enter the Number of elements in a list:"))
for i in range(0,n):
ele=int(input())
lst.append(ele)
print("The list is:")
for i in range(0,n):
print(lst[i],end=" ")
for i in range(0,n):
s+=lst[i]
print(end="\n")
print("Sum of all elements in the list is:",s)
In [3]: # Implement linear search using list (without using list functions).
l=[]
n=int(input("Enter the number of Elements:"))
for i in range(0,n):
ele=int(input())
l.append(ele)
print("The List is:")
for i in range(0,n):
print(l[i],end=" ")
print(end="\n")
s=int(input("Enter the element you want to search:"))
c=-1
for i in range(0,n):
if(l[i]==s):
c=i
if(c>=0):
print(s,"is present in the list at",c+1,"position")
else:
print(s,"is not present in the list")
In [ ]: # Create a tuple and find the minimum and maximum number from it.
t=[]
n=int(input("Enter the elements:"))
for i in range(0,n):
ele=int(input())
t.append(ele)
t=tuple(t)
print("Tuple is:",t)
a=min(t)
b=max(t)
print("The maximum and minimum values in tuple are:",a,",",b)
In [7]: """Create a set, add member(s) in a set and perform following operations: inte
sets, union of sets, set difference, symmetric difference, find length, maximu
minimum value in a set and clear a set. """
a=set()
n=int(input("Enter the number of elements:"))
for i in range(0,n):
el=int(input())
a.add(el)
print(a)
b=set()
p=int(input("Enter the number of elements:"))
for j in range(0,p):
ele=int(input())
b.add(ele)
print(b)
c=a.intersection(b)
print("Intersection of two sets is:",c)
s=a.union(b)
print("Union of two sets is:",s)
z=a.difference(b)
print("Set Difference is i.e. the elements which are present in a but not b ar
k=b.difference(a)
print("Set Difference is i.e. the elements which are present in b but not a ar
h=a.symmetric_difference(b)
print("Symmetric difference is:",h)
print("Length of set a is:",len(a))
print("Length of set b is:",len(b))
print("Maximum value of set a is:",max(a))
print("Minimum value of set a is:",min(a))
print("Maximum value of set b is:",max(b))
print("Minimum value of set b is:",min(b))
a.clear()
b.clear()
print(a)
print(b)
In [11]: """Create dictionary with day number as key and day as value & display it. (ex
days={1:"Monday",2:"Tuesday",3:"Wednesday",4:"Thursday",5:"Friday",6:"Saturday
for day_number,day in days.items():
print(day_number,day)
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday
In [8]: # Write a Python program to find the sum of all items in the dictionary.
u={}
n=int(input("Enter the number of elemnts:"))
for i in range(0,n):
k=input("Enter key:")
v=int(input("Enter value:"))
u[k]=v
print(u)
s=sum(u.values())
print("Sum of all items in dictionary is:",s)
In [9]: s={}
n=int(input("Enter the number of elements:"))
for i in range(0,n):
sapid=int(input("Enter SAP ID:"))
sm=float(input("Enter student marks:"))
s[sapid]=sm
print(s)
# a. Display all the keys
print("Display all the keys:",s.keys())
# b. Display all the values
print("Display all the values:",s.values())
# c. Take the sapid as the input and modify the grade given by use
i=int(input("Enter the SAP ID to modify the grade:"))
g=int(input("Enter the grade:"))
s[i]=g
print(s)
# d. Take the sapid from the user to remove that user from the dictionary
u=int(input("Enter the SAP ID you want to remove:"))
s.pop(u)
print(s)
# e. Give 5 marks as the bonus to all the students and display the new marks
for sapid,sm in s.items():
s[sapid]+=5
print("When given 5 marks in bonus:",s)
# f. Find the length of the dictionary using len function
print("Length of the dictionary is:",len(s))
# g. Create a new copy of dictionary using copy method
y=s.copy()
print("Copy of the dictionary is:",y)
f[k]=v
print(f)
g=list(f.keys())
g.sort()
b={i:f[i] for i in g}
print("Sorted Dictionary is:",b)