Two Mark Answers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

K.L.N.

COLLEGE OF ENGINEERING, Pottapalayam-630612


Department of Electrical and Electronics Engineering
20GE101 – PSPP CIT-4 QB Two Mark Answers
PART_A
1) Build a python program to count in a sentence using split() function.
f=open("new.txt","r")
content=f.readline()
words=content.split()
print(len(words))
2) Describe renaming and deleting a file in python.
Renaming a file:
import os
os.rename(“new1.txt”, “new2.txt”)
new1.txt →Old File Name
new2.txt → New File Name
Deleting a file:
import os
os.remove(“new1.txt”)
Deletes the particular file.
3) Difference between built-in exceptions and handling exceptions
Built-in Exception:
✓ Python has many built in exceptions which are raised when there is
some error in the program.
✓ It has been handled with try, except and else block.
✓ Some common exceptions are TypeError, ZeroDivisionError Etc.
Handling User Defined Exceptions:
✓ Sometimes users want to create their own exception for a specific
scenario to make error messages, which are called as user defined
exception.
✓ For example, Handling the Age for eligibility for vote as “InvalidAge
Error”
4) List out different modes of file opening.
‘r’ → Opens a file in read mode as text file (default)
‘w’ → Opens a file for writing purpose, if not exist means, creates a
new file
‘a’ → Opens a file to append data at the EOF
If ‘rb’ or ‘wb’ or ‘ab’ means it opens a file in binary mode.
5) How do you use command line arguments to give input to the program.
✓ In Python, command-line arguments are accessed through the
sys.argv list.
✓ The first item in the list is always the name of the script itself, and
subsequent items are the arguments passed to the script.
✓ The input parameters are passed to the script while executing them.
✓ Eg: $ python script.py arg1 arg2 arg3

6) Write the syntax for opening a file to write in text mode.


f = open(“new.txt”, ‘w’)
f.write(“Hi! Hello!”)
f.write(“Welcome to Python”)
f.close()
new.txt → File Name
‘w’ → Open the text file in write mode

7) How do you handle the exception inside a program when you try to
open a non-Existent file?
try:
f=open("new.txt",'r')
except IOError:
print("No Such File")
else:
print("File Opened")
8) Difference between write and append mode.
Write Mode Append Mode
• Open a file to write data, if file • Open a file to append data, if file
does not exist, it creates a new does not exist, it creates a new
file file
• It overwrites the existing data • It adds the data at the EOF
• Cursor placed in the beginning • Cursor placed in the End of the
of the file File.
• f = open (“new.txt”, ‘w’) • f = open (“new.txt”, ‘a’)

9) Define errors & list out the types of errors.


Errors are problems that occur in the program due to an illegal
operation performed by the user or by the fault of a programmer, which
halts the normal flow of the program. Some common types of errors are,
❖ Syntax Error
❖ Indentation Error
❖ IO Error
❖ Value Error
❖ Type Error
❖ EOF error
10) What is module? Give Example.
✓ A module is simply a file that defines one or more related functions
grouped together.
✓ To reuse the functions of a given module, we need to import the
module.
✓ Eg: Math Module, Random Module, Calendar Module, tkinter module,
Turtle Module Etc.,
-Prepared By
Mr.R.Thangasankaran, AP/EEE

You might also like