Python - Data (Type|Structure)

1 - About

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

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 variable

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 variable

  • 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

lang/python/type/type.txt · Last modified: 2018/11/21 20:11 by gerardnico