Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine a Python variable's type?

How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?

How do I view it?

like image 226
user46646 Avatar asked Dec 31 '08 07:12

user46646


People also ask

How do you determine the type of a variable in Python?

To get the type of a variable in Python, you can use the built-in type() function. In Python, everything is an object. So, when you use the type() function to print the type of the value stored in a variable to the console, it returns the class type of the object.

How do you determine the variable type of data?

To check the data type of variable in Python, use the type() method. The type() is a built-in Python method that returns the class type of the argument(object) passed as a parameter. You place the variable inside a type() function, and Python returns the data type.

Can you define variable type in Python?

Specify a Variable Type There may be times when you want to specify a type on to a variable. This can be done with casting. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types.


1 Answers

Use the type() builtin function:

>>> i = 123 >>> type(i) <type 'int'> >>> type(i) is int True >>> i = 123.456 >>> type(i) <type 'float'> >>> type(i) is float True 

To check if a variable is of a given type, use isinstance:

>>> i = 123 >>> isinstance(i, int) True >>> isinstance(i, (float, str, set, dict)) False 

Note that Python doesn't have the same types as C/C++, which appears to be your question.

like image 81
gregjor Avatar answered Sep 23 '22 12:09

gregjor