Introduction
Sometimes, you want to know what kind of data you have. The type() function helps you find out if a value is a number, text, or something else.
Jump into concepts and practice - no test required
type(object)type(5)
type('hello')
type([1, 2, 3])
a = 10 b = 'apple' c = [1, 2, 3] print(type(a)) print(type(b)) print(type(c))
type() function do in Python?type()type() function returns the data type of the given variable or value.type() tells the kind of data stored in a variable.x?type() functiontype(x).value = 3.14 print(type(value))
valuevalue is assigned 3.14, which is a decimal number, so its type is float.print(type(value)) outputstype() function returns <class 'float'> for a float value, which is printed.my_var = 'hello'
if type(my_var) == str:
print('It is a string')print is not indented, causing an indentation error.True if the input is an integer or a float, and False otherwise. Which code correctly uses type() to do this?x is either int or float.or float incorrectly, always returning truthy. def check_num(x):
return type(x) in [int, float] uses in to check membership in a list of types, which is concise and correct. def check_num(x):
return type(x) == 'int' or 'float' compares type to strings, which is wrong. def check_num(x):
return type(x) == int and float uses and incorrectly, only detecting ints.in to check multiple types [OK]