Variable data type is automatically determined by Python. Variables can store data of different types. Python has the following data types built-in by default, in these categories:
To find the data type we use built-in function type( ). Within the brackets, we must place the name of the variable or type of value which we we want to verify. So, for the following example we are placing different types of value to know its data-type. After executing the code, the result we obtain is “int”,”float” or data-type which indicates the value may be “int”,”float” or other.
If you have a variable and if this variable is not allocated with any value, we usually use a keyword as null in other languages, but we use None in Python.
Python numbers include integer (int), floating numbers (float) and complex numbers (complex). You can get the data type of any object by using the type() function.
Integers are the positive or negative numbers without decimal values. There is no limit for integer precision but the are limited by available memory size.
2. Floating Numbers
Floating Numbers are the positive or negative numbers including decimal values. A floating point number is accurate up to 15 decimal places. Float can also be scientific numbers with an “e” to indicate the power of 10. Division (/) always returns a float.
Complex Numbers are the combination of real part ‘x’ and imaginary part ‘y’ written in the form of x+yj.
why are complex numbers in Python denoted with ‘j’ instead of ‘i’?Python adopted the convention used by electrical engineers. In that field, i is used to represent current and use j as the square root of -1
Typecasting is used in python to convert one data type to another. In this tutorial we will learn converting Integer into different bases like hex, oct, binary and also to float numbers.
|int( )||converts any data type to integer||int(3.4)||3|
|float( )||converts any data type to float||int (3)||3.000000|
|hex( )||convert integer to hexadecimal string||hex(115)||‘0x73’|
|oct( )||convert integer to octal string||oct(32)||”0o40′|
Example : Convert meter to kilometer
Example : Convert Celsius to Fahrenheit
This page is contributed by Diwas & Sunil . If you like AIHUB and would like to contribute, you can also write an article & mail your article to firstname.lastname@example.org . See your articles appearing on AI HUB platform and help other AI Enthusiast.