Hi folks. I'm getting back into learning Python after a few months off, so I'm still a newbie, hehe. I'm using the free online book Dive Into Python. I ran the first program and it worked just fine, but it returned the output in the opposite order of the one showed in the book. Here's the code (directly from the book, zero modifications)

"""odbchelper.py sample script

This program is part of "Dive Into Python", a free Python book for
experienced programmers.  Visit http://diveintopython.org/ for the
latest version.

All this stuff at the top of the script is just optional metadata;
the real code starts on the "def buildConnectionString" line
"""

__author__ = "Mark Pilgrim (mark@diveintopython.org)"
__version__ = "$Revision: 1.2 $"
__date__ = "$Date: 2004/05/05 21:57:19 $"
__copyright__ = "Copyright (c) 2001 Mark Pilgrim"
__license__ = "Python"

def buildConnectionString(params):
	"""Build a connection string from a dictionary
	
	Returns string.
	"""
	return ";".join(["%s=%s" % (k, v) for k, v in params.items()])

if __name__ == "__main__":
	myParams = {"server":"mpilgrim", \
				"database":"master", \
				"uid":"sa", \
				"pwd":"secret"
				}
	print buildConnectionString(myParams)

My output using Python 2.6.1 is this:
IDLE 2.6.1 ==== No Subprocess ====
>>>
pwd=secret;database=master;uid=sa;server=mpilgrim
>>>

From the book: The output of odbchelper.py will look like this:
server=mpilgrim;uid=sa;database=master;pwd=secret

Does this have to do with the version of Python I'm running? Will I have to worry about this for future scripts? What other return orders might be affected other than Print?

I assume that re-ordering is due to the way that dictionaries are stored in memory... in order to provide faster searching and indexing dictionaries are stored in a special way... much unlike lists and tuples, which retain their order.... Example:

>>> d = {'cat':'purr', 'dog':'woof', 'aardvark':'?', 'chocolate rain':'feel the pain'}
>>> print d
{'aardvark': '?', 'chocolate rain': 'feel the pain', 'dog': 'woof', 'cat': 'purr'}
>>> l = ['cat:purr', 'dog:woof', 'aardvark:?', 'chocolate rain:feel the pain']
>>> print l
['cat:purr', 'dog:woof', 'aardvark:?', 'chocolate rain:feel the pain']
>>>

Looks like the book made a mild error to confuse the student, the output will be in the hash order of the dictionary, so don't worry.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.