Python is a popular programming language used for web development, system scripting, machine learning, Data Analysis and so on.
It’s available for many platforms such as Windows, Linux and Mac and from the official web site https://www.python.org it’s possible to download all releases.
The latest version is the 3 and, we can use many editor to write Python code such as Visual Studio Code.
If you decide to use VS Code, I advice you to install the Python extensions in order to have many features, like for example the IntelliSense.
Now, we will develop our first easy application in Python, called Application_One.py, with whom will be possible to run the four mathematical operations (addition, subtraction, multiplication and division) using two integer numbers in input.
[APPLICATION_ONE.PY]
# we have to import sys in order to use the method exit()
import sys
# definition of a method used for the basic mathematical operations
def MathOperation(value1,value2, operation):
if operation == "+":
print("Addition")
return value1 + value2
elif operation == "-":
print("Subtraction")
return value1-value2
elif operation == "*":
print("Multiplication")
return value1*value2
elif operation == "/":
print("Division")
return value1/value2
else:
return "This operation is not available"
# Insert first integer number
val1 = input("Insert the first value: ")
# Input verification
if val1.isnumeric()==False:
print("Values must be integer")
sys.exit()
# Insert first integer number
val2 = input("Insert the second value: ")
# Input verification
if val2.isnumeric()==False:
print("Values must be integer")
sys.exit()
# Insert operation
operation = input("Select the operation (now are available: +, -, *, /): ")
# run the operation and display the result
print(MathOperation(int(val1), int(val2), operation))
Now, we open a terminal (Terminal->New Terminal) and we run the application with command python3 Application_One.py
Output for an unavailable operation:
Output for a wrong value in input: