Unit summary
- A syntax error is found before running, while an exception occurs during execution.
- try holds risky code, and except handles named exceptions.
- else runs when no exception occurs, and finally always runs.
- raise creates an exception, and a user defined exception inherits from Exception.
- Exceptions form a hierarchy, and specific blocks must come before general ones.
- Matplotlib draws graphs with the pyplot module, imported as plt.
- plot draws lines, and sine and cosine curves use np.linspace.
- hist draws a histogram and pie draws a pie chart.
- 3D graphs use projection="3d" with plot3D, scatter3D and plot_surface.
Glossary
| Term | Meaning |
|---|---|
| Bin | An interval used in a histogram. |
| else | A clause that runs if no exception occurred. |
| except | A block that handles an exception. |
| Exception | A run-time error that interrupts normal flow. |
| finally | A clause that always runs. |
| Hierarchy | The tree of exception classes. |
| Histogram | A graph of how values are spread over intervals. |
| Line plot | A graph that joins data points with lines. |
| linspace | A NumPy function for evenly spaced values. |
| Matplotlib | A library for drawing graphs. |
| Pie chart | A circular graph showing shares of a whole. |
| pyplot | The Matplotlib module of plotting functions. |
| raise | A statement that creates an exception. |
| Surface plot | A 3D graph of height over a grid. |
| Syntax error | An error in the form of the code found before running. |
| Traceback | The message that shows where an exception occurred. |
| try | A block that holds code that may fail. |
| User defined exception | A new exception class made by the programmer. |
| ValueError | The exception for an unsuitable value. |
| ZeroDivisionError | The exception for division by zero. |
Question bank
Short Answer Questions (2 marks each)
- What is an exception?
- Distinguish a syntax error from an exception.
- Write the syntax of try-except.
- What is the use of the finally clause?
- What is the use of the raise statement?
- Name four built-in exceptions.
- What is Matplotlib?
- Which function draws a histogram?
- What does autopct do in a pie chart?
- How is a 3D axis created?
Medium Answer Questions (5 marks each)
- Explain the try-except-else-finally structure with an example.
- Explain the hierarchy of exceptions.
- Explain how a user defined exception is created and raised.
- Explain how a line plot is drawn with labels.
- Explain histograms and pie charts with examples.
- Explain 3D plotting with a surface plot.
Long Answer Questions (15 marks each)
- Explain exception handling with try, except, else and finally. Write a program to handle ZeroDivisionError and to detect duplicates in a list of integers.
- Explain the hierarchy of built-in exceptions and the raise statement with a user defined exception.
- Explain Matplotlib. Write programs to draw the sine and cosine curves and a line plot of month number against total profit with proper labels.
- Explain histograms, pie charts and 3D graphics with programs.
Sources
- Official BCA syllabus Python Programming, course code 26BCA403SEC01, Unit IV
