Skip to content

Commit 7231a7f

Browse files
Update QuetionBank.md
1 parent db4c660 commit 7231a7f

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

QuetionBank.md

+76
Original file line numberDiff line numberDiff line change
@@ -390,4 +390,80 @@ The cat meows
390390
The cow moos
391391

392392
```
393+
## 7.Describe the differences between lists, sets, tuples, and dictionaries in Python. Provide
394+
an example for each.
393395
![image](https://github.com/user-attachments/assets/fddf927e-3b53-4c9c-a39f-6a928309892e)
396+
397+
# 8.Explain the significance of the with statement in Python for file handling. Provide an
398+
example.
399+
400+
# The Significance of the `with` Statement in Python for File Handling
401+
402+
The **`with`** statement in Python is used to wrap the execution of a block of code. It simplifies exception handling and resource management, particularly when dealing with file operations. The **`with`** statement is often referred to as a **context manager** and ensures that resources are properly cleaned up after use, even in the event of an error.
403+
404+
When working with files, it is important to close the file after it has been used to free up system resources. The **`with`** statement automatically takes care of closing the file once the block of code is executed, eliminating the need to explicitly call `file.close()`.
405+
406+
### Significance of `with`:
407+
- **Automatic Resource Management**: It ensures that the file is closed automatically after the block is executed, even if an exception is raised.
408+
- **Cleaner Code**: It reduces the need for explicit `try`-`finally` blocks to close files, making the code more concise and readable.
409+
- **Error Handling**: It manages exceptions gracefully, ensuring that resources are cleaned up even if an error occurs.
410+
411+
### Example Code:
412+
413+
```python
414+
# Using 'with' for file handling
415+
file_name = "example.txt"
416+
417+
# Writing to a file using the 'with' statement
418+
with open(file_name, 'w') as file:
419+
file.write("Hello, world!\nThis is a file handling example.")
420+
421+
# Reading from a file using the 'with' statement
422+
with open(file_name, 'r') as file:
423+
content = file.read()
424+
print(content)
425+
426+
```
427+
# 9.Describe how the try, except, and finally blocks are used in Python to handle exceptions
428+
with an example.
429+
430+
In Python, exceptions are events that can disrupt the normal flow of a program. Exception handling allows you to gracefully handle errors and maintain control over program execution. The `try`, `except`, and `finally` blocks are used together to catch exceptions and perform necessary cleanup actions.
431+
432+
### Key Concepts:
433+
- **`try` Block**: The code that may raise an exception is placed inside the `try` block.
434+
- **`except` Block**: If an exception is raised inside the `try` block, the code inside the `except` block is executed to handle the exception.
435+
- **`finally` Block**: This block is always executed, regardless of whether an exception occurred or not. It is typically used for cleanup operations (e.g., closing files or releasing resources).
436+
437+
### Syntax:
438+
```python
439+
try:
440+
# Code that may cause an exception
441+
pass
442+
except <ExceptionType> as e:
443+
# Code to handle the exception
444+
pass
445+
finally:
446+
# Code that always executes (cleanup actions)
447+
pass
448+
```
449+
450+
# Example
451+
```python
452+
try:
453+
# Attempting to divide by zero
454+
num1 = 10
455+
num2 = 0
456+
result = num1 / num2
457+
except ZeroDivisionError as e:
458+
print(f"Error: {e}")
459+
finally:
460+
print("This will always execute.")
461+
462+
```
463+
# Output
464+
```python
465+
Error: division by zero
466+
This will always execute.
467+
468+
```
469+

0 commit comments

Comments
 (0)