In [1]:
Enter set of numbers and
To stop entering number enter #
Enter number : 12
Enter number : 23
Enter number : 23
Enter number : 34
Enter number : 45
Enter number : #
You have entered numbers are:
[12, 23, 23, 34, 45]
Median of the given list is:23
Mean of the given list is:27.4
Mode of the given list is:23
1) A group of statisticians at a local college has asked you to create a set of
functions that compute the median and mode of a set of numbers. Define these functions
in a module named stats.py. Also include a function named mean, which computes the
average of a set of numbers. Each function should expect a list of numbers as an
argument and return a single number. Each function should return 0 if the list is
empty. Include a main function that tests the three statistical functions with a given
list.
import statistics
print("Enter set of numbers and ")
print("To stop entering number enter #")
var =True
numlist = []
while(var ==True):
num =input("Enter number : ")
if(num =='#'):
var =False
break
else:
num =int(num)
numlist.append(num)
print("You have entered numbers are:")
print(numlist)
print()
def median(numlist):
length =len(numlist)
if((length%2)==0):
median1 = numlist[int((length/2))-1]
median2 = numlist[int(length/2)]
i =(median1+median2)/2
return i
else:
return numlist[length//2]
def mode(numlist):
return statistics.mode(numlist)
def mean(numlist):
return statistics.mean(numlist)
print("Median of the given list is:"+str(median(numlist)))
print("Mean of the given list is:"+str(mean(numlist)))
print("Mode of the given list is:"+str(mode(numlist)))
.ipynb - Jupyter Notebook http://localhost:8888/notebooks/.ipynb
1 of 4 1/21/2020, 2:56 PM
In [ ]:
In [3]:
1 : Write a program that allows the user to navigate the lines of text in a file.
The program should prompt the user for a filename and input the lines of text into
a list.
2 : The program then enters a loop in which it prints the number of lines in the f
ile and prompts the user for a line number. Actual line numbers range from 1 to th
e number of lines in the file.
3 : If the input is 0, the program quits. Otherwise, the program prints the line a
ssociated with that number.
Enter Line Number to read : 1
Line No 1 : Write a program that allows the user to navigate the lines of text in
a file. The program should prompt the user for a filename and input the lines of t
ext into a list.
Enter Line Number to read : 3
Line No 3 : If the input is 0, the program quits. Otherwise, the program prints th
e line associated with that number.
Enter number seprated with space : 23
{'23': '010111'}
2) Write a program that allows the user to navigate the lines of text in a file. The
program should prompt the user for a filename and input the lines of text into a list.
The program then enters a loop in which it prints the number of lines in the file and
prompts the user for a line number. Actual line numbers range from 1 to the number of
lines in the file. If the input is 0, the program quits. Otherwise, the program prints
the line associated with that number.
file=open("pr_2.txt","r")
lines=file.readlines()
lineNumber=len(lines)
for i in range(0,lineNumber):
print(str(i+1)+" : "+lines[i])
while(True):
ch=int(input("Enter Line Number to read : "))
if ch==0:
print("Exit")
break
elif ch>lineNumber:
print("Enter Valid Line Number")
else:
print("nnLine No "+str(ch)+" : "+lines[ch-1])
3) Write a program to convert each decimal number given in list to a fixed size binary
and generate a dictionary containing binary value as key and decimal number as value.
def decToBinary(no):
res = [int(i)for i in list('{0:06b}'.format(no))]
return str("".join(map(str, res)))
no =input("Enter number seprated with space : ")
arr = no.split(" ")
d =dict()
for element in arr:
ans = decToBinary (int(element))
d[element] =str(ans)
print(d)
.ipynb - Jupyter Notebook http://localhost:8888/notebooks/.ipynb
2 of 4 1/21/2020, 2:56 PM
In [4]:
In [5]:
11111001110
12200
310
7D
20
{'a', 'user', 'in', 'the', 'filename', 'list.', 'navigate', 'to', 'into', 'should
', 'file.', 'prompt', 'program', 'for', 'lines', 'Write', 'text', 'The', 'that', '
allows', 'input', 'of', 'and'}
{'a', 'user', 'then', 'in', 'file', 'range', 'which', 'the', 'number.', 'prints',
'to', '1', 'Actual', 'file.', 'program', 'loop', 'enters', 'number', 'line', 'for
', 'prompts', 'numbers', 'lines', 'it', 'The', 'from', 'of', 'and'}
{'If', 'is', 'program', 'prints', 'the', 'with', 'that', 'line', 'Otherwise,', 'as
sociated', '0,', 'quits.', 'input', 'number.'}
4) Define a function decimalToRep that returns the representation of an integer in a
given base. The two arguments should be the integer and the base. The function should
return a string. It should use a lookup table that associates integers with digits.
Include a main function that tests the conversion function with numbers in several
bases.
def decimalToRep(n,base):
convertString ="0123456789ABCDEF"
if n < base:
return convertString[n]
else:
return decimalToRep(n//base,base) + convertString[n%base]
if __name__=='__main__':
print(decimalToRep(1998,2))
print(decimalToRep(153,3))
print(decimalToRep(200,8))
print(decimalToRep(125,16))
print(decimalToRep(6,3))
5) Write a program that inputs a text file. The program should print all of the unique
words in the file in alphabetical order.
with open('pr_2.txt','r') as f:
for line in f:
print(set(line.split()))
6) A file concordance tracks the unique words in a file and their frequencies. Write
a program that displays a concordance for a file. The program should output the unique
words and their frequencies in alphabetical order.
.ipynb - Jupyter Notebook http://localhost:8888/notebooks/.ipynb
3 of 4 1/21/2020, 2:56 PM
In [6]:
In [7]:
In [8]:
Word List :
['am', 'and', 'and', 'are', 'hello', 'how', 'i', 'mind', 'of', 'slow', 'student',
'the', 'you']
The unique words and their frequencies
[('am', 1), ('and', 2), ('are', 1), ('hello', 1), ('how', 1), ('i', 1), ('mind',
1), ('of', 1), ('slow', 1), ('student', 1), ('the', 1), ('you', 1)]
[{'color_name': 'Black', 'color_code': '#000000'}, {'color_name': 'Red', 'color_co
de': '#FF0000'}, {'color_name': 'Maroon', 'color_code': '#800000'}, {'color_name':
'Yellow', 'color_code': '#FFFF00'}]
[[10, 20], [40], [30, 56, 25], [33]]
file=open("pr6.txt","r")
wordList=file.read().split()
wordList.sort()
uniList=list(set(wordList))
uniList.sort()
print("Word List : n")
print(wordList)
wordfreq=[]
for w in uniList:
wordfreq.append(wordList.count(w))
print("nnThe unique words and their frequenciesn")
print(str(list(zip(uniList,wordfreq))))
7) Write a Python program to convert list to list of dictionaries.
Sample lists: ["Black", "Red", "Maroon", "Yellow"], ["#000000", "#FF0000", "#800000",
"#FFFF00"] Expected Output: [{'color_name': 'Black', 'color_code': '#000000'},
{'color_name': 'Red', 'color_code': '#FF0000'}, {'color_name': 'Maroon', 'color_code':
'#800000'},
{'color_name': 'Yellow', 'color_code': '#FFFF00'}]
color_name = ["Black", "Red", "Maroon", "Yellow"]
color_code = ["#000000", "#FF0000", "#800000", "#FFFF00"]
print([{'color_name': f, 'color_code': c} for f, c in zip(color_name, color_code)])
8) Write a Python program to remove duplicates from a list of lists. Sample list :
[[10, 20], [40], [30, 56, 25], [10, 20], [33], [40]]
New List : [[10, 20], [30, 56, 25], [33], [40]]
# Python code to remove duplicate elements
def Remove(duplicate):
final_list = []
for num in duplicate:
if num not in final_list:
final_list.append(num)
return final_list
# Driver Code
duplicate = [[10, 20], [40], [30, 56, 25], [10, 20], [33], [40]]
print(Remove(duplicate))
.ipynb - Jupyter Notebook http://localhost:8888/notebooks/.ipynb
4 of 4 1/21/2020, 2:56 PM

