rev 482 - trunk/docs/tutorial
SVN User <[email protected]> Fri, 14 May 2004 03:16:01 -0400
| Newsgroups | gmane.comp.lang.prothon.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: mark
Date: 2004-05-14 03:15:57 -0400 (Fri, 14 May 2004)
New Revision: 482
Added:
trunk/docs/tutorial/tutorial4.htm
Modified:
trunk/docs/tutorial/index.htm
trunk/docs/tutorial/tutorial3.htm
Log:
added lists, tuples and dictionaries to tutorial
Modified: trunk/docs/tutorial/index.htm
===================================================================
--- trunk/docs/tutorial/index.htm 2004-05-14 03:18:24 UTC (rev 481)
+++ trunk/docs/tutorial/index.htm 2004-05-14 07:15:57 UTC (rev 482)
@@ -54,51 +54,71 @@
<li><a href="tutorial1.htm#toption">The -t Option</a>
</ul>
</ul>
- <li>2. <a href="tutorial2.htm#intro">Introduction to Prothon</a>
- <ul>
- <li><a href="tutorial2.htm#comments">2.1 Comments</a>
- <ul>
- <li><a href="tutorial2.htm#hash">Hash symbol</a>
- <li><a href="tutorial2.htm#cstyle">C-style</a>
- </ul>
- <li><a href="tutorial2.htm#indrules">2.2 Indentation</a>
- <ul>
- <li><a href="tutorial2.htm">Indent Rule</a>
- <li><a href="tutorial2.htm#spctabs">Mixing Spaces and Tabs</a>
- </ul>
- <li><a href="tutorial2.htm#linecont">2.3 Line Continuations</a>
- <ul>
- <li><a href="tutorial2.htm#backslash">Backslash</a>
- <li><a href="tutorial2.htm#parens">Open Parentheses</a>
- <li><a href="tutorial2.htm#xtraind">Extra Indents</a>
- </ul>
- <li><a href="tutorial2.htm#dtva">2.4 Data Types, Variables, and
- Assignment</a>
- <ul>
- <li><a href="tutorial2.htm#dtva">Data Types</a>
- <li><a href="tutorial2.htm#mixtype">Mixed Types</a>
- <li><a href="tutorial2.htm#varass">Assignment</a>
- </ul>
- <li><a href="tutorial3.htm#numeric">2.5 Numeric Data Types</a>
- <ul>
- <li><a href="tutorial3.htm#ints">Integers</a>
- <li><a href="tutorial3.htm#floats">Floats</a>
- </ul>
- <li><a href="tutorial3.htm#strings">2.6 Strings</a>
- <ul>
- <li><a href="tutorial3.htm#quotes">Quotes</a>
- <li><a href="tutorial3.htm#escapes">Escape Sequences</a>
- <li><a href="tutorial3.htm#types">R and X Strings</a>
- <li><a href="tutorial3.htm#triple">Triple Quote Strings</a>
- <li><a href="tutorial3.htm#concat">Concatenation</a>
- <li><a href="tutorial3.htm#index"> Indexing</a>
- <li><a href="tutorial3.htm#slices">Slices</a>
- <li><a href="tutorial3.htm#negnum">Negative Numbers</a>
- <li><a href="tutorial3.htm#defind"> Default Indexes</a>
- </ul>
-
- </ul>
- </ul>
+
+ <li>2. <a href="tutorial2.htm#intro">Introduction to Prothon</a>
+ <ul>
+ <li><a href="tutorial2.htm#comments">2.1 Comments</a>
+ <ul>
+ <li><a href="tutorial2.htm#hash">Hash symbol</a>
+ <li><a href="tutorial2.htm#cstyle">C-style</a>
+ </ul>
+ <li><a href="tutorial2.htm#indrules">2.2 Indentation</a>
+ <ul>
+ <li><a href="tutorial2.htm">Indent Rule</a>
+ <li><a href="tutorial2.htm#spctabs">Mixing Spaces and Tabs</a>
+ </ul>
+ <li><a href="tutorial2.htm#linecont">2.3 Line Continuations</a>
+ <ul>
+ <li><a href="tutorial2.htm#backslash">Backslash</a>
+ <li><a href="tutorial2.htm#parens">Open Parentheses</a>
+ <li><a href="tutorial2.htm#xtraind">Extra Indents</a>
+ </ul>
+ <li><a href="tutorial2.htm#dtva">2.4 Data Types, Variables, and Assignment</a>
+ <ul>
+ <li><a href="tutorial2.htm#dtva">Data Types</a>
+ <li><a href="tutorial2.htm#mixtype">Mixed Types</a>
+ <li><a href="tutorial2.htm#varass">Assignment</a>
+ </ul>
+ <li><a href="tutorial3.htm#numeric">2.5 Numeric Data Types</a>
+ <ul>
+ <li><a href="tutorial3.htm#ints">Integers</a>
+ <li><a href="tutorial3.htm#floats">Floats</a>
+ </ul>
+ <li><a href="tutorial3.htm#strings">2.6 Strings</a>
+ <ul>
+ <li><a href="tutorial3.htm#quotes">Quotes</a>
+ <li><a href="tutorial3.htm#escapes">Escape Sequences</a>
+ <li><a href="tutorial3.htm#types">R and X Strings</a>
+ <li><a href="tutorial3.htm#triple">Triple Quote Strings</a>
+ <li><a href="tutorial3.htm#concat">Concatenation</a>
+ <li><a href="tutorial3.htm#index"> Indexing</a>
+ <li><a href="tutorial3.htm#slices">Slices</a>
+ <li><a href="tutorial3.htm#negnum">Negative Numbers</a>
+ <li><a href="tutorial3.htm#defind"> Default Indexes</a>
+ <li><a href="tutorial3.htm#strfunc">String Functions</a>
+ </ul>
+ <li><a href="tutorial4.htm#lists">2.7 Lists</a>
+ <ul>
+ <li><a href="tutorial4.htm#entlist">Entering a List</a>
+ <li><a href="tutorial4.htm#indlist">List Indexing</a>
+ <li><a href="tutorial4.htm#mutlist">Mutability</a>
+ <li><a href="tutorial4.htm#listassign">Assignment and del</a>
+ <li><a href="tutorial4.htm#listfunc">List Functions and !</a>
+ </ul>
+ <li><a href="tutorial4.htm#tuples">2.8 Tuples</a>
+ <ul>
+ <li><a href="tutorial4.htm#tucr">Entering a Tuple</a>
+ <li><a href="tutorial4.htm#tufun">Functions</a>
+ </ul>
+ <li><a href="tutorial4.htm#dicts">2.9 Dictionaries</a>
+ <ul>
+ <li><a href="tutorial4.htm#keyval">Keys and Values</a>
+ <li><a href="tutorial4.htm#keyrules">Rules about Keys</a>
+ <li><a href="tutorial4.htm#dictenter">Entering a Dictionary</a>
+ <li><a href="tutorial4.htm#dictmutable">Assignment and del</a>
+ <li><a href="tutorial4.htm#mutlist">Dictionary Functions and !</a>
+ </ul>
+ </ul>
</td>
</tr>
</table>
Modified: trunk/docs/tutorial/tutorial3.htm
===================================================================
--- trunk/docs/tutorial/tutorial3.htm 2004-05-14 03:18:24 UTC (rev 481)
+++ trunk/docs/tutorial/tutorial3.htm 2004-05-14 07:15:57 UTC (rev 482)
@@ -25,14 +25,14 @@
Page</a></td>
<td width="183" valign="middle" align="center" height="25"><a href="index.htm">Tutorial
Outline</a></td>
- <td width="139" valign="middle" align="center" height="25"><a href="tutorial2.htm">Next
+ <td width="139" valign="middle" align="center" height="25"><a href="tutorial4.htm">Next
Page</a></td>
</tr>
</table>
<br>
<table width="630" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffee" height="1707">
<tr>
- <td height="2286" valign="top">
+ <td height="2265" valign="top">
<p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="numeric"></a>2.5
Numeric Data Types</font></p>
<p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="ints"></a></font>Prothon
@@ -88,9 +88,9 @@
<pre>
\n new line
\t tab
- \\ backslash character
- \nnn octal value char
- \xNN hex value char</pre>
+ \\ backslash character
+ \nnn octal value char
+ \xNN hex value char</pre>
<p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="types"></a></font>There
are a number of special types of strings. If you prefix a string with
the letter r as in r"xxx", then escape sequences are not converted.
@@ -106,7 +106,7 @@
last type of string is useful for entering multiple lines of text. You
use three quotes instead of one and it will include line endings.</p>
<pre>x = """ line 1
- line 2 """</pre>
+ line 2 """\</pre>
<p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="concat"></a></font>Strings
can be added together (concatenated) just like numbers. You can actually
add a string on the left to anything on the right and Prothon will convert
@@ -146,6 +146,24 @@
abcdef
>>> print x[-2], x[-3:-1], x[3:], x[:2], x[:2:-1], x[-1:-1:-1], x[:]
XXX</pre>
+ <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="strfunc"></a></font>There
+ are many useful string functions, but we will point out just a few here:</p>
+ <pre>>>>Len("abc") # number of chars in string
+XXX
+>>>"abc".upper() # convert string to uppercase
+XXX
+>>>"ABC".lower() # converts string to lowercase
+XXX
+>>>"abc def".capitalize() # capitalize string
+XXX
+>>>"abc def".capWords() # capitalize words in string
+XXX
+>>>'<'+" x ".lStrip()+'>' # remove white space from left
+XXX
+>>>'<'+" x ".rStrip()+'>' # remove white space from right
+XXX
+>>>'<'+" x ".strip()+'>' # remove white space from sides
+XXX</pre>
</td>
</tr>
</table>
@@ -157,7 +175,7 @@
Page</a></td>
<td width="183" valign="middle" align="center" height="25"><a href="index.htm">Tutorial
Outline</a></td>
- <td width="139" valign="middle" align="center" height="25"><a href="description-2.htm">Next
+ <td width="139" valign="middle" align="center" height="25"><a href="tutorial4.htm">Next
Page</a></td>
</tr>
</table>
Added: trunk/docs/tutorial/tutorial4.htm
===================================================================
--- trunk/docs/tutorial/tutorial4.htm 2004-05-14 03:18:24 UTC (rev 481)
+++ trunk/docs/tutorial/tutorial4.htm 2004-05-14 07:15:57 UTC (rev 482)
@@ -0,0 +1,266 @@
+<html>
+<head>
+<title>Prothon Tutorial - 2.7</title>
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
+</head>
+
+<body bgcolor="#FFFFFF" text="#000000">
+<div align="center">
+ <table width="630" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFCC">
+ <tr>
+ <td height="121" width="109"><img src="../images/logo_left.jpg" width="109" height="135"></td>
+ <td valign="center" height="43">
+ <div align="center"><font size="5" face="Verdana, Arial, Helvetica, sans-serif">Prothon
+ Tutorial</font></div>
+ </td>
+ <td valign="center" height="121" width="106"><img src="../images/logo_right.jpg" width="109" height="135"></td>
+ </tr>
+ </table>
+ <br>
+ <table width="630" border="0" cellpadding="0" cellspacing="0">
+ <tr>
+ <td width="114" height="25" valign="top"> <a href="http://prothon.org">Prothon
+ Home</a></td>
+ <td width="194" valign="middle" align="center" height="25"><a href="tutorial3.htm">Previous
+ Page</a></td>
+ <td width="183" valign="middle" align="center" height="25"><a href="index.htm">Tutorial
+ Outline</a></td>
+ <td width="139" valign="middle" align="center" height="25"><a href="index.htm">Next
+ Page</a></td>
+ </tr>
+ </table>
+ <br>
+ <table width="630" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffee" height="1707">
+ <tr>
+ <td height="3521" valign="top">
+ <p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="lists"></a>2.7
+ Lists</font></p>
+ <p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="entlist"></a></font>A
+ Prothon list is a sequential list of objects. Each object can be of
+ any type and you can mix types in a list. You specify a list object
+ (yes, a list is also an object) as a pair of brackets with the items
+ seperated by commas. You may put an extra comma at the end for convenience.
+ Also lines automatically <a href="tutorial2.htm#parens">continue</a>
+ when a left bracket is present and the right bracket has not been typed
+ yet. The following are all legal lists:</p>
+ <pre>[1, 2, 3] # a simple list with 3 numbers
+[] # an empty list
+[1, 'abc', 3.5] # a list with mixed types
+[1, [2,3], 4] # a list in a list
+x = [ "one", # a list in multiple lines
+ "two",
+ "three", # extra comma for convenience
+ ]</pre>
+ <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="indlist"></a></font>The
+ items in a list can be <a href="tutorial3.htm#index">indexed</a> just
+ like the characters in a string. You can index them with a single number
+ with the first item at the zero position. You can access a slice with
+ a pair of numbers, where the first number indexes the first item and
+ the second number indexes one past the last item. You can use three
+ numbers where the third indicates a step amount and the sign indicates
+ a direction. All of this is identical to the way string indexing works.</p>
+ <pre>>>> list = [0,1,2,3,4,5]
+[0, 1, 2, 3, 4, 5]
+>>> print list[2], list[-1], list[:2], list[3:], list[-4:-2], list[::-1]
+2 5 [0, 1] [3, 4, 5] [2, 3] [5, 4, 3, 2, 1, 0]
+>>></pre>
+ <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="mutlist"></a></font>All
+ of the numeric types and the string type we discussed before are immutable
+ types. That means that you cannot change them once they are created.
+ When you say "x = 1 + 2" you are creating a new integer and
+ not modifying any existing integer. The list type is a mutable type.
+ This means you can change the object itself instead of just using it
+ to help create a new object. There are two ways to change a list object.
+ One is to assign a value to an indexed item or slice of the list, and
+ the second is to use a function that modifies the list.</p>
+ <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="listassign"></a></font>To
+ modify items in a list by assignment, just place an indexed list on
+ the left side of an assignment statement. You can also delete using
+ the "del" command:</p>
+ <pre>>>> list = [0,1,2,3,4,5]
+[0, 1, 2, 3, 4, 5]
+>>> list[1] = 111 # replace one item
+111
+>>> print list
+[0, 111, 2, 3, 4, 5]
+>>> list [2:] = [222,333] # replace a big slice
+[222, 333] # with a smaller one
+>>> print list
+[0, 111, 222, 333] # it is shorter now!
+>>> del list[0] # delete one item
+>>> print list
+[111, 222, 333]
+>>>>>> del list[1:] # delete a slice
+>>> print list
+[111]
+>>></pre>
+ <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="listfunc"></a></font>There
+ are a number of list functions, just like there are string functions.
+ Some of the list functions modify the list object itself (they mutate
+ the object). These functions all have a special feature in that their
+ name ends with an exclamation mark. This is to remind you that you are
+ changing the list object. When you write functions yourself, you should
+ always put an exclamation mark at the end of your function name if it
+ modifies the object also:</p>
+ <pre>>>> list=[1,4,3,5,2,0]
+[1, 4, 3, 5, 2, 0]
+>>> Len(list) # Len is the same as for strings
+XXX
+>>> list.sort!() # fast sort even on big lists
+[0, 1, 2, 3, 4, 5] # changes list and returns list
+>>> list.min()
+0
+>>> list.max()
+5
+>>> list.count(2) # count the number of 2's
+1
+>>> list.index(4) # find the index of the first 4
+4
+>>> list * 2 # does not change list
+[0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5]
+>>> list + [6,7] # does not change list
+[0, 1, 2, 3, 4, 5, 6, 7]
+>>> list.append!(6) # these change the list
+[0, 1, 2, 3, 4, 5, 6]
+>>> list.extend!([7])
+[0, 1, 2, 3, 4, 5, 6, 7]
+>>> list.insert!(2,99) # insert 99 at index 2
+[0, 1, 99, 2, 3, 4, 5, 6, 7]
+>>> list.remove!(5) # remove the first 5
+[0, 1, 99, 2, 3, 4, 6, 7]
+>>> list.pop!()
+7
+>>> list.pop!(2)
+99
+>>> print list
+[0, 1, 2, 3, 4, 6]
+>>> list.reverse!()
+[6, 4, 3, 2, 1, 0]
+>>></pre>
+ <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="tuples"></a>2.8
+ Tuples</font> </p>
+ <p>Tuples are almost exactly like lists except for one big difference.
+ They are immutable. This means you cannot change the items in a tuple
+ once the tuple has been created, you can only use them in expressions
+ on the right side of an assignment like strings and numbers.</p>
+ <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="tucr"></a></font>You
+ create a tuple like a list except you use parentheses instead of brackets.
+ Also, when there is only one item, you must follow it with a comma.</p>
+ <pre>>>> tuple = (1,2,3)
+(1, 2, 3)
+>>> tuple[1]
+2
+>>> (2,)
+(2)
+>>></pre>
+ <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="tufun"></a></font>You
+ can do anything with a tuple that you can do with a list except assign
+ to them on the left side of an assignment statement, use the del command
+ on them, or use any mutable function on them. These functions are easy
+ to tell because they are the ones that end with an exclamation mark.</p>
+ <pre>>>> tuple = (1,2,3)
+(1, 2, 3)
+>>> tuple + (4,5,6)
+(1, 2, 3, 4, 5, 6)
+>>> tuple * 2
+(1, 2, 3, 1, 2, 3)
+>>> tuple.min()
+1
+>>> tuple.max()
+3
+>>> tuple.count(2)
+1
+>>> tuple.index(2)
+1
+>>></pre>
+ <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="dicts"></a>2.9
+ Dictionaries</font> </p>
+ <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="keyval"></a></font>A
+ dictionary is a mutable (changeable) collection of objects like the
+ list, but the objects are grouped into pairs of objects and there is
+ no order to them. Each pair consists of a key and a value. Whenever
+ you store anything into the dictionary you must specify both a key and
+ a value. To retrieve a value, you specify just the key, and it looks
+ up the value by finding the pair with the matching key. There cannot
+ be two keys in a dictionary that are equal to each other. If you attempt
+ to add a second key when that key is already present, the first key/value
+ pair is replaced with the new key/value pair.</p>
+ <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="keyrules"></a></font>The
+ definition of "equal" for keys varies from type to type but
+ for common types like integer and string the common-sense meaning is
+ valid. One rule about keys is that they must be immutable objects. One
+ of the main reasons tuples exist is so that they can be used as keys
+ since lists are mutable and cannot be used.</p>
+ <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="dictenter"></a></font>Dictionaries
+ are entered in Prothon code much like lists and tuples, except that
+ braces are used instead of brackets or parentheses, and items are entered
+ as key/value pairs seperated by a colon:</p>
+ <pre>>>> print {1:2, 3:'a', 'b':'c'}, {}, {1:2}
+{1:2, 'b':'c', 3:'a'} {} {1:2}
+>>></pre>
+ <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="dictmutable"></a></font>Dictionaries
+ are accessed to look up values much like lists and tuples, but any object
+ can be used as the index since the index is the key to be searched for.
+ There are no slices in dictionaries so there is no equivalent of multiple
+ indexes. Since dictionaries are mutable they can be on the left side
+ of an assignment statement and the del command can be used to remove
+ key/value pairs.</p>
+ <pre>>>> dict = {1:2, 3:'a', 'b':'c'}
+{1:2, 'b':'c', 3:'a'}
+>>> dict[1] # simple lookup
+2
+>>> dict[4] = 5 # adding a new key/value pair
+5
+>>> print dict
+{1:2, 'b':'c', 3:'a', 4:5} # not in any order
+>>> del dict['b'] # delete command
+>>> print dict
+{1:2, 3:'a', 4:5}
+>>> dict[4] = 99 # replacing 4:5 with 4:99
+99
+>>> print dict
+{1:2, 3:'a', 4:99}
+>>></pre>
+ <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="dictfunc"></a></font>Here
+ are some of the dictionary functions. Once again since dictionary is
+ mutable, there are functions that modify the dictionary objects themselves
+ and so their names end with exclamation marks:</p>
+ <pre>>>> dict = {1:2, 3:'a', 'b':'c'}
+{1:2, 'b':'c', 3:'a'}
+>>> dict.items() # get list of tuple pairs
+[(1, 2), ('b', 'c'), (3, 'a')]
+>>> dict.keys() # get list of keys
+[1, 'b', 3]
+>>> dict.values() # get list of values
+[2, 'c', 'a']
+>>> dict.get(1,99) # get value for key 1
+2 # if no key 1, default to 99
+>>> dict.get(4,99)
+99
+>>> dict.update!({4:5,6:7}) # merge other dictionary in
+{1:2, 'b':'c', 3:'a', 4:5, 6:7}
+>>> dict.popItem!() # remove arbitrary tuple pair
+(1, 2)
+>>> dict.popItem!()
+('b', 'c')
+>>> dict.clear!() # erase all contents
+{}
+>>></pre>
+ </td>
+ </tr>
+ </table>
+ <table width="630" border="0" cellpadding="0" cellspacing="0">
+ <tr>
+ <td width="114" height="25" valign="top"> <a href="http://prothon.org">Prothon
+ Home</a></td>
+ <td width="194" valign="middle" align="center" height="25"><a href="tutorial3.htm">Previous
+ Page</a></td>
+ <td width="183" valign="middle" align="center" height="25"><a href="index.htm">Tutorial
+ Outline</a></td>
+ <td width="139" valign="middle" align="center" height="25"><a href="index.htm">Next
+ Page</a></td>
+ </tr>
+ </table>
+</div>
+</body>
+</html>