Python - Data Type
About
data (type|structure) in Python of a name generally a variable
Articles Related
List
- Python - Integer - an integer is a positive or negative whole number.
my_int = 7
- booleans (True or False). Never use quotation marks (' or “) with booleans, and always capitalize the first letter because Python is case-sensitive .
my_bool = True
Comparison
Comparing objects of different types is legal. See Comparing objects
How to
get the type of a value
with the type function
print type(1)
print type(1.23)
print type('string')
print type({'Name':'John Cleese'})
print type((1,2))
<type 'int'>
<type 'float'>
<type 'unicode'>
<type 'dict'>
<type 'tuple'>
The 'unicode' type is a special type of string.
verify the type of a value
- Builtin
if (type(p1) == int or type(p1) == float):
return "An integer or float!"
else:
return "Not an integer or float!"
- isinstance
if isinstance(p1,str):
return "An string !"
if isinstance(p1,module.myclass):
return "It's an instance of module.myclass"