Last login: Wed Oct 11 10:26:06 on console Welcome to Darwin! mdhcp11:~ mahoney$ python Python 2.3.5 (#1, Mar 20 2005, 20:38:20) [GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> vowels = [ 'a', 'e', 'i', 'o', 'u' ] >>> vowels ['a', 'e', 'i', 'o', 'u'] >>> "hi" 'hi' >>> 'hi' 'hi' >>> 'What\'s up?' "What's up?" >>> word = 'hello' >>> hello[-1] Traceback (most recent call last): File "", line 1, in ? NameError: name 'hello' is not defined >>> word[-1] 'o' >>> word[1] 'e' >>> def member(thing, list): ... """Return true if 'thing' is in 'list'""" ... for element in list: ... if element == thing: ... return True ... return False ... >>> member(2, [1,2,3]) True >>> member(7, [1,2,3]) False >>> 'a' in ['a', 'e', 'i', 'o', 'u'] True >>> 10 % 3 1 >>> 10 % 5 0 >>> def is_factor(n, factor): ... """return True if factor divides n""" ... if n % factor == 0: ... return True ... else: ... return False ... >>> is_factor(10,5) True >>> is_factor(10,3) False >>> 4 <= 4 True >>> 4 =< 4 File "", line 1 4 =< 4 ^ SyntaxError: invalid syntax >>> 4 != 4 False >>> 4 is 4 True >>> [1,2,3] == [1,2,3] True >>> [1,2,3] is [1,2,3] False >>> "Hi there" is "Hi there" True >>> def foo(n,m) File "", line 1 def foo(n,m) ^ SyntaxError: invalid syntax >>> def foo(n,m): ... return n % m == 0 ... >>> foo(10,5) True >>> def foo(n,m): ... answer = (n % m == 0) ... return answer ... >>> is_factor >>> help(is_factor) >>> def test(n): ... # This function does .... ... return 1 ... >>> test(100) 1 >>> help(test) >>> def get_factors(n): ... """return a list of the factors of n including 1 but not n""" ... answer = [] ... for i in range(1,n): ... if is_factor(n,i): ... answer.append(i) ... return answer ... >>> get_factors(10) [1, 2, 5] >>> get_factors(100) [1, 2, 4, 5, 10, 20, 25, 50] >>> sum([1,2,3,4,5]) 15 >>> def is_perfect(n): ... """return True if n is perfect""" ... factors = get_factors(n) ... return sum(factors) == n ... >>> is_perfect(10) False >>> is_perfect(2) False >>> for i in range(100): ... if is_perfect(i): ... print i, "is perfect" ... 0 is perfect 6 is perfect 28 is perfect >>> get_factors(6) [1, 2, 3] >>> get_factors(28) [1, 2, 4, 7, 14] >>> import math >>> print math.exp(1) 2.71828182846 >>> dir(math) ['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'] >>> math.e 2.7182818284590451 >>> math.pi 3.1415926535897931 >>>