Home arrow Python arrow Data Types in Python

Data Types in Python

In this second of nine parts focusing on a quick overview of the Python language for experienced programmers, you'll learn how Python handles data types such as strings, and more. This article is excerpted from chapter four of Python in a Nutshell, Second Edition, written by Alex Martelli (O'Reilly; ISBN: 0596100469). Copyright 2007 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. Data Types in Python
  2. Complex numbers
  3. Strings
  4. Tuples
By: O'Reilly Media
Rating: starstarstarstarstar / 4
September 18, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Data Types

The operation of a Python program hinges on the data it handles. All data values in Python are objects, and each object, or value, has a type. An object's type determines which operations the object supports, or, in other words, which operations you can perform on the data value. The type also determines the object's attributes and items (if any) and whether the object can be altered. An object that can be altered is known as a mutable object, while one that cannot be altered is an immutable object. I cover object attributes and items in detail in "Object attributes and items" on page 46.

The built-in type(obj) accepts any object as its argument and returns the type object that is the type of obj . Built-in function isinstance(obj, type) returns True if object obj has type type (or any subclass thereof); otherwise, it returns False.

Python has built-in types for fundamental data types such as numbers, strings, tuples, lists, and dictionaries, as covered in the following sections. You can also create user-defined types, known as classes, as discussed in "Classes and Instances" on page 82.

Numbers

The built-in number objects in Python support integers (plain and long), floating-point numbers, and complex numbers. In Python 2.4, the standard library also offers decimal floating-point numbers, covered in "The decimal Module" on page 372. All numbers in Python are immutable objects, meaning that when you perform any operation on a number object, you always produce a new number object. Operations on numbers, also known as arithmetic operations, are covered in "Numeric Operations" on page 52.

Note that numeric literals do not include a sign: a leading + or -, if present, is a separate operator, as discussed in "Arithmetic Operations" on page 52.

Integer numbers

Integer literals can be decimal, octal, or hexadecimal. A decimal literal is represented by a sequence of digits in which the first digit is nonzero. To denote an octal literal, use 0 followed by a sequence of octal digits
(0 to 7). To indicate a hexadecimal literal, use 0x followed by a sequence of hexadecimal digits
(0 to 9 and A to F, in either upper- or lowercase). For example:

  1, 23, 3493         # Decimal integers
  01, 027, 06645      # Octal integers
  0x1, 0x17, 0xDA5    # Hexadecimal integers

In practice, you don't need to worry about the distinction between plain and long integers in modern Python, since operating on plain integers produces results that are long integers when needed (i.e., when the result would not fit within the range of plain integers). However, you may choose to terminate any kind of integer literal with a letter L (or l) to explicitly denote a long integer. For instance:

  1L, 23L, 99999333493L         # Long decimal integers
  01L, 027L, 01351033136165L    # Long octal integers
  0x1L, 0x17L, 0x17486CBC75L    # Long hexadecimal integers

Use uppercase L here, not lowercase l, which might look like the digit 1. The difference between long and plain integers is one of implementation. A long integer has no predefined size limit; it may be as large as memory allows. A plain integer takes up just a few bytes of memory and its minimum and maximum values are dictated by machine architecture. sys.maxint is the largest positive plain integer available, while
-sys.maxint-1 is the largest negative one. On 32-bit machines, sys.maxint is 2147483647 .

Floating-point numbers

A floating-point literal is represented by a sequence of decimal digits that includes a decimal point (.), an exponent part (an e or E, optionally followed by + or
-, followed by one or more digits), or both. The leading character of a floating-point literal cannot be e or E; it may be any digit or a period (.). For example:

  0., 0.0, .0, 1., 1.0, 1e0, 1.e0, 1.0e0

A Python floating-point value corresponds to a C double and shares its limits of range and precision, typically 53 bits of precision on modern platforms. (Python offers no way to find out the exact range and precision of floating-point values on your platform.)



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

 


Dev Shed Tutorial Topics: