Assignment 1
QA parts
) Given two Python lists of same length. Write a program to
iterate
both lists simultaneously and display items from list1 in original
order and
items from list2 in reverse order. For given two lists:
list1 = [10, 20, 30, 40]
list2 = [100, 200, 300, 400]
Expected output is:
10 400
20 300
30 200
40 100
2)Given a Python list, write a program to remove all
occurrences of a given item. Example if the given item is 20
and given list is [5, 20, 15, 20, 25, 50, 20] then Expected
output is: [5, 15, 25, 50]
3)Write a Python function that takes two lists and returns True if
they have at least one common member.
4)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.
5)Write a Python function which accepts a list and a search
element as arguments and returns the index of the search
element if it is present in the list and returns -1 if it is not
present in the list .
Write a main function which reads a list and a search element
from the key board and calls the above mentioned function and
print and appropriate message based on the return value of the
function.
6) Fill code with appropriate Slicing in the blank space that
would give the following output:
a)s =list( 'pythonista')
print(__________)
Output:
['i', 'n',' o', 'h']
b) What will be the output of the following slicing on s:
A) s[3:7:-1] B) s[7:3:-1]
C) s[6:3]
D) s[6:2:-1]
7) reversed_list = my_list[___]
8) my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(my_list[7:3:-1])
9) What will be the output of the following Python code? Explain
why?
L = ['a', 'b', 'c', 'd', 'e']
L[1:4] = [1, 2, 3]
print(L)
10) What will be the output of the following Python code?
Explain why?
L = ['a', 'b', 'c', 'd', 'e']
L[1:2] = [1, 2, 3]
print(L)
Solutions
#Ans1iteration through list
print("Enter equal number of elements in the list")
L1=[int(x) for x in input("Enter the elements of L1").split()]
L2=[int(x) for x in input("Enter the elements of L2").split()]
if len(L1)!=len(L2):
print("Sorry you have not entered equal number of
elements:")
else:
k=len(L1)-1
for i in range(len(L1)):
print(L1[i],L2[k-i])
#Ans2 Removing Occurances
L1=[int(x) for x in input("Enter the elements separated by
space").split()]
print("Given list is",L1)
ele=int(input("Enter the element to remove its occurances"))
for i in L1:
if i==ele:
[Link](i)
print("Updated list is",L1)
#Ans3 checking common elements
def common(L1,L2):
count=0
for i in L1:
for j in L2:
if i==j:
count+=1
if count>0:
return True
else:
print("No common element")
#Ans4 Checking prime in list
def checkprime(L):
Lnew=[]
for i in L:
count=0
for j in range(1,i+1):
if i%j==0:
count+=1
if count==2:
[Link](i)
if Lnew==L:
return True
else:
return False
L=[2,3,5,7,0]
print(checkprime(L))
#Ans5 part1
def presence(L,ele):
count=0
for i in L:
if i==ele:
count+=1
if count>0:
return [Link](ele)
else:
return -1
if __name__=="__main__":
L=[int(x) for x in input("Enter the elements in the list
separated by space").split() ]
ele=int(input("Enter the element to find its index in the given
list"))
print(presence(L,ele))
#Ans5 part2
import Q5
def main():
L=[int(x) for x in input("Enter the elements in the list
separated by space").split() ]
ele=int(input("Enter the element to find its index in the given
list"))
a=[Link](L,ele)
if a==-1:
print("Element not present")
else:
print("Element is present and its index is",a)
main()
#Ans7 a)s =list( 'pythonista')
print(s[-4:-8:-1])
Output:
['i', 'n',' o', 'h']
(b)(i)[]
(ii)['s', 'i', 'n', 'o']
(iii)[]
(iv)['i', 'n', 'o', 'h']
#Ans8
[8,7,6,5]
#Ans9 ['a', 1, 2, 3, 'e']
The output will be this because we are replacing the elements
using slicing
As we used slicing in the order[1:4]
So the elements that will be replaced are the elements at
indices 1,2,3
#Ans10 the output will be
['a', 1, 2, 3, 'c', 'd', 'e']
This time we have used the slicing in the format L[1,2]=[1,2,3]
So the element at the 1th index will be replaced by the element
in the given list
QB Dictionary Quiz
2) Submit the code for the Quiz problem using Dictionary and
discussed in the class .
Problem statement.
Create a dictionary of 28 elements. Each element has state
name of India and associated value is the name of its capital.
Hard code the dictionary. Ask the player how many state
names he wishes to guess? Store his answer as n (n < 28).
Quiz starts by displaying a randomly selected state name from
the dictionary. Player should be give maximum 3 attempts to
guess the capital name. Match user input from the exact capital
extracted from the dictionary to check if the user entered the
correct answer. Update his marks, number of correct answers
and number of wrong answers. After three attempts (or after
getting the correct answer earlier) the player should be shown
the new randomly selected state (the state already asked
should be removed from the list of states created from the keys
of the dictionary and used for randomly selecting the states).
After shown n states, outer loop is over and the user will see
his score, number of correct answers and number of wrong
answers.
User should be displayed initially each correct answer will give
him 2 marks and each wrong answer will deduct 1
mark(negative marking
#ANS
import random
Dict_quiz= {
"Andhra Pradesh": "Amaravati",
"Arunachal Pradesh": "Itanagar",
"Assam": "Dispur",
"Bihar": "Patna",
"Chhattisgarh": "Raipur",
"Goa": "Panaji",
"Gujarat": "Gandhinagar",
"Haryana": "Chandigarh",
"Himachal Pradesh": "Shimla",
"Jharkhand": "Ranchi",
"Karnataka": "Bengaluru",
"Kerala": "Thiruvananthapuram",
"Madhya Pradesh": "Bhopal",
"Maharashtra": "Mumbai",
"Manipur": "Imphal",
"Meghalaya": "Shillong",
"Mizoram": "Aizawl",
"Nagaland": "Kohima",
"Odisha": "Bhubaneswar",
"Punjab": "Chandigarh",
"Rajasthan": "Jaipur",
"Sikkim": "Gangtok",
"Tamil Nadu": "Chennai",
"Telangana": "Hyderabad",
"Tripura": "Agartala",
"Uttar Pradesh": "Lucknow",
"Uttarakhand": "Dehradun",
"West Bengal": "Kolkata"
}
score=0
correct=0
wrong=0
print("Lets start the game you will have 3 attempts to guess the
correct capital")
print("Marking system is +2 for correct and -1 for wrong
answers")
n=int(input("Enter the number of capitals you wanna guess out
of 28 and obviously 0<n<29"))
L_state=list(Dict_quiz.keys())
for i in range(n):
k=[Link](L_state)
L_state.remove(k)
print("You have 3 attempts to guess the capital of",k)
attempt=3
for i in range(1,4):
ans=input("Enter the capital of the state without any
spaces with first char capital and rest small")
if ans==Dict_quiz[k]:
correct+=1
score+=2
print("That was correct")
break
else:
print("Thats wrong try again you have",attempt-1,"left")
wrong+=1
score-=1
attempt-=1
if attempt==0:
print("The correct ans is",Dict_quiz[k])
print("Congrats you played [Link]
answered",correct,"correctly and your total score is",score)
Dear Mam i have tried my best to fulfil all the conditions in the
above quiz game the only error i couldnt resolve is that after the
number of attempts reaches 0 it still gives a statement that you
have 0 attm left and then tells the correct answers.
Apart from that this works smoothly.