Mastering Python

G

Gerald

Hi ,Im a BSc4 Maths/Computer Science student.Unfortunately my
curriculum did not include Python programming yet I see many vacancies
for Python developers.I studied programming Pascal,C++ and Delphi.So I
need to catch up quickly and master Python programming.How do you
suggest that I achieve this goal?Is python platform independent?What
is the best way?And how long would it take before I can develop
applications using python?Can you recommend websites that feature a
gentle introduction to Python?
 
M

Marc 'BlackJack' Rintsch

Can you recommend websites that feature a gentle introduction to Python?

If you already know programming in general, `Dive Into Python`_ might be a
good starting point. And of course the tutorial in the Python
documentation.

_Dive Into Python: http://www.diveintopython.org/

Ciao,
Marc 'BlackJack' Rintsch
 
B

Bruno Desthuilliers

Gerald a écrit :
Hi ,Im a BSc4 Maths/Computer Science student.Unfortunately my
curriculum did not include Python programming yet I see many vacancies
for Python developers.I studied programming Pascal,C++ and Delphi.So I
need to catch up quickly and master Python programming.How do you
suggest that I achieve this goal?Is python platform independent?What
is the best way?And how long would it take before I can develop
applications using python?Can you recommend websites that feature a
gentle introduction to Python?

Most of your questions are answered on python.org. May I suggest that
you start there ?

Briefly:
* "mastering" a language takes years, whatever the language.
* the first step is of course learning the language !-)
* given your background, I'd suggest first the official Python
tutorial, then diveintopython. Reading this ng might help too.
* Yes, Python is (mostly) platform-independent - at least as long a
you don't use platform-dependent modules
* An average programmer may become productive with Python in a matter
of days - but really taking advantage of Python's power is another story.


If you like programming, chances are you'll enjoy Python. So welcome on
board !-)

HTH
 
P

Paul McGuire

Hi ,Im a BSc4 Maths/Computer Science student.Unfortunately my
curriculum did not include Python programming yet I see many vacancies
for Python developers.I studied programming Pascal,C++ and Delphi.So I
need to catch up quickly and master Python programming.How do you
suggest that I achieve this goal?Is python platform independent?What
is the best way?And how long would it take before I can develop
applications using python?Can you recommend websites that feature a
gentle introduction to Python?

Stop thinking about *how* to start and *just start*. Python is pretty
intuitive, especially if you have other language background to relate
to. Download the Python dist for your platform (Linux? probably
already there - Windows? binary installers from python.org or
activestate will install in a snap). Run through the first few pages
of any of the dozen or more online tutorials to start getting your
fingernails dirty. You'll need a text editor with integrated building
- I find SciTE and PyScripter to be good for beginners (and I still
use SciTE after 4 years of Python programming).

You know C++ and Pascal? You already know the basic if-then-else,
while, and for control structure concepts. Here are some C++-to-
Python tips:
- There's no switch statement in Python. Make do with cascading if/
elif/else until you come across the dict dispatch idiom.
- There's no '?' operator in Python. If you download the latest
version (2.5), there is an equivalent "x if y else z" which would map
to "y ? x : z" using the ternary operator. But lean towards explict
readability vs. one-liner obscurity at least for a few days.
- Forget about new/delete. To construct an object of type A, call A's
constructor using "newA = A()". To delete A, let if fall out of
scope, or explicitly unbind the object from the name "newA" with "newA
= None".
- Forget about "for(x = 0; x < 10; x++)". Python loops iterate over
collections, or anything with an __iter__ method or __getitem__
method. This is much more like C++'s "for(listiter = mylist.first();
listiter != mylist.end(); ++listiter)". To force a for loop to
iterate 'n' times, use "for i in range(n):". The range built-in
returns the sequence [0, 1, 2, ..., n-1]. Don't commit this beginner's
blunder:
list1 = [ 1, 2, 3 ]
for i in range(len(list1)):
# do something with list1
Instead do:
for elem in list1:
# do something with elem, which points to each element of
# list1 each time through the loop
If you really need the list index, use enumerate, as in:
for i,elem in enumerate(list1):
print "The %d item of the list is %s" % (i,elem)
(Hey, check out those string formatting placeholders, they borrow
heavily from C's printf notation. Oh, they don't teach that anymore,
and you used iostreams in C++ instead? Bummer.)
- Forget about braces {}'s. For some reason, this is a big deal for
some people, but give it a chance. Just indent code as you would
normally, and leave out the braces. Personally I set my editor to
replace tabs with spaces, this is a style choice - but do NOT mix tabs
and spaces. In the end, you will find this liberating, especially if
you have ever been on a project that had to define a coding standard,
and spent way too much time (more then 30 seconds) arguing about
"where the braces should go."
- Don't forget the ()'s. To invoke a method on an object, you must
include the parens. This wont do anything:
a = "some string"
a = a.lower
You need this:
a = a.lower()
- Stop thinking about variables as addresses and storage locations,
and start thinking about them as values bound to names. Even so, I
still find myself using words like "assignment" and "variable", when
strictly I should be saying "binding" and "name".

