Below are the set of 20 questions which will help to give the basic knowledge of all type of programming on Data Structure, Lists, Dictionary, set, tuple in Python. You can visit website pynative.com or https://www.geeksforgeeks.org/.
Steps to run below programs:
- Install Anaconda in your PC/ Laptop.
- Open Spyder.
- Copy and paste below programs.
- Press Run button. You can see the result in Console section at right side.
#1. Program to find odd even from give list and add into new list
#Answer: [8, 10, 15, 21, 6, 16, 24]
#Solution:
l1=[8,6,10,11,15,56,21]
l2=[2,6,12,16,41,24,45]
l3=[]
even= l1[0::2]
odd= l2[1::2]
l3.extend(even)
l3.extend(odd)
print(l3)
#2. Program to insert and remove item from middle of the list
#Answer: [54, 44, 91, 27, 79, 41], [54, 44, 91, 27, 79, 41, 91]
#Solution:
list1= [54,44,27,79,91,41]
removemiddle= list1.pop(4)
print(removemiddle)
list1.insert(2,removemiddle)
print(list1)
list1.append(removemiddle)
print(list1)
#3. Program to create set from list
#Answer: {(7, 49), (2, 4), (4, 16), (8, 64), (6, 36), (3, 9), (5, 25)}
#Solution:
#Note: we use zip function to take two or more iterables (like list,string, dict) and aggregates them in a tuple and return it
first_list = [2,3,4,5,6,7,8]
second_list = [4,9,16,25,36,49,64]
re = zip(first_list, second_list)
set_re = set(re)
print (set_re)
#4. Program to find intersection (common) of two sets & remove those elements from the first set
#Answer: {24, 17, 29, 78}, {86, 62, 31}
#Solution:
first_set = {86, 31, 62, 24, 78, 17, 29}
second_set = {81, 24, 29, 17, 73, 43, 78}
intersection = first_set.intersection(second_set)
print(intersection)
for item in intersection:
first_set.remove(item)
print(first_set)
#5. Program to get all values from the dictionary and add them to a list but don't add duplicates
#Answer: [47, 52, 44, 53, 54]
#Solution:
speed = {'jan': 47, 'feb': 52, 'march': 47, 'April': 44, 'May': 52, 'June': 53,
'july': 54, 'Aug': 44, 'Sept': 54}
speed_list=[]
for val in speed.values():
if val not in speed_list:
speed_list.append(val)
print(speed_list)
#6. Remove duplicates from a list and create a tuple and find the min and max number
#Answer: [65, 99, 41, 45, 87, 94]
#Solution:
sample_list = [87, 45, 41, 65, 94, 41, 99, 94]
samplist = list(set(sample_list))
print(samplist)
t = tuple(samplist)
min_tuple = min(t)
max_tuple = max(t)
#7. Program to reverse the list by two methods
#Answer: [500, 400, 300, 200, 100]
#Solution:
lis1= [100,200,300,400,500]
lis1.reverse()
print(lis1)
lis2= lis1[::-1]
print(lis2)
# 8. Program to add two lists index-wise
#Answer: ['My', 'Name', 'is', 'Akansha']
#Solution:
list1= ["M", "Na", "i", "Akan"]
list2=["y", "me", "s", "sha"]
final_list = [i+j for i, j in zip(list1, list2)]
print(final_list)
# 9. Program to convert every item in the list into its square
#Answer: [4, 25, 49, 64, 121, 169]
#Solution:
lis = [2, 5, 7, 8, 11, 13]
lis1= [i*i for i in lis]
print (lis1)
# 10. Program to merge or concatenate two lists in the following order
#Answer: ['HelloAkansha', 'HelloRu?', 'HowAkansha', 'HowRu?']
#Solution:
list1 =["Hello", "How"]
list2 =["Akansha", "Ru?"]
res = [x+y for x in list1 for y in list2]
print(res)
#11. Program to convert lists into dictionary
#Answer: {'One': 1, 'two': 2, 'three': 3, 'four': 4}
#Solution:
keys =['One','two','three','four']
values= [1,2,3,4]
dict = dict(zip(keys, values))
print(dict)
#12. Program to merge two python dictionaries
# Answer: {'One': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8}
# Solution:
dict1={'One': 1, 'two': 2, 'three': 3, 'four': 4}
dict2= {'five': 5, 'six': 6, 'seven': 7, 'eight': 8}
final_dict= {**dict1, **dict2}
print (final_dict)
#Another method to merge
fin_dic = dict1.copy()
fin_dic.update(dict2)
print(fin_dic)
# 13. Program of dictionary with default values
# Answer: {'name': 'Akansha', 'Designation': 'Devloper'}
# Solution:{'Akansha': {'lecture': 'Assitant Prof', 'Salary': 45000}, 'Isha': {'lecture': 'Assitant Prof', 'Salary': 45000}}
#{'lecture': 'Assitant Prof', 'Salary': 45000}
Teacher = ['Akansha', 'Isha']
default = {"lecture": "Assitant Prof","Salary" :45000}
dict_result = dict.fromkeys(Teacher, default)
print (dict_result)
print(dict_result["Akansha"])
#14. Program to create a new dictionary by using one or two keys from given dictionary
# Answer: {'name': 'Akansha', 'Designation': 'Devloper'}
# Solution:
Employee_dict = {"name": "Akansha", "Orgaization": "TCS", "Designation": "Devloper", "Salary": 45000}
keys = ["name", "Designation"]
new_dict = {}
for k in keys:
new_dict.update({k:Employee_dict[k]})
print (new_dict)
# 15. Program to delete particular key value pair from given dictionary
# Answer: {'Orgaization': 'TCS', 'Salary': 45000}
# Solution:
Employee_dict = {"name": "Akansha", "Orgaization": "TCS", "Designation": "Devloper", "Salary": 45000}
keys = ["name", "Designation"]
for k in keys:
Employee_dict.pop(k)
print (Employee_dict)
# 16. Program to reverse the tuple
# Answer: (76, 34, 89, 67, 45, 23)
# Solution:
tup = (23,45,67,89,34,76)
tup1 = tup[::-1]
print(tup1)
# 17. Program to create a tuple with any single item
# Answer: (34,)
# Solution:
tup =(34,)
print(tup)
# 18. Program to swap two tuples
# Answer: (90, 67) (56, 78)
# Solution:
tup1 =(56,78)
tup2 =(90,67)
tup1, tup2 = tup2, tup1
print (tup1, tup2)
# 19. Program to replace the value 45 with 455 in the given string
# Answer: (12, 34, 89, [67, 455], 91)
# Solution:
tup = (12, 34, 89, [67,45], 91)
tup[3][1] = 455
print(tup)
# 20. Program to count the number of occurances of 67 in given tuple
# Answer: 3
# Solution:
tup =(67,90,87,56,67,34,67,23, 92)
print(tup.count(67))
No comments:
Post a Comment