ue228120
ue228120
Python Programming
B.E. (IT) 5th Semester
Session: 2024-25
Submitted By:
PRANAV KUMAR
UEM228120
IT- Section (2)
Q1. Write a python program for a shop to search a book using a unique number/code.
Ans. CODE:
# Sample book database
books = {
"B001": "The Great Gatsby",
"B002": "1984",
"B003": "To Kill a Mockingbird",
"B004": "The Catcher in the Rye",
"B005": "Pride and Prejudice"
}
OUTPUT:
OUTPUT:
Q3. What are exceptions? How is exception handling done in Python?
Ans. In Python, exceptions are errors that occur during the execution of a program, disrupting its
normal flow. Unlike syntax errors, which occur when the code is being parsed, exceptions arise
when the code is running. For example, trying to divide by zero, access an undefined variable, or
open a file that doesn’t exist will all raise exceptions. Without handling, exceptions stop the
program and display an error message.
How is Exception Handling Done in Python?
To handle exceptions gracefully, Python provides the try-except block. By enclosing code in a try
block, you instruct Python to attempt execution and catch any exceptions in the corresponding
except block.
This way, the program can recover or handle issues without crashing.
Here is the structure of exception handling: try:
1. ArithmeticError:
o Raised for numeric errors.
o Example: ZeroDivisionError, OverflowError, FloatingPointError.
2. IndexError:
o Raised when attempting to access an index that is out of range in a sequence (like
lists or tuples).
4. NameError:
o Raised when referencing a variable that hasn’t been defined.
5. ValueError:
o Raised when a function receives an argument of the correct type but inappropriate
value.
6. TypeError:
o Raised when an operation is performed on incompatible types.
7. FileNotFoundError:
o Raised when a file or directory is requested but cannot be found.
8. IOError:
o Raised when an I/O operation fails, like file reading or writing issues.
Q4. What are regular expressions? How regular expressions are used for extracting data.
Ans. Regular Expressions (regex) are sequences of characters that define search patterns. These patterns are
powerful tools for matching and manipulating strings based on specific sequences of characters or rules.
Regular expressions allow users to search for and identify patterns within text, making them invaluable for
data extraction, validation, and formatting.
In Python, the re module provides support for regular expressions, making it possible to apply
complex text matching and extraction operations with ease.
• Simple characters that match exactly themselves, like "a", "1", or "cat".
Metacharacters:
Special symbols that modify the meaning of characters or patterns:
• re.search(pattern, string): Searches for the first match of the pattern in the string and returns
a Match object.
• re.findall(pattern, string): Finds all non-overlapping occurrences of the pattern in the string
and returns them as a list.
• re.finditer(pattern, string): Finds all matches and returns them as an iterator of Match
objects.
• re.sub(pattern, replacement, string): Replaces occurrences of the pattern in the string with a
specified replacement.
3. Examples of Data Extraction Using Regular Expressions
Example 1: Extracting Email Addresses
Suppose we want to extract all email addresses from a block of text.
Code:
import re