Determine variable type in Python
To get the type of a variable in python you can use the built in type() method.
x=1 print(type(x))
output:
<class ‘int’>
In the following code we are printing the value the data type of variable x.
x=1
print(type(x))
y=type(x)
if y==int:
print("variable is integer")
elif y==str:
print("variable is string")
elif y==float:
print("variable is float")
If you execute the code above then the following will be printed:
variable is integer
try changing the value of x to different types such as string,float.To set float value you can assign x as:
x=1.1
Similarly to assign string to x:
x=”Hello!”
Leave a Reply