What does Python have that C++ doesn't?
- The biggie: dynamic typing (sometimes called "duck typing").
Dynamic typing is a huge simplifier for development:
. no variable declarations
. no method type signatures
. no interface definitions needed
. no templating for collections
. no method overloading by differing argument type signatures
("Imagine there's no data types - I wonder if you can..."). What? No
static type-checking at compile time? Nope, not really. If your
method expects an object of type X, use it like an X. If it's not an
X, you may be surprised how often this is not a problem. For
instance, here's a simple debugging routine:
def printClassOf(x):
print x.__class__.__name__
Every object has the attribute __class__ and every class has the
attribute __name__. In C++, I'd have to go through extra contortions
*not* to type the variable x, probably call it something non-intuitive
like "void*". Or look at this example:
def printLengthOf(x):
print "Length of x is", len(x)
x could be any collection class, or user-defined class that is
sufficiently like a collection to support len (such as implementing
the __len__ method). This class doesn't even have to exist when you
write printLengthOf, it may come along years later.
- An interactive interpreter. Awfully handy for just trying things
out, without having to go through the compile/link/run cycle. Also
good for getting at documentation on built-in and custom objects and
methods - type "help(blah)" to get help on method or class blah.
- Language built-in types for list, tuple (a type of list that is
immutable), dict (akin to map<x,y> in the C++ STL), and set. Since
Python does dynamic typing, no need to templatize these collection
types, just iterate over them and use the objects in them.
. Lists look like [ 1, 2, 3, "ABC", [ 4,5 ] ]
. Tuples look like ( "Bob", "Smith", "12345 River St.", 52 )
. Dicts look like { "Bob" : 52, "Joe" : 24 }
. Sets look like set("A", "B", "C")
- Language built-in types for string and unicode
- Multiple variable assignment - you can unpack a list into individual
variables using:
a,b,c = 1,2,3
list1 = [ 4,5,6 ]
a,b,c = list1 (will assign 4 to a, 5 to b, and 6 to c)
Forget about the classic C chestnut to swap a and b:
a ^= b; b ^= a; a ^= b;
Just do:
a,b = b,a

- Compound return types - need 3 or 4 values returned from a
function? Just return them. No need for clunky make_pair<>
templates, or ad hoc struct definitions just to handle some complex
return data, or (ick!) out parameters. Multiple assignment will take
care of this:
def func():
return 4,5,6

a,b,c = func()

- Flexible and multiline quoting. Quoted string literals can be set
of using ""s, ''s, or triple quotes (""" """, or ''' '''). The triple
quote versions can extend to multiple lines.
- Built-in doc strings. If you have a function written like this:
def func():
"A function that returns 3 consecutive ints, starting with 4"
return 4,5,6
then typing "help(func)" at the interactive interpreter prompt will
return the string "A function that...". This is called the function's
docstring, and just about any object (class, function, module) can
have one.
- A huge library of common application modules. The latest version
includes support for the SQLite database.

And a part of the Python "getting it" that usually takes place in the
first hour or two of *just starting* is encapsulated in the Zen of
Python. Type "import this" at the interpreter command line, and
you'll see a list of basic concepts behind the language and its
design. It is true, there are some dorky in-jokes in there, but look
past them and pick up the nuggets of Python wisdom.

Wow, are you still reading? Quit wasting time and go download a
Python dist and get started already!

-- Paul
 
P

Paul McGuire

Hi ,Im a BSc4 Maths/Computer Science student.Unfortunately my
curriculum did not include Python programming yet I see many vacancies
for Python developers.I studied programming Pascal,C++ and Delphi.So I
need to catch up quickly and master Python programming.How do you
suggest that I achieve this goal?Is python platform independent?What
is the best way?And how long would it take before I can develop
applications using python?Can you recommend websites that feature a
gentle introduction to Python?

P.S. You'll get further faster with Python than with Java or Perl,
where you posted similar "how do I master language X?" requests. I've
used Java and suffered through Perl. Compared to Perl, Python a) has
less magic symbology/punctuation, and b) treats you more like an adult
("open x or die"? Come on!). Java syntax is so bloated you have to
tack on Eclipse plug-ins by the fistful to auto-generate the wrapper
junk code around the code you really wanted to get run.

Get thee to www.python.org, and get going, post haste!
 
D

Dave Hansen

Stop thinking about *how* to start and *just start*. Python is pretty

Indeed. Of all the fortune cookies I've eaten over the years, I've
saved (and taped to my monitor) only one fortune. It reads:

Begin...the rest is easy.

Regards,
-=Dave
 
B

BartlebyScrivener

Wow, are you still reading? Quit wasting time and go download a
Python dist and get started already!

I think you should extract that and spend twenty minutes tidying it up
and then publish it to the Python for Programmers page or make it a
downloadable .pdf.

http://wiki.python.org/moin/BeginnersGuide/Programmers

rd

"The chief contribution of Protestantism to human thought is its
massive proof that God is a bore."

--H.L. Mencken
 
P

paul

Paul said:
What does Python have that C++ doesn't?
- The biggie: dynamic typing (sometimes called "duck typing").
Dynamic typing is a huge simplifier for development:
. no variable declarations
. no method type signatures
. no interface definitions needed
. no templating for collections
. no method overloading by differing argument type signatures
("Imagine there's no data types - I wonder if you can..."). What? No
static type-checking at compile time? Nope, not really. If your
method expects an object of type X, use it like an X. If it's not an
X, you may be surprised how often this is not a problem.
But sometimes it is ;) Typical example: input and CGI/whatever. If one
element is checked you'll get a string, if you select multiple (i.e.
checkboxes) you'll get a list. Both support iteration

Now if you iterate over the result:

case 1, input -> "value1":
for elem in input:
#in real life we might validate here...
print elem

-> 'v' 'a' 'l' 'u' 'e' '1'

case 2, input -> ["value1", "value2"]
for elem in input:
print elem

-> "value1" "value2"

cheers
Paul

Disclaimer: I like python and I write tests but i wish unittest had
class/module level setUp()...
 
J

Jan Danielsson

Gerald said:
Hi ,Im a BSc4 Maths/Computer Science student.Unfortunately my
curriculum did not include Python programming yet I see many vacancies
for Python developers.I studied programming Pascal,C++ and Delphi.So I
need to catch up quickly and master Python programming.How do you
suggest that I achieve this goal?

1. Chose a project for yourself
2. Write it

I needed a distributed white board application for working on a World
Domination Plan with my friends, so I selected that as a good "get to
learn Python" project.
Is python platform independent?

Mostly..
What
is the best way?And how long would it take before I can develop
applications using python?

Depends on your learning skills, and what kind of applications we're
talking about.


--
Kind regards,
Jan Danielsson
------------ And now a word from our sponsor ------------------
Want to have instant messaging, and chat rooms, and discussion
groups for your local users or business, you need dbabble!
-- See http://netwinsite.com/sponsor/sponsor_dbabble.htm ----
 
D

Dennis Lee Bieber

Hi ,Im a BSc4 Maths/Computer Science student.Unfortunately my
curriculum did not include Python programming yet I see many vacancies
for Python developers.I studied programming Pascal,C++ and Delphi.So I

<blink><blink>

Pardon my surprise, but what school only covers simple Pascal,
Object Pascal (in the guise of Delphi), and C++ without at least
introducing other languages. Three fairly similar languages (with the
same similar flaws -- like needing to remember to put begin/end ({})
around multi-line blocks...

25 years ago I had FORTRAN, COBOL, assembly (these 3 were mandatory
in sequences of F, adv. F, assembly; C, adv. C, DBTG Database), Pascal,
BASIC, APL (electives) -- and even did a report on Ada (which is fun,
considering it was 8 months before Mil-Std 1815 was released <G>)

Ada was part of a program language design course, each student had
to do a report on some language -- so we had some quick summary of such
things as SNOBOL, LISP, Algol, PL/1, etc.

need to catch up quickly and master Python programming.How do you

Mastery and quickly are opposing terms <G> Took me 15 years on a job
using FORTRAN 77 and I still wouldn't have called myself a master. (I'm
more of a JoAT)
suggest that I achieve this goal?Is python platform independent?What
is the best way?And how long would it take before I can develop
applications using python?Can you recommend websites that feature a
gentle introduction to Python?

If on a Windows box... I'd probably suggest grabbing the ActiveState
installer; it gives a Python install, a simple IDE (PythonWin), the
Win32 extensions, Python documentation as Windows CHM format -- and
includes (well, it did in v2.4) the text of Dive Into Python.

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
D

Diez B. Roggisch

Dennis said:
<blink><blink>

