Archive for November, 2007

CHAPTER 4 DICTIONARIES: WHEN INDICES (Free web hosting music) WON T DO

Friday, November 30th, 2007

CHAPTER 4 DICTIONARIES: WHEN INDICES WON T DO 71 people = { ‘Alice’: { ‘phone’: ‘2341′, ‘addr’: ‘Foo drive 23′ }, ‘Beth’: { ‘phone’: ‘9102′, ‘addr’: ‘Bar street 42′ }, ‘Cecil’: { ‘phone’: ‘3158′, ‘addr’: ‘Baz avenue 90′ } } # Descriptive labels for the phone number and address. These will be used # when printing the output. labels = { ‘phone’: ‘phone number’, ‘addr’: ‘address’ } name = raw_input(’Name: ‘) # Are we looking for a phone number or an address? request = raw_input(’Phone number (p) or address (a)? ‘) # Use the correct key: if request == ‘p’: key = ‘phone’ if request == ‘a’: key = ‘addr’ # Only try to print information if the name is a valid key in our dictionary: if name in people: print “%s’s %s is %s.” % (name, labels[key], people[name][key]) String Formatting with Dictionaries In Chapter 3, you saw how you could use string formatting to format all the values in a tuple. If you use a dictionary (with only strings as keys) instead of a tuple, you can make the string formatting even snazzier. After the % character in each conversion specifier, you add a key (enclosed in parentheses), which is followed by the other specifier elements:
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

70 CHAPTER 4 DICTIONARIES: WHEN INDICES WON T (Web hosting isp)

Thursday, November 29th, 2007

70 CHAPTER 4 DICTIONARIES: WHEN INDICES WON T DO The expression k in d(where dis a dictionary) looks for a key, not a value. The expression v in l, on the other hand (where l is a list) looks for a value, not an index. This may seem a bit inconsistent, but it is actually quite natural when you get used to it. After all, if the dictionary has the given key, checking the corresponding value is easy. Tip Checking for key membership in a dictionary is much more efficient than checking for membership in a list and the difference is greater the larger the data structures are. The first point that the keys may be of any immutable type is the main strength of dictionaries, while the second point is important, too. Just look at the difference here: >>> x = [] >>> x[42] = ‘Foobar’ Traceback (most recent call last): File ““, line 1, in ? IndexError: list assignment index out of range >>> x = {} >>> x[42] = ‘Foobar’ >>> x {42: ‘Foobar’} First, I try to assign the string ‘Foobar’to position 42 in an empty list clearly impossible because that position does not exist. To make this possible, I would have to initialize x with [None]*43 or something, rather than simply []. The next attempt, however, works perfectly. Here I assign ‘Foobar’ to the key 42 of an empty dictionary; no problem! A new item is simply added to the dictionary and I m in business. Example Listing 4-1 shows the code for the telephone book example. Here is a sample run of the program: Name: Beth Phone number (p) or address (a)? p Beth’s phone number is 9102. Listing 4-1. Dictionary Example # A simple database # A dictionary with person names as keys. Each person is represented as # another dictionary with the keys ‘phone’ and ‘addr’ referring to their phone # number and address, respectively.
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.

CHAPTER 4 DICTIONARIES: WHEN INDICES (1 on 1 web hosting) WON T DO

Wednesday, November 28th, 2007

CHAPTER 4 DICTIONARIES: WHEN INDICES WON T DO 69 The dict Function You can use the dict function to construct dictionaries from other mappings (for example, other dictionaries) or from sequences of (key, value) pairs: >>> items = [(’name’, ‘Gumby’), (’age’, 42)] >>> d = dict(items) >>> d {’age’: 42, ‘name’: ‘Gumby’} >>> d[’name’] ‘Gumby’ It can also be used with keyword arguments, as follows: >>> d = dict(name=’Gumby’, age=42) >>> d {’age’: 42, ‘name’: ‘Gumby’} Although this is probably the most useful application of dict, you can also use it with a mapping argument to create a dictionary with the same items as the mapping. (If used without any arguments, it returns a new empty dictionary, just like other similar functions such as list, tuple, or str.) If the other mapping is a dictionary (which is, after all, the only built-in mapping type), you can use the dictionary method copyinstead, as described later. Note The dict function isn t really a function at all. It is a type, just like list, tuple, and str. Basic Dictionary Operations The basic behavior of a dictionary in many ways mirrors that of a sequence: len(d) returns the number of items (key-value pairs) in d, d[k] returns the value associated with the key k, d[k] = v associates the value v with the key k, del d[k] deletes the item with key k, and k in d checks whether there is an item in d that has the key k. Although they share several common characteristics, there are some important distinctions: Dictionary keys don t have to be integers (though they may be). They may be any immutable type, such as floating-point (real) numbers, strings, or tuples. You can assign a value to a key even if that key isn t in the dictionary to begin with; a new item will be created. You cannot assign a value to an index outside the list s range (without using append or something like that).
We recommend high quality webhost to host and run your jsp application: christian web host services.

