tags:

views:

1853

answers:

9

As a complete beginner with no programming experience, I am trying to find beautiful Python code to study and play with. Please answer by pointing to a website, a book or some software project.

I have the following criterias:

  • complete code listings (working, hackable code)
  • beautiful code (highly readable, simple but effective)
  • instructional for the beginner (yes, hand-holding is needed)

I've tried learning how to program for too long now, never gotten to the point where the rubber hits the road. My main agenda is best spelled out by Nat Friedman's "How to become a hacker".

I'm aware of O'Reilly's "Beautiful Code", but think of it as too advanced and confusing for a beginner.

+1  A: 

I've seen How to Think Like a Computer Scientist recommended in many blogs.

Alexander Kojevnikov
+3  A: 

The Python project itself maintains a nice list of beginner's guides.

vog
+6  A: 

Read the Python libraries themselves. They're working, hackable, elegant, and instructional. Some is simple, some is complex.

Best of all, you got it when you downloaded Python itself. It's in your Python library directory. Nothing more to do except start poking around.

S.Lott
Thanks for the great advice. I've looked into the source and find it to be very much like what I looked for. I like that expressions, functions and etcetera is spelled out, and nicely commented. I'll need a couple of session before I start changing anything here though. :-)
Aputsiaq
S.Lott: Do you recommend any module/modules in particular? Or should I just start with ones I use frequently (e.g. ctypes, unittest, doctest)?
MikeRand
@MikeRand: If course you should start with the ones you use -- you already have a sense of what they are and how they work. Digging into unfamiliar territory may be too much to learn at once.
S.Lott
+2  A: 

Beautiful is so hard to define, there's no real answer to this question. Your best advice to follow what Nat says in the post you linked:

  1. Download the source code to the program you want to change
  2. Untar it on your hard drive
  3. Get it to build and run
  4. Open the source code in an editor
  5. Find the part of the code that you need to change to make the program do what you want it to do
  6. Make the changes you need to make to the code and test it to make sure it works
  7. Run the diff -u command and email the output to the mailing list

There is no point looking for beautiful code. Just look at and fix bugs in projects that you use (Django & Twisted might be good candidates).

Harley
Yeah, I'd come to the somewhat the same conclusion; i.e. read a lot more code and figure out how things fit together. Start with changing a line and see what happens, just for the fun of it - later on trying to improve the thing. Most important: just spend actual time with the code!
Aputsiaq
+13  A: 

Buy Programming Collective Intelligence. Great book of interesting AI algorithms based on mining data and all of the examples are in very easy to read Python.

The other great book is Text Processing in Python

Lou Franco
when i saw this question, it took me .1 seconds to think of this book - too bad the answer was already here :) voted up!
ryw
I've ordered both of them now and additionally "Python Cookbook" by Alex Martelli (et al).
Aputsiaq
Keep in mind there are some unpythonic idioms used in that book's code. But it still may be good for exposure to the lanaguage and it is readable.
Greg
+1  A: 

I personally think that reading good code won't work until you have a firm understanding of the language, especially of its idioms. First, I recommend the basic Wikibook "Non-Programmer's Tutorial for Python" to start out. If most of that makes sense, you have a good understanding of the basics already.

After that, I recommend Dive into Python. You'll see a lot of other people recommending this book, because it's comprehensive and free. You'll learn a lot of language specific idioms in Dive into Python, especially in the first few chapters. As you're reading it, try to do basic programs using the techniques Mark Pilgrim shows.

Dive into Python gets into specific modules later in the book. That will probably get a little boring, and when it does, you might want to look at code. I don't feel qualified to rank the code used by these, but Django and Deluge are both bigger projects that will show you the organization of large programs. Though they will probably be overwhelming unless you take the time to really attack them one piece at a time and get a firm understanding.

Martin W
A: 

I'd recommend you review Exaile music player for linux. It includes a lot of practically useful things like plugins, lambda, decorators, settings manager, gui (using GTK+) and much more.

Exaile source code is not an ideal but will give you enough helpful information and basic Python coding concepts.

Sergei Stolyarov
+3  A: 

Just do it.

Seriously, you're never going to learn to be a good programmer until you write some programs. First you'll write bad programs, then you'll fix them, then you'll write better ones, etc...

If you aren't insatiably motivated to try coding, then maybe it isn't for you. One way to get motivated is to get a job that requires you to code... for me, there's nothing like having my salary and pride on the line to get me working :)

Dan
You're right about just getting started, but I've also been into the situation too many times now where I've read introductory material and writing "Hello World" in C++/Java/Python/PHP without going anywhere useful for real. I think the reason is I never read much really useful code.
Aputsiaq
What do you *want* to write? If you don't have some goal for what you want to program, you won't learn any programming language well enough to do practical things.
Dan
Thanks Dan, I'm aware that you're hitting a soft spot. :) A project I have in mind is adding features onto a todo-list program found on Showmedo (#1830010). Could be a turning-point just to start now.To quote Aristotle: "For the things we have to learn before we can do, we learn by doing."
Aputsiaq
Sounds good, give that project a shot. Sorry, I don't mean to poke you where it hurts ;-). But you'll be glad for it after you've written some code. Another thing to try: look around for tasks that are tedious, and see if you can write programs to automate them. I am lazy, so I do this a bunch.
Dan
+1  A: 

I've learned quite a bit of beautiful and useful Python from O'Reilly's Python Cookbook. http://oreilly.com/catalog/9780596001674/

I've also learned much from ActiveState's Python Recipe's web page. http://code.activestate.com/recipes/langs/python/

David Poole
I got Python Cookbook a few months ago and I agree, the sort of efficient, commented, walk-through code included is very relevant.
Aputsiaq