tags:

views:

327

answers:

6

I've got a good grasp on C, my first programming language. I know a reasonable number of tricks and techniques and have written quite a few programs, mostly for scientific stuff. Now I'd like to branch out and understand OOP, and Python seems like a good direction to go.

I've seen several questions on how to learn Python, but most of them were from folks who were looking to start programming for the first time. I don't need a tutorial that will tell me what a string is, but I do need one that can tell me how to make a string in Python. Any help on some good sources to look through? Bonus points if the source is free :)

+3  A: 

I'd recommend the book How to Think Like a Computer Scientist in Python. It really helped me get going in Python (now my favorite language) coming from Java, C and C++.

theycallmemorty
+11  A: 

I learned everything I know about Python from the official documentation: http://docs.python.org/

And it's free.

carl
Yes me too, go to the tutorial. As Python is fairly simple, you'll learn the language in one or two days: http://docs.python.org/tutorial/index.html
Eike
+6  A: 

dive into python is a good place to start

fire up an interpreter, IPython is even better than the plain Python interpreter

use dir() and help() to poke around

and don't forget to read through the official docs at least once

gnibbler
I came from PHP and found dive into python as a great resource. Very easy to follow and understand.
Jayrox
+1 for mentioning 'Dive into Python'.
Alceu Costa
+16  A: 

I knew C before I knew Python. No offence intended, but I don't think that your C knowledge is that big a deal. Unless you read very, very slowly, just set out to learn Python. It won't take that long to skim through the material you're familiar with, and it's not as if a Python tutorial aimed at C programmers will make you a better Python programmer - it might teach you things in a different order, is all, and raise some specific things that you would do in C but that you should not do in Python.

Strings in Python actually are somewhat different from strings in C, and they're used differently. I strongly recommend learning them "from scratch", rather than thinking about them in terms of their differences from C strings. For one thing, in Python 2 it's best not to use Python's "string" class to represent strings: there's a separate unicode string class and for practical Python apps (pretty much anything involving user data), you need that. (Python 3 fixes this, making the str class a unicode string). You need to establish a good working practice for unicode/byte data and decode/encode.

A common mistake when learning a second programming language, is to think "I know how to program, I just need to translate what I do in C into Python". No, you don't. While it's true that an algorithm can be basically the same in different languages, the natural way to do a particular thing can be completely different in different languages. You will write better Python code if you learn to use Python idiomatically, than if you try to write Python like a C programmer. Many of the "tricks" you know that make sense in C will be either pointless or counter-productive in Python. Conversely many things that you should do happily in a typical Python program, like allocating and freeing a lot of memory, are things that in C you've probably learned to think twice about. Partly because the typical C program has different restrictions from the typical Python program, and partly because you just have to write more code and think harder to get that kind of thing right in C than you do in Python.

If you're learning the language because you urgently need to program a system/platform which has Python but doesn't have C, then writing Python programs that work like C programs is a reasonable interim measure. But that probably doesn't apply to you, and even if it did it's not the ultimate goal.

One thing you might be interested to look at because of your C experience, is the Python/C API. Python is great for many things, but it doesn't result in the fastest possible computational core of scientific apps [neither does C, probably, but let's not go into FORTRAN for now ;-)]. So if you're aiming to continue with scientific programming through your move in Python, and your programs are typically memory-bus- and CPU-bound doing immense amounts of number-crunching (billions of ops), then you might like to know how to escape into C if you ever need to. Consider it a last resort, though.

You do need to understand Python reasonably well before the Python/C API makes much sense, though.

Oh yes, and if you want to understand OOP in general, remember later on to take a look at something like Java, Objective-C, C++, or D. Python isn't just an OO language, it's a dynamic OO language. You might not realise it from comparing just C with Python, but dynamic vs static types is a completely independent issue from the OOP-ness of Python. Python objects are like hashtables that allow you to attach new fields willy-nilly, but objects in many other OO languages store data in ways which are much more like a C struct.

Steve Jessop
Thanks for screwing my head on straight. Very helpful.
Spencer Nelson
It *is* best to use the `str` type for strings in Python 3.x.
dan04
@dan: good point.
Steve Jessop
+1 coming from purely or nearly purely imperative languages, it took me an insane amount of time to understand the functional aspects of Python. Until about 3 months into writing Python code on a fairly regular basis, I was essentially writing a weird hybrid of C and Java-style programs using Python syntax.
Chinmay Kanchi
@Chinmay: if it's not full of iterator comprehensions, it's not Python ;-)
Steve Jessop
@Steve: Agreed.
Chinmay Kanchi
+2  A: 

Diveintopython, official docs, "Learning python" by Mark Lutz(4th edition) is one of the best books.

creitve
+2  A: 

If you have a programming background, Python is pretty straightforward to pick up. The most onerous task is learning the libraries and idioms. The documentation at python.org is quite good and free. If you're doing number crunching you'll almost certainly want to become familiar with the numpy extension.

Brian Hawkins