What is Number data type and type conversion in python?

To start with the python number data type and type conversion, Let’s first see Number data type.

Python number data type is the data type which is used to store the numeric data. You can perform arithmetic operations on this data type. When you assign a new value to numeric data type a new object gets created. This is immutable data type, Hence as soon as you try to change its value, new object gets created.

In python, There are basically three sub-types of Number data type.

  • Python Integer:

    • Integers are whole numbers which can have positive and negative values but without decimal point in it. Starting from python 3.0 there is no length limit on integer data type in python.
    • Few numbers which belongs to integer data type: 40, 65, 89 etc.
  • Python Float:

    • Float is numeric data with decimal point in it. Float data type in python stores the real numbers. Examples of float are 2.35, 3.68 etc. are the float numbers.
    • Basic difference between integer and float is, Integers are without decimal point and floats are with the decimal point in it.
    • Float is a integer along with its fractional part. e.g In example “8.31”, Number 8 is a integer and number 8.31 is a float.
  • Python Complex numbers:

    • This data type has two parts i.e real and imaginary part. For example “5+8j” is a complex number where 5 is real part and 8j is an imaginary part of it.
    • To get the real and imaginary part of any complex numbers following are the commands.

How to print real and imaginary part of complex numbers in python

In above image we have stored the complex number 5+8j into variable named “complex_variable”.

  • print(type(Variable_Name)) : This line prints the data type of variable in python.
  • print(Variable_Name.real) : This line prints the real part of complex number variable.
  • print(Variable_Name.imag) : This line prints the imaginary part of complex number.

Type conversion in python:

Converting the value of one data type into another is known as type conversion. This conversion is also called as Coercion.

Type conversion in python works in two ways. First way is interpreter itself understands the requirement of type conversion and performs conversion task. Second way is, You have to specifically tell the conversion requirements and then the interpreter will do it for you.

  1. Implicit type conversion
  2. Explicit type conversion

Implicit type conversion:

Implicit conversion is one of the type conversion in which python interpreter automatically converts one data type into another. There is no need of user to specify the conversion requirements.

To understand this implicit type conversion better, Let’s see one example. We are having two numbers say x=5.3 and y=8, Here x is a float since it is having decimal point in it and y is an integer. Now we’ll do the addition of these two numbers and see the data type of result.

How to find the data type in python

In above example we have added two numbers of different data type i.e. one is integer and other is float. The data type of result is automatically converted to float. We explicitly didn’t performed anything to convert the result into float. This automatic conversion is known as implicit type conversion.

Explicit type conversion:

In explicit type conversion method user has to explicitly specify the conversion requirements. After reading the conversion requirements interpreter will perform the type conversion work for you.

While specifying the explicit conversion requirements you have to provide the requirements very clearly. Since you are forcing the interpreter to perform conversion forcefully, There are chances of data loss as well.

Syntax to specify the explicit data conversion in python is as below;

Required data type(Expression)

Example 1:

How to do explicit type conversion in python

In example above, we are having two float variables named “first_float_variable” and “second_float_variable”. “Add” is variable created to store the addition of both the float numbers. Now we have printed the value of “Add” variable before type conversion and after the type conversion.

Before type conversion value of “Add” variable (11.0949999~) is printed along with decimal point and its fraction part. After type conversion variable “Add” is converted into integer (11) and same is printed on screen.

Example 2:

Let’s try to explicitly convert the string data type into list data type.

Type conversion between string data type to list data type in python

In above example we have converted string “Cake” into list data type. We have printed the value of “string_variable” before and after type conversion. You can see both the results, highlighted result is the value of “string_variable” after it is converted to list data type.

Continue reading..

Variables and Data types in python

String concatenation and replication in python

String slicing and indexing in python

Share this Post

Leave a Comment

Your email address will not be published.