0% found this document useful (0 votes)
7 views9 pages

Divyanshiexp 4 Py

Uploaded by

janvijain996
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)
7 views9 pages

Divyanshiexp 4 Py

Uploaded by

janvijain996
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/ 9

2/7/24, 1:34 PM pythonexperiment4 - Jupyter Notebook

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)

Enter the Number of elements in a list:5


45
56
56
76
676
The list is:
45 56 56 76 676
Sum of all elements in the list is: 909

localhost:8888/notebooks/Desktop/python practice/pythonexperiment4.ipynb# 1/9


2/7/24, 1:34 PM pythonexperiment4 - Jupyter Notebook

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")

Enter the number of Elements:3


88
99
77
The List is:
88 99 77
Enter the element you want to search:99
99 is present in the list at 2 position

localhost:8888/notebooks/Desktop/python practice/pythonexperiment4.ipynb# 2/9


2/7/24, 1:34 PM pythonexperiment4 - Jupyter Notebook

In [5]: """Demonstrate following operations on number list using list functions


a. insert 13 at position 4
b. Sort the list in ascending order
c. delete the last element
d. remove 31
e. Reverse the list
f. Append one number to list
g. extend the list with [20,30,40]
h. print the number of elements in the list using while"""
num=[]
n=int(input("Enter the number of elements but enter more than 4:"))

for i in range(0,n):
ele=int(input())
num.append(ele)

print("The list is:",num)


# a part
print("Insert 13 at 4 position:")
num.insert(3,13)
print(num)
# b part
print("Sorting in ascending order:")
num.sort()
print(num)
# c part
print("Delete the last element:")
num.pop() # to delete last element use pop
print(num)
# d part
print("Remove 31:")
num.remove(31)
print(num)
# e part
print("To reverse a list:")
num.reverse()
print(num)
# f part
print("Append one number to list:")
p=int(input("Enter a number which you want to add in the list:"))
num.append(p)
print(num)
# g part
print("extend the list with [20,30,40] ")
lis=[20,30,40]
num.extend(lis)
print(num)
# g part
print("print the number of elements in the list using while")
i=0
while i<len(num):
print(num[i],end=" ")
i+=1

localhost:8888/notebooks/Desktop/python practice/pythonexperiment4.ipynb# 3/9


2/7/24, 1:34 PM pythonexperiment4 - Jupyter Notebook

Enter the number of elements but enter more than 4:4


35
31
56
96
The list is: [35, 31, 56, 96]
Insert 13 at 4 position:
[35, 31, 56, 13, 96]
Sorting in ascending order:
[13, 31, 35, 56, 96]
Delete the last element:
[13, 31, 35, 56]
Remove 31:
[13, 35, 56]
To reverse a list:
[56, 35, 13]
Append one number to list:
Enter a number which you want to add in the list:34
[56, 35, 13, 34]
extend the list with [20,30,40]
[56, 35, 13, 34, 20, 30, 40]
print the number of elements in the list using while
56 35 13 34 20 30 40

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)

localhost:8888/notebooks/Desktop/python practice/pythonexperiment4.ipynb# 4/9


2/7/24, 1:34 PM pythonexperiment4 - Jupyter Notebook

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)

localhost:8888/notebooks/Desktop/python practice/pythonexperiment4.ipynb# 5/9


2/7/24, 1:34 PM pythonexperiment4 - Jupyter Notebook

Enter the number of elements:2


23
24
{24, 23}
Enter the number of elements:12
1
2
3
4
5
6
7
8
9
10
11
12
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
Intersection of two sets is: set()
Union of two sets is: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 23, 24}
Set Difference is i.e. the elements which are present in a but not b are: {2
4, 23}
Set Difference is i.e. the elements which are present in b but not a are:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
Symmetric difference is: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 23, 24}
Length of set a is: 2
Length of set b is: 12
Maximum value of set a is: 24
Minimum value of set a is: 23
Maximum value of set b is: 12
Minimum value of set b is: 1
set()
set()

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

localhost:8888/notebooks/Desktop/python practice/pythonexperiment4.ipynb# 6/9


2/7/24, 1:34 PM pythonexperiment4 - Jupyter Notebook

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)

Enter the number of elemnts:2


Enter key:23
Enter value:55
Enter key:66
Enter value:66
{'23': 55, '66': 66}
Sum of all items in dictionary is: 121

localhost:8888/notebooks/Desktop/python practice/pythonexperiment4.ipynb# 7/9


2/7/24, 1:34 PM pythonexperiment4 - Jupyter Notebook

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)

Enter the number of elements:3


Enter SAP ID:34
Enter student marks:33
Enter SAP ID:66
Enter student marks:77
Enter SAP ID:017
Enter student marks:66
{34: 33.0, 66: 77.0, 17: 66.0}
Display all the keys: dict_keys([34, 66, 17])
Display all the values: dict_values([33.0, 77.0, 66.0])
Enter the SAP ID to modify the grade:017
Enter the grade:66
{34: 33.0, 66: 77.0, 17: 66}
Enter the SAP ID you want to remove:017
{34: 33.0, 66: 77.0}
When given 5 marks in bonus: {34: 38.0, 66: 82.0}
Length of the dictionary is: 2
Copy of the dictionary is: {34: 38.0, 66: 82.0}

localhost:8888/notebooks/Desktop/python practice/pythonexperiment4.ipynb# 8/9


2/7/24, 1:34 PM pythonexperiment4 - Jupyter Notebook

In [10]: # Write a program to sort the dictionary in order of the keys.


f={}
n=int(input("Enter the number of elements:"))

for i in range(0,n):
k=int(input("Enter key:"))
v=input("Enter value:")

f[k]=v
print(f)
g=list(f.keys())
g.sort()
b={i:f[i] for i in g}
print("Sorted Dictionary is:",b)

Enter the number of elements:2


Enter key:23
Enter value:22
Enter key:77
Enter value:99
{23: '22', 77: '99'}
Sorted Dictionary is: {23: '22', 77: '99'}


localhost:8888/notebooks/Desktop/python practice/pythonexperiment4.ipynb# 9/9

You might also like