Home arrow Python arrow Page 4 - Introducing Python Object Types

Numbers - Python

If you want to add Python to your repertoire of computer languages, you'll find it helpful to check out this four-part series on object types. It is excerpted from chapter four of the book Learning Python, Third Edition, written by Mark Lutz (O'Reilly, 2008; ISBN: 0596513984). Copyright © 2008 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

TABLE OF CONTENTS:
  1. Introducing Python Object Types
  2. Why Use Built-in Types?
  3. Python’s Core Data Types
  4. Numbers
By: O'Reilly Media
Rating: starstarstarstarstar / 5
January 15, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

If you’ve done any programming or scripting in the past, some of the object types in Table 4-1 will probably seem familiar. Even if you haven’t, numbers are fairly straightforward. Python’s core object set includes the usual suspects: integers (numbers without a fractional part), floating-point numbers (roughly, numbers with a decimal point in them), and more exotic types (unlimited-precision “long” integers, complex numbers with imaginary parts, fixed-precision decimals, and sets).

Although it offers some fancier options, Python’s basic number types are, well, basic. Numbers in Python support the normal mathematical operations. For instance, the plus sign (+) performs addition, a star (*) is used for multiplication, and two stars (**) are used for exponentiation:

  >>> 123 + 222        # Integer addition
  345
  >>> 1.5 * 4           # Floating-point multiplication
  6.0
  >>> 2 ** 100          # 2 to the power 100
  1267650600228229401496703205376L

Notice the L at the end of the last operation’s result here: Python automatically converts up to a long integer type when extra precision is needed. You can, for instance, compute 2 to the 1,000,000 power in Python (but you probably shouldn’t try to print the result—with more than 300,000 digits, you may be waiting awhile!). Watch what happens when some floating-point numbers are printed:

  >>> 3.1415 * 2           # repr: as code
  6.2830000000000004
  >>> print 3.1415 * 2    # str: user-friendly
  6.283

The first result isn’t a bug; it’s a display issue. It turns out that there are two ways to print every object: with full precision (as in the first result shown here), and in a user-friendly form (as in the second). Formally, the first form is known as an object’s as-code repr, and the second is its user-friendlystr. The difference can matter when we step up to using classes; for now, if something looks odd, try showing it with aprint statement.

Besides expressions, there are a handful of useful numeric modules that ship with Python:

  >>> import math
  >>> math.pi
  3.1415926535897931
  >>> math.sqrt(85)
  9.2195444572928871

Themathmodule contains more advanced numeric tools as functions, while therandommodule performs random number generation and random selections (here, from a Python list, introduced later in this chapter):

  >>> import random
 
>>> random.random()
 
0.59268735266273953
  >>>
random.choice([1, 2, 3, 4])

Python also includes more exotic number objects—such as complex numbers, fixed-precision decimal numbers, and sets—and the third-party open source extension domain has even more (e.g., matrixes and vectors). We’ll defer discussion of the details of these types until later in the book.

So far, we’ve been using Python much like a simple calculator; to do better justice to its built-in types, let’s move on to explore strings.

Please check back next week for the continuation of this article.



 
 
>>> More Python Articles          >>> More By O'Reilly Media
 

blog comments powered by Disqus
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: