Python Program to Create a Basic Calculator 


In this article, we will write a python program to create a basic calculator using class, objects, and other methods. 

To better understand this example, make sure you have knowledge of the following tutorials:-




Python Class and Objects
Python Constructor
Python Functions
Python Operators



1. A Basic Calculator after using Python:


def cal():
    x = ('1. Add \n2. Sub \n3. Multiply \n4. Divide')
    print(x)
cal()

cal1 = input('Enter your choice: ')

num1 = int(input('Enter First number: '))
num2 = int(input('Enter Second number: '))

if cal1 == '1':
    x1 = num1 + num2
    print('Total number: ' + str(x1))
elif cal1 == '2':
    x2 = num1 - num2
    print('Total number: ' + str(x2))
elif cal1 == '3':
    x3 = num1 * num2
    print('Total number: ' + str(x3))
elif cal1 == '4':
    x4 = num1 / num2
    print('Total number: ' + str(x4))
else:
    print("Invalid input")   


2. A Basic Calculator after using Python Function:


def add(x, y):
    return x + y
def sub(x, y):
    return x - y
def multiply(x, y):
    return x * y
def divide(x, y):
    return x / y
def menu():
    opt = '1. Add \n2. Sub \n3. Multiply \n4. Divide'
    print(opt)
menu()

choice = int(input('Please select one of the following : '))

num1 = int(input('Enter your First name : '))
num2 = int(input('Enter your Second name : '))




while True:
    if choice == 1:
        print('Result : ', add(num1, num2))
        break
    elif choice == 2:
        print('Result: ', sub(num1, num2))
        break
    elif choice == 3:
        x3 = num1 * num2
        print('Result: ', multiply(num1, num2))
        break
    elif choice == 4:
        print('Result: ', divide(num1, num2))
        break
    else:
        print('Invaild option')
        break
print()






3. A Basic Calculator after using Python Class and Objects:


class cal():
    def __init__(self,a,b):
        self.a = a
        self.b = b
    def add(self):
        return self.a + self.b
    def sub(self):
        return self.a - self.b
    def multiply(self):
        return self.a * self.b
    def divide(self):
        return self.a / self.b

a = int(input('Enter First number : '))
b = int(input('Enter Second number : '))        
obj=cal(a,b)
while True:
    def menu():
        x = ('1. Add \n2. Sub \n3. Multiply \n4. Divide') 
        print(x)
    menu()
    choice = int(input('Please select one of the following : ')) 
    if choice == 1:
        print("Result: ",obj.add())
    elif choice == 2:
        print("Result: ",obj.sub())
    elif choice == 3:
        print("Result: ",obj.multiply())    
    elif choice == 4:
        print("Result: ",obj.divide())
    elif choice == 0:
        print('Again try one of the following')
        break
    else:
        print('Invalid option') 
        break       
print()


How to download Jenkins and Tomcat via shell script

In this article that how to download Jenkins and Tomcat via shell script with the log. This bash script is very easy and everyone can understand. please let us know if you are facing any issue regarding bash script with function and without function.


Example: 1

There is two function in which you can download both software links but we are using the same URL to download both applications. it's just an example.

#! /bin/bash
jen_url="http://mirrors.estointernet.in/apache/tomcat/tomcat-8/v8.5.47/bin/apache-tomcat-8.5.47-windows-x64.zip"
tom_url="http://mirrors.estointernet.in/apache/tomcat/tomcat-8/v8.5.47/bin/apache-tomcat-8.5.47-windows-x64.zip"
log_file="jen_tom_download.log"

download_jenkins(){
echo "Downloading Jenkins" | tee -a $log_file
wget $jen_url
if [ $? -eq 0 ]
then
echo "Successfully download jenkins" | tee -a $log_file
else
echo "Jenkins download is failed. Please check it manually" | tee -a $log_file #or >>$log_file
fi
}
download_tomcat(){
echo "Downloading Tomcat" | tee -a $log_file
wget $tom_url
if [ $? -eq 0 ]
then
echo "Successfully download Tomcat" | tee -a $log_file
else
echo "Tomcat download is failed. Please check it manually" | tee -a $log_file
fi
}
download_tomcat
download_jenkins

Example: 2 

There is a single function in which you can download both software links but we are using same URL to download both applications. it's just an example.

#! /bin/bash

jen_url="http://mirrors.estointernet.in/apache/tomcat/tomcat-8/v8.5.47/bin/apache-tomcat-8.5.47-windows-x64.zip"
tom_url="http://mirrors.estointernet.in/apache/tomcat/tomcat-8/v8.5.47/bin/apache-tomcat-8.5.47-windows-x64.zip"

download_url(){
app_name=$1
req_url=$2
echo "Downloading $app_name"
wget $req_url


if [ $? -eq 0 ]
then
echo "Successfully download $app_name"
else
echo "$app_name download is failed. Please check it manually"
fi
}

download_url jenkins $jen_url
download_url tomcat $tom_url

How to use Function in shell scripting


In this article, we will see that how to use about the shell functions. Functions enable you to break down the overall functionality of a script into smaller, logical subsections, which can then be called upon to perform their individual tasks when needed.


Using functions to perform repetitive tasks is an excellent way to create code reuse. This is an important part of modern object-oriented programming principles.

Shell functions are similar to subroutines, procedures, and functions in other programming languages.

Creating Functions

To declare a function, simply use the following syntax −

function_name () { 
   list of commands
}





The name of your function is function_name, and that's what you will use to call it from elsewhere in your scripts. The function name must be followed by parentheses, followed by a list of commands enclosed within braces.


#! /bin/bash

function name(){
    commands
}
name(){
    commands
}


Example:

function Hello(){
    echo "Hello"
}
quit(){
    exit
}

Hello
echo "fool"





#function with argument whatever you want to add argument in function but make sure that quit function always be in last

function print(){
    echo $0 $1 $2 $3 $4
}
quit(){
    exit
}

print Hello raju
print My name is kali
echo "fool"
quit

#local variable and print that file in function

function print(){
    name=$1
    echo "The name is $name"
}
print tam

#local command is used in the function to not change global command variable during output

function print(){
   local name=$1
    echo "The name is $name"
}

name="man"
echo "Name is $name before"
print tam
echo "Name is $name after"







How to use loop operator in shell scripting


In this article, we will learn that how to shell loop control in Unix. So far you have looked at creating loops and working with loops to accomplish different tasks. Sometimes you need to stop a loop or skip iterations of the loop.



In this chapter, we will learn the following two statements that are used to control shell loops−

The break statement

The continue statement

The Infinite Loop:

All the loops have a limited life and they come out once the condition is false or true depending on the loop.





A loop may continue forever if the required condition is not met. A loop that executes forever without terminating executes for an infinite number of times. For this reason, such loops are called infinite loops.

#! /bin/bash

#break and continue

#Break

for (( i=1; i<=10; i++ ))
do
     if [ $i -gt 5 ]
then
     break
fi
     echo "$i"
done

#continue




for (( i=1; i<=10; i++ ))
do
   if [ $i -eq 5 ]
then
    continue
fi
    echo "$i"
done

Example:

for (( i=1; i<=10; i++ ))
do
    if [ $i -eq 5 -o $i -eq 9 ]
then
     continue
fi
      echo "$i"
done

How to use For loop in shell script

In this article we will see that How to use for loop in shell script.

The for loop operates on lists of items. It repeats a set of commands for every item in a list.



Syntax: 

for var in word1 word2 ... wordN
do
   Statement(s) to be executed for every word.
done





Here var is the name of a variable and word1 to wordN are sequences of characters separated by spaces (words). Each time the for loop executes, the value of the variable var is set to the next word in the list of words, word1 to wordN.

#! /bin/bash
#for loops
for VARIABLE in 1 2 3 4 5 ... N
do
      command1
      command2
      commandN
done

for VARIABLE in file1 file2 file3
do
      command1 on $VARIABLE
      command2
      commandN
done

for OUTPUT in $(Linux-or-unix-command-here)
do
     command1 on $OUTPUT
     command2 on $OUTPUT
     commandN
done

for (( EXP1; EXP2; EXP3; ))

do
   command1
   command2
   commandN
done


#Example:
for time in 1 2 3 4 5
do
      echo $time
done
 
for time in {1..10}
do
       echo $time
done

Example:-

for time in {1..10..4}
do 
     echo $time
done
     echo ${BASH_VERSION}
      for (( time=0; time<5; time++ ))
do
      echo $time
done





#list command with for loops

for command in pwd ls date
do
      echo " ...................... $command ...................."
      $command
done

#all the word will be consider in for loop
for item in *
do
    if [ -d $item ]
    then
          echo $item
    fi
done

How to use Until loop in shell script


The while loop is perfect for a situation where you need to execute a set of commands while some condition is true. Sometimes you need to execute a set of commands until a condition is true.



Syntax:-

until command
do
   Statement(s) to be executed until command is true
done





Here the Shell command is evaluated. If the resulting value is false, given statement(s) are executed. If the command is true then no statement will be executed and the program jumps to the next line after the done statement.

#! /bin/bash

until loops (if condition is false the until loops work)

until [ condition ]
do
     command1
     command2
     command3
done


Example:

n=1
until [ $n -le 6 ]
do
      echo $n
      n=$(( n+1 ))
done

Example:

n=1
until [ $n -ge 6 ]
do
         echo $n
         n=$(( n+1 ))
done