Mercurial > python-for-js-programmers
changeset 10:58662d737f49
Added TODOs
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Thu, 05 Jun 2008 16:31:40 -0700 |
parents | eaf4ddc3b092 |
children | df25b4e145e1 |
files | PythonForJsProgrammers.txt |
diffstat | 1 files changed, 69 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/PythonForJsProgrammers.txt Thu Jun 05 16:11:22 2008 -0700 +++ b/PythonForJsProgrammers.txt Thu Jun 05 16:31:40 2008 -0700 @@ -108,7 +108,8 @@ If there's a function you're interested in learning more about, you can look at the built-in documentation metadata associated with the -object by querying its ``__doc__`` attribute: +object--known as the `docstring`--by querying the object's ``__doc__`` +attribute: >>> print "a string".join.__doc__ S.join(sequence) -> string @@ -216,7 +217,20 @@ >>> bar(1, z=6) 8 -As in JavaScript, they're first-class citizens and can be passed +You can also write documentation for functions by providing a string +immediately following the function signature: + + >>> def foo(): + ... "Does something useless" + ... pass + +As mentioned earlier, this string is called the `docstring`, and is +actually attached to the function object as its ``__doc__`` attribute. +Creating docstrings for your functions not only helps document your +code, but also makes it easier for Python users to interactively +explore your code, too. + +As in JavaScript, Functions are first-class citizens and can be passed around as parameters to other functions and so forth. Global Variables @@ -281,18 +295,30 @@ implementation-specific variables are conventionally preceded with one or two underscores. -Lists -===== +Sequences +========= -Lists are a lot like JavaScript arrays. Iterating through them is -easy: +Lists are a lot like JavaScript arrays: >>> mylist = ["hello", "there"] + +Iterating through them is easy: + >>> for i in mylist: ... print i hello there +Strings are just sequences of characters, so they can be used +similarly: + + >>> for c in "boof": + ... print c + b + o + o + f + Tuples are just like lists, only they're immutable and differentiated from lists by using parentheses instead of brackets: @@ -306,14 +332,45 @@ >>> mytuple = ("hello",) # Without the comma, it'd just be a string. -It's also not possible for there to be "holes" in Python arrays like -there are in Javascript ones: +It's also not possible for there to be "holes" in Python lists like +there are in Javascript arrays: >>> a = [1, 2, 3] >>> del a[1] # Deletes '2' >>> a [1, 3] +Control Flow +============ + +You've already seen examples of ``for``, ``if``, and ``if...else``. +Python also supports ``if...elif``: + + >>> if 1 == 2: + ... pass + ... elif 1 == 1: + ... print "Hooray!" + ... else: + ... print "Boo." + Hooray! + +It also supports ``while``: + + >>> while False: + ... print "This should never display." + +However, Python does not have a ``do...while`` loop. + +To loop through a range of numbers, you can use the ``range()`` +built-in function, which returns a list of numbers in the range you +specify: + + >>> for i in range(3): + ... print i + 0 + 1 + 2 + Indexing and Slicing ==================== @@ -432,3 +489,7 @@ in `PEP 8`_. .. _`PEP 8`: http://www.python.org/dev/peps/pep-0008 + +TODO: Mention TinyPy. +TODO: Mention "Batteries Included" +TODO: Mention generators, generator comprehensions/expressions, array comprehensions, other stuff lifted from Python by JS.