Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

COMPUTATIONAL

STATISTICS

NAME: KUNAL DAS


STREAM: CSBS
ROLLNO: 27
UNIVERSITYROLLNO: 10931121027
SUBJECTCODE: BSC-391
1.Write a Python program to add a key to a
dictionary.

Sample Dictionary : {0: 10, 1: 20}


Expected Result : {0: 10, 1: 20, 2: 30}

Programe=>
d =
print(d)
d.update({2:30}{0:10, 1:20})
print(d)

output=>
{0:10, 1:20}
{0:10, 1:20, 2:30}

2. Write a Python program to concatenate 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}

Programe=>
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
dic4 = {}
for d in (dic1, dic2, dic3): dic4.update(d)
print(dic4)
output=>
{1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
3. Write a Python program to check whether a given
key
already exists in a dictionary.
Programe=>
d = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
def is_key_present(x):
if x in d:
print('Key is present in the dictionary')
else:
print('Key is not present in the dictionary')
is_key_present(5)
is_key_present(9)

output=>
Key is present in the dictionary
Key is not present in the dictionary

4. Write a Python program to generate and print a dictionary


that contains a number (between 1 and n) in the form (x, x*x).

Sample Dictionary ( n = 5)
Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Programe=>
n=int(input("Input a number "))
d = dict()

for x in range(1,n+1):
d[x]=x*x

print(d)

output=>
5
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
5. Write a Python program to remove a key from a
dictionary.

Programe=>
myDict = {'a':1,'b':2,'c':3,'d':4}
print(myDict)
if 'a' in myDict:
del myDict['a']
print(myDict)

output=>
{'a':1,'b':2,'c':3,'d':4}
{'b':2,'c':3,'d':4}

You might also like