List File Operations in Python
List File Operations in Python
CO6
Ans – opening, reading, writing, closing, renaming, deleting.
2. Write use of lambda function in python.
The lambda function, which is also called anonymous function. A lambda function
can
take any number of arguments, but can only have one expression.
Syntax: lambda arguments : expression
Example: x= lambda a,b : a*b
Print(x(10,5)
Output: 50
3. Explain Local and Global variable.
Global variables: global variables can be accessed throughout the program body by
all functions.
Local variables: local variables can be accessed only inside the function in which
they are declared.
17. Write python program to read contents of abc.txt and write same content to
pqr.txt.
with open('abs.txt','r') as firstfile, open('prq.txt','w') as secondfile:
# read content from first file
for line in firstfile:
# write content to second file
secondfile.write(line)
18. Example module. How to define module.
19. Design a class student with data members; Name, roll number address. Create suitable
method for reading and printing students details.
class Student:
def getStudentDetails(self):
self.rollno=input("Enter Roll Number : ")
self.name = input("Enter Name : ")
self.address =input("Enter Address : ")
def printStudentDetails(self):
print(self.rollno,self.name, self.address)
S1=Student()
S1.getStudentDetails()
print("Student Details ")
S1.printStudentDetails ()
Output:
Enter Roll Number : 001
Enter Name : ABC
Enter Address : New York
Student Details :
001 ABC New York
20. Create a parent class named Animals and a child class Herbivorous which will
extend the class Animal. In the child class Herbivorous over side the method feed (
). Create a object.
# parent class
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breath
def breathe(self):
print("I breathe oxygen.")
# function feed
def feed(self):
print("I eat food.")
# child class
class Herbivorous(Animal):
# function feed
def feed(self):
print("I eat only plants. I am vegetarian.")
herbi = Herbivorous()
herbi.feed()
# calling some other function
herbi.breathe()
Output:
I eat only plants. I am vegetarian.
I breathe oxygen.