CHAPTER 4 DICTIONARIES: WHEN INDICES WON T DO (Top web site)

Tuesday, November 27th, 2007

CHAPTER 4 DICTIONARIES: WHEN INDICES WON T DO Note You might wonder why I have used strings to represent the telephone numbers why not integers? Consider what would happen to Dee-Dee s number then: >>> 0142 98 Not exactly what we wanted, is it? As mentioned briefly in Chapter 1, octal numbers are written with an initial zero. It is impossible to write decimal numbers like that. >>> 0912 File ““, line 1 0912 ^ SyntaxError: invalid syntax The lesson is this: Telephone numbers (and other numbers that may contain leading zeros) should be represented as strings of digits not integers. Once you ve created these lists, you can look up Cecil s telephone number as follows: >>> numbers[names.index(’Cecil’)] 3158 It works, but it s a bit impractical. What you really would want to do is something like the following: >>> phonebook[’Cecil’] 3158 Guess what? If phonebookis a dictionary, you can do just that. Dictionary Syntax Dictionaries are written like this: phonebook = {’Alice’: ‘2341′, ‘Beth’: ‘9102′, ‘Cecil’: ‘3258′} Dictionaries consist of pairs (called items) of keys and their corresponding values. In the preceding example, the names are the keys and the telephone numbers are the values. Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary (without any items) is written with just two curly braces, like this: {}. Note Keys are unique within a dictionary (and any other kind of mapping), while values may not be.
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

CHAPTER 4 Dictionaries: When Indices (Web design)

Monday, November 26th, 2007

CHAPTER 4 Dictionaries: When Indices Won t Do You ve seen that lists are useful when you want to group values into a structure and refer to each value by number. In this chapter, you learn about a data structure in which you can refer to each value by name. This type of structure is called a mapping, and the only built-in mapping type in Python is the dictionary. The values in a dictionary don t have any particular order but are stored under a key, which may be either a number, a string, or even a tuple. But What Are They For? There are many situations where a dictionary is more appropriate than a list. The name dictionary should give you a clue: an ordinary book is made for reading from start to finish. If you like, you can quickly open it to any given page. This is a bit like a Python list. Dictionaries, however (both real ones and their Python equivalent) are constructed so that you can look up a specific word (key) easily, to find its definition (value). Some arbitrary uses of Python dictionaries are as follows: Representing the state of a gaming board, with each key being a tuple of coordinates Storing file modification times, with file names as keys A digital telephone/address book Let s say you have a list of people: >>> names = [’Alice’, ‘Beth’, ‘Cecil’, ‘Dee-Dee’, ‘Earl’] What if you wanted to create a little database where you could store the telephone numbers of these people how would you do that? One way would be to make another list. Let s say you re only storing their four-digit extensions. Then you would get something like this: >>> numbers = [’2341′, ‘9102′, ‘3158′, ‘0142′, ‘5551′]
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

66 CHAPTER 3 WORKING (Free web hosting music) WITH STRINGS New

Sunday, November 25th, 2007

66 CHAPTER 3 WORKING WITH STRINGS New Functions in This Chapter Function Description string.maketrans(from, to) Makes a translation table for translate What Now? Lists, strings, and dictionaries are three of the most important data types in Python. You ve seen lists and strings, so guess what s next? In the next chapter, you see how dictionaries not only support indices, but other kinds of keys (such as strings or tuples) as well. Dictionaries also support a few methods, although not as many as strings.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Remote web server - CHAPTER 3 WORKING WITH STRINGS 65 An

Saturday, November 24th, 2007

CHAPTER 3 WORKING WITH STRINGS 65 An optional second argument can be supplied to translate, specifying letters that should be deleted. If you wanted to emulate a really fast-talking German, for instance, you could delete all the spaces: >>> ‘this is an incredible test’.translate(table, ‘ ‘) ‘thizizaninkredibletezt’ Tip Sometimes string methods such as lowerwon t work quite the way you want them to for instance, if you happen to use a non-English alphabet. Let s say you want to convert the uppercase Norwegian word B LLEFR to its lowercase equivalent: >>> print ‘B LLEFR ‘.lower() b llefr As you can see, this didn t really work because Python doesn t consider a real letter. In this case, you can use translate to do the translation: >>> table = maketrans(’ ‘, ‘ ‘) >>> word = ‘K PES M’ >>> print word.lower() k pes m >>> print word.translate(table) K PES M >>> print word.translate(table).lower() k pes m See also: replace, lower. A Quick Summary In this chapter, you have seen two important ways of working with strings: String formatting. The modulo operator (%) can be used to splice values into a string that contains conversion flags, such as %s. You can use this to format values in many ways, including right or left justification, setting a specific field width and precision, adding a sign (plus or minus), or left-padding with zeros. String methods. Strings have a plethora of methods. Some of them are extremely useful (such as split and join), while others are used less often (such as istitle or capitalize).
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

Web site domain - 64 CHAPTER 3 WORKING WITH STRINGS translate

Friday, November 23rd, 2007

