Python - Data Type

> Procedural Languages > Python > Python - Data Type

1 - 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.


3 - 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

4 - Comparison

Comparing objects of different types is legal. See Comparing objects

5 - How to

5.1 - 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.


5.2 - verify the type of a value

  • Builtin
if (type(p1) == int or type(p1) == float):
        return "An integer or float!"
        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"

6 - Documentation / Reference