How To Do Math in Python 3 with Operators

Posted in /  

How To Do Math in Python 3 with Operators
vinaykhatri

Vinay Khatri
Last updated on December 24, 2024

    Programming is mostly about numbers and number theories. Mostly all the complex programs related to data science, machine learning, artificial intelligence, and Deep learning run around numbers and statistics. Even in common Python programs, we use numbers to represent the dimension of an object, points on a scoreboard, balance in an account, the position of an object in a game program, and so on.

    However, knowing all the mathematical concepts is not a prerequisite to learn Python Programming , but the skill set of effectively using all the mathematical operations in programming is worth having. As a Python developer if you know how to use mathematical operation then you can definitely become a better programmer. In Python, there are two types of numerical data types integers and floats on which we can perform the Arithmetic operations using Python arithmetic Operators.

    • Integers are the positive and negative numeric values which represent only integer numbers. Example: …. -2, -1, 0, 1, 2, 3 ….
    • Floats are the real numbers that contain decimal points. Example: …. -2.999, -1.0, 0.0, 1.0, 1.01, …….. In this Python tutorial article, we will go through some of the basic python operators which can be used with Python numerical data types.

    What is an Operator?

    An operator is a symbol that requires operands to perform the operation. For instance, + is an addition operator and it can perform the addition operation between two numerical data types or operands. In Python to perform the math operation, we use the Python arithmetic operators . The arithmetic operators are similar operators we use in mathematics. Here is a list of all the Python basic math-related operators which require python numeric values Integers & Float for operation.

    Operator Operator name Operation Return Value
    + addition a + b It will return the sum of a and b.
    - Subtraction a - b It will return the difference of a and b
    -a minus -a Return a negative value of a.
    +a Identity value (plus) +a Return the same value.
    * Multiplication a*b Multiply a and b
    / division a/b Divide a by  b and return the quotient.
    // Floor division a//b Divide a by b and return floor value of quotient
    % Modulus a%b Divide a by b and return Remainder
    ** Exponential a**b Return a to the power b

    Python Addition and Subtraction

    In Python, we have the same operators for addition and subtraction as we have in mathematics. And the working of all the arithmetic operation follows the mathematic rules, if you want you can use the python terminal as a calculator.

    print(20+30)

    Output

    50

    We can also initialize the numerical value to the identifiers, and then perform the addition and subtraction operation using + and – symbols.

    a= 20
    b= 40
    print(a+b)

    Output

    60

    We can assign any integer value to the identifiers and perform the operation, and the operators will follow the same mathematics rules.

    a = -29
    b= 46
    print(a+b)

    Output

    17

    The same rules go for the float values.

    a = 23.34
    b= 48.93
    print(a+b)

    Output

    72.27

    Explicit type conversion in Python

    If we perform the subtraction, addition, multiplication, and division operation between a float number and an integer number then the output will also be a floating number. Because the python explicitly converts the integer value to its corresponding floating number and then perform the operation.

    Example

    i = 20
    f = 45.0
    print(i+f)

    Output

    65.0

    Unary Arithmetic Operations

    The unary operator performs on a single operand. In Python to represent we have two unary operators (+) plus and (-) minus. These unary operators have similar symbols to addition and subtraction but they perform different operations. The plus + operator returns an identity value, and the minus – operator returns a signed changed value. Unary operators are not often used in programming however as a Python developer you should know about these minor concepts.

    Python unary operator Example

    a = 20
    b = +a
    print(b)

    Output

    20

    The +a symbol specifies that we want the identity value of a without changing its sign.

    Python unary operator Example 2

    a= -20
    b= -a
    print(b)

    Output

    20

    The b = -a represents, return the change the sign of a and then assign it to b.

    <Note> Do not confuse the Python unary operators =+ with python compound operators +=, both are different.

    Python Multiplication and Division

    Similar to addition and subtraction we have * symbol for multiplication and / symbol for division operations in Python. When we perform the multiplication between a floating-point number and an integer number then we will always get results in a floating-point number.

    a= 20.3
    b= 4
    print(a*b)

    Output

    81.2

    If we use the division operator between two numerical values then the answer would always be a floating-point number. The division operator always returns a decimal point number.

    a = 20
    b = 5
    print(a/b)

    Output

    4.0

    Python Floor Division Operator //

    By default, the Python division operator always returns the floating-point number. But if you want an integer value as a result of division then you can use the floor division operator. The floor division operator in Python is represented by the double division symbol //.

    Python floor division operator example 1

    a= 20
    b = 5
    print(a//b)

    Output

    4

    If the number is not a complete dividend then the // operator returns a floor quotient integer value.

    Python floor division operator example 2

    a= 21
    b=5
    print(a//b)

    Output

    4

    Python Modulo or Modulus Operator

    In Python, the modulus operator represented by the % symbol and it returns the remainder value after the division. The modulus operator come very usefully to find out the multiples of a number, and you will be using this operator more often in your programming as compared to other arithmetic operators.

    Python Modulo Example

    divisor = 3
    dividend  = 31
    reminder  = dividend % divisor
    print(reminder)

    Output 1

    Python Power operator

    The ** symbol in Python is used as a Power operator. This operator raises the power of the left operand or value to the right operand. For example, 5**3 specify 5 to the power 3, which is equal to 5 * 5* 5 = 125

    Python Power operator Example 1

    value = 5
    power = 2
    print(value**power)

    Output

    25

    Python Operator Precedence

    The operator precedence represents which operator will be executed first in a given expression. Similar to the BODMAS rule in mathematic Python Arithmetic Operators follow the PEDMAS rule. PEDMAS stands for Parentheses, Exponent, Division, Multiplication, Addition and Subtraction.

    Example

    >>>print(8/2*(2+2))
    16.0

    Python compound Assignment Operator

    In Python, we use the assignment operator to assign a value to an Identifier.  But a compound assignment operator combines the arithmetic and assignment operators. The compound operators represented by the arithmetic operator followed by the assignment operator.

    Example

    a = 20
    a += 30
    print(a)

    Output

    50

    In the above example += is a compound assignment operator does not confuse it with unary operator =+.

    <Note>: While using a compound operator make sure that there is no space between the Arithmetic operator and assignment operator. Else you would receive a syntax error.

    Example

    >>> a = 20
    >>> a += 30
    >>> print(a)
    50
    >>> a = 20
    >>> a += 2
    >>> print(a)
    22
    >>> a -= 2
    >>> print(a)
    20
    >>> a *= 2
    >>> print(a)
    40
    >>> a /= 2
    >>> print(a)
    20.0
    >>> a **= 2
    >>> print(a)
    400.0
    >>> a %= 2
    >>> print(a)
    0.0

    Conclusion

    In this tutorial, you learned how to do math in Python using Python Arithmetic operators. Here you also learned about integer and floats values. And how the python arithmetic operations performed on the Python numeric values, and what numeric value we receive as an output. There are different data types present in Python but arithmetic operators only work on Numeric value.

    People are also reading:

    Leave a Comment on this Post

    0 Comments