diff --git a/about.html b/about.html index 0a60fdc..e654473 100644 --- a/about.html +++ b/about.html @@ -22,6 +22,7 @@ h1:before{content:""}
  • jQuery is served by Google AJAX Libraries API.
  • Other Javascript and CSS resources are minimized by YUI Compressor.
  • HTTP caching and other server-side options are optimized based on advice from YSlow. +
  • The text uses Unicode characters in place of graphics wherever possible.
  • The entire book was lovingly hand-authored in HTML 5. View-source; I typed that.

    © 2001–4, 2009 ark Pilgrim diff --git a/native-datatypes.html b/native-datatypes.html index 7790453..0c937e5 100644 --- a/native-datatypes.html +++ b/native-datatypes.html @@ -22,12 +22,20 @@ body{counter-reset:h1 2}

  • Diving in
  • Booleans
  • Numbers +
      +
    1. Coercing integers to floats and vice-versa +
    2. Common numerical operations +
    3. Fractions +
    4. Trigonometry +
    5. Numbers in a boolean context +
  • Lists
    1. Creating a list
    2. Slicing a list
    3. Adding items to a list
    4. Searching for values in a list +
    5. Lists in a boolean context
  • Dictionaries +
      +
    1. Creating a dictionary +
    2. Modifying a dictionary +
    3. Mixed-value dictionaries +
    4. Dictionaries in a boolean context +
  • None +
      +
    1. None in a boolean context +
  • Further reading

    Diving in

    @@ -90,6 +107,7 @@ body{counter-reset:h1 2}
  • Adding an int to an int yields an int.
  • Adding an int to a float yields a float. Python coerces the int into a float to perform the addition, then returns a float as the result. +

    Coercing integers to floats and vice-versa

    As you just saw, some operators (like addition) will coerce integers to floating point numbers as needed. You can also coerce them by yourself.

     >>> float(2)                
    @@ -115,6 +133,7 @@ body{counter-reset:h1 2}
     

    Python 2 had separate types for int and long. The int datatype was limited by sys.maxint, which varied by platform but was usually 232-1. Python 3 has just one integer type, which behaves mostly like the old long type from Python 2. See PEP 237 for details.

    +

    Common numerical operations

    You can do all kinds of things with numbers.

     >>> 11 / 2      
    @@ -141,7 +160,68 @@ body{counter-reset:h1 2}
     

    In Python 2, the / operator usually meant integer division, but you could make it behave like floating point division by including a special directive in your code. In Python 3, the / operator always means floating point division. See PEP 238 for details.

    -

    FIXME fractions, math module, numbers in a boolean context +

    Fractions

    +

    Python isn't limited to integers and floating point numbers. It can also do all the fancy math you learned in high school and promptly forgot about. +

    +>>> import fractions              
    +>>> x = fractions.Fraction(1, 3)  
    +>>> x
    +Fraction(1, 3)
    +>>> x * 2                         
    +Fraction(2, 3)
    +>>> fractions.Fraction(6, 4)      
    +Fraction(3, 2)
    +
      +
    1. To start using fractions, import the fractions module. +
    2. To define a fraction, create a Fraction object and pass in the numerator and denominator. +
    3. You can perform all the usual mathematical operations with fractions. Operations return a new Fraction object. 2 * (1/3) = (2/3) +
    4. The Fraction object will automatically reduce fractions. (6/4) = (3/2) +
    +

    Trigonometry

    +

    You can also do basic trigonometry in Python. +

    +>>> import math
    +>>> math.pi                
    +3.1415926535897931
    +>>> math.sin(math.pi / 2)  
    +1.0
    +>>> math.tan(math.pi / 4)  
    +0.99999999999999989
    +
      +
    1. The math module has a constant for π, the ratio of a circle's circumference to its diameter. +
    2. The math module has all the basic trigonometric functions, including sin(), cos(), tan(), and variants like asin(). +
    3. Note, however, that Python does not have infinite precision. tan(π / 4) should return 1.0, not 0.99999999999999989. +
    +

    Numbers in a boolean context

    +

    You can use numbers in a boolean context, such as an if statement. Zero values are false, and non-zero values are true. +

    +>>> def is_it_true(anything):             
    +...   if anything:
    +...     print("yes, it's true")
    +...   else:
    +...     print("no, it's false")
    +...
    +>>> is_it_true(1)                         
    +yes, it's true
    +>>> is_it_true(-1)
    +yes, it's true
    +>>> is_it_true(0)
    +no, it's false
    +>>> is_it_true(0.1)                       
    +yes, it's true
    +>>> is_it_true(0.0)
    +no, it's false
    +>>> import fractions
    +>>> is_it_true(fractions.Fraction(1, 2))  
    +yes, it's true
    +>>> is_it_true(fractions.Fraction(0, 1))
    +no, it's false
    +
      +
    1. Did you know you can define your own functions in the Python interactive shell? Just press ENTER at the end of each line, and ENTER on a blank line to finish. +
    2. In a boolean context, non-zero integers are true; 0 is false. +
    3. Non-zero floating point numbers are true; 0.0 is false. Be careful with this one! If there's the slightest rounding error (not impossible, as you saw in the previous section) then Python will be testing 0.0000000000001 instead of 0 and will return True. +
    4. Fractions can also be used in a boolean context. Fraction(0, n) is false for all values of n. All other fractions are true. +

    Lists

    Lists are Python's workhorse datatype. When I say “list,” you might be thinking “array whose size I have to declare in advance, that can only contain items of the same type, &c.” Don't think that. Lists are much cooler than that.

    @@ -263,6 +343,23 @@ ValueError: list.index(x): x not in list
  • The index() method finds the first occurrence of a value in the list. In this case, 'new' occurs twice in the list, in a_list[2] and a_list[4], but the index() method will return only the index of the first occurrence.
  • As you might not expect, if the value is not found in the list, Python raises an exception. This is notably different from most languages, which will return some invalid index (like -1). While this may seem annoying at first, I think you will come to appreciate it. It means your program will crash at the source of the problem instead of failing strangely and silently later. +

    Lists in a boolean context

    +

    You can also use a list in a boolean context, such as an if statement. +

    +>>> def is_it_true(anything):  
    +...   if anything:
    +...     print("yes, it's true")
    +...   else:
    +...     print("no, it's false")
    +...
    +>>> is_it_true([])             
    +no, it's false
    +>>> is_it_true(['a'])          
    +yes, it's true
    +
      +
    1. In a boolean context, an empty list is false. +
    2. Any list with at least one item is true. +
    , the len() function gives you the number of items in a dictionary.
  • 1000 is a key in the SUFFIXES dictionary; its value is a list of eight items (eight strings, to be precise).
  • Similarly, 1024 is a key in the SUFFIXES dictionary; its value is also a list of eight items.
  • Since SUFFIXES[1000] is a list, you can address individual items in the list by their 0-based index. +

    Dictionaries in a boolean context

    +

    You can also use a list in a boolean context, such as an if statement. +

    +>>> def is_it_true(anything):  
    +...   if anything:
    +...     print("yes, it's true")
    +...   else:
    +...     print("no, it's false")
    +...
    +>>> is_it_true({})             
    +no, it's false
    +>>> is_it_true({'a': 1})       
    +yes, it's true
    +
      +
    1. In a boolean context, an empty dictionary is false. +
    2. Any dictionary with at least one key-value pair is true. +

    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 a special constant in Python. It is a null value. None is not the same as False. None is not 0. None 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.

     >>> type(None)
    @@ -357,14 +474,25 @@ KeyError: 'db.diveintopython3.org'
    >>> x == y True
  • +

    None in a boolean context

    +

    In a boolean context, None is false and not None is true. +

    +>>> def is_it_true(anything):
    +...   if anything:
    +...     print("yes, it's true")
    +...   else:
    +...     print("no, it's false")
    +...
    +>>> is_it_true(None)
    +no, it's false
    +>>> is_it_true(not None)
    +yes, it's true

    Further reading

    © 2001–4, 2009 ark Pilgrim • open standards • open content • open source