64 CHAPTER 3 WORKING WITH STRINGS translate Similar to replace, translate replaces parts of a string, but unlike replace, translate only works with single characters. Its strength lies in that it can perform several replacements simultaneously, and can do so more efficiently than replace. There are quite a few rather technical uses for this method (such as translating newline characters or other platform-dependent special characters), but let s consider a simpler (although slightly more silly) example. Let s say you want to translate a plain English text into one with a German accent. To do this, you must replace the character c with k, and s with z. Before you can use translate, however, you must make a translation table. This translation table is a full listing of which characters should be replaced by which. Because this table (which is actually just a string) has 256 entries, you won t write it out yourself: You ll use the function maketrans from the string module. The maketransfunction takes two arguments: two strings of equal length, indicating that each character in the first string should be replaced by the character in the same position in the second string. Got that? In the case of our simple example, the code would look like the following: >>> from string import maketrans >>> table = maketrans(’cs’, ‘kz’) WHAT S IN A TRANSLATION TABLE? A translation table is a string containing one replacement letter for each of the 256 characters in the ASCII character set: >>> table = maketrans(’cs’, ‘kz’) >>> len(table) 256 >>> table[97:123] ‘abkdefghijklmnopqrztuvwxyz’ >>> maketrans('’, ‘’)[97:123] ‘abcdefghijklmnopqrstuvwxyz’ As you can see, I ve sliced out the part of the table that corresponds to the lowercase letters. Take a look at the alphabet in the table and that in the empty translation (which doesn t change anything). The empty translation has a normal alphabet, while in the preceding code, the letter c has been replaced by k, and s has been replaced by z. Once you have this table, you can use it as an argument to the translatemethod, thereby translating your string: >>> ‘this is an incredible test’.translate(table) ‘thiz iz an inkredible tezt’
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.

CHAPTER 3 (Submit web site) WORKING WITH STRINGS 63 split

Thursday, November 22nd, 2007

CHAPTER 3 WORKING WITH STRINGS 63 split A very important string method, splitis the inverse of join, and is used to split a string into a sequence: >>> ‘1+2+3+4+5′.split(’+') [’1′, ‘2′, ‘3′, ‘4′, ‘5′] >>> ‘/usr/bin/env’.split(’/') ['’, ‘usr’, ‘bin’, ‘env’] >>> ‘Using the default’.split() [’Using’, ‘the’, ‘default’] Note that if no separator is supplied, the default is to split on all runs of consecutive whitespace characters (spaces, tabs, newlines, and so on). See also: join. In Appendix B: rsplit, splitlines. strip The strip method returns a string where whitespace on the left and right (but not internally) has been stripped (removed): >>> ‘ internal whitespace is kept ‘.strip() ‘internal whitespace is kept’ As with lower, strip can be useful when comparing input to stored values. Let s return to the user name example from the section on lower, and let s say that the user inadvertently types a space after his name: >>> names = [’gumby’, ’smith’, ‘jones’] >>> name = ‘gumby ‘ >>> if name in names: print ‘Found it!’ … >>> if name.strip() in names: print ‘Found it!’ … Found it! >>> You can also specify which characters are to be stripped, by listing them all in a string parameter: >>> ‘*** SPAM * for * everyone!!! ***’.strip(’ *!’) ‘SPAM * for * everyone’ Stripping is only performed at the ends, so the internal asterisks are not removed. In Appendix B: lstrip, rstrip.
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

Web host sites - 62 CHAPTER 3 WORKING WITH STRINGS As

Wednesday, November 21st, 2007

62 CHAPTER 3 WORKING WITH STRINGS As you can see, the sequence elements that are to be joined must all be strings. Note how in the last two examples I make use of a list of directories and format them according to the conventions of UNIX and DOS/Windows simply by using a different separator (and adding a drive name in the DOS version). See also: split. lower The lower method returns a lowercase version of the string: >>> ‘Trondheim Hammer Dance’.lower() ‘trondheim hammer dance’ This can be useful if you want to write code that is case-insensitive that is, code that ignores the difference between uppercase and lowercase letters. For instance, you want to check whether a user name is found in a list. If your list contains the string ‘gumby’ and the user enters his name as ‘Gumby’, you won t find it: >>> if ‘Gumby’ in [’gumby’, ’smith’, ‘jones’]: print ‘Found it!’ … >>> The same will of course happen if you have stored ‘Gumby’ and the user writes ‘gumby’, or even ‘GUMBY’. A solution to this is to convert all names to lowercase both when storing and searching. The code would look something like this: >>> name = ‘Gumby’ >>> names = [’gumby’, ’smith’, ‘jones’] >>> if name.lower() in names: print ‘Found it!’ … Found it! >>> See also: translate. In Appendix B: islower, capitalize, swapcase, title, istitle, upper, isupper. replace The replace method returns a string where all the occurrences of one string have been replaced by another: >>> ‘This is a test’.replace(’is’, ‘eez’) ‘Theez eez a test’ If you have ever used the search and replace feature of a word processing program, you will no doubt see the usefulness of this method. See also: translate. In Appendix B: expandtabs.
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.