Python Number, Type Conversion and Mathematics

    In this tutorial, we will discuss Python Number Type Conversion and Mathematics. We will also see how can we convert other data types to Numbers and also perform some operators on Numbers.

    Python Number Type Conversion and Mathematics

    Number Data Type in Python

    Every digit (0-9) in python with or without decimal falls under the Number data type. Python has different types of numbers such as integers ( int ), float ( float), and complex. Integers and floats in python mainly serve the same purpose the only difference is integers are those which do not have decimals while floats are decimal numbers.

    For example

    If we write 60 it is an integer but 60.0 will become afloat though both are the same but belong to different number types. On the other hand, the complex is a special kind of number though we do not use complex often like float and integer, they come very handily when we discuss the real and imaginary concept of mathematics. We write complex numbers in the form of x + y j where x and y could be integer or float numbers and j is used to represent iota. Here x represents the real number and y represents the imaginary number.

    Let’s understand it with an example.

    i = 55
    f = 55.0
    c = 55 + 5j
    
    print(type(i))
    print(type(f))
    print(type(c))

    #Output

    <class 'int'>
    <class 'float'>
    <class 'complex'>

    Binary, Octal, and Hexadecimal

    In python, we can convert a binary or hexadecimal or Octal to integers by using the prefix 0b for binary, 0o for Octal, and 0x for Hexadecimal.

    Let’s understand with an example:

    print(0b001001)
    print(0x7b)
    print(0o15)

    #Outputs

    9
    123
    13

    Type Conversion:

    In python, we can convert the data type of one variable to another by using some special functions. This conversion is also known as coercion. The type conversion is divided into two parts.

    • Implicit type conversion
    • Explicit type conversion

    In Implicit type conversion, the python interpreter itself converts the type of variable without the user's consent.

    For example

    >>> 9+9.0
    18.0

    In Explicit type conversion, the user or developer can explicitly change the data type of the variable by using the functions int(), float(), and complex(). Even using these functions we can convert string to number only if that string contains only digits.

    Let’s convert some data types Explicitly

    >>> int(7.0)
    7              #output
    
    >>> float(7)
    7.0          #output
    
    >>> complex(2,3)
    (2+3j)    #output
    
    >>> complex("3+2j")
    (3+2j)

    Python Decimal Module:

    In Python when it comes to the decimal numbers the first thing strike in our mind is float because by default float deal with the number having decimal values. Though when we perform some operation on float numbers, it not reliable every time there are many sorts coming seen in float such as conventionally or by default float store 15 places after the decimal it does not matter if you had written those 15 numbers or not float assume that they are there. for example, let's perform an arithmetic operation on floats number and see the output.

    >>> 1.1+2.2
    3.3000000000000003

    In the above example, we add 1.1 with 2.2 the answer is supposed to 3.3 but we are getting a huge number of decimal numbers. Python comes with a decimal module that is used to alter the floating numbers and by using this module we can use the float.

    Let’s understand it with an Example

    from decimal import Decimal
    
    print(Decimal('1.1')+Decimal('2.2'))

    #Output

    3.3

    Decimal or Float?

    Though Float and Decimal use to represent the same values, sometimes there are some conflicts in values so to be more precise use decimal instead of float.

    Python Fraction module

    Python has a module which is known as fractions which is used to represent the number in fraction form. When we use the fraction module, we can represent the number in the form of numerator and denominator.

    Let’s understand it with an example

    from fractions import Fraction
    
    f1 = fractions.Fraction(3,2)           #where 3 is numerator and 2 is denominator
    f2 = fractions.Fraction(7,5)
    
    print(f1+f2)

    #Output

    29/10

    Python Math Module

    Python Math module provides many functions and concepts of math that come very handy when we write a program with probability and statistics.

    Let’s understand it with an example:

    import math
    print(math.pi)
    print(math.factorial(5))
    print(math.log10(100))

    #Output

    3.141592653589793
    120
    2.0