klenwell information services : PywellInterpreter

Revision history for PywellInterpreter


Revision [3048]

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]]
~- [[http://docs.python.org/2/library/stdtypes.html#string-formatting | sprintf String Formatting]]
~- [[http://www.python.org/dev/peps/pep-0020/ | PEP 20 -- The Zen of Python]]
Deletions:
return to [[PyWell Tutorial Index]]
~- [[http://docs.python.org/2/library/stdtypes.html#string-formatting sprintf String Formatting]]
~- [[http://www.python.org/dev/peps/pep-0020/ PEP 20 -- The Zen of Python]]


Revision [2348]

Edited on 2012-12-14 15:44:52 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
To learn Python you obviously need to be able to run Python. This tutorial assumes you have it installed. 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.
Deletions:
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.


Revision [2347]

Edited on 2012-12-14 15:36:19 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Deletions:
* Note documentation version may differ from version of python you're using.


Revision [2344]

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


Revision [2341]

Edited on 2012-12-14 15:33:14 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
~- Use Python interactive interpreter
~- Introduce ##dir## function
~- [[http://www.python.org/dev/peps/pep-0020/ PEP 20 -- The Zen of Python]]
Deletions:
~- Use Python on the command line
~- Introduce `dir` function
===Interactive Interpreter===
~- [[http://docs.python.org/2/library/functions.html Python Builtin Functions]]*
~- [[http://docs.python.org/2/tutorial/ Official Python Tutorial]]
----
Next Lessons: [[PywellModules Modules]], [[PywellExceptions Exceptions]]


Revision [2339]

Edited on 2012-12-14 15:27:20 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
~- Use Python on the command line
~- Introduce `dir` function
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 simply type ##python##. The interpreter will tell you what version it's running and give you a prompt for input:
>>> range(5)
Type the following commands from in the Python interpreter:
>>> license
>>> 'version %.1f of %s supports %s-like formatting' % (2, 'python', 'sprintf')
>>> 1 % 2
>>> 2 % 2
>>> 2 / 0
>>> math.pow(5,2)
>>> import math
>>> math.pow(5,2)
>>> import this
Any ideas as to what's happening in each case?
~1) Is sprintf-type formatting supported in version 3 of Python?
~- [[http://docs.python.org/2/library/stdtypes.html#string-formatting sprintf String Formatting]]
Deletions:
~- Use python on the command line
~- Run a script
~- Introduce the sys module
~- Introduce some of the idiosyncracies of python
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:
===Scripts===
Here's a simple Python script:
%%(python)
#!/usr/bin/env python
"""
Pywell Python Tutorial: Scripts

A basic python script. This is the script's "docstring". Docstrings are
a great place to summarize your script or module and include licensing
information.
Copyright (c) 2012 MIT License: Tom Atwell
http://www.opensource.org/licenses/mit-license.php
"""
#
# Imports
#
import sys
#
# Globals / Constants
#
#
# Main
#
def main():
args = sys.argv[1:]
message = "hello %s" % (" ".join(args))
print message
if __name__ == "__main__":
main()
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 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."
Copy and paste that into a text file and save it as ##hello.py## somewhere on your computer -- say your ##tmp## dir. Now your can run it from your terminal by typing ##python## followed by the file path. For example (**make sure to adjust path if you used a different one**):
python /tmp/hello.py
Now try these:
python /tmp/hello.py world
python /tmp/hello.py planet earth
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:
python /tmp/add.py 1 2 3
~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 [2335]

Edited on 2012-12-11 13:36:47 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 called from the command line, run what follows."
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 follow."


Revision [2334]

Edited on 2012-12-11 12:34:56 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
Copy and paste that into a text file and save it as ##hello.py## somewhere on your computer -- say your ##tmp## dir. Now your can run it from your terminal by typing ##python## followed by the file path. For example (**make sure to adjust path if you used a different one**):
Deletions:
Copy and paste that into a text file and save it as ##hello.py## somewhere on your computer -- say your ##tmp## dir. Now your can run it from your terminal by typing ##python## followed by the file path:


Revision [2333]

Edited on 2012-12-11 12:31:54 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
python /tmp/hello.py
python /tmp/hello.py planet earth


Revision [2332]

Edited on 2012-12-11 12:30:36 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
%%(python)
#!/usr/bin/env python
"""
Pywell Python Tutorial: Scripts

A basic python script. This is the script's "docstring". Docstrings are
a great place to summarize your script or module and include licensing
information.
Copyright (c) 2012 MIT License: Tom Atwell
http://www.opensource.org/licenses/mit-license.php
"""
#
# Imports
#
import sys
#
# Globals / Constants
#
#
# Main
#
def main():
args = sys.argv[1:]
message = "hello %s" % (" ".join(args))
print message
if __name__ == "__main__":
main()


Revision [2331]

Edited on 2012-12-11 11:26:03 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
~- [[http://docs.python.org/2/tutorial/ Official Python Tutorial]]


Revision [2330]

Edited on 2012-12-11 11:15:52 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
=====Pywell Lesson: Interpreter=====
Deletions:
=====Pywell Lesson 1=====


Revision [2329]

Edited on 2012-12-11 11:15:02 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
~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.)
Deletions:
~2) Modify your to interactively prompt the user (you) for a name to say hello to. (See the x function).


Revision [2328]

Edited on 2012-12-11 11:09:46 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
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>""?
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:
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 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 follow."
Copy and paste that into a text file and save it as ##hello.py## somewhere on your computer -- say your ##tmp## dir. Now your can run it from your terminal by typing ##python## followed by the file path:
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:
~1) Evidently the ##^## character is not used for exponentiation. What does it do?
Deletions:
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: `3^2` or ""<tt>3**2</tt>""?
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:
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 the `if __name__ == "__main__":` 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 follow."
Copy and paste that into a text file and save it as `hello.py` somewhere on your computer -- say your `tmp` dir. Now your can run it from your terminal by typing `python` followed by the path name:
Make a copy of the `hello.py` script named `add.py` and modity it add a series of numbers from the command line like so:
~1) Evidently the `^` character is not used for exponentiation. What does it do?


Revision [2327]

Edited on 2012-12-11 11:07:12 by KlenwellAdmin [Replaces old-style internal links with new pipe-split links.]
Additions:
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: `3^2` or ""<tt>3**2</tt>""?
Deletions:
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: `3^2` or `3**2`?


Revision [2326]

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