Python Slip 10

468b667d351b4c90bd71e34b2166cbe9

Q1. Write a python program which create a lambda function that multiplies argument x with argument y and print the result.

f = lambda a, b : a * b
x=int(input("Enter The Value of X\t"))
y=int(input("Enter The Value of Y\t"))
print("Multiplication of",x,"and",y,"is:")
print(f(5, 6))
Enter The Value of X	4
Enter The Value of Y	6
Multiplication of 4 and 6 is:
30

Q2. Write a python program to display all files in directory and subdirectories.

import os

path ="G:\MSc"
filelist = []
for root, dirs, files in os.walk(path):
    for file in files:
        #append the file name to the list
        filelist.append(os.path.join(root,file))
for name in filelist:
    print(name)
G:\MSc\Mid Term Question Bank.docx
G:\MSc\Practical\Slip 2.docx
G:\MSc\Practical\Slip 3.docx
G:\MSc\Practical\Slip 4.docx
G:\MSc\Practical\Slip 1.docx
G:\MSc\PDF\Springer.Python.Scripting.for.Computational.Science.3rd.Edition.Jan.2008.eBook-BBL.pdf
G:\MSc\PDF\A_Practical_Introduction_to_Python_Programming_Heinold.pdf
G:\MSc\PDF\Beginning Programming with Python For Dummies Mueller, John Paul [SRG].pdf
G:\MSc\PDF\M.Sc. (Part-I) (Computer Applications) (Revised)_16.062020(1).pdf
G:\MSc\PDF\M.Sc. (Part-II) (Computer Applications)_16.062020.pdf
G:\MSc\PDF\Python.pdf
G:\MSc\PDF\python_book_01.pdf
G:\MSc\PDF\python_tutorial.pdf
G:\MSc\PPT\Python4HPC.pptx
G:\MSc\PPT\01-basics-functions.ppt
G:\MSc\PPT\Chapter_1.ppt
G:\MSc\PPT\intro22.ppt
G:\MSc\PPT\nov28.ppt
G:\MSc\PPT\programming_intro.pptx
G:\MSc\PPT\python1.ppt

Q2. Write a python program to delete repeated lines from a file.

outputFile = open('output.txt', "w")
inputFile = open('sample.txt', "r")
lines = set()
print("Given Input File Content is")
for line in inputFile:
    print(line)
    if line not in lines:
        outputFile.write(line)
        lines.add(line)       
inputFile.close()
outputFile.close()
outputFile = open('output.txt', "r")
print("\n\nGiven OutPut File Content is")
for line in outputFile:
    print(line)
Given Input File Content is
Hello
World Of python
Hello
How Are You
Hello
World

Given OutPut File Content is
Hello
World Of python
How Are You
World

0 Comments:

Post a Comment