Random passwords generation (Python vs Perl) =)

James Stroud jstroud at mbi.ucla.edu
Mon Jan 29 13:01:37 CET 2007

NoName wrote:
> Perl:
> @char=("A".."Z","a".."z",0..9);
> do{print join("", at char[map{rand @char}(1..8)])}while(<>);
> !!generate passwords untill U press ctrl-z
> Python (from CookBook):
> from random import choice
> import string
> print ''.join([choice(string.letters+string.digits) for i in 
> range(1,8)])
> !!generate password once :(

Make a loop.

> who can write this smaller

I won't write it for you, but I'll tell you how:

Write a function for the whole loop and the password generator and from 
then on it will be a one-liner (as a function call).

 > or without 'import'?

Yes, make sure you do the import inside the function.

If you follow my advice, you will be able to get as many passwords as 
you want with one line (and no spaces, even!):


It also reads much clearer than the perl version, don't you think?

By the way, you can also make a script. Call it "genpass", then at the 
command prompt, it will be far less than one line:

     % genpass

Or, if you are using DOS/Windows:

     C:> genpass

A lot of systems don't have a standard command called "g", so then you 
can turn it into *ONE LETTER*!!! That beats the pants off perl!

     % g

Or, of course, for DOS:

     C:> g

Good luck!


More information about the Python-list mailing list