paython practical

  • 1.
    In [1]: Enter setof numbers and To stop entering number enter # Enter number : 12 Enter number : 23 Enter number : 23 Enter number : 34 Enter number : 45 Enter number : # You have entered numbers are: [12, 23, 23, 34, 45] Median of the given list is:23 Mean of the given list is:27.4 Mode of the given list is:23 1) A group of statisticians at a local college has asked you to create a set of functions that compute the median and mode of a set of numbers. Define these functions in a module named stats.py. Also include a function named mean, which computes the average of a set of numbers. Each function should expect a list of numbers as an argument and return a single number. Each function should return 0 if the list is empty. Include a main function that tests the three statistical functions with a given list. import statistics print("Enter set of numbers and ") print("To stop entering number enter #") var =True numlist = [] while(var ==True): num =input("Enter number : ") if(num =='#'): var =False break else: num =int(num) numlist.append(num) print("You have entered numbers are:") print(numlist) print() def median(numlist): length =len(numlist) if((length%2)==0): median1 = numlist[int((length/2))-1] median2 = numlist[int(length/2)] i =(median1+median2)/2 return i else: return numlist[length//2] def mode(numlist): return statistics.mode(numlist) def mean(numlist): return statistics.mean(numlist) print("Median of the given list is:"+str(median(numlist))) print("Mean of the given list is:"+str(mean(numlist))) print("Mode of the given list is:"+str(mode(numlist))) .ipynb - Jupyter Notebook http://localhost:8888/notebooks/.ipynb 1 of 4 1/21/2020, 2:56 PM
  • 2.
    In [ ]: In[3]: 1 : Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. 2 : The program then enters a loop in which it prints the number of lines in the f ile and prompts the user for a line number. Actual line numbers range from 1 to th e number of lines in the file. 3 : If the input is 0, the program quits. Otherwise, the program prints the line a ssociated with that number. Enter Line Number to read : 1 Line No 1 : Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of t ext into a list. Enter Line Number to read : 3 Line No 3 : If the input is 0, the program quits. Otherwise, the program prints th e line associated with that number. Enter number seprated with space : 23 {'23': '010111'} 2) Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program quits. Otherwise, the program prints the line associated with that number. file=open("pr_2.txt","r") lines=file.readlines() lineNumber=len(lines) for i in range(0,lineNumber): print(str(i+1)+" : "+lines[i]) while(True): ch=int(input("Enter Line Number to read : ")) if ch==0: print("Exit") break elif ch>lineNumber: print("Enter Valid Line Number") else: print("nnLine No "+str(ch)+" : "+lines[ch-1]) 3) Write a program to convert each decimal number given in list to a fixed size binary and generate a dictionary containing binary value as key and decimal number as value. def decToBinary(no): res = [int(i)for i in list('{0:06b}'.format(no))] return str("".join(map(str, res))) no =input("Enter number seprated with space : ") arr = no.split(" ") d =dict() for element in arr: ans = decToBinary (int(element)) d[element] =str(ans) print(d) .ipynb - Jupyter Notebook http://localhost:8888/notebooks/.ipynb 2 of 4 1/21/2020, 2:56 PM
  • 3.
    In [4]: In [5]: 11111001110 12200 310 7D 20 {'a','user', 'in', 'the', 'filename', 'list.', 'navigate', 'to', 'into', 'should ', 'file.', 'prompt', 'program', 'for', 'lines', 'Write', 'text', 'The', 'that', ' allows', 'input', 'of', 'and'} {'a', 'user', 'then', 'in', 'file', 'range', 'which', 'the', 'number.', 'prints', 'to', '1', 'Actual', 'file.', 'program', 'loop', 'enters', 'number', 'line', 'for ', 'prompts', 'numbers', 'lines', 'it', 'The', 'from', 'of', 'and'} {'If', 'is', 'program', 'prints', 'the', 'with', 'that', 'line', 'Otherwise,', 'as sociated', '0,', 'quits.', 'input', 'number.'} 4) Define a function decimalToRep that returns the representation of an integer in a given base. The two arguments should be the integer and the base. The function should return a string. It should use a lookup table that associates integers with digits. Include a main function that tests the conversion function with numbers in several bases. def decimalToRep(n,base): convertString ="0123456789ABCDEF" if n < base: return convertString[n] else: return decimalToRep(n//base,base) + convertString[n%base] if __name__=='__main__': print(decimalToRep(1998,2)) print(decimalToRep(153,3)) print(decimalToRep(200,8)) print(decimalToRep(125,16)) print(decimalToRep(6,3)) 5) Write a program that inputs a text file. The program should print all of the unique words in the file in alphabetical order. with open('pr_2.txt','r') as f: for line in f: print(set(line.split())) 6) A file concordance tracks the unique words in a file and their frequencies. Write a program that displays a concordance for a file. The program should output the unique words and their frequencies in alphabetical order. .ipynb - Jupyter Notebook http://localhost:8888/notebooks/.ipynb 3 of 4 1/21/2020, 2:56 PM
  • 4.
    In [6]: In [7]: In[8]: Word List : ['am', 'and', 'and', 'are', 'hello', 'how', 'i', 'mind', 'of', 'slow', 'student', 'the', 'you'] The unique words and their frequencies [('am', 1), ('and', 2), ('are', 1), ('hello', 1), ('how', 1), ('i', 1), ('mind', 1), ('of', 1), ('slow', 1), ('student', 1), ('the', 1), ('you', 1)] [{'color_name': 'Black', 'color_code': '#000000'}, {'color_name': 'Red', 'color_co de': '#FF0000'}, {'color_name': 'Maroon', 'color_code': '#800000'}, {'color_name': 'Yellow', 'color_code': '#FFFF00'}] [[10, 20], [40], [30, 56, 25], [33]] file=open("pr6.txt","r") wordList=file.read().split() wordList.sort() uniList=list(set(wordList)) uniList.sort() print("Word List : n") print(wordList) wordfreq=[] for w in uniList: wordfreq.append(wordList.count(w)) print("nnThe unique words and their frequenciesn") print(str(list(zip(uniList,wordfreq)))) 7) Write a Python program to convert list to list of dictionaries. Sample lists: ["Black", "Red", "Maroon", "Yellow"], ["#000000", "#FF0000", "#800000", "#FFFF00"] Expected Output: [{'color_name': 'Black', 'color_code': '#000000'}, {'color_name': 'Red', 'color_code': '#FF0000'}, {'color_name': 'Maroon', 'color_code': '#800000'}, {'color_name': 'Yellow', 'color_code': '#FFFF00'}] color_name = ["Black", "Red", "Maroon", "Yellow"] color_code = ["#000000", "#FF0000", "#800000", "#FFFF00"] print([{'color_name': f, 'color_code': c} for f, c in zip(color_name, color_code)]) 8) Write a Python program to remove duplicates from a list of lists. Sample list : [[10, 20], [40], [30, 56, 25], [10, 20], [33], [40]] New List : [[10, 20], [30, 56, 25], [33], [40]] # Python code to remove duplicate elements def Remove(duplicate): final_list = [] for num in duplicate: if num not in final_list: final_list.append(num) return final_list # Driver Code duplicate = [[10, 20], [40], [30, 56, 25], [10, 20], [33], [40]] print(Remove(duplicate)) .ipynb - Jupyter Notebook http://localhost:8888/notebooks/.ipynb 4 of 4 1/21/2020, 2:56 PM