diff --git a/dip3.packed.js b/dip3.packed.js index b151f24..1e56b41 100644 --- a/dip3.packed.js +++ b/dip3.packed.js @@ -1 +1 @@ -function getPreContent(e){if(e.nodeType==3){return e.nodeValue.replace(//g,">")}if(e.nodeType!=1){return""}if(e.nodeName.toLowerCase()=="span"){return""}if(e.className&&e.className=="widgets"){return""}var d=e.childNodes;var c=d.length;var b="";for(var a=0;a"+getPreContent(b));a.document.close()}function toggleBlock(d){var c=document.getElementById(d);if(!c){return}var b=document.getElementById(d+"toggle");if(!b){return}var a=document.getElementById(d+"block");if(!a){return}if(b.firstChild.nodeValue=="show"){a.style.display="block";b.firstChild.nodeValue="hide"}else{a.style.display="none";b.firstChild.nodeValue="show"}}window.onload=function(){var b=document.getElementsByTagName("pre");for(var h=b.length-1;h>=0;h--){var k=b[h];if(k.className=="screen"||k.firstChild.nodeName.toLowerCase()=="code"){var a=k.id;if(!a){a="autopre"+h;k.id=a}k.innerHTML='
[hide] [open in new window]
'+k.innerHTML+"
"}}for(var h=b.length-1;h>=0;h--){var k=b[h];var d=k.getElementsByTagName("span");if(d.length==0){continue}var p=k.nextSibling;while(p&&(p.nodeType!=1)){p=p.nextSibling}if(p.nodeName.toLowerCase()!="ol"){continue}var q=p.getElementsByTagName("li");if(q.length!=d.length){alert("Number of callouts != number of callout list items:\n"+k.innerHTML);continue}for(var g=d.length-1;g>=0;g--){var m=d[g].parentNode;var f=q[g];m._li=f;f._div=m;m.onmouseover=function(){this.className="hover";this._li.className="hover"};f.onmouseover=function(){this.className="hover";this._div.className="hover"};m.onmouseout=function(){this.className="";this._li.className=""};f.onmouseout=function(){this.className="";this._div.className=""}}}var e=document.getElementsByTagName("table");for(var h=e.length-1;h>=0;h--){var o=e[h];var n=document.getElementById("skip"+o.id);if(!n){continue}var r=n.getElementsByTagName("li");var c=o.getElementsByTagName("tr");if(r.length==0){continue}for(var g=c.length-1;g>=1;g--){var s=c[g];var l=r[g-1];s._li=l;l._tr=s;s.onmouseover=function(){this.className="hover";this._li.className="hover"};l.onmouseover=function(){this.className="hover";this._tr.className="hover"};s.onmouseout=function(){this.className="";this._li.className=""};l.onmouseout=function(){this.className="";this._tr.className=""}}}}; \ No newline at end of file +function getPreContent(e){if(e.nodeType==3){return e.nodeValue.replace(//g,">")}if(e.nodeType!=1){return""}if(e.nodeName.toLowerCase()=="span"){return""}if(e.className&&e.className=="widgets"){return""}var d=e.childNodes;var c=d.length;var b="";for(var a=0;a"+getPreContent(b));a.document.close()}function toggleBlock(d){var c=document.getElementById(d);if(!c){return}var b=document.getElementById(d+"toggle");if(!b){return}var a=document.getElementById(d+"block");if(!a){return}if(b.firstChild.nodeValue=="show"){a.style.display="block";b.firstChild.nodeValue="hide"}else{a.style.display="none";b.firstChild.nodeValue="show"}}window.onload=function(){var p=document.getElementsByTagName("pre");for(var q=p.length-1;q>=0;q--){var c=p[q];if(c.className=="screen"||c.firstChild.nodeName.toLowerCase()=="code"){var n=c.id;if(!n){n="autopre"+q;c.id=n}var h="";var f=c.previousSibling;while(f&&(f.nodeType!=1)){f=f.previousSibling}if(f&&(f.nodeName.toLowerCase()=="p")&&(f.className=="download")){h=" "+f.innerHTML;f.parentNode.removeChild(f)}c.innerHTML='
'+c.innerHTML+"
"}}for(var q=p.length-1;q>=0;q--){var c=p[q];var b=c.getElementsByTagName("span");if(b.length==0){continue}var e=c.nextSibling;while(e&&(e.nodeType!=1)){e=e.nextSibling}if(e.nodeName.toLowerCase()!="ol"){continue}var s=e.getElementsByTagName("li");if(s.length!=b.length){alert("Number of callouts != number of callout list items:\n"+c.innerHTML);continue}for(var o=b.length-1;o>=0;o--){var d=b[o].parentNode;var t=s[o];d._li=t;t._div=d;d.onmouseover=function(){this.className="hover";this._li.className="hover"};t.onmouseover=function(){this.className="hover";this._div.className="hover"};d.onmouseout=function(){this.className="";this._li.className=""};t.onmouseout=function(){this.className="";this._div.className=""}}}var k=document.getElementsByTagName("table");for(var q=k.length-1;q>=0;q--){var u=k[q];var r=document.getElementById("skip"+u.id);if(!r){continue}var l=r.getElementsByTagName("li");var m=u.getElementsByTagName("tr");if(l.length==0){continue}for(var o=m.length-1;o>=1;o--){var a=m[o];var g=l[o-1];a._li=g;g._tr=a;a.onmouseover=function(){this.className="hover";this._li.className="hover"};g.onmouseover=function(){this.className="hover";this._tr.className="hover"};a.onmouseout=function(){this.className="";this._li.className=""};g.onmouseout=function(){this.className="";this._tr.className=""}}}}; \ No newline at end of file diff --git a/index.html b/index.html index fa10b92..1d01cd7 100644 --- a/index.html +++ b/index.html @@ -18,6 +18,7 @@ ul{list-style:none} diff --git a/native-datatypes.html b/native-datatypes.html index be72b7d..42f0d68 100644 --- a/native-datatypes.html +++ b/native-datatypes.html @@ -13,7 +13,7 @@ body{counter-reset:h1 2}

skip to main content -

+

Native datatypes

@@ -23,6 +23,7 @@ body{counter-reset:h1 2}
  • Diving in
  • Booleans
  • Numbers +
  • Lists +
  • Sets +
  • Dictionaries
  • None

    Diving in

    -

    A short digression is in order. Put aside your first Python program for just a minute, and let's talk about Python's native datatypes. These types are the foundation on which you will build all your future Python programs. +

    A short digression is in order. Put aside your first Python program for just a minute, and let's talk about datatypes. Every variable has a datatype, even though you don't declare it explicitly. Based on each variable's original assignment, Python figures out what type it is and keeps tracks of that internally.

    Python has many native datatypes. Here are the important ones:

    1. Booleans are either True or False. @@ -67,19 +87,80 @@ body{counter-reset:h1 2} >>> size < 0 True

      Numbers

      - -

      Integers

      - -

      Floating point numbers

      - -

      Fractions

      - -

      Complex numbers

      - -

      Common operations on numbers

      - -

      The math module

      - +

      FIXME +

      Lists

      +

      FIXME +

      Sets

      +

      FIXME +

      Dictionaries

      +

      One of Python's most important datatypes is the dictionary, which defines one-to-one relationships between keys and values. +

      +

      A dictionary in Python is like a hash in Perl 5. In Perl 5, variables that store hashes always start with a % character. In Python, variables can be named anything, and Python keeps track of the datatype internally. +

      +

      Creating a dictionary is easy. The syntax is similar to sets, but instead of values, you have key-value pairs. Once you have a dictionary, you can look up values by their key. +

      +>>> a_dict = {"server":"db.diveintopython3.org", "database":"mysql"}  
      +>>> a_dict
      +{'server': 'db.diveintopython3.org', 'database': 'mysql'}
      +>>> a_dict["server"]                                                  
      +'db.diveintopython3.org'
      +>>> a_dict["database"]                                                
      +'mysql'
      +>>> a_dict["db.diveintopython3.org"]                                  
      +Traceback (most recent call last):
      +  File "<stdin>", line 1, in <module>
      +KeyError: 'db.diveintopython3.org'
      +
        +
      1. First, you create a new dictionary with two elements and assign it to the variable a_dict. Each element is a key-value pair, and the whole set of elements is enclosed in curly braces. +
      2. 'server' is a key, and its associated value, referenced by a_dict["server"], is 'db.diveintopython3.org'. +
      3. 'database' is a key, and its associated value, referenced by a_dict["database"], is 'mysql'. +
      4. You can get values by key, but you can't get keys by value. So a_dict["server"] is 'db.diveintopython3.org', but a_dict["db.diveintopython3.org"] raises an exception, because 'db.diveintopython3.org' is not a key. +
      +

      Dictionaries do not have any predefined size limit. You can add new key-value pairs to a dictionary at any time, or you can modify the value of an existing key. Continuing from the previous example: +

      +>>> a_dict
      +{'server': 'db.diveintopython3.org', 'database': 'mysql'}
      +>>> a_dict["database"] = "blog"  
      +>>> a_dict
      +{'server': 'db.diveintopython3.org', 'database': 'blog'}
      +>>> a_dict["user"] = "mark"      
      +>>> a_dict                       
      +{'server': 'db.diveintopython3.org', 'user': 'mark', 'database': 'blog'}
      +>>> a_dict["user"] = "dora"      
      +samp class="prompt">>>> a_dict
      +{'server': 'db.diveintopython3.org', 'user': 'dora', 'database': 'blog'}
      +>>> a_dict["User"] = "mark"      
      +>>> a_dict
      +{'User': 'mark', 'server': 'db.diveintopython3.org', 'user': 'dora', 'database': 'blog'}
      +
        +
      1. You can not have duplicate keys in a dictionary. Assigning a value to an existing key will wipe out the old value. +
      2. You can add new key-value pairs at any time. This syntax is identical to modifying existing values. +
      3. The new dictionary item (key 'user', value 'mark') appears to be in the middle. In fact, it was just a coincidence that the elements appeared to be in order in the first example; it is just as much a coincidence that they appear to be out of order now. +
      4. Assigning a value to an existing dictionary key simply replaces the old value with the new one. +
      5. Will this change the value of the user key back to "mark"? No! Look at the key closely — that's a capital U in "User". Dictionary keys are case-sensitive, so this statement is creating a new key-value pair, not overwriting an existing one. It may look similar to you, but as far as Python is concerned, it's completely different. +
      +

      Dictionaries aren't just for strings. Dictionary values can be any datatype, including integers, booleans, arbitrary objects, or even other dictionaries. And within a single dictionary, the values don't all need to be the same type; you can mix and match as needed. Dictionary keys are more restricted, but they can be strings, integers, and a few other types. You can also mix and match key datatypes within a dictionary. +

      In fact, you've already seen a dictionary with non-string keys and values, in your first Python program. +

      SUFFIXES = {1000: ('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'),
      +            1024: ('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')}
      +

      Let's tear that apart in the interactive shell. +

      +>>> SUFFIXES = {1000: ('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'),
      +...             1024: ('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')}
      +>>> len(SUFFIXES)      
      +2
      +>>> SUFFIXES[1000]     
      +('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
      +>>> SUFFIXES[1024]     
      +('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')
      +>>> SUFFIXES[1000][3]  
      +'TB'
      +
        +
      1. As with lists and sets, the len() function gives you the number of items in a dictionary. +
      2. 1000 is a key in the SUFFIXES dictionary; its value is a tuple of eight items (eight strings, to be precise). +
      3. Similarly, 1024 is a key in the SUFFIXES dictionary; its value is also a tuple of eight items. +
      4. Since SUFFIXES[1000] is a tuple, you can address individual items in the tuple by their 0-based index. +

      None

      None is a special constant in Python. It is a null value. None is not False; it is not 0; it is not an empty string. Comparing None to anything other than None will always return False.

      None is the only null value. It has its own datatype (NoneType). You can assign None to any variable, but you can not create other NoneType objects. All variables whose value is None are equal to each other. @@ -101,193 +182,6 @@ body{counter-reset:h1 2} >>> x == y True -

      © 2001-4, 2009 ark Pilgrim, CC-BY-3.0 diff --git a/table-of-contents.html b/table-of-contents.html index 0da7b4d..b66a209 100644 --- a/table-of-contents.html +++ b/table-of-contents.html @@ -47,41 +47,23 @@ ul li ol{margin:0;padding:0 0 0 2.5em}

  • Native Python datatypes
      -
    1. Diving in -
    2. Booleans -
    3. Numbers -
        -
      1. Integers -
      2. Floating point numbers -
      3. Fractions -
      4. Complex numbers -
      5. Common operations on numbers -
      6. The math module -
      -
    4. Lists -
        -
      1. Creating new a list -
      2. Modifying a list -
      3. Searching a list -
      4. Deleting elements from a list -
      5. Common operations on lists -
      -
    5. Sets -
        -
      1. Creating a new set -
      2. Modifying a set -
      3. Deleting elements from a set -
      4. Common operations on sets -
      5. Frozen sets -
      -
    6. Dictionaries -
        -
      1. Creating a new dictionary -
      2. Modifying a dictionary -
      3. Deleting items from a dictionary -
      4. Common operations on dictionaries -
      -
    7. None +
    8. Diving in +
    9. Booleans +
    10. Numbers + +
    11. Lists +
    12. Sets +
    13. Dictionaries +
    14. None
  • Strings
      diff --git a/your-first-python-program.html b/your-first-python-program.html index 9472dec..80c11d7 100644 --- a/your-first-python-program.html +++ b/your-first-python-program.html @@ -22,6 +22,9 @@ body{counter-reset:h1 1}
      1. Diving in
      2. Declaring functions +
          +
        1. How Python's datatypes compare to other programming languages +
      3. Writing readable code
        1. Docstrings @@ -80,7 +83,7 @@ if __name__ == "__main__":

          Declaring functions

          Python has functions like most other languages, but it does not have separate header files like C++ or interface/implementation sections like Pascal. When you need a function, just declare it, like this:

          def approximate_size(size, a_kilobyte_is_1024_bytes=True):
          -

          Note that the keyword def starts the function declaration, followed by the function name, followed by the arguments in parentheses. Multiple arguments are separated with commas. +

          The keyword def starts the function declaration, followed by the function name, followed by the arguments in parentheses. Multiple arguments are separated with commas.

          Also note that the function doesn't define a return datatype. Python functions do not specify the datatype of their return value; they don't even specify whether or not they return a value. (In fact, every Python function returns a value; if the function ever executes a return statement, it will return that value, otherwise it will return None, the Python null value.)

          In some languages, functions (that return a value) start with function, and subroutines (that do not return a value) start with sub. There are no subroutines in Python. Everything is a function, all functions return a value (even if it's None), and all functions start with def. @@ -89,7 +92,7 @@ if __name__ == "__main__":

          In Java, C++, and other statically-typed languages, you must specify the datatype of the function return value and each function argument. In Python, you never explicitly specify the datatype of anything. Based on what value you assign, Python keeps track of the datatype internally.

          -

          How Python's Datatypes Compare to Other Programming Languages

          +

          How Python's datatypes compare to other programming languages

          An erudite reader sent me this explanation of how Python compares to other programming languages:

          statically typed language