# prints numbers 0-4 on separate lines: for i in range(5): print i # also prints numbers 0-4 on separate lines: for i in range(0,5): print i # print is the python equivalent of printf. # just join everything with commas. print "This is a string with numbers", 1, ",", 2, ", and", 3 # you can also use print with interpolation, like printf's format string # the '%' operator takes a format string and an tuple of values to interpolate print "This is a string with numbers %d, %d, and %d" % (1,2,3) # tuples in python are like immutable arrays in other languages # declare and print a tuple with three elements tuple = (1, 2, 3) print tuple # destructure tuple into three different variables # verify that the values are what we think they are. (one, two, three) = tuple assert one == 1, "one is not 1!" assert two == 2, "two is not 2!" assert three == 3, "three is not 3!" # here's another way to pull elements out of a tuple one = tuple[0] two = tuple[1] three = tuple[2] assert one == 1, "one is not 1!" assert two == 2, "two is not 2!" assert three == 3, "three is not 3!" # tuples are readonly---you can't change a value in a tuple: #tuple[1] = 4 # this would fail #tuple[2] = 9 # this would fail # here's a tuple with no (zero) elements zeroElementTuple = () print "null tuple:", zeroElementTuple # here's a tuple with one element # the comma is necessary to make sure python knows this is a tuple oneElementTuple = (1,) print "one-element tuple:", oneElementTuple # tuples can be concatenated with the '+' operator: concatenatedTuple = oneElementTuple + oneElementTuple assert concatenatedTuple == (1,1), "concatenation didn't work!" print "Here's a concatenated tuple:", concatenatedTuple # or the += operator: concatenatedTuple += oneElementTuple assert concatenatedTuple == (1,1,1), "concatenation didn't work!" print "Here's a concatenated tuple:", concatenatedTuple # concatenating the null tuple does what you'd expect concatenatedTuple = () + concatenatedTuple + () assert concatenatedTuple == (1,1,1), "concatenation didn't work!" print "Here's a concatenated tuple:", concatenatedTuple # dictionaries are hash tables with some reasonable built-in hash function dict = {} # create an empty dictionary dict[123] = 456 dict[789] = 0 dict['foo'] = 'bar' assert dict[123] == 456, "value didn't match!" assert dict[789] == 0, "value didn't match!" assert dict['foo'] == 'bar', "value didn't match!"