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)

  1. What is an exception?
  2. Distinguish a syntax error from an exception.
  3. Write the syntax of try-except.
  4. What is the use of the finally clause?
  5. What is the use of the raise statement?
  6. Name four built-in exceptions.
  7. What is Matplotlib?
  8. Which function draws a histogram?
  9. What does autopct do in a pie chart?
  10. How is a 3D axis created?

Medium Answer Questions (5 marks each)

  1. Explain the try-except-else-finally structure with an example.
  2. Explain the hierarchy of exceptions.
  3. Explain how a user defined exception is created and raised.
  4. Explain how a line plot is drawn with labels.
  5. Explain histograms and pie charts with examples.
  6. Explain 3D plotting with a surface plot.

Long Answer Questions (15 marks each)

  1. Explain exception handling with try, except, else and finally. Write a program to handle ZeroDivisionError and to detect duplicates in a list of integers.
  2. Explain the hierarchy of built-in exceptions and the raise statement with a user defined exception.
  3. Explain Matplotlib. Write programs to draw the sine and cosine curves and a line plot of month number against total profit with proper labels.
  4. Explain histograms, pie charts and 3D graphics with programs.

Sources

  • Official BCA syllabus Python Programming, course code 26BCA403SEC01, Unit IV