Python Built-in Data types - IT magazine

IT magazine

Knowledge that matters

Python Built-in Data types

Share This

Python standard data types can be grouped into several types. These are numeric types, sequences,  sets and mappings. Some of the types are only available in certain versions of the language.
boolean:
             The type of the built-in values True and False . Useful in conditional expressions, and anywhere else you want to represent the truth or falsity of some condition. Mostly interchangeable with the integers 1 and 0. In fact, conditional expressions will accept values of any type, treating special ones like boolean False , integer 0 and the empty string "" as equivalent to False , and all other values as equivalent to True . But for safety’s sake, it is best to only use boolean values in these places.

Numeric types:
  • int: Integers; equivalent to C longs in Python 2.x, non-limited length in Python 3.x.
  • long: Long integers of non-limited length; exists only in Python 2.x.
  • float: Floating-Point numbers, equivalent to C doubles.
  • complex: Complex Numbers.
Sequences:
  • str: String; represented as a sequence of 8-bit characters in Python 2.x, but as a sequence of Unicode characters (in the range of U+0000 - U+10FFFF) in Python 3.x.
  • byte: a sequence of integers in the range of 0-255; only available in Python 3.x.
  • byte array: like bytes, but mutable (see below); only available in Python 3.x.
  • list.
  • tuple.
Sets:
  • set: an unordered collection of unique objects; available as a standard type since Python 2.6.
  • frozen set: like set, but immutable (see below); available as a standard type since Python 2.6
Mappings:
  • dict: Python dictionaries, also called hashmaps or associative arrays, which means that an element of the list is associated with a definition, rather like a Map in Java.
Mutable vs Immutable Objects:
           In general, data types in Python can be distinguished based on whether objects of the type
are mutable or immutable. The content of objects of immutable types cannot be changed after they are created.
Some immutable types:
  • int, float, long, complex
  • str
  • bytes
  • tuple
  • frozen set
Some mutable types:
  • byte array
  • list
  • set
  • dict

Only mutable objects support methods that change the object in place, such as reassignment of a sequence slice, which will work for lists, but raise an error for tuples and strings. It is important to understand that variables in Python are really just references to objects in memory. If you assign an object to a variable as below
a = 1
s = 'abc'
l = ['a string', 456, ('a', 'tuple', 'inside', 'a', 'list')]
all you really do is make this variable (a , s , or l ) point to the object (1 , 'abc' , ['a string', 456, ('a', 'tuple', 'inside', 'a', 'list')] ), which is kept somewhere in memory, as a convenient way of accessing it. If you reassign a variable as below
a = 7
s = 'xyz'
l = ['a simpler list', 99, 10]
Makes the variable point to a different object (newly created). As stated above, only mutable objects can be changed in place (l[0] = 1 is correct, but s[0] = 'a' raises an error). This becomes tricky, when an operation is not explicitly asking for a change to happen in place, as is the case for the += (increment) operator, for example. When used on an immutable object (as in a += 1 or in s += 'qwertz' ), Python will silently create a new object and make the variable point to it. However, when used on a mutable object (as in l += [1,2,3] ), the object pointed to by the variable will be changed in place. While in most situations, you do not have to know about this different behavior, it is of relevance when several variables are pointing to the same object. In our example, assume you set p = s and m = l , then s += 'etc' and l += [9,8,7] .
This will change s and leave p unaffected, but will change both m and l since both point to the same list object. Python’s built-in id() function, which returns a unique object identifier for a given variable name, can be used to trace what is happening under the hood.

No comments:

Post a Comment