12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Such operations are convenient when dealing with large data sets; the intersection oftwo sets contains objects in common to both, for example, and the union containsall items in either set. Here’s a more realistic example of set operations at work,applied to lists of people in a hypothetical company:>>> engineers = set(['bob', 'sue', 'ann', 'vic'])>>> managers = set(['tom', 'sue'])>>>>>> engineers & managers # Who is both engineer and manager?set(['sue'])>>>>>> engineers | managers # All people in either categoryset(['vic', 'sue', 'tom', 'bob', 'ann'])>>>>>> engineers – managers # Engineers who are not managersset(['vic', 'bob', 'ann'])In addition, the set object provides method calls that implement more exotic setoperations. Although set operations can be coded manually in <strong>Python</strong> (and oftenwere in the past), <strong>Python</strong>’s built-in sets use efficient algorithms and implementationtechniques to provide quick and standard operation.For more details on sets, see <strong>Python</strong>’s library reference manual.BooleansIn <strong>Python</strong> 3.0, the literal notation {1, 2, 3} is proposed to have thesame effect as the current call set([1, 2, 3]), and will thus be anotherway to create a set object. This is a future extension, so see the 3.0release notes for details.Some argue that the <strong>Python</strong> Boolean type, bool, is numeric in nature because its twovalues, True and False, are just customized versions of the integers 1 and 0 that printthemselves differently. Although that’s all most programmers need to know, let’sexplore this type in a bit more detail.<strong>Python</strong> 2.3 introduced a new explicit Boolean data type called bool, with the valuesTrue and False available as new preassigned built-in names. Internally, the newnames True and False are instances of bool, which is in turn just a subclass (in theobject-oriented sense) of the built-in integer type int. True and False behave exactlylike the integers 1 and 0, except that they have customized printing logic—they printthemselves as the words True and False, instead of the digits 1 and 0 (technically,bool redefines its str and repr string formats). ** Some argue that the <strong>Python</strong> Boolean type, bool, is numeric in nature because its two values, True and False,are just customized versions of the integers 1 and 0 (they simply display themselves differently whenprinted). For most practical purposes, this is all you’ll need to know about Booleans.Other Numeric Types | 109

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!