You are on page 1of 11

5/27/2020 CS

Question 1
Write a Python program to get the largest and smallest number from a list

In [1]: a=[]
n=int(input("Enter the number of numbers in the list"))
for i in range(n):
a.append(int(input("Enter the number")))
print("the biggest number is ",max(a))
print("the smallest number is ",min(a))

Enter the number of numbers in the list5


Enter the number2
Enter the number34
Enter the number54
Enter the number5
Enter the number2
the biggest number is 54
the smallest number is 2

Question 2
Write a Python program to count the number of strings where the string length is 2 or more and the first and last
character are same from a given list of strings

In [3]: a=input("Enter the space seperated strings of the list: ").split()


c=0
for i in a:
if(len(i)>=2 and i[0]==i[-1]):
c+=1
print("\nThere are ",c," Strings whose length is two or greater and its first
and last character are same")

Enter the space seperated strings of the list: Hi mam i am bob

There are 2 Strings whose length is two or greater and its first and last c
haracter are same

Question 3
Write a Python function that takes two lists and returns True if they have at least
one common member

localhost:8888/nbconvert/html/Documents/Untitled Folder/CS.ipynb?download=false 1/11


5/27/2020 CS

In [4]: def check(a,b):


c=0
for i in a:
if(i in b):
c+=1
return c!=0
a=input("Enter the space seperated first list: ").split()
b=input("Enter the space seperated first list: ").split()
print(check(a,b))

Enter the space seperated first list: 1 2 3 4 5


Enter the space seperated first list: 5 6 7 8
True

Question 3 (Negative case)


Write a Python function that takes two lists and returns True if they have at least
one common member

In [2]: def check(a,b):


c=0
for i in a:
if(i in b):
c+=1
return c!=0
a=input("Enter the space seperated first list: ").split()
b=input("Enter the space seperated first list: ").split()
print(check(a,b))

Enter the space seperated first list: 1 2 3 4 5


Enter the space seperated first list: 6 7 8 9 10
False

Question 4
Write a Python program to print a specified list after removing the 0th, 4th and 5t
h elements

localhost:8888/nbconvert/html/Documents/Untitled Folder/CS.ipynb?download=false 2/11


5/27/2020 CS

In [5]: a=[]
n=int(input("Enter the number of items in the list(greater than 5): "))
if(n<=5):
print("Invalid input ")
else:
for i in range(n):
a.append(input("enter the item: "))
a.remove(a[5])
a.remove(a[4])
a.remove(a[0])
print(a)

Enter the number of items in the list(greater than 5): 6


enter the item: 1
enter the item: 2
enter the item: 3
enter the item: 4
enter the item: 5
enter the item: 6
['2', '3', '4']

Question 5
Write a Python program to reverse a tuple

In [6]: a=tuple(input("Enter the space seperated tuple: ").split())


a=list(a)[::-1]
print("The reversed tuple is ",tuple(a))

Enter the space seperated tuple: 1 2 3 4 5 6 7


The reversed tuple is ('7', '6', '5', '4', '3', '2', '1')

Question 6
Write a Python program to convert a tuple to a dictionary

localhost:8888/nbconvert/html/Documents/Untitled Folder/CS.ipynb?download=false 3/11


5/27/2020 CS

In [1]: a=[]
n=int(input("Enter the number key-value pair in the dictionary "))
for i in range(n):
a.append((input("Enter the key: "),input("Enter the value: ")))
b=dict(a)
print("The tuple is ",tuple(a))
print("The dictionary is",b)

Enter the number key-value pair in the dictionary 3


Enter the key: Name
Enter the value: Vigneshwaran
Enter the key: Age
Enter the value: 18
Enter the key: Gender
Enter the value: Male
The tuple is (('Name', 'Vigneshwaran'), ('Age', '18'), ('Gender', 'Male'))
The dictionary is {'Name': 'Vigneshwaran', 'Age': '18', 'Gender': 'Male'}

Question 7
Write a Python script to concatenate following dictionaries to create a new one
(i) dic1={1:10, 2:20}
(ii) dic2={3:30, 4:40}
(iii) dic3={5:50,6:60}

In [2]: dic1={1:10,2:20}
dic2={3:30,4:40}
dic3={5:50,6:60}
d={**dic1,**dic2,**dic3}
print("The final dictionary is ",d)

The final dictionary is {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}

Question 8
Write a Python script to check whether a given key already exists in a dictionary

localhost:8888/nbconvert/html/Documents/Untitled Folder/CS.ipynb?download=false 4/11


5/27/2020 CS

In [3]: d={}
n=int(input("Enter the number of key value pair: "))
for i in range(n):
a=input("Enter the key: ")
d[a]=input("Enter the value: ")
a=input("Enter the key you want to search: ")

if(a in d.keys()):
print("YES,",a," is a key of the dictionary")
else:
print("NO,",a,"is not a key of the dictionary")

Enter the number of key value pair: 3


Enter the key: 1
Enter the value: 2
Enter the key: 3
Enter the value: 4
Enter the key: 5
Enter the value: 6
Enter the key you want to search: 3
YES, 3 is a key of the dictionary

Question 8 (negative case)


Write a Python script to check whether a given key already exists in a dictionary

In [9]: d={}
n=int(input("Enter the number of key value pair: "))
for i in range(n):
a=input("Enter the key: ")
d[a]=input("Enter the value: ")
a=input("Enter the key you want to search: ")

if(a in d.keys()):
print("YES,",a," is a key of the dictionary")
else:
print("NO,",a,"is not a key of the dictionary")

Enter the number of key value pair: 3


Enter the key: 1
Enter the value: 2
Enter the key: 3
Enter the value: 4
Enter the key: 5
Enter the value: 6
Enter the key you want to search: 2
NO, 2 is not a key of the dictionary

localhost:8888/nbconvert/html/Documents/Untitled Folder/CS.ipynb?download=false 5/11


5/27/2020 CS

Question 9
Write a Python script to generate and print a dictionary that contains a number (between 1 and n) in the form (x,
x*x)

In [4]: d={}
n=int(input("Enter the number n"))
for i in range(1,n+1):
d[i]=i*i
print("The dictionary is ",d)

Enter the number n10


The dictionary is {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 8
1, 10: 100}

Question 10
Write a Python script to print a dictionary where the keys are numbers between 1 and 15 (both included) and the
values are square of keys

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}

