[Tutor] Writing Scripts.
washakie at gmail.com
Mon Oct 25 01:27:51 CEST 2010
Here's a basic script, if you save this in a file called hello.py and
type 'python hello.py' at the prompt, or as others are saying using
the python launcher, you should get some output. hth, john
SCRIPT (include lines below here):
user = os.environ.get('USER')
print "Hello %s!" % (user)
==== NOW SOME COMMENTS ====
the first line is somewhat 'unix or linux' dependent, but it tells
your 'shell' to call the file using python
the next line "import os" is a python import statement, telling python
to import the os module
the next line "user os.environ.get('USER')" is call a method (get)
from the environ class of the os module and returns the name of the
logged in user to the variable 'user'
the next line is doing a few things. First it is calling the built in
function 'print' to print a string "Hello %s!". However, the "%s" does
something fancy. This is string substitution, so by having the
variable 'user' after the second '%' sign, it replaces "%s" with that
variable. Don't worry, it will come fast! Have fun with it!!
On Wed, Oct 20, 2010 at 6:13 PM, Autumn Cutter <autumnc90 at gmail.com> wrote:
> I just started learning Python last night and I'm a little stuck on how to write a Python script.
> I'm using Python 2.5.0 on a Mac OS X 10.6.4. I've tried using programs like Idle, Applescript and Textmate but I'm still unable to write a script and run it successfully. I'm wondering if I'm using the write program or if I understand correctly how to write and run scripts. Help! :(
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
Zope (Zope 2.9.7-final, python 2.4.4, linux2),
Procmail v3.22 2001/09/10
More information about the Tutor