0% found this document useful (0 votes)
13 views3 pages

Python Practical All 20 Programs

The document contains a series of Python programs demonstrating exception handling, file operations, and data structure implementations. Each program includes a description, code snippet, and expected output. Key topics covered include handling errors like ZeroDivisionError, FileNotFoundError, and TypeError, as well as implementing stacks and queues.

Uploaded by

studypassion2026
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Python Practical All 20 Programs

The document contains a series of Python programs demonstrating exception handling, file operations, and data structure implementations. Each program includes a description, code snippet, and expected output. Key topics covered include handling errors like ZeroDivisionError, FileNotFoundError, and TypeError, as well as implementing stacks and queues.

Uploaded by

studypassion2026
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q1. Program to handle a ZeroDivisionError when dividing a number by zero.

Program:
try: a = int(input("Enter numerator: ")) b = int(input("Enter denominator: ")) print(a / b) except
ZeroDivisionError: print("Division by zero not allowed")
Explanation: This program handles division by zero using exception handling.
Output: Division by zero not allowed

Q2. Program to handle a FileNotFoundError when opening a non-existing file.

Program:
try: f = open("[Link]", "r") print([Link]()) except FileNotFoundError: print("File not found")
Explanation: This program handles error when a file does not exist.
Output: File not found

Q3. Program to handle a TypeError when concatenating incompatible data types.

Program:
try: a = 10 b = "Hello" print(a + b) except TypeError: print("Type mismatch error")
Explanation: This program handles TypeError caused by incompatible data types.
Output: Type mismatch error

Q4. Program to demonstrate the use of try-except-else clause.

Program:
try: x = int(input("Enter x: ")) y = int(input("Enter y: ")) print(x / y) except ZeroDivisionError:
print("Division by zero") else: print("Division successful")
Explanation: This program shows the use of else block in exception handling.
Output: Division successful

Q5. Program to handle multiple exceptions in a single try block.

Program:
try: a = int(input("Enter number: ")) b = int(input("Enter number: ")) print(a / b) except
(ZeroDivisionError, ValueError): print("Error occurred")
Explanation: This program handles multiple exceptions in one try block.
Output: Error occurred

Q6. Program to raise a custom exception when a certain condition is met.

Program:
class MyError(Exception): pass try: age = int(input("Enter age: ")) if age < 18: raise MyError
print("Eligible") except MyError: print("Custom error raised")
Explanation: This program raises a custom exception based on condition.
Output: Custom error raised

Q7. Program to handle an IndexError when accessing elements from a list.

Program:
try: lst = [1, 2, 3] print(lst[5]) except IndexError: print("Index out of range")
Explanation: This program handles IndexError in list.
Output: Index out of range

Q8. Program to handle a KeyError when accessing a dictionary.


Program:
try: d = {"a":1, "b":2} print(d["c"]) except KeyError: print("Key not found")
Explanation: This program handles KeyError in dictionary.
Output: Key not found

Q9. Program to handle a KeyboardInterrupt gracefully.

Program:
try: while True: print("Running") except KeyboardInterrupt: print("Program stopped")
Explanation: This program handles keyboard interruption.
Output: Program stopped

Q10. Program to read and display the contents of a text file.

Program:
f = open("[Link]", "r") print([Link]()) [Link]()
Explanation: This program reads data from a file.
Output: File content displayed

Q11. Program to write user input to a text file.

Program:
f = open("[Link]", "w") [Link]("Hello Python") [Link]()
Explanation: This program writes data into a file.
Output: Data written

Q12. Program to append new data to an existing text file.

Program:
f = open("[Link]", "a") [Link]("New Data") [Link]()
Explanation: This program appends data to file.
Output: Data appended

Q13. Program to implement a stack using a list.

Program:
stack = [] [Link](10) [Link](20) print(stack)
Explanation: This program implements stack using list.
Output: [10, 20]

Q14. Program to implement a queue using a list.

Program:
queue = [] [Link](10) [Link](20) print(queue)
Explanation: This program implements queue using list.
Output: [10, 20]

Q15. Program to push an element onto the stack.

Program:
stack = [10, 20] [Link](30) print(stack)
Explanation: This program pushes element into stack.
Output: [10, 20, 30]
Q16. Program to pop an element from the stack.

Program:
stack = [10, 20, 30] [Link]() print(stack)
Explanation: This program pops element from stack.
Output: [10, 20]

Q17. Program to insert an element into the queue.

Program:
queue = [10, 20] [Link](30) print(queue)
Explanation: This program inserts element into queue.
Output: [10, 20, 30]

Q18. Program to delete an element from the queue.

Program:
queue = [10, 20, 30] [Link](0) print(queue)
Explanation: This program deletes element from queue.
Output: [20, 30]

Q19. Program to check if a stack is empty.

Program:
stack = [] if not stack: print("Stack is empty")
Explanation: This program checks if stack is empty.
Output: Stack is empty

Q20. Program to check if a queue is empty.

Program:
queue = [] if not queue: print("Queue is empty")
Explanation: This program checks if queue is empty.
Output: Queue is empty

You might also like