Python Programs

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Dictionary Programs:

1. To find the value assigned to the key using get() method:

student_name={1:"Rohit", 2:"Rahul", 3:"Shawn", 4:"Chris",


5:"Karl"}
i=int(input("Enter the key value: "))
print("required stored value: ", student_name.get(i))

2. To find the key assigned to the value:

student_name={"Rohit":1, "Rahul":2, "Shawn":3, "Chris":4 ,


"Karl":5}
i=int(input("Enter the value: "))
for key in student_name:
if(student_name[key]==i):
v=key

print("Key assigned to the value: ",v)

List Programs
1. Enter a list and input 1 to add an element, 2 to delete an
element and 3 to exit the program (it should happen
endlessly until 3 is inputed):
l=[]
a=0
f=0
n=int(input("Enter the number of elements: "))
for i in range(0,n,1):
a=int(input("enter number: "))
l.append(a)
while(True):
c=int(input("Enter 1 to insert an element, 2 to delete an
element, 3 to exit the program: "))
if(c==1):
x=input("enter the element to enter into a list: ")
l.append(x)
print("new list: ")
f+=1
for i in range(0,n+f,1):
print(l[i])
elif(c==2):
x=int(input("Enter the index number of the element to
remove from the list: "))
l.pop(x)
f+=1
print("New list: ")
for i in range(0,n-f,1):
print(l[i])
elif(c==3):
break

2. To find the sum of prime numbers in a list:


def prime(n):
k=0
for i in range(1,n+1,1):
if (n%i==0):
k+=1
if(k==2):
return True
else:
return False
l=[]
a=0
sum=0
n=int(input("Enter the number of elements: "))
for i in range(0,n,1):
a=int(input("enter number: "))
l.append(a)

for i in range (0,n,1):


if(prime(l[i])==True):
sum=sum+l[i]
else:
continue
print("Sum of the prime numbers in the list: ",sum)

You might also like