Objects and Literals

Objects are also called data structures. Python comes with some built-in objects. Some are used so often that Python has a quick way to make these objects, called literals. The literals include the string, unicode string, integer, float, long, list, tuple and dictionary types.

String Literals

Python has many different types of string literals. There are actually too many to get into, so I'll just show you a few examples

# single quotes
'Who said "to be or not to be"?'

# double quotes
"DNA goes from 5' to 3'."

# escaped quotes
"\"That's not fair!\" yelled my sister."
# creates: "That's not fair!" yelled my sister

# triple quoted strings, with single quotes
'''This one string can go
over several lines'''

# "raw" strings, mostly used for regular expressions
r"\"That's not fair!\" yelled my sister."
# creates: \"That's not fair!\" yelled my sister

# You can even have raw triple double quoted strings!
r"""So there!"""
You should usually use double quoted strings. Use raw strings for regular expressions and use triple quoted strings for long pieces of text (like doc strings).

You can tell the string to include a special character that you can't write. For example, "\n" is used to make a new line and "\t" is for the tab character. You can put a backslash before a quote character to escape it, and before a backslash to escape that as well.


>>> print "Line\t1\n\"Line 2\"\n\\Line 3\n"
Line	1
"Line 2"
\Line 3

>>>

Caution: Microsoft Windows uses path names like C:\Program Files\accessories\wordpad.exe. The \a is a special character (it makes a beep sound) so you should escape the \ as in "C:\\Program Files\\accessories\\wordpad.exe".

Unicode

Normal character strings can only draw from a set of 256 characters. There are many more than 256 characters in the world (eg, with notations like α = 5 Å) which means some things can't be written using a simple (byte) string. Instead, people have agreed on Unicode strings, which extends the character space to up to 65536 characters and even more.

That said, I won't cover Unicode at all because it's too complicated for an introductory class when all the data files we'll use are written in the limited set of ASCII charaters.

Numeric Literals

You can refer to numeric values using integers, floating point numbers, scientific notation, hexadecimal notation, octal, and complex numbers: Python integers can be an size. Integers larger than 2147483647 are called "long" integers because they can't be stored in 32 bits. (The distinction between integers and longs is slowly disappearing.)


123       # an integer

1.23      # a floating point number
  
-1.23     # a negative floating point number
  
1.23E45;   # scientific notation
  
0x7b;      # hexadecimal notation  (decimal 123)
  
0173;      # octal notation (decimal 123)

12+3*j;    # complex number 12 + 3i (Note that Python uses "j"!)

2147483648L # a long integer

List literal



>>> data = [1, 4, 9, 16]
>>> data[0]
1
>>> data[1]
4
>>> data[2] = 7
>>> data
[1, 4, 7, 16]
>>> data[1:3]
[4, 9]
>>>

Tuple literal



>>> data = (1, 4, 9, 16)
>>> data[1]
4
>>> data[2] = 7
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: object doesn't support item assignment
>>> 

Dictionary literal


>>> d = {"A": "ALA", "C": "CYS", "D": "ASP"}
>>> print d["A"]
ALA
>>> 

We'll talk about lists and dictionaries more in the future.

This page based on Literals
Andrew Dalke
Last modified: Tue Feb 3 12:24:02 SAST 2004