Intrinsic operations are built into the standard libraries in Python. These operations can be performed on Python objects including standard data types. We already dis- cussed some operations involving the standard data types. These include variable assignments, forming expressions by using variables and operators, and some stan- dard built-in functions. We briefly introduced the type() and id() functions in the previous chapter. Let’s see how the id() and type() functions can be used to extract the type and identity of an object. For example,
>>> type(‘abcd’) <type ‘str’> >>> type(0xdd) <type ‘int’> >>> a=9+4j >>> type(a) <type ‘complex’>
In the preceding examples, the type() function returns the type of the object passed to it as the argument. The id() function can be used to return the memory address of an object. For example,
>>> a=9+4j >>> id(a) 9639792 >>> a=a+76 >>> id(a) 7965324
Notice that because number is an immutable data type, changing the value of a vari- able that contains the number creates another object with another memory address assigned to that object.
REGULAR EXPRESSIONS
The raw strings feature that enables the interpreter to counter the behavior of special characters is useful when composing Regular Expressions. Regular Expressions are special strings that are used to define special search models for strings. They contain special symbols to denote characters, variable names, character classes, and criteria according to which characters are grouped and matched. Regular Expressions also contain the symbols that denote escape sequences. Therefore, using raw strings helps avoid the confusion between the escape sequences and characters that are a part of Regular Expressions.
>>> a=[31,’ddd’] >>> id(a) 17429076 >>> a.append(‘abcd’) >>> id(a) 17429076
Notice that in the previous examples, because list is a mutable data type, changing the items in a list does not change the memory address of the list.
Another function that can be used on all data types is the cmp() function. The syn- tax of the cmp() built-in function is this:
cmp(ob1,ob2)
The cmp() function compares two Python objects, ob1 and ob2, and returns 0 if ob1equals ob2, 1 if ob1 is greater than ob2, and -1 if ob1 is less than ob2.
>>> a,b=-8,13 >>> cmp(a,b) -1 >>> hex(34) ‘0x22’ >>> cmp(0x22,34) 0
In the first example, an integer is compared with another integer, and therefore it returns -1 because ob1 is less than ob2. In the second example, the hexadecimal value of 34 is compared with 34. The result is 0 because both the numbers are converted to the same form before comparison and therefore evaluate to the same value. In other words, numeric data types are compared according to their numeric value.
Sequence objects, such as strings, lists, tuples, and dictionaries, can be compared with other objects. The comparison is made using lexicographical ordering. This means that first the Python interpreter compares the first two items. If they are different, the output is determined based on this comparison. If they are identical, the next two items are compared. If they differ, the output is determined by this comparison. This contin- ues until the last item in the sequence is exhausted.
>>> cmp((1,2,3,4),(1,2,4)) -1
>>> a,b=’abc’,’pqr’ >>> cmp(a,b)
-1
If all the items in the two sequences are equal, the sequences are considered equal. If the first few items of one sequence are the same as in the other sequence, the smaller sequence is considered to be less than the longer one.
>>> cmp([123,’abc’,888],[123,’abc’]) 1
If the sequences being compared contain other sequences as their data items, the comparison is made recursively.
>>> cmp([‘abcd’,(123,’abc’,555)],[‘abcd’,(123,’xyz’,897)]) -1
You can also compare objects of different types. The types are compared by their names. Therefore, dictionaries are smaller than lists, lists are smaller than strings, and strings are smaller than tuples.
In addition to the functions that can be performed on all data types, there are func- tions that can be performed only on sequence types, such as the len(), max(), and min()functions. We learned that the len() function is used to find the length of a sequence. The max() and min() functions can be used to find the element with the minimum and maximum values, respectively. For example, when using a string, these functions return the highest and lowest characters, respectively.
>>> max(‘abc’) ‘c’
>>> min(‘abc’) ‘a’
Consider another example for lists as follows:
>>> list=[23,23.1,23L,234e-1] >>> max(list)
23.399999999999999 >>> min(list) 23
When the elements of a sequence are of different types, each element is treated as a separate object that has to be compared lexicographically. The order of precedence for the standard data types is as follows:
dictionaries < lists< strings<tuples
>>> list_str=[‘jjj’,445,[‘vf’,23]] >>> max(list_str)
‘jjj’
>>> min(list_str) 445
After the brief introduction to the operations on data types, let’s consider each data type individually.