Pardon my surprise, but what school only covers simple Pascal,
Object Pascal (in the guise of Delphi), and C++ without at least
introducing other languages. Three fairly similar languages (with the
same similar flaws -- like needing to remember to put begin/end ({})
around multi-line blocks...

Nowadays, you can get through CS graduate studies with only Java. Sad, but
true.

Diez
 
C

cga2000

I think you should extract that and spend twenty minutes tidying it up
and then publish it to the Python for Programmers page or make it a
downloadable .pdf.

Not sure where it should go.

Perhaps add a little nook and cranny link named "facts & advocacy".

I'm only an occasional user of Python and admittedly not as well-read as
many on this list .. but I must say that this is the first time I run
into something that so clearly states what makes Python stand apart from
those other .. er .. "difficult" .. should I say .. languages.

Make it a wiki so everyone can add his two cents?

Start a DocBook, LaTex, CSS.. competition and turn it into some kind of
marketing tool with all the trimmings?

In any event it would be a shame to waste it.

Thanks,
cga
 
A

Alex Martelli

Dennis Lee Bieber said:
Mastery and quickly are opposing terms <G> Took me 15 years on a job
using FORTRAN 77 and I still wouldn't have called myself a master. (I'm
more of a JoAT)

My favorite "Stars!" PRT, mind you -- but when some language interests
me enough, I do tend to "master" it... guess it's correlated with what
Brooks saw as the ideal "language lawyer" in his "surgical team"
approach, an intrinsic fascination with bunches of interconnected rules.


Alex
 
J

John Nagle

Alex said:
My favorite "Stars!" PRT, mind you -- but when some language interests
me enough, I do tend to "master" it... guess it's correlated with what
Brooks saw as the ideal "language lawyer" in his "surgical team"
approach, an intrinsic fascination with bunches of interconnected rules.

Python just isn't that complicated. The syntax is straightforward,
and the semantics are similar to most other dynamic object-oriented languages.
If you know Perl or Smalltalk or LISP or JavaScript, Python does about
what you'd expect.

Execution model: dynamic stack-type interpreter.
Memory model: reference counting with backup garbage collector.
Syntax: roughly C-like, with indentation for structure.
Typing model: dynamic only
Object model: class definitions with multiple inheritance.
Object structure: dictionary hash.
Exception model: explicit throw/try/catch
Theading model: multiprogramming in interpreter.
Safe memory model: Yes.
Closures: Yes.
Design by contract: No.

That's Python.

Biggest headache is finding out what doesn't work in the libraries.

John Nagle
 
A

Alex Martelli

John Nagle said:
...
Python just isn't that complicated. The syntax is straightforward,

Neither is/was Fortran 77, net perhaps of a few syntax quirks that were
easily avoided; yet few practitioners took the trouble (for example) of
learning what manners of data aliasing (e.g. between routine parameters
and/or data in COMMON blocks) were legal and which were not -- such a
simple rule (if you write data through an alias and read or write the
same memory through a different alias, you're breaking the rules of the
language and the compiler's free to make dragons fly out of your nose),
yet I've lost count of the number of times I've seen it broken during my
Fortran days (broken by professional programmers who used Fortran to
make a living and yet didn't care enough to know better, mind you -- I'm
not talking about accidental mistakes, which of course can easily happen
for a rule that the compiler need not enforce, but total ignorance of
this simple rule).


Alex
 
A

Aahz

Python just isn't that complicated. The syntax is straightforward,
and the semantics are similar to most other dynamic object-oriented
languages. If you know Perl or Smalltalk or LISP or JavaScript, Python
does about what you'd expect.

Yes and no. At the time I learned Python, I was a Perl expert (but not
a master), and I had a bunch of other languages under my belt (including
Fortran, Pascal, C, Ada). Nevertheless, for the first month of Python,
I found that I kept having problems because I tried to make Python fit
the mold of other languages rather than accepting it on its own terms.

(Admittedly, part of my problem was that I was learning Python under
duress -- I saw no reason to learn Yet Another Scripting Language.)

Then there are all the little odd corners of Python that stand in the
way of true mastery, like what happens with refcounts and exception
tracebacks.
 
P

Paul McGuire

I think you should extract that and spend twenty minutes tidying it up
and then publish it to the Python for Programmers page or make it a
downloadable .pdf.

http://wiki.python.org/moin/BeginnersGuide/Programmers

rd

"The chief contribution of Protestantism to human thought is its
massive proof that God is a bore."

--H.L. Mencken

I've added it to this page, see the last entry. Hope some find it
entertaining, if not informative.

-- Paul
 
B

Ben Finney

Paul McGuire said:
I think you should extract that and spend twenty minutes tidying
it up and then publish it to the Python for Programmers page or
make it a downloadable .pdf.

http://wiki.python.org/moin/BeginnersGuide/Programmers

I've added it to [the above wiki page], see the last entry.

I missed this the first time around. Thanks for letting us know you'd
done this.
Hope some find it entertaining, if not informative.

It looks like a great get-started guide for the many C++ refugees I
hope we can gain :)
 
B

Bruno Desthuilliers

Paul McGuire a écrit :
(snip)
- Don't forget the ()'s. To invoke a method on an object, you must
include the parens. This wont do anything:
a = "some string"
a = a.lower

It will actually do something: rebind name 'a' to the method lower() of
the string previously binded to 'a'

(snip)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top