Home arrow Python arrow Page 4 - Python Expressions and Operators

Sequence Operations - Python

In this fourth part of a nine-part series on Python, you'll learn about expressions, operators, sequences, and more. This article is excerpted from chapter four of the book 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.

  1. Python Expressions and Operators
  2. The Python 2.5 ternary operator
  3. Arithmetic Operations
  4. Sequence Operations
By: O'Reilly Media
Rating: starstarstarstarstar / 1
October 02, 2008

print this article



Python supports a variety of operations applicable to all sequences, including strings, lists, and tuples. Some sequence operations apply to all containers (including, for example, sets and dictionaries, which are not sequences), and some apply to all iterables (meaning "any object on which you can loop," as covered in "Iterables" on page 40; all containers, be they sequences or otherwise, are iterable, and so are many objects that are not containers, such as files, covered in "File Objects" on page 216, and generators, covered in "Generators" on page 78). In the following, I use the terms sequence, container, and iterable, quite precisely and specifically, to indicate exactly which operations apply to each category.

Sequences in General

Sequences are containers with items that are accessible by indexing or slicing. The built-in len function takes any container as an argument and returns the number of items in the container. The built-in min and max functions take one argument, a nonempty iterable whose items are comparable, and return the smallest and largest items, respectively. You can also call min and max with multiple arguments, in which case they return the smallest and largest arguments, respectively. The built-in sum function takes one argument, an iterable whose items are numbers, and returns the sum of the numbers.

Sequence conversions

There is no implicit conversion between different sequence types, except that plain strings are converted to Unicode strings if needed. (String conversion is covered in detail in "Unicode" on page 198.) You can call the built-ins tuple and list with a single argument (any iterable) to get a new instance of the type you're calling, with the same items (in the same order) as in the argument.

Concatenation and repetition

You can concatenate sequences of the same type with the + operator. You can multiply a sequence S  by an integer n  with the * operator. S*n  or n*S  is the concatenation of n copies of S . When n<=0, S*n  is an empty sequence of the same type as S .

Membership testing

The x  in S operator tests to check whether object x equals any item in the sequence (or other kind of container or iterable) S . It returns True if it does and False if it doesn't. The x not in S  operator is just like not (x in S). In the specific case of strings, though, x  in S  is more widely applicable; in this case, the operator tests whether x  equals any substring of string S , not just any single character.

Indexing a sequence

The nth item of a sequence S is denoted by an indexing: S[n]. Indexing is zero-based (S' s first item is S[0]). If S  has L  items, the index n  may be 0, 1...up to and including L-1, but no larger. n  may also be -1, -2...down to and including -L , but no smaller. A negative n  indicates the same item in S as L+n  does. In other words, S[-1], like S[L-1], is the last element of S , S[-2] is the next-to-last one, and so on. For example:

  x = [1, 2, 3, 4]
  x[1]                 # 2
  x[-1]                # 4

Using an index >=L or <-L  raises an exception. Assigning to an item with an invalid index also raises an exception. You can add one or more elements to a list, but to do so you assign to a slice, not an item, as I'll discuss shortly.

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

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: