Table of contents
1.
Introduction
2.
Performing Arithmetic in CherryPy Python
3.
Addition Operator in CherryPy Python
4.
Subtraction Operator in CherryPy python
5.
Multiplication Operator in CherryPy python
6.
Frequently Asked Question
6.1.
What is CherryPy?
6.2.
What are arithmetic operators in CherryPy?
6.3.
Does CherryPy support the MVC framework?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Performing Arithmetic in CherryPy Python

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

CherryPy is a Python web framework that offers Python developers a user-friendly interface to develop web applications. It enables developers to create web apps in the same manner as traditional object-oriented Python programs are created. As a result, smaller source code at high speed. Now, Let's see how Arithmetic operators in Cherrypy are done.
 

Performing arithmetic in CherryPy Python

Performing Arithmetic in CherryPy Python

Combining operands with one and more arithmetic operators defines an arithmetic operation. The built-in functions SUBTRACTADDMULTIPLY, and DIVIDE can also be used to specify arithmetic operations.

In arithmetic operations, you can use the following operators:

Addition Operator in CherryPy Python

The sum of numeric operands or string concatenation is done by the addition operator (+).

Addition Operator in CherryPy Python

Approach:

  • Create a user interface to receive user input.
  • Create a Cherrypy program to carry out the Addition operations.

 

HTML Code to Accept User Input:- 

<html>
<head>
   
  </head>
<body>
  
  <div class="container">  
    <h2><u><i>Operation</i></u></h2>
    <form action="store" id="form" method="GET">
    <input type="number" name="number1" /><br />
    <input type="number" name="number2" /><br />
    <input type="number" name="number3" /><br />
  
  
    <input style="margin-left: 250px;" id=" submit" type="submit"/></div>
  </div>    
    </form>
  </div>
              
</body>
</html>

 

Addition Code from Cherrypy:

import cherrypy
class Root(object):
    @cherrypy.expose
    def index(self):
        return """<html>
<head> 
  </head>
<body> 
  <div class="container">  
    <h2><u><i>We are doing Addition</i></u></h2>
    <form action="store" id="form" method="GET">
    <input type="number" name="number1" /><br />
    <input type="number" name="number2" /><br />
    <input type="number" name="number3" /><br />
    <input style="margin-left: 250px;" id=" submit" type="submit"/></div>
  </div>    
    </form>
  </div>
              
</body>
</html>"""  
    
    @cherrypy.expose
    def store(self, number1, number2, number3):
        num1=int(number1)
        num2=int(number2)
        num3=int(number3)
        answer=num1+num2+num3
        
        out= """<html>
        <body>    
		<p> Sum: %s</p>    
        <a style="color:red; font-size:35px;" id="shutdown"; href="./shutdown"><i> Server Shutdown</i></a>
        </body>
        </html>     
        """         
        return out % (str(answer))     
    @cherrypy.expose
    def shutdown(self):
        cherrypy.engine.exit()
  
if __name__=="__main__":
    cherrypy.config.update({'server.socket_port': 8087})
      
    cherrypy.quickstart(Root())
You can also try this code with Online Python Compiler
Run Code

 

Output:

output

output

Subtraction Operator in CherryPy python

The subtraction operator (-) subtracts the two operands, resulting in the difference between them.

Subtraction Operator in CherryPy python

 

Approach:

  • Create a user interface to receive user input.
  • Create a Cherrypy program to carry out the Subtraction operations.

 

HTML Code to Accept User Input:-

<html>
<head>   
  </head>
<body>
  
  <div class="container">  
    <h2><u><i>Operation</i></u></h2>
    <form action="store" id="form" method="GET">
    <input type="number" name="number1" /><br />
    <input type="number" name="number2" /><br />
    <input style="margin-left: 250px;" id=" result" type="submit"/></div>
  </div>    
    </form>
  </div>
              
</body>
</html>

 

Subtraction Operator from CherryPy:-

