# Sharp marks comments to end of line # Python is case sensitive # ; is optional print 'hello', 'there', print 'world' # Prints 'hello there world' i = 3; I = 2 * i print i, I # 3 6 # Python is self typed - no declarations needed # Creates ints, floats, strings, as needed # and selects appropriate operations j = 2 * i print j # 6 - i and j are ints f = 2.3 * i print f # 6.9 - f is a float s = "Hello" t = 3 * s # three copies of the string print t # HelloHelloHello # Won't allow misusing strings as ints # u = s + 3 # Error: Won't convert 3 to string u = s + '3' # + concatenates strings print u # Hello3 n = '34' # n = n + 1 # Error: Won't convert n to int # Can use explicit conversions a = '1' b = 2 * int(a) print b # 2 c = str(2) + a print c # 21 d = int(5.9) print d # 5 - truncates # Type of a variable can change d = "abc" # d is now a string print d # Prints abc # Long integers have infinite precision x = 8111111111111111111111L y = 3 * x print y # 24333333333333333333333L # Will convert between int and float as needed for output # Will convert to string as needed for output print '%i %f %s' % (i,i,i) # 3 3.000000 3 print '%i %f %s' % (f,f,f) # 6 6.900000 6.9 print '%s %s %s' % (s,s,s) # must use %s for string variables n = '34' #print '%i' % n # Error: Won't convert to int automatically