Python TutorialGetting Started with PythonPython Basic SyntaxPython DatatypesPython IndentationPython Collection TypesPython Basic Input and OutputPython Built in Modules and FunctionsPython FunctionsChemPy - python packageCreating Python packagesFunctional Programming in PythonIncompatibilities moving from Python 2 to Python 3IoT Programming with Python and Raspberry PIKivy - Cross-platform Python Framework for NUI DevelopmentMutable vs Immutable (and Hashable) in PythonPyInstaller - Distributing Python CodePython *args and **kwargsPython 2to3 toolPython Abstract Base Classes (abc)Python Abstract syntax treePython Alternatives to switch statement from other languagesPython and ExcelPython Anti-PatternsPython ArcPyPython ArraysPython Asyncio ModulePython Attribute AccessPython AudioPython Binary DataPython Bitwise OperatorsPython Boolean OperatorsPython Checking Path Existence and PermissionsPython ClassesPython CLI subcommands with precise help outputPython Code blocks, execution frames, and namespacesPython Collections modulePython Comments and DocumentationPython Common PitfallsPython Commonwealth ExceptionsPython ComparisonsPython Complex mathPython concurrencyPython ConditionalsPython configparserPython Context Managers (with Statement)Python Copying dataPython CountingPython ctypesPython Data SerializationPython Data TypesPython Database AccessPython Date and TimePython Date FormattingPython DebuggingPython DecoratorsPython Defining functions with list argumentsPython DeploymentPython Deque ModulePython DescriptorPython Design PatternsPython DictionaryPython Difference between Module and PackagePython DistributionPython DjangoPython Dynamic code execution with `exec` and `eval`Python EnumPython ExceptionsPython ExponentiationPython Files & Folders I/OPython FilterPython FlaskPython Functools ModulePython Garbage CollectionPython GeneratorsPython getting start with GZipPython graph-toolPython groupby()Python hashlibPython HeapqPython Hidden FeaturesPython HTML ParsingPython HTTP ServerPython IdiomsPython ijsonPython Immutable datatypes(int, float, str, tuple and frozensets)Python Importing modulesPython Indexing and SlicingPython Input, Subset and Output External Data Files using PandasPython Introduction to RabbitMQ using AMQPStorm

Python Datatypes

From WikiOD

In Python, a variable is a variable. It has no type. What we call "type" is the type of object in the memory that the variable refers to.

Booleans[edit | edit source]

bool: A boolean value of either True or False. Logical operations like and, or, not can be performed on booleans.

x or y    # if x is False then y otherwise x 
x and y   # if x is False then x otherwise y
not x     # if x is True then False, otherwise True

In Python 2.x and in Python 3.x, a boolean is also an int. The bool type is a subclass of the int type and True and False are its only instances:

issubclass(bool, int) # True

isinstance(True, bool) # True
isinstance(False, bool) # True

If boolean values are used in arithmetic operations, their integer values (1 and 0 for True and False) will be used to return an integer result:

True + False == 1 # 1 + 0 == 1
True * True  == 1 # 1 * 1 == 1

Numbers[edit | edit source]

  • int: Integer number
a = 2
b = 100
c = 123456789
d = 38563846326424324

Integers in Python are of arbitrary sizes.

Note: in older versions of Python, a long type was available and this was distinct from int. The two have been unified.

  • float: Floating point number; precision depends on the implementation and system architecture, for CPython the float datatype corresponds to a C double.
a = 2.0
b = 100.e0
c = 123456789.e1
  • complex: Complex numbers
a = 2 + 1j
b = 100 + 10j

The <, <=, > and >= operators will raise a TypeError exception when any operand is a complex number.

Strings[edit | edit source]

Python 3.x3.0

  • str: a unicode string. The type of 'hello'
  • bytes: a byte string. The type of b'hello'

Python 2.x2.7

  • str: a byte string. The type of 'hello'
  • bytes: synonym for str
  • unicode: a unicode string. The type of u'hello'

Sequences and collections[edit | edit source]

Python differentiates between ordered sequences and unordered collections (such as set and dict).

  • strings (str, bytes, unicode) are sequences
  • reversed: A reversed order of str with reversed function
a = reversed('hello')
  • tuple: An ordered collection of n values of any type (n >= 0).
a = (1, 2, 3)
b = ('a', 1, 'python', (1, 2))
b[2] = 'something else' # returns a TypeError

