SlideShare ist ein Scribd-Unternehmen logo
1 von 4
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Ähnlich wie paython practical

ANSHUL RANA - PROGRAM FILE.pptx
ANSHUL RANA - PROGRAM FILE.pptxANSHUL RANA - PROGRAM FILE.pptx
ANSHUL RANA - PROGRAM FILE.pptxjeyel85227
 
python practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdfpython practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdfrajatxyz
 
For this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfFor this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfherminaherman
 
A short tutorial on r
A short tutorial on rA short tutorial on r
A short tutorial on rAshraf Uddin
 
Develop a system flowchart and then write a menu-driven C++ program .pdf
Develop a system flowchart and then write a menu-driven C++ program .pdfDevelop a system flowchart and then write a menu-driven C++ program .pdf
Develop a system flowchart and then write a menu-driven C++ program .pdfleventhalbrad49439
 
Introduction-to-Iteration (2).pptx
Introduction-to-Iteration (2).pptxIntroduction-to-Iteration (2).pptx
Introduction-to-Iteration (2).pptxKeshavBandil2
 
Xi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programsXi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programsProf. Dr. K. Adisesha
 
Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!Paige Bailey
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysisVishal Singh
 
Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1Giovanni Della Lunga
 
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILECOMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILEAnushka Rai
 
python lab programs.pdf
python lab programs.pdfpython lab programs.pdf
python lab programs.pdfCBJWorld
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Pedro Rodrigues
 

Ähnlich wie paython practical (20)

ANSHUL RANA - PROGRAM FILE.pptx
ANSHUL RANA - PROGRAM FILE.pptxANSHUL RANA - PROGRAM FILE.pptx
ANSHUL RANA - PROGRAM FILE.pptx
 
Mcs 011 solved assignment 2015-16
Mcs 011 solved assignment 2015-16Mcs 011 solved assignment 2015-16
Mcs 011 solved assignment 2015-16
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
python practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdfpython practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdf
 
C- Programming Assignment 3
C- Programming Assignment 3C- Programming Assignment 3
C- Programming Assignment 3
 
For this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfFor this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdf
 
A short tutorial on r
A short tutorial on rA short tutorial on r
A short tutorial on r
 
Develop a system flowchart and then write a menu-driven C++ program .pdf
Develop a system flowchart and then write a menu-driven C++ program .pdfDevelop a system flowchart and then write a menu-driven C++ program .pdf
Develop a system flowchart and then write a menu-driven C++ program .pdf
 
Arrays
ArraysArrays
Arrays
 
Introduction-to-Iteration (2).pptx
Introduction-to-Iteration (2).pptxIntroduction-to-Iteration (2).pptx
Introduction-to-Iteration (2).pptx
 
Xi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programsXi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programs
 
Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!
 
Python
PythonPython
Python
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysis
 
Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1
 
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILECOMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
 
python lab programs.pdf
python lab programs.pdfpython lab programs.pdf
python lab programs.pdf
 
simple programs.docx
simple programs.docxsimple programs.docx
simple programs.docx
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
 
Session 4
Session 4Session 4
Session 4
 

Kürzlich hochgeladen

Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 

Kürzlich hochgeladen (20)

Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 

paython practical

  • 1. 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
  • 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