Python 2.2.2 (#1, Nov 3 2002, 16:12:14) [GCC 2.95.3 20010315 (release) [FreeBSD]] on freebsd4 Type "help", "copyright", "credits" or "license" for more information. # Strings >>> print "Python in 10 minutes" Python in 10 minutes >>> print "Python in %d minutes" % (6 - 4) Python in 2 minutes >>> print "Python in 10 minutes".upper() PYTHON IN 10 MINUTES >>> print "Python in 10 minutes".lower() python in 10 minutes >>> print "Python in 10 minutes".capitalize() Python in 10 minutes >>> print "Python in 10 minutes"[4:] on in 10 minutes >>> print "Python in 10 minutes"[4:16] on in 10 min >>> print "Python in 10 minutes"[-14:] in 10 minutes >>> val = int("345") >>> print val 345 >>> x = 2 # Lists >>> l = ["minutes","seconds"] >>> print "Python in %d %s" % ( x, l[x%2]) Python in 2 minutes >>> x += 1 >>> print "Python in %d %s" % ( x, l[x%2]) Python in 3 seconds >>> d = "Python in %d %s" % ( x, l[x%2]) >>> for character in d: ... print "..%c.." % character ... ..P.. ..y.. ..t.. ..h.. ..o.. ..n.. .. .. ..i.. ..n.. .. .. ..3.. .. .. ..s.. ..e.. ..c.. ..o.. ..n.. ..d.. ..s.. >>> l = d.split() >>> l ['Python', 'in', '3', 'seconds'] >>> for item in l: ... print item.count("o") ... 1 0 0 1 >>> dir (l) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__repr__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>> while 1: ... try: ... print l.pop() ... except: ... break ... seconds 3 in Python >>> str = "this is only a test" >>> l = [] >>> for c in str: ... l.append(ord(c)) ... >>> print l [116, 104, 105, 115, 32, 105, 115, 32, 111, 110, 108, 121, 32, 97, 32, 116, 101, 115, 116] >>> for x in xrange(0,len(l)): ... print l[x] % (x + 1) ... 0 0 0 3 2 3 3 0 3 0 9 1 6 13 2 4 16 7 2 # Dictionaries (Hashes) >>> dict = {"d" : "e", "f" : ["g",1,{}]} >>> print dict.keys() ['d', 'f'] >>> dict.has_key("c") 0 >>> dict[0] Traceback (most recent call last): File "", line 1, in ? KeyError: 0 >>> dict['d'] 'e' >>> dict['f'] ['g', 1, {}] >>> dict['f'][0] 'g' >>> dict['f'][1] 1 >>> dict['f'][2] {} >>> dict['f'][2].keys() [] >>> dict['f'][2]["test"] = "mable" >>> print dict {'d': 'e', 'f': ['g', 1, {'test': 'mable'}]} >>> del dict["f"][1] >>> print dict {'d': 'e', 'f': ['g', {'test': 'mable'}]} # functions >>> def foo(c): ... l = [] ... for x in xrange(0,c / 2 + 1): ... l.append([x,c - x]) ... return l ... >>> foo(10) [[0, 10], [1, 9], [2, 8], [3, 7], [4, 6], [5, 5]] # classes -- file: MathClass.py -- class FourFuncException(Exception): pass class FourFunc: def __init__(self,a,b): self.a = a self.b = b def add(self): return self.a + self.b def subtract(self): return self.a - self.b def multiply(self): return self.a * self.b def divide(self): if not self.b: raise FourFuncException, "Cannot divide by zero!" return float(self.a) / self.b -- end file: MathClass.py >>> import MathClass >>> calc = MathClass.FourFunc(81,45) >>> calc.add() 126 >>> calc.subtract() 36 >>> calc.multiply() 3645 >>> calc.divide() 1.8