diff options
Diffstat (limited to 'wired/old/published/Webmonkey/Python/python_intro.txt')
-rw-r--r-- | wired/old/published/Webmonkey/Python/python_intro.txt | 336 |
1 files changed, 336 insertions, 0 deletions
diff --git a/wired/old/published/Webmonkey/Python/python_intro.txt b/wired/old/published/Webmonkey/Python/python_intro.txt new file mode 100644 index 0000000..f7e5de0 --- /dev/null +++ b/wired/old/published/Webmonkey/Python/python_intro.txt @@ -0,0 +1,336 @@ +Python is an easy-to-learn yet powerful scripting language. Whether you need to automate some part of you desktop workflow, create a website or build a full-fledged desktop application, Python will fit the bill. + +While both powerful and flexible, Python has the advantage of being much simpler than other scripting languages like Perl and results in far cleaner, more maintainable code than PHP. + +Python embraces object-oriented programming and offers an easy syntax for packaging, distributing and importing code, which makes it easy to take advantage of all the great open source python projects that are free available on the web. + +And, because it's a high-level language, Python has high-level data types built in -- arrays, dictionaries and many more. Python also supports all the operators you're likely familiar with from other language like if/else, while and for loops (though Python's for loops may be slightly different than what you've used elsewhere). + +##Python - What is it?## + +So, what is this whole Python business all about? + +Python was created by Guido van Rossum (who now works for Google, which uses Python extensively in its various web services). The name comes from Monty Python's Flying Circus, not the reptile, which is why many Python tutorials, this one included, feature shameless Monty Python puns. + +Python is a compiled application that can be installed in a variety of environments -- your desktop, a web server, embedded devices like your phone and more. Python can run on all the major platforms and comes pre-installed on Mac OS X and most Linux distros. + +There are two main ways to use Python, either by starting it directly from the command line or by calling an external script. Obviously the later method is much more flexible unless you're just doing a quick one-time program. + +If you're running Python on a web server there are a variety of ways to serve the scripts, from a cgi-bin folder just as you would with a Perl script, to faster more robust options like mod_python or mod_wsgi, two Apache modules that call on the Python interpreter to process your pages. + +Before we dig in, you should know about a site called [http://www.python.org/ Python.org]. Python is an open-source language, and Python.org is its control center, with [http://docs.python.org/ref/ref.html extensive reference material], additional tutorials and [http://docs.python.org/lib/lib.html a complete library] of all the elements available in Python. + +##Installing Python## + +Python code can be written as scripts and saved in text files with the .py extension, however there's also a shell interpreter that makes it very easy to get started just by typing <code>python</code> in your shell prompt. For now that's what we'll be using to show some of the basic language principles. + +Go ahead and fire up a terminal window and type <code>python</code>. If you have Python installed, you'll get a message like this: + +<pre> +Python 2.4.4 (#1, Oct 18 2006, 10:34:39) +[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin +Type "help", "copyright", "credits" or "license" for more information. +>>> +</pre> + +The three angle brackets are the standard Python command prompt which lets you know you're now using the Python interpreter. Any time you see code snippets written with the >>> you know the author is referring to the Python shell. + +If typing <code>python</code> in your shell didn't load the interpreter, you'll need to install Python. You can [http://www.python.org/download/ download the source] from the Python website or use one of the many package installers available for most platforms. + +As of this writing the latest version of Python is 2.5.2. Mac OS X and most Linux distros ship with Python 2.5. Windows users may need to install the latest version. + +Although Python 2.5 offers some nice new features over previous versions, for backwards compatibility reasons we'll stick to concept and tools that will work with Python 2.3 and greater. + +##Python Differences## + +For the most part Python behaves much like PHP, Perl, Ruby and other language you may be familiar with, however, there are some important and noteworthy differences. + +Perhaps the most obvious (and Python aficionados would argue, important) is that line breaks and indentions in your code have meaning in Python. Whereas PHP and others use a semicolon or other mark to designate the end of a line, Python sees all new lines as, well, new lines. + +Also where PHP and others use angle brackets to enclose code blocks, Python merely wants the code indented. + +Python forces you to properly indent code blocks and eschews end-of-line punctuation like PHP's semicolons in favor of simple line breaks. + +This has some import consequences. First and foremost, it makes Python code much easier to read. The structure of a Python script is a snap to figure out at first glance. Even if you have no idea what the code is doing, you can tell how it does it just by glancing at it. + +Python forces neat, well structured code, but it also forces you pay more attention to how you write your code. Consider the following two code snippets, which do very different things: + +def myfunction(): + if x == y: + return True + +def myfunction(): + if x == y: + return true; + +In the first code block our return statement is indented, and therefore within the if statement. In the second code block we didn't indent the return statement so that function always returns true, regardless of our if test (technically that second function would generate an error because Python expects an indented block after the colon). + +###Spaces versus Tabs### + +As the joke goes, the most popular way to write Python code is to indent with spaces. The second most popular way to write Python is with tabs. Most good text editors have an "entab/detab" function which can convert tabs to spaces and vice versa. + +The important thing is to be consistent. <b>Don't mix tab and space indenting in the same script!</b> Doing so will cause Python to throw an error and your code won't execute. + + +##Getting Started## + +Assuming you've got Python installed, fire up a shell window and type <code>python</code> to start the interpreter. Here's a simple script, just type <code>print 'hello world'</code> and hit return. + +<pre> +>>> print 'hello world' +hello world +>>> +</pre> + +Any time you want feedback from Python, use the <code>print</code> statement. As with any language, Python has built-in tools for doing common things, like in this case, print something out. + +Let's create some variable assignments and play around with some of Python's built-in types. We'll start by creating a string and playing around with it: + +<pre> +>>> x = 'spam' +>>> x[0] +'s' +>>> x[:1] +'s' +>>> x[1:] +'pam' +</pre> + +The first line just assigns x the value of "spam." Python is dynamically typed language, that is, there's no need to tell Python that x is going to be a string, it will figure that out at run time. + +It's also worth noting that Python is a strongly typed language, which means that "types" are always enforced. If you try to treat x as a number after you've already assigned it a string value Python will throw an error. In order to do that you'd have the explicitly recast x as a number. + +The next line <code>x[0]</code> shows how Python's treats strings -- much like a list (array) with each letter being like an element of the list. The <code>x[:1]</code> is an example of Python's slicing methods. The basic syntax is <code>variable[start:end]</code>. Slice indices always start with 0 and by default, omitting first index defaults that value to zero, while omitting second index defaults to the size of the string/list. + +here's few more operators: + +<code> +>>> x = 'spam' +>>> len(x) +4 +>>> y = x +>>> x = 'knights' +>>> y +'spam' +>>> +</code> + +Note that last sequence, we set a new variable y to the value of x and then change the value of x, but y stays the same. In other words y doesn't become a reference to x, it's a new variable with the value of x. Just because x changes doesn't mean y will as well. + +##More Strings, Formatting and Comments## + +We already know that line endings and indentions matter in Python, but what about within Python strings? For instance: + +>>> long_string = "Python is the best language ever, but I will\n\ +... keep that to myself and not start flame wars on Slashdot.\n\ +... Just because\n\ +... I think it's the best doesn't mean everyone needs to" +>>> long_string +"Python is the best language ever, but I will\nkeep that to myself and not start flame wars on Slashdot.\nJust because\n\tI think it's the best doesn't mean everyone needs to" +>>> print long_string +Python is the best language ever, but I will +keep that to myself and not start flame wars on Slashdot. +Just because + I think it's the best doesn't mean everyone needs to +>>> + +So what's going on here? How did we write multiple lines if line breaks mean the end of a line? The trick is the <code>\</code> which tells Python to ignore the end of the line. Also note the difference between calling our variable directly and printing it using the <code>print</code> statement. + +Escaping line breaks with a backslash also works within normal Python code blocks as well, not just within strings. In some cases it can help make your code more readable. + +The other Python concept that might look a bit funny to those coming from other languages is the string formatting tools. In other languages the common way to add data to a string is to just concatenate the string like this: + +"the beginning of a sentence" + variable_data + "the end of a sentence" + +While this will work in Python as well, there is a far more elegant way to write it using the <code>%s</code> operator. + +>>> b = 'beginning of a sentence' +>>> e = 'end of a sentence' +>>> v = 'variable data' +>>> '%s, %s, %s' %(b, v, e) +'beginning of a sentence, variable data, end of a sentence' + +The other nice thing about using <code>%s</code> is that it will force the value to a string, whereas straight concatenation won't. [note that there is a similar <code>%f</code> for inserting numbers into a string]. + +One last note on strings: to create comments in Python you have several options. Like Perl the hash mark can be used for inline comments, like so: + + x = 'spam' #initial value + +The other way to comment your code is with triple quotes. In other words: + +<code> +def superfunction(params): + """ The super function can do things you've only dreamed of""" + print 'Spam!' +</code> + +As it happens, this particular use of comment as the beginning of a function definition is special type in Python known as a doc string and you can even access it in your code. + +##List Dictionaries and Tuples## + +Arrays are one of the most useful constructs in a language, they allow you store and manipulate compound data. Python has three distinct objects for handling compound data types: lists, dictionaries and tuples. + +Perhaps the most useful is the list, which pretty straightforward, it's a list of data: +<code> +>> l = ['spam', 'ham', 314, 23] +>>> l +['spam', 'ham', 314, 23] +>>> l[0] +'spam' +</code> +As you can see, we construct a list using square brackets and assign it to the variable <code>l</code>. From there we can get the whole list by calling it directly, or access individual list members using a zero-based index, just like we did earlier with the strings. + +Also note that lists can mix together any data-type you want, here we used both number and strings, but you could add anything you want, even other lists. Lists have some other neat tricks up their sleeve. For instance let's try replacing some items: + +<code> +>>> l[0] = 'monkey' +>>> l +['monkey', 'ham', 314, 23] +>>> # slightly more complex replace method: +>>> l[0:2] = ['banana', 18] +>>> l +['banana', 18, 314, 23] +>>> # we can even insert the list itself: +>>> l[:0] = l +>>> l +['banana', 18, 314, 23, 'banana', 18, 314, 23] +</code> + +As you can see lists are mutable, you can do pretty much anything you want with them. As you start getting more comfortable with Python you'll find yourself using lists all the time. Be sure to check out the Python docs for [http://www.python.org/doc/current/lib/typesseq-mutable.html a complete list of list methods]. + + +###Dictionaries### + +Another useful compound data-type is the dictionary. A Python dictionary is created using curly brackets, like so: +<code> +>>> d = {"monkey":"banana", "spam":"eggs"} +>>> d +{'monkey': 'banana', 'spam': 'eggs'} +</code> +The name:value pairs are the dictionaries keys and values. To access dictionary data you just call the key using the square bracket syntax like lists, but instead of a number you'll use a key: +<code> +>>> d['monkey'] +'banana' +>>> #try to call something that doesn't exist and you'll get a KeyError. +>>> #this means that you can't get keys from values: +>>> d['banana'] +Traceback (most recent call last): + File "<stdin>", line 1, in ? +KeyError: 'webmonkey' +</code> +Like lists, dictionaries are mutable, which means you can add key:value pairs whenever you want. Just keep in mind two things: you can't have duplicate keys, the second will overwrite the first, and key names are case sensitive. + +>>> #Start with an empty dictionary +>>> d = {} +>>> # add two key:value pairs +>>> d['monkey'] = 'banana' +>>> d['spam'] = 'eggs' +>>> d +{'monkey': 'banana', 'spam': 'eggs'} +>>> #try to add another monkey key +>>> d['monkey'] = 'wrench' +>>> # oops, we overwrought the original value: +>>> d +{'monkey': 'wrench', 'spam': 'eggs'} +Example 3.2. Modifying a Dictionary + +As with lists dictionaries can store any type of data your want. Even dictionary keys can be a variety of data-types, so long as the data-type is immutable (i.e. strings, integers, tuples, etc). And you can mix and match data-types within the same dictionary. + +For more details on what sort of manipulations you can perform with dictionaries, check out [http://www.python.org/doc/current/lib/typesmapping.html the Python documentation page]. + +###Tuples### + +What the #$@# is a Tuple? Tuple is an immutable list. Once you stick data in a Tuple you can never change it. Since you can't change anything about them, it wouldn't make much sense to have any methods for Tuples so there aren't any. + +Tuples are created using parentheses and you can access data in a Tuple just as we did with a lists, but beyond that there isn't a lot to tell. + +>>> t = ('spam', 'ham', 314, 23) +>>> t +('spam', 'ham', 314, 23) +>>> t[0] +'spam' +>>> t[0] = 'eggs' +Traceback (most recent call last): + File "<stdin>", line 1, in ? +TypeError: object does not support item assignment + + +So you might be wondering, what's the point of a Tuple if it behaves like a crippled list? The answer is that Tuples are much much faster than lists. If you have data that you know isn't going to change, put it in a Tuple. + +Tuples are also handy for data that should not need to be changed, like in a private class method or the like. Using a tuple instead of a list prevents outside tampering. + +##Assigning multiple values at once## + +One of the cool tricks in Python is ability to assign multiple variables values at the same time. For instance: + +>>> m,s = 'monkey','spam' +>>> m +'monkey' +>>> s +'spam' + +But this gets much more powerful with more complex data. For instance we can also do this: + +>>> a = ('b','c','d') +>>> (x, y, z) = a +>>> x +'b' + +Another variation on this theme is the built-in range function. Consider this: + +>>> range(7) +[0, 1, 2, 3, 4, 5, 6] +>>> (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday) = range(7) +>>> Sunday +0 + +##The Python <code>for</code> loop## + +Of all the things that will give you pause when learning Python, the <code>for</code> loop is probably the most obvious difference from other language. Rather than iterating over a progression of numbers, or allowing you to define both the iteration step and halting condition (as in C), Python's for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. + +The best way to understand how it works is to look at an example. In this case we'll create a list and then loop through the values. + +>>> a = ['spam', 'ham', 314, 23] +>>> for b in a: +... print b +... +spam +ham +314 +23 + +Note that the variable <code>b</code> is totally arbitrary, you can name the iterator whatever you would like (preferably what makes sense, for instance, if your list holds a punch of urls, you might write <code>for url in urls:</code>) + +We'd use the exact same technique to loop through a tuple, a string and other data-types, but what about dictionaries? That's a little bit different, but we can borrow some of the multiple values techniques we mentioned above to help out. + +>>> dict = {'guido':'python','larry':'perl','rasmus':'php'} +>>> for key in dict: +... print key, 'created', dict[key] +... +larry created perl +rasmus created php +guido created python + +It's worth noting that while Python's for loops are just as capable as those in other language, chances are you won't use them as much. Part of the reason is that Python offers something called a list comprehension which does something similar to what you might need a for loop for in other language. + +For instance suppose you have list of numbers and you want to perform some multiplication function on them. You might be tempted to do something like this: + +>>> li = [0, 1, 1, 2, 3, 5] +>>> for num in li: +... new_num = num * 2 +... new_list = [new_num] +etc + +But in Python it's much easier to just use a list comprehension. Here's the same function in one line: + +>>> li = [0, 1, 1, 2, 3, 5] +>>> [2*num for num in li] +[0, 2, 2, 4, 6, 10] +>>> # or if you want to store the results: +>>> new_li = [2*num for num in li] +>>> new_li +[0, 2, 2, 4, 6, 10] + +It may take a while to wrap your head around, but list comprehensions are very very powerful and well worth spending some time with so you understand not just how they work, but when and where to use them. + +Have a look at the [http://www.python.org/doc/current/tut/node7.html#SECTION007140000000000000000 official Python tutorial] for more information.
\ No newline at end of file |