[08/31/2011] Programming languages: C++ - 100 Java - 75 C - 65 Python - 20 Perl - 8 C# - 10 PHP - 24 Ruby - 6 HTML/CSS/JavaScript - 40 Python -- What's the big deal? a scripting language? #!/usr/local/bin/python Python -- OO stuff is optional Design goals: easy to read, easy to learn, easy to maintain known as a productive language -- rapid prototyping Python is an interpreter compilation: g++ C++ source =====> Object code (.o) =======> executable (native code) javac Java source =======> Byte Code (.class) ======> Java Virtual Machine (JVM) PORTABILITY Python source ======> Byte code (.pyc) ========> Python Virtual Machine (.py) (PVM) TO DO: download and install..... http://python.org/getit/ <==== download 3.x (latest stable) Advantages: free garbage collection functional programming (list processing) self-documenting Disadvantages: slower (but can mix Python with compiled C, C++ code, etc.) Python at times is also considered "glue code" C++, Java, C ---- the "int" data type 32-bit int range -2,147,483,648 to 2,147,483,647 Python has no limit on range of integers (see below) Every variable in Python is simply a reference variable (or pointer) cost = 15.99 +---------+ cost =============> | 15.99 | +---------+ cost = 18.99 +---------+ | 15.99 | (garbage) +---------+ +---------+ cost =============> | 18.99 | +---------+ Below is copy-and-paste of most of a python session on a Linux box (note this is v2.5 of Python, so the division operator is different than 3.x) >>> 9.8 * 6 58.800000000000004 >>> 9.8 / 4.2 2.3333333333333335 >>> 9.8 // 4.2 2.0 >>> 9.8 % 4.2 1.4000000000000004 >>> 5 ** 1000 933263618503218878990089544723817169617091446371708024621714339795966910975775634454440327097881102359594989930324242624215487521354032394841520817203930756234410666138325150273995075985901831511100490796265113118240512514795933790805178271125415103810698378854426481119469814228660959222017662910442798456169448887147466528006328368452647429261829862165202793195289493607117850663668741065439805530718136320599844826041954101213229629869502194514609904214608668361244792952034826864617657926916047420065936389041737895822118365078045556628444273925387517127854796781556346403714877681766899855392069265439424008711973674701749862626690747296762535803929376233833981046927874558605253696441650390625L >>> 5.0 ** 1000 Traceback (most recent call last): File "", line 1, in OverflowError: (34, 'Numerical result out of range') >>> 1/3 0 >>> 10/3 3 >>> 100/3 33 >>> 1j 1j >>> 8j * 8j (-64+0j) >>> import math >>> math.pi 3.1415926535897931 >>> math.e 2.7182818284590451 >>> math.sqrt( 87 ) 9.3273790530888157 >>> math.factorial( 3 ) Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'factorial' >>> math.fact( 3 ) Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'fact' >>> 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'] >>> help( math.floor ) Help on built-in function floor in module math: floor(...) floor(x) Return the floor of x as a float. This is the largest integral value <= x. >>> help( math.hypot ) Help on built-in function hypot in module math: hypot(...) hypot(x,y) Return the Euclidean distance, sqrt(x*x + y*y). >>> >>> dir( __builtins__ ) ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', 'abs', 'all', 'any', 'apply', 'basestring', 'bool', 'buffer', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'min', 'object', 'oct', 'open', 'ord', 'pow', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip'] >>> >>> float( 3 ) 3.0 >>> help( pow ) Help on built-in function pow in module __builtin__: pow(...) pow(x, y[, z]) -> number With two arguments, equivalent to x**y. With three arguments, equivalent to (x**y) % z, but may be more efficient (e.g. for longs). >>> int( 4.56 ) 4 >>> round( 4.56 ) 5.0 >>> int( 18.64 ) 18 >>> round( 18.64, 2 ) 18.640000000000001 >>> round( 18.64, 1 ) 18.600000000000001 >>> 20 * 30.5 610.0 >>> 'http://' 'http://' >>> "http://" 'http://' >>> 'she said, "no way!"' 'she said, "no way!"' >>> "what's up doc" "what's up doc" >>> """ ... ab"cd ... ef'gh ... """ '\nab"cd\nef\'gh\n' >>> >>> len( 'abcd' ) 4 >>> len( '' ) 0 >>> 5 + len( 'abcd' ) 9 >>> 'hot' + 'dog' 'hotdog' >>> 'hot' * 2 + 'dog' 'hothotdog' >>> 10 * 'hee' 'heeheeheeheeheeheeheeheeheehee' >>> 3 * 'ha' + 'ho' * 3 'hahahahohoho' >>> float( '3.14' ) 3.1400000000000001 >>> int( '500' ) 500 >>> int( '500' * 3 ) 500500500 >>> >>> str( -9.876 ) '-9.876' >>> >>> 'cost is $' + 15.99 * 1.08 Traceback (most recent call last): File "", line 1, in TypeError: cannot concatenate 'str' and 'float' objects >>> 'cost is $' + str( 15.99 * 1.08 ) 'cost is $17.2692' >>> 'cost is $' + str( round( 15.99 * 1.08 ) ) 'cost is $17.0' >>> 'cost is $' + str( round( 15.99 * 1.08 ), 2 ) Traceback (most recent call last): File "", line 1, in TypeError: str() takes at most 1 argument (2 given) >>> 'cost is $' + str( round( 15.99 * 1.08, 2 ) ) 'cost is $17.27' >>> cost = 15.99 * 1.08 >>> cost 17.269200000000001 >>> round( cost, 2 ) 17.27 >>> cost 17.269200000000001 >>> cost = round( cost, 2 ) >>> cost 17.27 >>> >>> cost = 'too much' >>> cost 'too much' >>> lumberjack = 'okay' >>> lumberjack 'okay' >>> lumberjack.capitalize() 'Okay' >>> lumberjack.center( 40 ) ' okay ' >>> lumberjack 'okay' >>> lumberjack.lower() 'okay' >>> lumberjack.upper() 'OKAY' >>> lumberjack.islower() True >>> name = 'david eric goldschmidt' >>> name.capitalize() 'David eric goldschmidt' >>> name.title() 'David Eric Goldschmidt' >>> >>> x, y, z = 5, 3.14, 'pi' >>> x 5 >>> y 3.1400000000000001 >>> z 'pi' >>> x, y = y, x >>> x 3.1400000000000001 >>> y 5 >>> >>> radius = input( 'Enter radius: ' ) Enter radius: 5.67 >>> radius 5.6699999999999999 >>> quit()