See All Titles |
![]() ![]() Built-In Data TypesBuilt-in data types are types that are already built into the interpreter. They are divided into two groups: Immutable Data TypesThese objects cannot have their values altered (for example, strings, numbers, and tuples). Mutable Data TypesThese objects can have their values manipulated (for example, lists and dictionaries). Sometimes, it becomes necessary to assign a null value to a variable using the special data type known as None: >>> x = 1 >>> x 1 >>> print x 1 >>> x = None >>> x >>> As you could see, nothing was returned. However, if you try to print this value, the print method of the object will specially handle the None value by returning a None result. This is shown in the following: >>> print x None NumbersPython provides the following numeric data types: integer, floating-point, hexadecimal (base 16), and octal (base 8). Some examples of these data types are 43, 1.5, 0xB3, and 045, respectively. Python can do a lot of things with numbers: >>> 3*(3.0/34) 0.264705882353 >>> round(12.32,1) 12.3 >>> x = 2 >>> 0<x<5 1 It can make binary operations, such as shifting and masking: >>> 16<<2 64 >>> 40&0xab 40 >>> 2|1 3 >>> ~2 -3 >>> 3^4 7 A very important detail is the fact that Python truncates integer divisions: >>> 3/2 1 If you really want the decimals, you have two options. Either you pass a converted number to the division function, or you put a decimal point in your number, as illustrated here: >>> x = 3 >>> float(x)/2 1.5 >>> x 3 >>> 3.0/2 1.5 Python supports long integers—with unlimited size. To let Python know that it should handle an integer as a long integer, you need to put an L at the end of the number: >>> 2L**100 1267650600228229401496703205376L Otherwise you get an error message: >>> 2**100 Traceback (innermost last): File "<stdin>", line 1, in ? OverflowError: integer pow() Chapter 4, "Exception Handling," teaches you how to interpret this exception message. Python also handles complex numbers in the format (real part + imaginary part): >>> 2j**2 (-4+0j) StringsPython considers a string as a sequence of characters. Therefore, every time you use, for example, the string "Parrot", internally Python handles it as the sequence ["P", "a", "r", "r", "o", "t"]. The first indexer value is always the number zero. Hence, to have access to the letter P, you need to say "Parrot"[0] and to access the letter a, you need to say "Parrot"[1]. Using the same concept, we can get access to all the other elements. The following is an example of string operators: >>> "dead parrot " + "sketch" # concatenation "dead parrot sketch" >>> "parrot " * 2 # repetition "parrot parrot" >>> "parrot"[1] # indexing "a" >>> "parrot"[-1] # indexing backward "t" >>> "parrot"[1:3] # slicing (*) "ar" When slicing, it isn't necessary to include both first and last elements. Whenever you omit one of the elements, it is assumed that you want everything in that direction. Note that the second argument is always a positional reference. >>> "parrot"[1:] "arrot" >>> "parrot"[:3] "par" Always remember that assigning z = x doesn't make a copy of the object x. Instead, it creates a new reference for that object (as you already saw in the earlier round example). If you have to create a copy of a sequence named x, you need to type: >>> z = x[:] The variable z will identify the middle of the variable x, and it will be initialized with everything from the left direction plus everything from the right direction. Note that since Python 1.5, id(s) == id(s[:]) for strings because of string interning. Strings cannot be modified after creation. It isn't possible to assign a value to a substring because strings are immutable. See the error message in the next example: >>> t = "pxrrot" >>> t[1:2] = "a" Traceback (innermost last): File "<stdin>", line 1, in ? TypeError: object doesn't support slice assignment In cases like this, the usual solution is a little trick: s = s[:left_element] + new_substring + s[right_element:] For example >>> t = "pxrrot" >>> t = t[:1] + "a" + t[2:] >>> t "parrot" Let me show you other useful operations that you can do with strings: >>> len("parrot") # Get its length 6 >>> "parrot" < "sketch" # Compare one string against another. 1 >>> "t" in "parrot" # This logical test needs a char left operand 1 >>> "\n, \0, \x" # Use escape codes "\012, \000, \\x" Table 2.1 lists the escape codes supported by Python strings. Next is an example of escape code: >>> print "I am a lumberjack\ nand I am OK" I am a lumberjack and I am OK You can use either single quotes or double quotes. They are both interpreted the same way. Both strings 'Spam' and "Spam" are basically the same thing. Python also accepts triple quotes for remarks that span across several lines: >>> t = """I am a lumberjack … and I am OK""" >>> print t I am a lumberjack and I am OK >>> t "I am a lumberjack\ 012and I am OK" Note that the escape code \012 becomes part of the string. If you need to create strings with the / (slash literal), you must use raw strings. Raw strings are identified by the letter r right before the first quote, as shown in the following: >>> print r"\n, \f, \x" \n, \f, \x There is one more thing that I think you should know about strings. The enclosing backticks `` tell the interpreter to understand that the enclosed object is of string data type: >>> n = 123 >>> print `n` + " Parrot" 123 Parrot Prior to version 2.0, you had to rely on the string module to manipulate your string objects because the string-manipulation functionality was in the string module. With this new release, the methods were pushed to the string type. Note that old string module was not removed from the distribution because it is still necessary for backwards compatibility. The following example shows how to call a method from a string object. >>> 'Python '.join('World') Python World Note that 'Python '.join('World') is equivalent to the old string module: string.join("World", "Python ") Besides the methods that were inherited from the string module, two new methods were added: startswith() and endswith(). s.startswith(t) is equivalent to s[:len(t)] == t and s.endswith(t) is equivalent to s[-len(t):] == t. Unicode SupportUnicode is a new immutable string data type supported by Python 2.0. Basically, it can represent characters using 16-bit numbers, instead of the 8-bit numbers used by the ASCII standard, which means that Unicode strings can support up to 65,536 distinct characters. Note that when combining an 8-bit string and an Unicode string, the resulting string is an Unicode string. In order to write a Unicode string in Python, you need to use the notation u"string". If you need to write arbitrary Unicode characters, you can use the new escape sequence, \uHHHH, where HHHH is a 4-digit hexadecimal number from 0000 to FFFF. Note that you can also use the existing \xHHHH escape sequence. Another option is to use octal escapes whenever you need to write characters up to U+01FF (represented by \777). True and False Logical ValuesFalsity is represented by zeros, empty structures, or the value None (for example, 0, [], {}, (), None). Logical Truth is represented by results different from zero and non-empty structures (for example, 1, [2], (1,2,3), "abc"). The following if statement checks whether the variable t has any value; in this case, the statement returns true, allowing the block contents to be executed: >>> t = "Parrot" >>> if t: … print "Parrot" … Parrot
|
© 2002, O'Reilly & Associates, Inc. |