Addition Operator in CherryPy Python
The sum of numeric operands or string concatenation is done by the addition operator (+).

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:


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

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:


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

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:


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 Algorithms, Competitive Programming, JavaScript, System Design, and many more!