[Tutor] new to python... [transition from perl->python]

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Sat, 2 Jun 2001 23:06:23 -0700 (PDT)


On Sun, 3 Jun 2001, sean schroeder wrote:

> I am a heavy Perl programmer and very interested in learning how to
> use Python - mainly for the following type of tasks:
> 		- cgi scripting
> 		- databases  access (mysql, oracle, postgress)
> 		- Reading and writing to files.

You'll definitely want to look at the topic pages on Web (CGI) scripting
and Databases here:

    http://python.org/topics/web/
    http://python.org/topics/database/

There are introductions there that will help get you up to speed on this.



> Last Note:  is there a Python compiler avaialable  for either Linux or NT?

Not yet.  I believe that a compiler for Microsoft's .NET platform is being
developed, so you might want to ask on comp.lang.python for more details
on that.



> So, with the above information in mind. I am looking for a resource
> that will help me easily make the transition.  More specifically I am
> looking for a PDF or book that basically shows/explains how to mvoe
> from Perl to Python as painlessly as possible.

Hmmm... try the official tutorial:

    http://python.org/doc/current/tut/tut.html

It assumes experience with a programming language like Perl or C++.  The
other tutorials on:

    http://python.org/doc/Intros.html

should also be helpful for you.  After you go through the official
tutorial, you should be ok.




Here are a list of some caveats in the transition from Perl to Python:


String interpolation --- Python doesn't automatically interpolate into
strings: there's a separate string formatting operator '%'.  So, for
example, in Perl, we could do something like this:

###
$name = "sean";
$sentence = "Hello $name";
###

The equivalent in Python would be:

###
name = "sean"
sentence = "Hello %s" % (name)
###

The official tutorial touches on this under the "Fancier Output
Formatting" topic:

http://python.org/doc/current/tut/node9.html#SECTION009100000000000000000





Integer Division --- Python has a distinction between integer division and
floating point division.  It's different in Perl, since Perl uses floating
point throughout.

###
half = 1 / 2
###

won't assign .5 to our 'half' variable in Python --- we'll get 0.




String manipulation --- Python's uses the idea that strings are immutable,
so, if we have something like:

###
myline = "Hello, I am a line with a trailing newline.\n"
###

to get rid of that last character, we don't have Perl's chomp() operator,
but we do have something similar:

###
myline = myline.strip()   ### Take off the trailing newline.
###

An alternative way to take off that last character includes:

###
myline = myline[:-1]      ## Keep all but the last character
###

which you can think of almost like chop().

When you're working with Python, keep the immutability of strings in mind.  
The tutorial will cover how do to string manipulation, so we'll leave the
details there.



Those are the big ones I can think of at the moment.

Hope this helps!