Jump into Python, Part 1

Pythoneer

Wednesday, April 20. 2005

Jump into Python, Part 1

Beforehand a huge thanks to Mark Pilgrim, his "Dive into Python" is a great starting point for programmers who want to learn Python. As an experienced programmer you can skip some stuff. But I read every single word, simply because I was scared to miss anything important. And sometimes the little side notes are most astonishing and worth being read.

Because I know that there is simply too much stuff in such a tutorial and that I forget exectly those that I wanted to remember, I've copy+pasted some stuff that I found interesting and that I felt would be good to have close at hand.

All the following are just quotes from http://diveintopython.org that simply feel good to read :-)

Good stuff

everything in Python is an object. Strings are objects. Lists are objects. Functions are objects. Even modules are objects.

indentation is a language requirement and not a matter of style

Python gives you an added incentive: the doc string is available at runtime as an attribute of the function

Python does not support in-line assignment, so there's no chance of accidentally assigning the value you thought you were comparing

A negative index accesses elements from the end of the list counting backwards. The last element of any non-empty list is always li[-1].

You can get a subset of a list, called a “slice”, by specifying two indices. li[0:3]

In Python, and and or perform boolean logic as you would expect, but they do not return boolean values; instead, they return one of the actual values they are comparing. This can really reduce the code size and the potential errors and and and ...

In Python 2.2 and later, you can inherit classes directly from built-in datatypes like dict

Good to know and to remember, because I am sure I will need to look that up

In a boolean context the following applies

  • 0 is false; all other numbers are true.
  • An empty string ("") is false, all other strings are true.
  • An empty list ([]) is false; all other lists are true.
  • An empty tuple (()) is false; all other tuples are true.
  • An empty dictionary ({}) is false; all other dictionaries are true.
  • The and-or trick, bool and a or b, will not work like the C expression bool ? a : b when a is false in a boolean context. Solution: (1 and [a] or [b])[0]
Lists
  • Lists can also be concatenated with the + operator. list = list + otherlist has the same result as list.extend(otherlist). But the + operator returns a new (concatenated) list as a value, whereas extend only alters an existing list. This means that extend is faster, especially for large lists.
  • li += ['two'] is equivalent to li.extend(['two']).
  • li = [1, 2] * 3 is equivalent to li = [1, 2] + [1, 2] + [1, 2]
Tuples
  • A tuple can not be changed in any way once it is created.
  • Tuples Have No Methods
  • You can't find elements in a tuple. Tuples have no index method.
  • Tuples are faster than lists.
  • Tuples can be used as keys in a dictionary (because they are immutable), so a tuple of lists can not be used!
  • (user,) - the comma is required when defining a tuple with one element

Strings

  • String formatting in Python uses the same syntax as the sprintf function in C.
  • join works only on lists of strings; it does not do any type coercion. Joining a list that has one or more non-string elements will raise an exception.
  • " ".join(s.split()) remove multiple white spaces and new lines, tabs etc. and replace by a single whitespace

Functions

  • in Python, arguments can be specified by name, in any order (i.e. info(a="x", b="y") or the same info(b="y", a="x"))
  • processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s) processFunc is now a function that collapses s if collapse is True

Any

  • In Python, you can use either == None or is None, but is None is faster.
  • In Java, you determine whether two string variables reference the same physical memory location by using str1 == str2. This is called object identity, and it is written in Python as str1 is str2. To compare string values in Java, you would use str1.equals(str2); in Python, you would use str1 == str2.
  • There are no constants in Python. Everything can be changed if you try hard enough.

Why?

Of course I have some questions. I guess all of them have been discussed already a hundered times in forums, anyway.

  • Why does an if, else and a def need a colon (:) at the end?
  • Why is deleting an element from a dictionary done by using a function del d[42] and why not d[42].del()? I thought everything was an object! d.clear() is also a method for clearing the dictionary ...
  • Slicing has nice shortcuts inside the brackets, why not allow li[] = 'new element' for appending a new element, instead of the append() method?
  • The mentioned tutorial says: "extend concatenates lists". Ok, so call it concat() instead of extend()! Geee.
  • Why does the first argument in every method have to be self? If Python would provide it by default one could save overhead and falling in the trap.
  • I read a couple of times: "... None, which is the Python null value." Why the hell is it not called Null?

I have not gotten through all the tutorial yet, but I will. And I will keep you informed about further complaints :-)

Posted by Wolfram in Programming at 13:13
Comment (1)
Trackbacks (0)

Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)

Overall a nice intro to python, but you contradicted yourself at one point. You said tuples cannot be changed once they have been created, then later you said everything can be changed if you try hard enough. I'm just imagining a frustrated programmer trying *really hard* to change an immutable object :P
#1 Aidzakr on 2006-05-11 16:32 (Reply)

Add Comment


To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

 
 

Calendar

Back November '08
Mon Tue Wed Thu Fri Sat Sun
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

Quicksearch

Categories

  • XML JavaScript
  • XML Mac stuff
  • XML Miscellaneous
  • XML Missing or not found
  • XML Music
  • XML Private
  • XML Programming


All categories

Archives

November 2008
October 2008
September 2008
Recent...
Older...

supersized.org

Owner login

supersized s9y blog hosting
Free s9y blog!

Disclaimer

Syndicate This Blog

XML RSS 0.91 feed
XML RSS 1.0 feed
XML RSS 2.0 feed
ATOM/XML ATOM 0.3 feed
ATOM/XML ATOM 1.0 feed
XML RSS 2.0 Comments

Top Referrers

www.google.com (8)
chamnapchhorn.blogspot.com (1)
search.live.com (1)
www.google.ca (1)
www.google.ch (1)
www.google.com.au (1)
www.google.de (1)

Top Exits

The owner of supersized.org is not responsible for any content on this page!