Supports indexing; immutable; hashable if all its members are hashable

  • list: An ordered collection of n values (n >= 0)
a = [1, 2, 3]
b = ['a', 1, 'python', (1, 2), [1, 2]]
b[2] = 'something else' # allowed

Not hashable; mutable.

  • set: An unordered collection of unique values. Items must be hashable.
a = {1, 2, 'a'}
  • dict: An unordered collection of unique key-value pairs; keys must be hashable.
a = {1: 'one',
     2: 'two'}

b = {'a': [1, 2, 3],
     'b': 'a string'}

An object is hashable if it has a hash value which never changes during its lifetime (it needs a __hash__() method), and can be compared to other objects (it needs an __eq__() method). Hashable objects which compare equality must have the same hash value.

Built-in constants[edit | edit source]

In conjunction with the built-in datatypes there are a small number of built-in constants in the built-in namespace:

  • True: The true value of the built-in type bool
  • False: The false value of the built-in type bool
  • None: A singleton object used to signal that a value is absent.
  • Ellipsis or ...: used in core Python3+ anywhere and limited usage in Python2.7+ as part of array notation. numpy and related packages use this as a 'include everything' reference in arrays.
  • NotImplemented: a singleton used to indicate to Python that a special method doesn't support the specific arguments, and Python will try alternatives if available.
a = None # No value will be assigned. Any valid datatype can be assigned later

Python 3.x3.0

None doesn't have any natural ordering. Using ordering comparison operators (<, <=, >=, >) isn't supported anymore and will raise a TypeError.

Python 2.x2.7

None is always less than any number (None < -32 evaluates to True).

Testing the type of variables[edit | edit source]

In python, we can check the datatype of an object using the built-in function type.

a = '123'
# Out: <class 'str'>
b = 123
# Out: <class 'int'>

In conditional statements it is possible to test the data type with isinstance. However, it is usually not encouraged to rely on the type of the variable.

i = 7
if isinstance(i, int):
    i += 1
elif isinstance(i, str):
    i = int(i)
    i += 1

For information on the differences between type() and isinstance() read: Differences between isinstance and type in Python

To test if something is of NoneType:

x = None
if x is None:
    print('Not a surprise, I just defined x as None.')

Converting between datatypes[edit | edit source]

You can perform explicit datatype conversion.

For example, '123' is of str type and it can be converted to integer using int function.

a = '123'
b = int(a)

Converting from a float string such as '123.456' can be done using float function.

a = '123.456'
b = float(a)
c = int(a)    # ValueError: invalid literal for int() with base 10: '123.456'
d = int(b)    # 123

You can also convert sequence or collection types

a = 'hello'
list(a)  # ['h', 'e', 'l', 'l', 'o']
set(a)   # {'o', 'e', 'l', 'h'}
tuple(a) # ('h', 'e', 'l', 'l', 'o')

Explicit string type at definition of literals[edit | edit source]

With one letter labels just in front of the quotes, you can tell what type of string you want to define.

  • b'foo bar': results bytes in Python 3, str in Python 2
  • u'foo bar': results str in Python 3, unicode in Python 2
  • 'foo bar': results str
  • r'foo bar': results so called raw string, where escaping special characters is not necessary, everything is taken verbatim as you typed
normal  = 'foo\nbar'   # foo
                       # bar
escaped = 'foo\\nbar'  # foo\nbar   
raw     = r'foo\nbar'  # foo\nbar

Mutable and Immutable Data Types[edit | edit source]

An object is called mutable if it can be changed. For example, when you pass a list to some function, the list can be changed:

def f(m):
    m.append(3)  # adds a number to the list. This is a mutation.

x = [1, 2]
x == [1, 2]  # False now, since an item was added to the list

An object is called immutable if it cannot be changed in any way. For example, integers are immutable, since there's no way to change them:

def bar():
    x = (1, 2)
    x == (1, 2)  # Will always be True, since no function can change the object (1, 2)

Note that variables themselves are mutable, so we can reassign the variable x, but this does not change the object that x had previously pointed to. It only made x point to a new object.

Data types whose instances are mutable are called mutable data types, and similarly for immutable objects and datatypes.

Examples of immutable Data Types:

  • int, long, float, complex
  • str
  • bytes
  • tuple
  • frozenset

Examples of mutable Data Types:

  • bytearray
  • list
  • set
  • dict