We have understood how to declare and initiate a variable and how to validate the user input let us now understand how we can perform mathematical operations of these variables.
In programming, there are following operations that you can perform on variables.
Addition Addition operation is used to perform mathematical addition of two variables. In programming, we refer to “+” as a symbol of addition. Please note that Addition can only be carried out on Integer, Float, Double and String datatypes only. You can use this operator to concatenate two strings on either side of the operator.
For example, if there are two float variables, “x” and “y” declared in your program. Where x = 1.1, and y = 2.2. If we perform addition operation these variables in programming, the result is going to be 3.3
Subtraction
Subtraction operation is used to perform mathematical subtraction of two variables. In programming, we refer to “-” as a symbol of subtraction. Please note that subtraction can only be carried out on Integer, Float, Double datatypes only.
For example, if there are two integer variables “a” and “b” declared in your program. Where a = 3, and b = 2. If we perform the subtraction operation of these variables in programming, the result is going to be 1.
Multiplication
Multiplication operation is used to perform the mathematical multiplication of two variables. In programming, we refer to “*” as a symbol of multiplication. Please note that multiplication can only be carried out on Integer, Float and Double datatypes.
Division
Division operation is used to perform the mathematical division of two variables. In programming, we refer to “/” as a symbol of division. Please note that Division can only be carried out on Integer, Float and Double datatypes.
Modulus
Modulus operation is used to perform the mathematical remainder of two variables. In programming, we refer to “%” as a symbol of modulus. Modulus operator divides variable on the left to the variable on right and returns the remainder. Please note that modulus can only be carried out on integer and float datatypes in Python. For example, if there are two integer variables “x” and “y” declared in your program. Where x = 10, and y = 3. If we perform modulus operation on these variables in programming, the result is going to be 1.