Saturday, July 9, 2016

My First Python Program

Hi Folks,

Greetings to all of you. I haven't post anything since a long time. I am passionate about learning new things. In the due course, I started to learn another programming language "Python". I heard a lot of buzz about this due to the Data Analytics.

Anyway, I am just taking the baby steps towards a big journey. During my baby steps, I tried to write a python program which demonstrates the basic arithmetic operations on integers. Below is the program.

# !/usr/bin/python

#################################################################################
# Program   : IntMathOpsDemo                                                              #
# Purpose    : Demonstrate the Basic Arithmetic Operations on Integers using sub-routines #
#             ( Addition, Subtraction, Multiplication and Division which also supports    #
#             Exception Handling of ZeroDivideError)                                      #
# Author    : Kamesh Akundi                                                               #
# Date      : 08th July, 2016                                                             #
#################################################################################


# sub-routine to add two integers
def addition(a, b) -> object:
    if a == 0:
        return b
    elif b == 0:
        return a
    else:
        return a + b


# sub-routine to subtract two integers
def subtraction(a, b):
    if a == 0:
        return -1 * b
    elif b == 0:
        return a
    else:
        return a - b


# sub-routine to multiply two integers
def multiplication(a, b):
    if a == 1:
        return b
    elif b == 1:
        return a
    else:
        return a * b


# sub-routine to divide two integers
def division(a, b):
    try:
        if a == 0:
            return 0
        else:
            return a / b
    except ZeroDivisionError:
        err_msg = 'Division with Zero Denominator is an Error!'
        return err_msg


# sub-routine to divide two integers
def modulos_division(a, b):
    try:
        if a == 0:
            return 0
        else:
            return a % b
    except ZeroDivisionError:
        err_msg = 'Modulo with Zero is an Error!'
        return err_msg


# sub-routine to call all the operations
def demo_operations(a, b):
    print("Sum of ", a, ",", b, "is :", addition(a, b))
    print("Difference between ", a, ",", b, "is :", subtraction(a, b))
    print("Product of ", a, ",", b, "is :", multiplication(a, b))
    print("Dividend of ", a, ",", b, "is :", division(a, b))
    print("Remainder of ", a, ",", b, "is :", modulos_division(a, b))


# main routine to demonstrate the integer arithmetic operations
while True:
    try:
        x = input("Enter a number: ")
        a = int(x)
        break
    except ValueError:
        print(x, " is not a valid integer!")

while True:
    try:
        y = input("Enter another number: ")
        b = int(y)
        break
    except ValueError:
        print(y, " is not a valid integer!")
print('================================================================\n')
print('You entered two integers', a, b)
print('\nNow it is good to proceed with Arithmetic Operations...\n')
print('================================================================\n')
print('Results are:')
demo_operations(a, b)