Ans To Worksheet Theory

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

1)Function name must be followed by parentheses ().

2) def keyword is used to define a function.


3)Function will perform its action only when it is invoked.
4) Give one difference between Text file and Binary File:
Ans: Text File: Contains readable characters like letters and numbers, using a
system like ASCII or UTF-8.
Binary File: Contains data in a format only computers can easily understand,
like images, audio, or programs.
5) Write a Python statement to open a text file “DATA.TXT” so that new contents
can be written on it.
Ans: f=open(“DATA.txt”,”w”)
6) Write a Python statement to open a text file “DATA.TXT” so that new content
can beadded to the end of file
Ans: f=open(“DATA.txt”,”a”)
7) Write a Python statement to open a text file “DATA.TXT” so that existing
contents can be read from file.
Ans: f=open(“DATA.txt”,”r”)
8) What is the difference in file opening mode “a” and “w”and “r” ?

Ans: "a" mode (append mode): Adds new content to the end of the file without deleting
what's already there. If the file doesn't exist, it creates a new one.

"w" mode (write mode): Deletes all existing content in the file before writing new content.
If the file doesn't exist, it creates a new one.

"r" (read mode): Opens the file for reading. The file must exist, and its content is
preserved. No writing or modifying is allowed.

9) What is the difference between readline() and readlines() ?

Ans: readline(): Reads one line from the file each time you call it.

readlines(): Reads all the lines from the file at once and returns them as a list.

10) What is the purpose of using flush() in file handling operations ?


Ans: The purpose of using flush() is to ensure that all buffered data is immediately written
to the file.

11) What is the advantage of opening file using ''with‟ keyword?

Ans: The advantage of opening a file using the "with" keyword is that it automatically
manages resources, ensuring that the file is properly closed after its block of code is
executed.

12) What is the difference in write() and writelines()?

Ans: write(): Writes a single string to the file.

writelines(): Writes a list of strings to the file, each string as a separate line

13)What is a File?
Ans: a file is a collection of data stored in a computer that is given a name and a location
on the storage device. It can contain text, images, programs, or any other type of
information.
14)What is-
a)Text File- A text file in Python is a file that stores data in the form of plain text, containing
human-readable characters like letters, numbers, and symbols, rather than binary data.
b)Binary File- A binary file in Python is a type of file that contains data in a format that is not
meant to be human-readable, typically composed of 0s and 1s, representing raw data rather than
text.
c)CSV File- A CSV file in Python is a plain text file where data is separated by commas, with each
line representing a row of data and each comma separating different values.
15)Difference between read() and read(n):

Ans: read(): Reads the entire file.

read(n): Reads the next n characters from the file.

16)Advantages of CSV File.

Ans: Simple: Creating, reading, and editing CSV files is easy with basic text editors or
spreadsheet software.
Compatible: CSV files work with many software applications, making them versatile for
sharing data.
Compact: They have small file sizes, saving storage space and speeding up data transfer.
Readable: CSV files are plain text, making their content easily readable by both humans and
machines, aiding in transparency and problem-solving.

17)Difference between seek() and tell()

Ans: seek(): Moves the file pointer to a specific position.

tell(): Returns the current position of the file pointer.

18)Difference between open and with open:

Ans: open:

 Requires manually closing the file with file.close().


 Can lead to resource leaks if not closed properly.

with open:

 Automatically handles opening and closing the file.


 Ensures the file is closed even if an error occurs, making the code safer.

19)Types of Errors:

 SyntaxError: Occurs when the syntax of the code is invalid.


 IndentationError: Raised when indentation is not properly aligned.
 NameError: Happens when a variable or function name is used but not defined.
 TypeError: Occurs when an operation is performed on a value of inappropriate type.
 ValueError: Raised when a function receives an argument of the correct type but with
an inappropriate value.

 ZeroDivisionError: Happens when division or modulo operation is performed with zero


as the divisor.

 IndexError: Raised when trying to access an index that is out of range.

 KeyError: Happens when trying to access a key that does not exist in a dictionary.
 AttributeError: Occurs when trying to access or use an attribute of an object that does
not exist.
 ImportError: Raised when an import statement fails to find the module.

RuntimeError: General error indicating a problem occurred that does not belong to any
specific category.

EOFError: An EOFError occurs when Python tries to read input or a file but
unexpectedly reaches the end of the file.

You might also like