role of semilcolon

Tim Peters tim_one at email.msn.com
Mon Apr 12 22:06:20 EDT 1999


[Reuben Sumner]
> In the python interpreter doing 'a=1;b=2;b=3' does what I would
> expect, three seperate assignments.  However ; doesn't seem to appear
> in the language reference except in the list of delimeters.  Is the
> above example formally valid python?

Yup, and the semicolon's formal role in the grammar is captured in the
non-terminal "stmt_list", which you'll find in the manual's chapter on
compound statements.

Note that you'll hardly ever see a semicolon in an experienced Python
programmer's code, though!  "a, b, c = 1, 2, 3" is more idiomatic for what
you did above, and in general Python is succinct so you can afford to write
code readably and still fit a major algorithm on a single screen.

> BTW as you can tell I am a newbie

Yes:  nobody else ever looks at the reference manual <wink>.

> but I am impressed with the ease at which I was able to implement
> some number theoretic algorithms (gcd, primaility testing, pollard
> rho factoring, jacobian...)

So let's pick on gcd.  Here's the bare-bones idiomatic way to spell that:

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

Close to what you did?  Finding the *fastest* way to write gcd in Python is
something I've been working on for years <wink>.

> Is there a big practical difference between using long numbers and
> the mpz package?  (is the latter faster)

Python's bigint implementation is straightforward, very compact (in code
size), and extremely portable.  It wasn't aiming at speed or completeness.
In contrast, the GNU GMP package has been labored over for years, with
hyper-optimized core assembler routines on most major platforms.  If you
need peak speed, use that.  At one time Andrew Kuckling combined the two,
but don't know how usable that still is; see the first link at:

     http://starship.python.net/crew/amk/python/code.html

big-ints-for-big-brains-ly y'rs  - tim






More information about the Python-list mailing list