-->

Operations and Usage of Numbers - Python

Python provides four different data types to handle the numbers/numeric data.
    1. int: positive or negative numbers without decimal point
    Ex: -2, -1, 1, 0, 1, 2, etc.

    2. long: same as int. The only difference is that it can handle unlimited size.
    Ex: 98754632154568754555545545821

    3. float: numbers with decimal points
    Ex: 5546.2563, 78546.25634, etc.
    4. complex numbers: complex numbers are of the form x + yj, where 'x' and 'y' are floats and 'j' represents imaginary number.
    j = square root of -1.

    Addition of Numbers in python


    # let's try with integer data type
    num1 = 100
    num2 = 152
    sum_of_num1_and_num2 = num1 + num2
    print("num1 + num2 = ", sum_of_num1_and_num2)
    # Output: num1 + num2 = 252
    # let's try with float data type
    num1 = 100.55
    num2 = 152.23
    sum_of_num1_and_num2 = num1 + num2
    print("num1 + num2 = ", sum_of_num1_and_num2)
    # Output: num1 + num2 = 252.77999999999997
    # lets try with float and integer
    num1 = 152
    num2 = 189.1121
    print(num1 + num2)
    # Output: 341.1121
    # python automatically converts the integer to float and then it performs addition on numbers
    complex1 = 123 + 50j
    complex2 = 100 + 55j
    print(complex1 + complex2)
    # Output: (223+105j)

    Subtraction of Numbers in python


    # let's try with integer data type
    num1 = 100
    num2 = 152
    difference = num1 - num2
    print("num1 - num2 = ", difference)
    # Output: num1 - num2 = -52
    # let's try with float data type
    num1 = 100.55
    num2 = 152.23
    difference = num1 - num2
    print("num1 - num2 = ", difference)
    # Output: num1 - num2 =
    # lets try with float and integer
    num1 = 152
    num2 = 189.1121
    print(num1 - num2)
    # Output: -37.1121
    complex1 = 123 - 50j
    complex2 = 100 - 55j
    print(complex1 - complex2)
    # Output: (23+5j)
    python automatically converts the integer to float and then it performs addition on numbers

    Division of Numbers in python


    # integer data type
    num1 = 10
    num2 = 3
    print(num1/num2)
    # Output: 3
    # float division
    num1 = 152.33
    num2 = 12.55
    print(num1/num2)
    # Output: 12.1378486056
    print(25.5/3)
    # Output: 8.5
    print(25/3)
    # Output: 8
    print(25/3.0)
    # Output: 8.33333333333
    print(25.0/3)
    # Output: 8.33333333333
    # Now, let's do complex numbers division
    num1 = 12+2j
    num2 = 10+2j
    print(num/num2)
    # Output: (1.1923076923076923-0.03846153846153849j)
    print(num1/2)
    # Output: (6+1j)
    print(num1 / 2j)
    # Output: (12+2j)

    Multiplication of Numbers in python


    # let's start with integers
    num1 = 12
    num2 = 6
    print(num1 * num2)
    # Output: 72
    num1 = 2.5
    num2 = 4
    print(num1 * num2)
    # Output: 10
    num1 = 2.5 + 45j
    num2 = 40 - 2j
    print(num1 * num2)
    # Output: (190+1795j)

    How to get remainder with modulus "%" operator ?


    num1 = 55
    num2 = 2
    remainder = num1 % num2
    print(remainder)
    # Output: remainder

    How to convert string to number ?


    s = "14"
    # convert string to int
    i = int(s)
    print(i)
    # Output: 14
    # convert to float
    f = float(s)
    print(f)
    # Output: 14.0
    # convert to long
    ln = long(s)
    print(ln)
    # Output: 14
    # convert to complex
    ln = complex(s)
    print(ln)
    # Output: (14+0j)
    s = '3+2j'
    n = complex(s)
    print(ln)
    # Output: (3+2j)

    # Now, let's try Errors
    s = "12.5"
    n = int(s)
    Output: ValueError: invalid literal for int() with base 10: '12.5'
    n = int("12test")
    # Output: ValueError: invalid literal for int() with base 10: '12test'
    n = float("12+33j")
    # Output: ValueError: invalid literal for float(): 12+33j
    n = float("12+3")
    # Output: ValueError: invalid literal for float(): 12+3
    n = complex("12+33")
    # Output: ValueError: complex() arg is a malformed string

    How to round float to two or three decimal points ?


    num = round(2.6463636263,2)
    print (num)
    # Output: 6.65
    num = round(2.6463636263, 4)
    print (num)
    # Output: 2.6464
    # we can do it with string format
    s = "%.2f" % 3.14159
    print(s)
    # Output: '3.14'
    s = "%.5f" % 3.1
    print(s)
    # Output: '3.10000'
    s = "{0:.2f}".format(13.949999999999999)
    print(s)
    # Output: '13.95'

    How to find "Floor" of a number in python ?


    import math
    n = math.floor(3.1)
    print(n)
    # Output: 3.0
    n = math.floor(3.9)
    print(n)
    # Output: 3.0

    How to find "Ceil" of a number in python ?


    import math
    n = math.ceil(3.1)
    print(n)
    # Output: 4.0
    n = math.ceil(3.9)
    print(n)
    # Output: 4.0

    Buy a product to Support me