import cherrypy  
class Root(object):
     
    @cherrypy.expose
    def index(self):
        return """<html>
<head>  
  </head>
<body>
  
  <div class="container">  
    <h2><u><i>We are doing Subtraction</i></u></h2>
    <form action="store" id="form" method="GET">
    <input type="number" name="number1" /><br />
    <input type="number" name="number2" /><br />
    <input style="margin-left: 250px;" id=" result" type="submit"/></div>
  </div>    
    </form>
  </div>
              
</body>
</html>"""
     
      
    @cherrypy.expose
    def store(self, number1, number2):
        num1=int(number1)
        num2=int(number2)        
        answer=num1-num2        
        out= """<html>
        <body>
                   
<p> Result: %s</p>          
        <a style="color:red; font-size:35px;" id="Refresh"; href="./shutdown"><i>Refresh Server</i></a>
        </body>
        </html>         
        """         
        return out % (str(answer))  
    @cherrypy.expose
    def shutdown(self):
        cherrypy.engine.exit()
  
if __name__=="__main__":
    cherrypy.config.update({'server.socket_port': 8087})
      
    cherrypy.quickstart(Root())
You can also try this code with Online Python Compiler
Run Code

 

Output:

output

output

Multiplication Operator in CherryPy python

The multiplication operator (*) multiplies the two operands, resulting in the product of two numbers.

Multiplication Operator in CherryPy python

 

Approach:

  • Create a user interface to receive user input.
  • Create a Cherrypy program to carry out the Multiplication operations.

 

HTML Code to Accept User Input:-

<html>
<head>   
  </head>
<body>
  
  <div class="container">  
    <h2><u><i>Operation</i></u></h2>
    <form action="store" id="form" method="GET">
    <input type="number" name="number1" /><br />
    <input type="number" name="number2" /><br />
    <input style="margin-left: 250px;" id=" result" type="submit"/></div>
  </div>    
    </form>
  </div>
              
</body>
</html>

 

Subtraction Operator from CherryPy:-

import cherrypy
class Root(object):
    @cherrypy.expose
    def index(self):
        return """<html>
<head>
</head>
<body>
<div class="container">
    <h2><u><i>We are doing Multiplication</i></u></h2>
    <form action="store" id="form" method="GET">
    <input type="number" name="num1" /><br />
    <input type="number" name="num2" /><br />


    <input style="margin-left: 250px;" id=" result" type="submit"/></div>
</div>
    </form>
</div>

</body>
</html>"""
    @cherrypy.expose
    def store(self, num1, num2):
        mul1 = int(num1)
        mul2 = int(num2)
        result = mul1*mul2
        out = """<html>
        <body>
                
<p> Sum: %s</p>                
        <a style="color:red; font-size:35px;" id="Refresh"; href="./shutdown"><i>Refresh Server</i></a>
        </body>
        </html>        
        """
        return out % (result)
    @cherrypy.expose
    def shutdown(self):
        cherrypy.engine.exit()
if __name__ == "__main__":
    cherrypy.config.update({'server.socket_port': 8087})

    cherrypy.quickstart(Root())
You can also try this code with Online Python Compiler
Run Code

 

Output:

output

output

This is all about operators in CherryPy if you want to know about more related topics, here are some references

Frequently Asked Question

What is CherryPy?

CherryPy is a famous Python framework. Web applications can be constructed or built faster and more reliably with CherryPy. It's also known as a web application library. Because it is based on object-oriented Python programming, it is used for its simplicity, resulting in less source code in less time.

What are arithmetic operators in CherryPy?

Combining operands with one or more arithmetic operators defines an arithmetic operation. The built-in functions SUBTRACT, ADD, DIVIDE, and MULTIPLY can also be used to specify arithmetic operations.

Does CherryPy support the MVC framework?

CherryPy was created using the multithreading principle. It benefits from being able to manage several jobs at once as a result. It also adopts a modular strategy, building web services under the Model View Controller (MVC) paradigm; as a result, it is quick and developer-friendly.

Conclusion

In this article, we learned about Performing arithmetic operations in CherryPy python and how to perform Addition, Subtraction, and Multiplication in CherryPy.

After reading about Performing arithmetic in CherryPy python, are you not feeling excited to read/explore more articles on the topic of Ruby? Don't worry; Coding Ninjas has you covered. To learn, see Multithreading in Python, Descriptors in Python, and BorderLayout in Java.

Check out this article - C++ String Concatenation

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! 

Live masterclass