In [5]: d={}
for i in range(1,16):
d[i]=i**2
print("The dictionary is ",d)

The dictionary is {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 8


1, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}

Question 11
Write a Python program to print all unique values in a dictionary.

localhost:8888/nbconvert/html/Documents/Untitled Folder/CS.ipynb?download=false 6/11


5/27/2020 CS

In [7]: d={}
n=int(input("Enter number of items of the dictionary: "))
for i in range (1,n+1):
a=input("Enter the "+str(i)+"th key: ")
d[a] = input("Enter its value: ")
print("The dictionary is ",d)
print("The unique values are",set(d.values()))

Enter number of items of the dictionary: 3


Enter the 1th key: 1
Enter its value: 2
Enter the 2th key: 3
Enter its value: 2
Enter the 3th key: 4
Enter its value: 5
The dictionary is {'1': '2', '3': '2', '4': '5'}
The unique values are {'5', '2'}

Question 12
Write a Python program to remove an item from a set if it is present in the set

In [8]: a=set()
n=int(input("Enter the number of inputs you want to give (need not be equal to
length of set if u duplicate input)"))
for i in range(n):
a.update(input("Enter the item: "))
b=input("Enter the term you want to delete ")
print("The set is ",a)
if(b in a):
a.remove(b)
print("The final set is",a)
else:
print(b,"is not in the set")

Enter the number of inputs you want to give (need not be equal to length of s
et if u duplicate input)4
Enter the item: 1
Enter the item: 2
Enter the item: 3
Enter the item: 4
Enter the term you want to delete 2
The set is {'1', '2', '3', '4'}
The final set is {'1', '3', '4'}

Question 12 (Negative case)


Write a Python program to remove an item from a set if it is present in the set

localhost:8888/nbconvert/html/Documents/Untitled Folder/CS.ipynb?download=false 7/11


5/27/2020 CS

In [10]: a=set()
n=int(input("Enter the number of inputs you want to give (need not be equal to
length of set if u duplicate input)"))
for i in range(n):
a.update(input("Enter the item: "))
b=input("Enter the term you want to delete ")
print("The set is ",a)
if(b in a):
a.remove(b)
print("The final set is",a)
else:
print(b,"is not in the set")

Enter the number of inputs you want to give (need not be equal to length of s
et if u duplicate input)4
Enter the item: 1
Enter the item: 2
Enter the item: 3
Enter the item: 4
Enter the term you want to delete 5
The set is {'1', '4', '2', '3'}
5 is not in the set

Question 13
Write a Python program to find maximum and the minimum value in a set

In [28]: a=[]
n=int(input("Enter the number of terms "))
for i in range(n):
a.append(int(input("Enter the "+str(i+1)+" term")))
a=set(a)
print("The biggest term of the set is ",max(a))
print("The smallest term of the set is",min(a))

Enter the number of terms 4


Enter the 1 term65
Enter the 2 term43
Enter the 3 term566
Enter the 4 term3423
The biggest term of the set is 3423
The smallest term of the set is 43

Question 14
Write a Python function to reverse a string

localhost:8888/nbconvert/html/Documents/Untitled Folder/CS.ipynb?download=false 8/11


5/27/2020 CS

In [9]: def stringrev(a):


c=""
for i in a:
c=i+c
return c
a=input("Enter the string: ")
print("The reversed string is: ",stringrev(a))

Enter the string: Vigneshwaran


The reversed string is: narawhsengiV

Question 15
Write a Python function that accepts a string and calculate the number of upper cas
e letters and lower case letters

In [10]: def check(a):


l,u=0,0

for i in a:
if(i.islower()):
l+=1
elif(i.isupper()):
u+=1
else:
pass
return l,u
a=input("Enter the string: ")
v=check(a)
print("There are ",v[0]," Lower case and ",v[1],"Upper case characters")

Enter the string: VigneshWaran


There are 10 Lower case and 2 Upper case characters

Question 16
Write a Python function that checks whether a passed string is palindrome or not

In [12]: def palin(a):


if(a==a[::-1]):
print(a,"is a palindrome")
else:
print(a,"is not a palindrome")
palin(input("Enter the string: ").casefold())

Enter the string: MalaYalam


malayalam is a palindrome

localhost:8888/nbconvert/html/Documents/Untitled Folder/CS.ipynb?download=false 9/11


5/27/2020 CS

Question 16 (Negative case)


Write a Python function that checks whether a passed string is palindrome or not

In [1]: def palin(a):


if(a==a[::-1]):
print(a,"is a palindrome")
else:
print(a,"is not a palindrome")
palin(input("Enter the string: ").casefold())

Enter the string: Vigneshwaran


vigneshwaran is not a palindrome

Question 17
Write a Python program of recursion list sum

In [5]: def summer(l,i=0,sum=0):


if(i>=len(l)):
return sum

else:
return summer(l,i+1,sum+l[i])
a=[]
n=int(input("Enter the number of terms "))
for i in range(n):
a.append(float(input("Enter the numbers ")))

print(summer(a))

Enter the number of terms 4


Enter the numbers 1
Enter the numbers 2
Enter the numbers 3
Enter the numbers 4
10.0

Question 18
Write a Python program to find the greatest common divisor (gcd) of two integers

localhost:8888/nbconvert/html/Documents/Untitled Folder/CS.ipynb?download=false 10/11


5/27/2020 CS

In [6]: def gcd(a,b):


if(a%b==0):
return b
else:
return gcd(b,a%b)
a=int(input("Enter the first number: "))
b=int(input("Enter the second number: "))
print(gcd(a,b))

Enter the first number: 18


Enter the second number: 24
6

Question 19
Write a Python program to calculate the value of 'a' to the power 'b

In [8]: def mul(a,b,p=1):


if(b<=0):
return p
else:
return mul(a,b-1,p*a)
a=int(input("Enter the base: "))
b=int(input("Enter the exponent: "))
print(a,"To the power of",b,"is",mul(a,b))

Enter the base: 4


Enter the exponent: 5
4 To the power of 5 is 1024

localhost:8888/nbconvert/html/Documents/Untitled Folder/CS.ipynb?download=false 11/11

You might also like