B2a Heheh
B2a Heheh
B2a Heheh
#Questions on LIST
def chec_prime(n):
for i in range(2,n//2+1):
if n%i==0:
return False
return True
flag=True
lst=[int(x) for x in (input("Enter a sequence of natural numbers separated
by \",\": ").split(","))]
for i in lst:
if not(chec_prime(i)):
flag=False
break
print(flag)
#Q2. Write a Python program to find the second smallest number in a list .
def co_less(n,l):
s=0
for i in l:
if i<n:
s+=1
return s
def freq_cou(n,l):
s=0
for i in l:
if n==i:
s+=1
return s
#Q4. Write a Python program to print to remove the element based on index
and element. Ask the user for the option.
samp=[(),(),("",),('a','b'),(),('a','b','c'),('d'),(),('hello',),()]
print("Sample Input:\n",samp)
samp_out=[]
for i in samp:
if i!=():
samp_out.append(i)
print("Sample Output:\n",samp_out)
'''Q4. Write a Python program to compute the sum of all the elements of
each tuple stored inside a
list.
Original list of tuples:
[(1, 2), (2, 3), (3, 4)]
Sum of all the elements of each tuple stored inside the said list:
[3, 5, 7]
Original list of tuples:
[(1, 2, 6), (2, 3, -6), (3, 4), (2, 2, 2, 2)]
Sum of all the elements of each tuple stored inside the said list:
[9, -1, 7, 8]'''
print(lst)
#Questions on DICTIONARIES
#Q1: Write a Python program to iterate over dictionaries using for loops .
l=int(input("Enter how many (Key Value) pair you wish to enter: "))
print("Enter key values pair separated by ' '")
d=dict(input().split() for i in range(l))
print("Output:\nKey\tPair")
for key in d:
print(key,d[key])
'''
Q3: Combine two dictionaries having key of the first dictionary and value
of the second
dictionary.
Example: Input : test_dict1 = {"Gfg" : 20, "is" : 36, "best" : 100},
test_dict2 = {"Gfg2" : 26, "is2" : 20, "best2" : 70}
Output : {'Gfg': 26, 'is': 20, 'best': 70}
Explanation : Similar index keys' values assigned to dictionary 1.'' '
test_dict1={"Gfg":20,"is":36,"best":100}
test_dict2={"Gfg2":26,"is2":20,"best2":70}
print("test_dict1: ",test_dict1)
print("test_dict2: ",test_dict2)
print("Swapping key-value pairs...")
l=list(test_dict1.keys())
c=0
for val in test_dict2.values():
test_dict1[l[c]]=val
c+=1
print("Output:\n",test_dict1)
def print_pair(lst1,lst2):
for i in lst1:
for j in lst2:
print(i+j)
l=int(input("Enter how many (Key Value) pair you wish to enter: "))
print("Enter key values pair separated by ' '")
d=dict(input().split() for i in range(l))
print("Given Input:\n",d)
ch=input("Enter the key you wish to remove from dictionary: ")
if ch not in d:
print("Key doesn't exists!!")
else:
d.pop(ch)
print("New Dictionary:\n",d)
#Questions on COMPREHENSION
#Q1. Use list comprehension to find all of the numbers from 1-1000 that
are divisible by 7.
#Q2. Find the common numbers in two lists (without using a tuple or set)
list_a = [1, 2, 3, 4], list_b = [2, 3, 4, 5].
list_a=[1,2,3,4]