klenwell information services : PywellScripts

Revision history for PywellScripts


Revision [3049]

Last edited on 2016-07-09 16:54:44 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
return to [[PyWell | Tutorial Index]]
~1) Modify your script to interactively prompt the user (you) to enter a list of numbers to add. (See documentation for [[http://docs.python.org/2/library/functions.html#raw_input | raw_input]] function.)
~- [[http://docs.python.org/2/library/functions.html | Python Builtin Functions]]*
~- [[http://docs.python.org/2/library/sys.html | Python sys Documentation]]
Deletions:
return to [[PyWell Tutorial Index]]
~1) Modify your script to interactively prompt the user (you) to enter a list of numbers to add. (See documentation for [[http://docs.python.org/2/library/functions.html#raw_input raw_input]] function.)
~- [[http://docs.python.org/2/library/functions.html Python Builtin Functions]]*
~- [[http://docs.python.org/2/library/sys.html Python sys Documentation]]


Revision [2367]

Edited on 2012-12-24 13:04:44 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
The first thing to know about Python scripts is that Python is whitespace-sensitive. That is: you don't define blocks of code using braces or semi-colons as in many traditional programming languages:
%%(c)
main()
{
printf("Hello World");
}
You use indentation to define code blocks. As a student of poetry and a neat freak, I like this because it gives Python code a more poetic feel and encourages you to keep your code clean and readable.


Revision [2352]

Edited on 2012-12-21 23:11:14 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Deletions:
----
Next Lessons: [[PywellModules Modules]], [[PywellExceptions Exceptions]]


Revision [2351]

Edited on 2012-12-21 23:10:00 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
~- [[http://docs.python.org/2/library/sys.html Python sys Documentation]]
Deletions:
~- [[http://docs.python.org/2/tutorial/ Official Python Tutorial]]


Revision [2350]

Edited on 2012-12-21 22:25:15 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
Note the ""<tt>if __name__ == "__main__":</tt>"" line at bottom. This is a somewhat idiomatic line that checks to see if the current file is the script being invoked. It says basically, "if this script is being run directly and not imported by another module, run what follows."
The ##sys## module provides one very useful attribute: ##sys.argv##. This is a list of command line arguments, beginning with the script path. When you run ##python /tmp/hello.py planet earth##, the ##sys.argv## arguments are: ##['/tmp/hello.py', 'planet', 'earth']##.
So ##sys.argv[1:]## uses slice notation (##[1:]##) to say: give me all the command line arguments except the first one (or script path). More on slice notation when we get to lists and sequences.
Make a copy of the ##hello.py## script named ##add.py## and modify it to add a series of numbers from the command line like so:
Deletions:
Note the ""<tt>if __name__ == "__main__":</tt>"" line at bottom. This is a somewhat idiomatic line that checks to see if the current file is the script being invoked. It says basically, "if this script is called from the command line, run what follows."
Make a copy of the ##hello.py## script named ##add.py## and modify it add a series of numbers from the command line like so:


Revision [2349]

Edited on 2012-12-21 22:11:48 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
The script provides a simple template for your python script. It imports one module from the Python standard library: ##sys##. (More on modules next time.)
Deletions:
The script provides a simple template for your python script. It import one module from the Python standard library: ##sys##. (More on modules next time.)


Revision [2346]

Edited on 2012-12-14 15:35:43 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
return to [[PyWell Tutorial Index]]
Deletions:
[[PyWell Tutorial Index]]


Revision [2345]

Edited on 2012-12-14 15:35:30 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
[[PyWell Tutorial Index]]
Deletions:
return to [[PyWell index]]


Revision [2342]

Edited on 2012-12-14 15:34:37 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
=====Pywell Lesson: Scripts=====
return to [[PyWell index]]
~- Introduce the ##sys## module
~- Introduce some of the idiosyncracies of Python
The script provides a simple template for your python script. It import one module from the Python standard library: ##sys##. (More on modules next time.)
* Note documentation version may differ from version of Python you're using.
Deletions:
=====Pywell Lesson: Interpreter=====
~- Introduce the sys module
~- Introduce some of the idiosyncracies of python
The script provides a simple template for your python script. It import one module from the python standard library: ##sys##. (More on modules next time.)
* Note documentation version may differ from version of python you're using.


Revision [2340]

Edited on 2012-12-14 15:32:05 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
~1) Modify your script to interactively prompt the user (you) to enter a list of numbers to add. (See documentation for [[http://docs.python.org/2/library/functions.html#raw_input raw_input]] function.)
Deletions:
~- Use python on the command line
===Interactive Interpreter===
To learn python you obviously need to be able to run python. On linux, it comes pre-installed, so if you're using a linux os such as Ubuntu, as this tutorial assumes, you should be ready to get started.
One of my favorites features of python is the interactive interpreter. It allows you to easily play with python. Try it for yourself. Open a terminal and type python like so:
python
The interpreter will tell you what version it's running and give you a prompt for input:
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
At the prompt, try typing the following:
>>> print 'hello world'
>>> sum(range(5))
Input a valid command and you'll usually get some kind of output in return.
My favorite thing about the interactive interpreter is it allows you to quickly answer questions about the language without having to waste time looking up stuff online or -- worse yet, like I used to have to do before the internet -- in books! For instance, how do you do exponentiation in Python: ""<tt>3^2</tt>"" or ""<tt>3**2</tt>""?
>>> 3^2
>>> 3**2
One very useful builtin python function is ##dir##. It will give you a list of all the attributes and methods attached to an object. See the references section below for other builtin python functions. To learn a little about strings in python, try this:
>>> name = "guido"
>>> dir(name)
>>> name.capitalize()
===Scripts===
~1) Evidently the ##^## character is not used for exponentiation. What does it do?
~2) Modify your script to interactively prompt the user (you) to enter a list of numbers to add. (See documentation for [[http://docs.python.org/2/library/functions.html#raw_input raw_input]] function.)


Revision [2338]

The oldest known version of this page was created on 2012-12-14 15:10:19 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]