0% found this document useful (0 votes)
31 views8 pages

B2a Heheh

The document contains questions and solutions related to lists, tuples, dictionaries, and list comprehension in Python. It includes problems such as checking if numbers in a list are prime, finding the second smallest number, calculating frequency of elements, removing elements by index or value, cloning lists and tuples, finding sums of tuple elements, and more. Dictionary problems involve iteration, generating key-value pairs, combining dictionaries, and removing keys. List comprehension questions demonstrate finding numbers divisible by 7 and common numbers in lists.

Uploaded by

Kvian Anand
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)
31 views8 pages

B2a Heheh

The document contains questions and solutions related to lists, tuples, dictionaries, and list comprehension in Python. It includes problems such as checking if numbers in a list are prime, finding the second smallest number, calculating frequency of elements, removing elements by index or value, cloning lists and tuples, finding sums of tuple elements, and more. Dictionary problems involve iteration, generating key-value pairs, combining dictionaries, and removing keys. List comprehension questions demonstrate finding numbers divisible by 7 and common numbers in lists.

Uploaded by

Kvian Anand
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/ 8

Assignment-7

#Questions on LIST

#Q1. Write a Python program to check if each number is prime in a given


list of numbers. Return True if all numbers are prime otherwise False.

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

lst=[int(x) for x in (input("Enter a sequence of numbers separated by


\",\": ").split(","))]
for i in lst:
if co_less(i,lst)==1:
print("Second Smallest Number is: ",i)
break

# Q3. Write a Python program to get the frequency of elements in a list.

def freq_cou(n,l):
s=0
for i in l:
if n==i:
s+=1
return s

lst=input("Enter a sequence of elements separated by \",\": ").split(",")


uniq=[]
print("Element | Frequency")
for i in lst:
if i not in uniq:
uniq.append(i)
print(i,freq_cou(i,lst))

#Q4. Write a Python program to print to remove the element based on index
and element. Ask the user for the option.

lst=input("Enter a sequence of elements separated by \",\": ").split(",")


print("Enter the way you wish to remove an element\n1.Index\n2.Content")
ch=int(input("Enter your choice(1-2): "))
if ch==1:
ind=int(input("Enter the index: "))
lst.pop(ind)
elif ch==2:
cont=input("Enter the element you wish to remove: ")
if cont not in lst:
print("Element not present in list!!")
else:
lst.remove(cont)
print("Updated list:\n",lst)

#Q5. Write a Python program to create the colon of a list.

lst=input("Enter a sequence of elements separated by \",\": ").split(",")


l2=[]
l2.extend(lst)
print("Cloned list successfully:\n",l2)
# Questions on TUPLES

#Q1. Write a Python program to create the colon of a tuple.

tup=tuple(input("Enter a sequence of elements separated by \",\":


").split(","))
t2=()
t2=t2+tup
print("Cloned tuple successfully:\n",t2)

# Q2. Write a Python program to check whether an element exists within a


tuple.

tup=tuple(input("Enter a sequence of elements separated by \",\":


").split(","))
el=input("Enter the element you wish to check the existence of: ")
if el in tup:
print("Element exists")
else:
print("Element doesn't exists")

'''Q3. Write a Python program to remove an empty tuple(s) from a list of


tuples.
Sample data: [(), (), ('',), ('a', 'b'), ('a', 'b', 'c'), ('d')]
Expected output: [('',), ('a', 'b'), ('a', 'b', 'c'), 'd']'''

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]'''

l=int(input("Enter the number of tuples you wish to input: "))


lst=[]
for i in range(l):
tpl=tuple([int(x) for x in (input("Enter a sequence of numbers
separated by \",\": ").split(","))])
lst.append(tpl)
print("Sample input:\n",lst)
for i in lst:
s=0
for j in i:
s+=j
lst[lst.index(i)]=s

print(lst)

'''Q5. Join Tuples if similar initial element


Input : test_list = [(5, 6), (5, 7), (5, 8), (6, 10), (7, 13)]
Output : [(5, 6, 7, 8), (6, 10), (7, 13)]'''

l=int(input("Enter the number of tuples you wish to input: "))


uniq_in=[]
lst=[]
for i in range(l):
tpl=tuple([int(x) for x in (input("Enter a sequence of numbers
separated by \",\": ").split(","))])
lst.append(tpl)
print("Sample input:\n",lst)
i=0
while i<len(lst):
if lst[i][0] not in uniq_in:
uniq_in.append(lst[i][0])
j=i+1
while j<len(lst):
if lst[i][0]==lst[j][0]:
lst[i]+=lst[j][1:]
lst.remove(lst[j])
continue
j+=1
i+=1
print("Output:\n",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])

#Q2: Write a Python script to generate and print a dictionary that


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

n=int(input("Enter a natural number: "))


d=dict((i,i**2) for i in range(1,n+1))
print("Output:\n",d)

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

'''Q4: Write a Python program to create and display all combinations of


letters, selecting each
letter from a different key in a dictionary.
Sample data : {'1':['a','b'], '2':['c','d']}
Expected Output:
ac
ad
bc
bd'''

def print_pair(lst1,lst2):
for i in lst1:
for j in lst2:
print(i+j)

l=int(input("Enter number of key-value pair you wish to enter: "))


d=dict(input("Enter the key value pair(for list use as in python
programs)").split() for i in range(l))
d=dict([x,eval(d[x])] for x in d)
uniq=[]
for k in d:
uniq.append(k)
for i in d:
if i in uniq:
continue
print_pair(d[k],d[i])

#5: Write a Python program to remove a key from a dictionary.

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.

lst=[x for x in range(1,1000) if x%7==0]


print(lst)

#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]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy