Ad
Saturday, December 9, 2023
Chapter 4 Modules ,Working with files, Exception handling
- What is regEx? give example.
import re
# Regular Expression Example
pattern = re.compile(r'\b\d{3}-\d{2}-\d{4}\b') # Matches social security numbers
text = "John's SSN is 123-45-6789."
match = pattern.search(text)
if match:
print("SSN found:", match.group())
else:
print("No SSN found.")
- What is user defined Module? Give example.
# User-Defined Module Example
# Save this code in a file named mymodule.py
def greet(name):
return f"Hello, {name}!"
# In another script, you can use this module
import mymodule
message = mymodule.greet("Alice")
print(message)
-What is the use of seek( ) & tell ( ) functions?
seek(offset, whence): Moves the file cursor to the specified offset position, relative to whence (0 for the beginning, 1 for the current position, and 2 for the end).
tell(): Returns the current position of the file cursor.
Write a short note on exception handling.
Exception Handling:
Exception handling in Python allows you to deal with errors or exceptions that may occur during the execution of a program. It involves the use of try, except, else, and finally blocks.
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
else:
print(f"Result: {result}")
finally:
print("This block always executes.")
-What is a module? What is package? Explain with example.
Module: A module in Python is a file containing Python definitions and statements. It allows you to logically organize your Python code. For example:
# mymodule.py
def greet(name):
return f"Hello, {name}!"
# main.py
import mymodule
print(mymodule.greet("Alice")) # Output: Hello, Alice!
Package: A package is a way of organizing related modules into a single directory hierarchy. It helps in organizing the code and avoids naming conflicts. A package contains an __init__.py file and can have subpackages. Example:
mypackage/
├── __init__.py
├── module1.py
└── module2.py
Explain any 2 functions in time module.
--What are the types of file in Python?
- Write the use of seek & tell function.
- How to handle exception in Python?
-Explain any 2 metacharacters used in regular expression.
About Abhishek Dhamdhere
Qna Library Is a Free Online Library of questions and answers where we want to provide all the solutions to problems that students are facing in their studies. Right now we are serving students from maharashtra state board by providing notes or exercise solutions for various academic subjects
No comments:
Post a Comment