Data types determine whether an object can do something, or whether it just would not make sense. Other programming languages often determine whether an operation makes sense for an object by making sure the object can never be stored somewhere where the operation will be performed on the object (thistype system is called static typing). Python does not do that. Instead it stores the type of an object with the object, and checks when the operation is performed whether that operation makes sense for that object (this is called dynamic typing).
Python's built-in (or standard) data types can be grouped into several classes. Sticking to the hierarchy scheme used in the official Python documentation these arenumeric types, sequences, sets and mappings (and a few more not discussed further here). Some of the types are only available in certain versions of the language as noted below.
True
andFalse
. 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 booleanFalse
, integer 0 and the empty string""
as equivalent toFalse
, and all other values as equivalent toTrue
.Numeric types:
Sequences:
Sets:
Mappings:
Some others, such as type and callables
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.
Someimmutable types | Somemutable types |
---|---|
|
|
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=1s='abc'l=['a string',456,('a','tuple','inside','a','list')]
all you really do is make this variable (a,s, orl) 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=7s='xyz'l=['a simpler list',99,10]
you make the variable point to a different object (newly created ones in our examples). As stated above, only mutable objects can be changed in place (l[0] = 1 is ok in our example, buts[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 ina += 1 or ins += 'qwertz'), Python will silently create a new object and make the variable point to it. However, when used on a mutable object (as inl += [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 setp = s andm = l, thens += 'etc' andl += [9,8,7]. This will changes and leavep unaffected, but will change bothm andl since both point to the same list object. Python's built-inid() function, which returns a unique object identifier for a given variable name, can be used to trace what is happening under the hood.
Typically, this behavior of Python causes confusion in functions. As an illustration, consider this code:
defappend_to_sequence(myseq):myseq+=(9,9,9)returnmyseqtuple1=(1,2,3)# tuples are immutablelist1=[1,2,3]# lists are mutabletuple2=append_to_sequence(tuple1)list2=append_to_sequence(list1)print('tuple1 = ',tuple1)# outputs (1, 2, 3)print('tuple2 = ',tuple2)# outputs (1, 2, 3, 9, 9, 9)print('list1 = ',list1)# outputs [1, 2, 3, 9, 9, 9]print('list2 = ',list2)# outputs [1, 2, 3, 9, 9, 9]
This will give the above indicated, and usually unintended, output.myseq is a local variable of theappend_to_sequence function, but when this function gets called,myseq will nevertheless point to the same object as the variable that we pass in (t orl in our example). If that object is immutable (like a tuple), there is no problem. The += operator will cause the creation of a new tuple, andmyseq will be set to point to it. However, if we pass in a reference to a mutable object, that object will be manipulated in place (somyseq andl, in our case, end up pointing to the same list object).
Links:
Literal integers can be entered in three ways:
Floating point numbers can be entered directly.
Long integers are entered either directly (1234567891011121314151617181920 is a long integer) or by appending an L (0L is a long integer). Computations involving short integers that overflow are automatically turned into long integers.
Complex numbers are entered by adding a real number and an imaginary one, which is entered by appending a j (i.e. 10+5j is a complex number. So is 10j). Note that j by itself does not constitute a number. If this is desired, use 1j.
Strings can be either single or triple quoted strings. The difference is in the starting and ending delimiters, and in that single quoted strings cannot span more than one line. Single quoted strings are entered by entering either a single quote (') or a double quote (") followed by its match. So therefore
'foo' works, and"moo" works as well, but'bar" does not work, and"baz' does not work either."quux'' is right out.
Triple quoted strings are like single quoted strings, but can span more than one line. Their starting and ending delimiters must also match. They are entered with three consecutive single or double quotes, so
'''foo''' works, and"""moo""" works as well, but'"'bar'"' does not work, and"""baz''' does not work either.'"'quux"'" is right out.
Tuples are entered in parentheses, with commas between the entries:
(10,'Mary had a little lamb')
Also, the parenthesis can be left out when it's not ambiguous to do so:
10,'whose fleece was as white as snow'
Note that one-element tuples can be entered by surrounding the entry with parentheses and adding a comma like so:
('this is a singleton tuple',)
Lists are similar, but with brackets:
['abc',1,2,3]
Dicts are created by surrounding with curly braces a list of key/value pairs separated from each other by a colon and from the other entries with commas:
{'hello':'world','weight':'African or European?'}
Any of these composite types can contain any other, to any depth:
((((((((('bob',),['Mary','had','a','little','lamb']),{'hello':'world'}),),),),),),)
The Python analogue of null pointer known from other programming languages isNone.None is not a null pointer or a null reference but an actual object of which there is only one instance. One of the uses ofNone is in default argument values of functions, for which seePython Programming/Functions#Default_Argument_Values. Comparisons toNone are usually made usingis rather than ==.
Testing for None and assignment:
ifitemisNone:...another=NoneifnotitemisNone:...ifitemisnotNone:# Also possible...
Using None in a default argument value:
deflog(message,type=None):...
PEP8 states that "Comparisons to singletons like None should always be done with is or is not, never the equality operators." Therefore, "if item == None:" is inadvisable. A class can redefine the equality operator (==) such that instances of it will equal None.
You can verify that None is an object by dir(None) or id(None).
See alsoOperators#Identity chapter.
Links:
Type conversion in Python by example:
v1=int(2.7)# 2v2=int(-3.9)# -3v3=int("2")# 2v4=int("11",16)# 17, base 16v5=long(2)# Python 2.x only, not Python 3.xv6=float(2)# 2.0v7=float("2.7")# 2.7v8=float("2.7E-2")# 0.027v9=float(False)# 0.0vA=float(True)# 1.0vB=str(4.5)# "4.5"vC=str([1,3,5])# "[1, 3, 5]"vD=bool(0)# False; bool fn since Python 2.2.1vE=bool(3)# TruevF=bool([])# False - empty listvG=bool([False])# True - non-empty listvH=bool({})# False - empty dict; same for empty tuplevI=bool("")# False - empty stringvJ=bool(" ")# True - non-empty stringvK=bool(None)# FalsevL=bool(len)# TruevM=set([1,2])vN=set((1,2))# Converts any sequence, not just a listvO=set("abc")# {'c', 'b', 'a'}vP=set(b"abc")# {97, 98, 99}vQ=list(vM)vR=list({1:"a",2:"b"})# dict -> list of keysvS=tuple(vQ)vT=list("abc")# ['a', 'b', 'c']print(v1,v2,v3,type(v1),type(v2),type(v3))
Implicit type conversion:
int1=4float1=int1+2.1# 4 converted to float# str1 = "My int:" + int1 # Error: no implicit type conversion from int to stringstr1="My int:"+str(int1)int2=4+True# 5: bool is implicitly converted to intfloat2=4.5+True# 5.5: True is converted to 1, which is converted to 1.0
Keywords: type casting.
Links: