< BACKMake Note | BookmarkCONTINUE >
152015024128143245168232148039199167010047123209178152124239215162145081058105061222103253

Built-In Data Types

Built-in data types are types that are already built into the interpreter. They are divided into two groups:

Immutable Data Types

These objects cannot have their values altered (for example, strings, numbers, and tuples).

Mutable Data Types

These 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
				
					

Numbers

Python 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.

Tip

Hexadecimal numbers must always be preceded by 0x, and octal numbers must be preceded by 0.



Python can do a lot of things with numbers:

It can write equations:

						
>>> 3*(3.0/34)
0.264705882353

					

It can use functions:

						
>>> round(12.32,1)
12.3

					

It can make comparisons:

						
>>> 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)
				
					

Strings

Python 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.

Table 2.1. Escape Codes Supported by Python Strings
Escape Code Description
\\ backslash
\' single quote
\" double quote
\b backspace
\e escape
\0 null
\n linefeed, also known as \012
\v vertical tab
\t horizontal tab
\r carriage return
\f form feed
\0nn octal value, the nn domain is: 0..7
\xnn hexa value, the nn domain is: 0..9, A..F, a..f

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

					

Note

Python doesn't treat the contents of back quotes as commands to execute, as do Perl and sh.



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 Support

Unicode 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 Values

Falsity 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

					


Last updated on 1/30/2002
Python Developer's Handbook, © 2002 Sams Publishing

< BACKMake Note | BookmarkCONTINUE >

Index terms contained in this section

/ (slash literal)
      creating strings
\\ escape code

      escape code

      escape code
` (back quotes)
      strings
`` (backticks)
      strings
assigning
      null values to variables
      values to substrings
back quotes (`)
      strings
backticks (``)
      strings
binary operations
      numbers in
calling
     methods
            from string objects
charactersÓ
      Ò
codes
      escape
comparisons
      numbers
complex numbers
      handling
copying
      objects
creating
      strings, slash literal (/)
data types
      immutable
      None
decimals in numbers
double quotes (Ò
     )
            strings
duplicating
      objects
endswith() method
equations
      numbers in
error messages
      assigning values to substrings
      handling long integers
escape code 2nd 3rd 4th 5th 6th 7th 8th 9th 10th
escape codes
floating-point numbers
functions
      numbers in
handling
      complex numbers
      long integers
hexadecimal numbers
immutable data types
indexer values
      strings
integers
      division of, truncations
     long
            handling
long integers
      handling
masking
      numbers in
messages
     error
            assigning values to substrings
            handling long integers
methods
      calling from string objects
      endswith()
      print
      startswith()
modules
      string
None data types
null value
      assigning to variables
Ó
     (double quotes)
            strings
     Ó
            Ó (triple quotes);strings
Õ
     (single quote)
            strings
objects
      copying
     string
            calling methods
octal numbers
operations
     binary
            numbers in
print method
quotes
      strings 2nd
raw strings
      creating strings with slash literal (/)
shifting
      numbers in
single quotes (Ô
     )
            strings
slash literal (/)
      creating strings
slicing
      strings
startswith() method
string modules
string objects
     methods
            calling
strings 2nd 3rd 4th
stringsÓ
      Ò
substrings
      assigning values
support
      Unicode 2nd
triple quotes (Ò
     Ó
            Ó)strings
truncations
      division of integers
Unicode support 2nd
values
      assigning to substrings
     indexer
            strings
     null
            assigning to variables

© 2002, O'Reilly & Associates, Inc.