XII CS Unit1 TxT File DataStr Exception Notes
XII CS Unit1 TxT File DataStr Exception Notes
Write a Python function that displays all Write a Python function that finds and
the words containing @cmail from a text displays all the words longer than 5
file "Emails.txt". characters from a text file "Words.txt".
Sample Program
# Initialize an empty list to represent the stack
stack = []
A) You have a stack named BooksStack that contains records of books. Each book record
is represented as a list containing book_title, author_name, and publication_year.
Write the following user-defined functions in Python to perform the specified operations
on the stack BooksStack:
There is a stack named Uniform that contains records of uniforms Each record is
represented as a list containing uid, uame, ucolour, usize, uprice.
Write the following user-defined functions in python to perform the specified operations
on the stack Uniform :
A list, items contain the following record as list elements [itemno, itemname, stock]. Each
of these records are nested to form a nested list.
Write the following user defined functions to perform the following on a stack reorder.
i. Push(items)- it takes the nested list as its ii. Popitems() -It pops the objects
argument and pushes a list object containing one by one from the stack reorder
itemno and itemname where stock is less than 10 and also displays a message ‘Stack
empty’ at the end.
You have a stack named Inventory that contains records of medicines. Each record is
represented as a list containing code, name, type and price.
Write the following user-defined functions in Python to perform the specified operations
on the stack Inventory:
You have a stack named MovieStack that contains records of movies. Each movie record
is represented as a list containing movie_title, director_name, and release_year.
Write the following user-defined functions in Python to perform the specified operations
on the stack MovieStack:
Write the definition of a user defined function push_words(N) which accept list of words
as parameter and pushes words starting with A into the stack named InspireA
def push_words(N):
# Initialize an empty stack
InspireA = []
# Loop through each word in the list N
for word in N:
# Check if the word starts with the letter 'A'
if word.startswith('A') or word.startswith('a'):
# Push the word to the stack
InspireA.append(word)
# Return the stack (optional, to view the result)
return InspireA
Write the function pop_words(N) to pop topmost word from the stack and return it. if
the stack is empty, the function should display “Empty”.
def pop_words(N):
# Check if the stack is empty
if not N:
# If the stack is empty, return "Empty"
return "Empty"
else:
# Pop the topmost word from the stack and return it
return N.pop()
Write a Python program to input an integer and display all its prime factors in descending
order, using a stack. For example, if the input number is 2100, the output should be: 7 5
5 3 2 2 (because prime factorization of 2100 is 7x5x5x3x2x2)
An exception is a Python object that represents an error. When an error occurs during
the execution of a program, an exception is said to have been raised. Such an exception
needs to be handled by the programmer so that the program does not terminate
abnormally.
Types of Errors
1. Compile Time Errors: compile time errors are classified into two categories:
(i) Syntax Errors: A syntax error occurs when any of the Python programming
rules are violated or the program is not written correctly as per the format
required.
For Example,
x =+3 #Statement 1 not the correct operator,
if a = (a+b) #Statement 2 is incorrect.
(ii) Semantic Errors: Semantic Errors occur when the statement has no meaning
in the program.
For Example,
a=5 #Statement 1
b=7 #Statement 2
a+b=res #Statement 3 has a semantic error because an expression never
comes to the left side of the assignment operator.
2. Logical errors:
Quite often the programmer has written the code correctly without any syntax error or
semantic error. But didn’t get the exact result that is desired. This happens because of
the programmer’s mistake where the appropriate logic is not used.
3. Runtime errors:
Sometimes the program is correct syntactically and semantically, but when the
programmer runs the program, errors occur. Such kinds of errors are called runtime
errors. Runtime errors are very harder to detect in the programs. These errors may stop
the program execution abnormally or crash in between or runs infinite loops.
4. Exceptions:
An exception is a program event that occurs during program execution and disrupts the
flow of a program. When a Python program cannot cope with a situation, it raises an
exception. An exception is a Python object that represents an error.
Some common Python exceptions are as follows:
Exception Explanation
ValueError It is raised when a built-in method or operation mismatched or
inappropriate values are provided as input.
KeyboardInterrupt It is raised when the user accidentally presses delete or esc key or
cancels the execution.
Finally Clause
The statements inside the finally block are always executed regardless of whether an
exception has occurred in the try block or not.
The finally clause always executes at the end. The finally clause ends the exception-
handling process. Just have a look at the following:
II. Give an example code to handle ZeroDivisionError? The code should display the
message "Division by Zero is not allowed" in case of ZeroDivisionError exception, and the
message " " in case of any other exception.
Ans:
try:
a=int(input("Enter an integer: "))
print("Reciprocal of the number =",1/a)
except ZeroDivisionError:
print("Division by Zero is not allowed")
except:
print("Some Error Ocurred")
II. Give an example code to handle NameError? The code should display the message
"Some name is not defined" in case of NameError exception, and the message "Some
error occurred" in case of any other exception.
Ans:
try:
a=eval(input("Enter an integer: "))
print("Reciprocal of the number =",1/a)
except NameError:
print("Some name is not defined")
except:
print("Some Error Ocurred")