26 May 2010

Python 101

~ Some basics about python ~
Let's check out some python idioms:
How to print without automatically append a newline character? You can add a comma after the last object:
print 'Output with no newline', 
String comparison uses the same operators as numeric comparison: The standard comparisons (<, <=, >, >=, ==, !=) apply to strings.

How to iterate through pythonic dictionary?
dictionary = {'1': 'one', '2': 'two', '3': 'three'}
for key in dictionary.iterkeys():
     print key, dictionary[key]

dictionary = {'1': 'one', '2': 'two', '3': 'three'}
for key, value in dictionary.items():
     print key, value

No comments: