Course Content
Introduction to Programming and Python
Overview of Python History and evolution Python’s features and benefits Set-Up Development Environment Installing Python and IDE (e.g., PyCharm, VS Code) Writing and running your first Python program Hands-On Activity Write a simple Python program to display “Hello, World!”
0/3
Structure Of A Python Program
A comprehensive overview of the structure of a Python program, covering essential concepts efficiently.
0/1
Basic Syntax and Control Statements
Basic Syntax Structure of a Python program Data types, variables, and operators
0/3
Object-Oriented Programming (OOP)
0/3
Python Programming For Beginners

Write a Program to Handle Division by Zero

  • Objective: Write a Python program that handles division by zero using exception handling.
  • Steps:
    1. Prompt the user to enter two numbers.
    2. Perform division and handle ZeroDivisionError.
    3. Print the result or an error message.

    python

    try:
        numerator = float(input("Enter the numerator: "))
        denominator = float(input("Enter the denominator: "))
        result = numerator / denominator
        print(f"The result is {result}")
    except ZeroDivisionError:
        print("Error: Cannot divide by zero")
    

Create Custom Exceptions for a Specific Scenario

  • Objective: Create custom exceptions to handle specific scenarios, such as invalid input.
  • Steps:
    1. Define a custom exception class.
    2. Write a function that raises the custom exception for invalid input.
    3. Use a try-except block to handle the custom exception.

    python

    class InvalidInputError(Exception):
        pass
    
    def validate_input(value):
        if not isinstance(value, int):
            raise InvalidInputError("Input must be an integer")
    
    try:
        user_input = input("Enter an integer: ")
        validate_input(int(user_input))
        print("Valid input")
    except InvalidInputError as e:
        print(e)  # Output: Input must be an integer
    except ValueError:
        print("Error: Input must be an integer")
PAGE TOP