changeset 1:ea010fae8754

Added stuff.
author Atul Varma <varmaa@toolness.com>
date Thu, 05 Jun 2008 15:04:18 -0700
parents 027bea439a55
children 58b61d4a9883
files PythonForJsProgrammers.txt
diffstat 1 files changed, 132 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/PythonForJsProgrammers.txt	Thu Jun 05 14:21:46 2008 -0700
+++ b/PythonForJsProgrammers.txt	Thu Jun 05 15:04:18 2008 -0700
@@ -31,25 +31,112 @@
 The shell is also a useful way to explore Python's functionality; feel
 free to just use it as a desk calculator to get used to it.
 
-No Brackets
+Whitespace
+==========
+
+While not syntactically enforced by many languages, whitespace is
+semantically meaningful during the reading and writing of code.  Take
+the following example of C-like code::
+
+    if (someVar == 1)
+        doSomething();
+
+The line ``doSomething();`` is indented after the ``if`` statement to
+indicate that it should only be done if the statement it's below is
+true.  Given this, consider what the following code does::
+
+    if (someVar == 1)
+        doSomething();
+        doSomethingElse();
+
+It's clear from the use of whitespace that ``doSomethingElse();``
+should also only be executed if the statement it's indented under is
+true, but this is not the case for C-like languages.  Indeed, the
+programmer must add additional code to tell the compiler what he or
+she means::
+
+    if (someVar == 1) {
+        doSomething();
+        doSomethingElse();
+    }
+
+Why does the programmer have to write more code to tell the computer
+something it should already be able to understand from the use of
+whitespace?
+
+This is actually a violation of the `Don't Repeat Yourself`_ (DRY)
+principle popularized by Andy Hunt and Dave Thomas.  Because unneeded
+extra work is required when moving from a single-line clause to a
+multiple-line clause, it's a constant source of errors in C-like
+languages, and many stylistic rules and arguments have been spawned as
+a result of this mistake in language design.
+
+Python is one of the few languages that takes the simpler and more
+humane approach: whitespace has a consistent semantic meaning to the
+humans who write code, so the computer should take this into account
+when it processes the code.  This reduces the burden on the programmer
+from having to repeat their intent in multiple different ways.
+
+So, you won't see any brackets in Python.  Instead, if a statement ends
+with a colon, the next statement needs to be indented and begins a new
+block.  The block ends as soon as a line is encountered that's
+unindented at least one level, like so::
+
+    if someVar == 1:
+        doSomething()
+        doSomethingElse()
+    else:
+        doOtherThing()
+
+Also note that Python doesn't use semicolons, which is yet another
+language feature that reduces the cognitive burden on the programmer.
+
+.. _`Don't Repeat Yourself`: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself
+
+Expressions
 ===========
 
-Python doesn't have brackets; instead, indentation alone is used to
-lay out blocks of code.  For instance:
+Python's expression syntax is much like that of JavaScript, or any
+C-like language for that matter:
+
+    >>> 9 & 1              # Bitwise operations
+    1
+    >>> 2 << 2             # Shifting
+    8
+    >>> 5 >= 3             # Comparisons
+    True
+    >>> 8 + 2 * (3 + 5)    # Arithmetic
+    24
+    >>> 1 == 1             # Equivalence
+    True
+
+Some C-like expression constructs have been substituted for more
+readable alternatives:
 
-    >>> if 1 == 2:
-    ...   print "Something very strange is going on."
-    ...   print "Right now."
-    ... else:
-    ...   print "The world is fine."
-    The world is fine.
+    >>> not True           # 'not' instead of '!'
+    False
+    >>> True and True      # 'and' instead of '&&'
+    True
+    >>> True or False      # 'or' instead of '||'
+    True
+
+But there's some elements of C-like expressions that aren't supported,
+because they tend to be more trouble than they're worth.  The ``++``
+and ``--`` unary assignment operators aren't part of the
+language, and nor is JavaScript's ``===`` comparison operator.
 
-No Semicolons
-=============
+Some constructs that can be used in expressions in C-like languages
+can only be used in statements in Python:
 
-As with JavaScript, semicolons are actually optional.  Whereas always
-using semicolons is the accepted stylistic standard in JS, however,
-it's the reverse in Python: don't ever use them.
+    >>> a = 5              # Assignment works in statements.
+    >>> a += 1             # Add-assignment works in statements.
+    6
+
+    >>> if a = 1:          # But you can't assign in an expression.
+    ...     pass
+    Traceback (most recent call last):
+    ...
+    SyntaxError: invalid syntax
 
 Functions
 =========
@@ -64,7 +151,25 @@
     >>> foo(5)
     foo called with parameter: 5
 
-As in Javascript, they're first-class citizens and can be passed
+Unlike JavaScript, though, it's not possible to call them with fewer
+or more arguments than they'd expect:
+
+    >>> foo()
+    Traceback (most recent call last):
+    ... 
+    TypeError: foo() takes exactly 1 argument (0 given)
+
+Though it is possible to provide defaults for arguments:
+
+    >>> def bar(x, y=1, z=5):
+    ...   return x + y + z
+
+And it's also possible to specify arguments using keywords:
+
+    >>> bar(1, z=6)
+    8
+
+As in JavaScript, they're first-class citizens and can be passed
 around as parameters to other functions and so forth.
 
 Closures
@@ -124,8 +229,8 @@
 
     >>> mytuple = ("hello",)   # Without the comma, it'd just be a string.
 
-It's not possible for there to be "holes" in Python arrays like there
-are in Javascript ones, though:
+It's also not possible for there to be "holes" in Python arrays like
+there are in Javascript ones:
 
     >>> a = [1, 2, 3]
     >>> del a[1]               # Deletes '2'
@@ -154,6 +259,16 @@
     >>> "hello"[:4]      # Just like "hello".slice(0,4) in JS
     'hell'
 
+    >>> [1, 2, 3][1:2]   # Works on lists, too!
+    [2]
+
+If the datatype is mutable, you can even assign to slices:
+
+    >>> a = [1, 2, 3, 4]
+    >>> a[1:3] = [5]
+    >>> a
+    [1, 5, 4]
+
 Dictionaries
 ============