Unit 5: Exception Handling
Exception Handling in Python
An exception is a runtime error that occurs during program execution and stops the normal flow of the program.
Common Examples
- Division by zero
- File not found
- Invalid input
What is Exception Handling?
Exception handling is a mechanism to handle runtime errors gracefully so that the program does not crash suddenly.
Python uses:
try- except
- finally
try and except Clause
Syntax
Example
Handling Specific Exceptions
try – finally Clause
The finally block always executes, whether an exception occurs or not.
Syntax
Example
try – except – finally (Complete Form)
User-Defined Exceptions
User can create custom exceptions using Exception class.
Syntax
Example
Important Exception Keywords
| Keyword | Use |
|---|---|
| try | Test risky code |
| except | Handle error |
| finally | Always execute |
| raise | Throw exception |
Important Exam Points (Exception Handling)
- Exceptions occur at runtime
exceptprevents program crashfinallyalways executes- User-defined exceptions improve control
- Multiple
exceptblocks allowed
Basics of Python for Data Analysis
Python is widely used in Data Analysis due to libraries like NumPy, Pandas, Matplotlib.
Why Python for Data Analysis?
- Easy syntax
- Powerful libraries
- Handles large datasets
- Strong community support
Introduction to Pandas Library
Pandas is a Python library used for data manipulation and analysis.
Main Data Structures
- Series
- DataFrame
Introduction to Series
A Series is a one-dimensional labeled array.
Example
Series with Custom Index
Series Properties
| Property | Meaning |
|---|---|
| values | Data |
| index | Labels |
| dtype | Data type |
Introduction to DataFrame
A DataFrame is a two-dimensional table-like structure (rows & columns).
Diagram (Conceptual)
Creating DataFrame
Accessing DataFrame Data
Series vs DataFrame
| Feature | Series | DataFrame |
|---|---|---|
| Dimension | 1D | 2D |
| Structure | Single column | Rows & columns |
| Use | Single data | Tabular data |
Important Exam Points (Data Analysis)
- Pandas is core library for data analysis
- Series is 1D, DataFrame is 2D
- DataFrame works like Excel sheet
- Labels make data easy to handle
Frequently Asked Exam Questions
- What is exception handling?
- Explain try-except-finally
- What is user-defined exception?
- Define Series and DataFrame
- Difference between Series and DataFrame
Tags:
Python Programming