0
How to check which datatype does variable contain in python.
Like if I am creating a function.I want to check which datatype of argument is given how can i check it.
9 Answers
+ 2
Vinay Jaiswal Remove the parentheses from list():
if type(x) is list:
+ 5
foo = 42
bar = 'forty-two'
print(type(foo))
print(type(bar))
+ 5
Also, you can check it using isinstance (variable, datatype)
More about isinstance in https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/ref_func_isinstance.asp
+ 2
as per Seg Adamyan post, but you need to be very specific
var = 2
print(isinstance(var,int))
print(isinstance(var,str))
+ 1
Vinay Jaiswal
Try this as per visph suggestion
def define(x):
return type(x)
print(define(2))
print(define('abc'))
print(define([2,3,'a','b']))
PS: Had a glitch before, sorry if strange message got sent
0
This will not work visph.
0
Like.
"def check(x):
if type(x) is list():
print("Is of type list")
else:
print("This is of another type")"
I know logic is wrong.But which logic i apply.
0
type() function can be used to determine which data type class