In this tutorial, learn how to create number variables of various types in Python. You can create integer, float and complex number variables of Python. To create these variables in Python, you have to just assign these number type values to the number variable.

Learn these variable types with the examples and explanation given below.

How to Declare Integer to Create Number Variables in Python

If you want to define the integer in number variables of Python. Use the below-given syntax and assign the numeric value without the decimal point.

The below example showing the positive integer value of the integer. However, you assign a negative value to create a negative integer number variable in Python. You just have to put a negative(-) sign before the assigned value in the example.

Output

10

The above example prints only the positive integer in the output section. You also create your own integer variable using the above example.

Create Float Number Variable in Python

If you want to create a float integer variable in Python. You have to assign a float value with a decimal point as given in the example below. The below example showing the float value with one decimal after a decimal point.

You can add one or more decimal digits after the decimal point in the value. In addition to this, you can also create a negative as well as a positive floating variable in Python. To create a negative floating variable, you have to assign a negative value to the variable with a decimal point.

Output

9.4

The above example gives the floating the number in the output. The floating value contains one decimal digit after decimal point.

Declare Complex to Create Number Variable in Python

If you want to create a complex number variable in Python. You have to use below-given example syntax to create your own complex number variable.

The first part of the complex number is the real part with real value. The second part of the complex number is the imaginary part with ‘j’.

Output

(3+4j)

The above example prints the output with the complex number. It uses the print function of Python to print the complex number variable value. It gives output with the complex number inside the round bracket.

If you are not confirmed about the variable type and want to know the type in output. You have to read further to learn more about how to find the type of a variable in Python.

How to Find Out The Type of Variable in Python

To find out the type of the number variable in Python. You have to use the Python functiontype() and pass the variable as argument. Now, if you run the python code, you will get the type of the variable in the format as given below.

You have to use the below-given example syntax to get the type of your variable in Python.

Output

<class ‘int’>
<class ‘float’>
<class ‘complex’>

The above example contains integer, float and complex type number variables. To find out the confirmed type of these variables, you have to pass them as an argument of the type() function. After that, to print out the type in the output. You have to use the print function of Python.

Python also provides a syntax to convert one data type to another. To perform this task in Python, You have to read further.

Convert Number Variable From One Type to Another

You can also convert one variable type into another using Python. To perform this task, you have to use the below-given examples to convert to the required data type in Python.

You can convert an integer to float and complex number variables. Float to integer and complex number variables. However, you cannot convert complex to integer and float number variable.

Because complex number variable contains both the real part and imaginary part. These parts you cannot convert to a single one.

Convert Integer Variable to Float and Complex
To convert the integer number variable to another variable. Use the below-given example and change it as per your requirement.

Output

10.0
(10+0j)

The above example converts the integer to float and complex number variable. It prints out the output using the print statement of the Python.

The complex number comes out with the real and imaginary part. The imaginary part contains the value zero(0) and ‘j’.

Convert Float Variable to Integer and Complex
In addition to the above all, you can also convert the float to integer and complex number variable in Python. You have to just use the below-given example to convert your floating point number.

Output

9
(9.4+0j)

The above example prints the integer value but removes the digit after the decimal. It also prints the complex value with real part contains the float value and imaginary as zero(0) and ‘j’.

You may also like to read

Hope, you like this tutorial of how to create number variables of various types in Python. If you have any query regarding the tutorial, please comment below.

Also tell me, which method you are using to create your number variable in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.