Python - Data Type

About

data (type|structure) in Python of a name generally a variable

Every type is an object in Python (except for type). ints, strings, functions and classes are objects and have been created from a class.

List

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"

Documentation / Reference

Task Runner