Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Data Structures

The Data View

Computer programs are built with two essential features: data and processing. We started with processing elements of Python. We're about to start looking at data structures.

In Part I, “Language Basics”, we introduced almost all of the procedural elements of the Python language. We started with expressions, looking at the various operators and data types available. We described fourteen of the 21 statements that make up the Python language.

  • Expression statements - for example, a function evaluation where there is no return value.
  • The import statement - used to include a module into another module or program.
  • The print statement - used to provide visible output.
  • The assignment statements, from simple to augmented - used to set the type and value of a variable.
  • The del statement - used to remove a variable, function, module or other object.
  • The if statement - for conditionally performing suites of statements.
  • The pass statement - which does nothing, but is a necessary placeholder for an if or while suite that is empty.
  • The assert statement - used to confirm the program is in the expected state.
  • The for and while statements - for performing suites of statements using a sequence of values or while a condition is held true.
  • The break and continue statements - for short-cutting loop execution.
  • The def statement - used to create a function.
  • The return statement - used to exit a function, possibly providing the return value.
  • The global statement - used to adjust the scoping rules, allowing local access to global names. We discourage its use in the section called “The global Statement”.

The Other Side of the Coin. The next chapters focus on adding various data types to the basic Python language. The subject of data representation and data structures is possibly the most profound part of computer programming. Most of the killer applications - email, the world wide web, relational databases, are basically programs to create, read and transmit complex data structures.

We will make extensive use of the object classes that are built-in to Python. This experience will help us design our own object classes in Part III, “Data + Processing = Objects”.

We'll work our way through the following data structures.

  • Sequences. In Chapter 11, Sequences: Strings, Tuples and Lists we'll extend our knowledge of data types to include an overview various kinds of sequences: strings, tuples and lists. Sequences are collections of objects accessed by their numeric position within the collection.

    • In Chapter 12, Strings we describe the string subclass of sequence. The exercises include some challenging string manipulations.

    • We describe fixed-length sequences, called tuples in Chapter 13, Tuples .

    • In Chapter 14, Lists we describe the variable-length sequence, called a list. This list sequence is one of the powerful features that sets Python apart from other programming languages. The exercises at the end of the list section include both simple and relatively sophisticated problems.

  • Mappings. In Chapter 15, Mappings and Dictionaries we describe mappings and dictionary objects, called dicts. We'll show how dictionaries are part of some advanced techniques for handling arguments to functions. Mappings are collections of value objects that are accessed by key objects.

  • Sets. We'll cover set objects in Chapter 16, Sets . Sets are simple collections of unique objects with no additional kind of access.

  • Exceptions. We'll cover exception objects in Chapter 17, Exceptions . We'll also show the exception handling statements, including try , except , finally and raise statements. Exceptions are both simple data objects and events that control the execution of our programs.

  • Iterables. The yield statement is a variation on return that simplifies certain kinds of generator algorithms that process or create create iterable data structures. We can iterate through almost any kind of data collection. We can also define our own unique or specialized iterations. We'll cover this in Chapter 18, Generators and the yield Statement .

  • Files. The subject of files is so vast, that we'll introduce file objects in Chapter 19, Files . Files are so centrally important that we'll return files in Part IV, “Components, Modules and Packages”. We'll look at several of the file-related modules in Chapter 33, File Handling Modules as well as Chapter 34, File Formats: CSV, Tab, XML, Logs and Others .

In Chapter 20, Advanced Sequences we describe more advanced sequence techniques, including multi-dimensional processing, additional sequence-processing functions, and sorting.

Deferred Topics. There are a few topics that need to be deferred until later. The class statement will be covered in detail in chapters on object oriented programming, starting with Part III, “Data + Processing = Objects”. We'll look at the with statement there, also. We'll revisit the import statement in detail in Part IV, “Components, Modules and Packages”. Additionally, we'll cover the exec statement in Chapter 28, Modules .

Table of Contents

11. Sequences: Strings, Tuples and Lists
Overview of Sequences
Style Notes
12. Strings
String Semantics
String Literal Values
String Operations
String Comparison Operations
String Built-in Functions
String Methods
String Modules
String Exercises
Digression on Immutability of Strings
13. Tuples
Tuple Semantics
Tuple Literal Values
Tuple Operations
Tuple Comparison Operations
Tuple Statements
Tuple Built-in Functions
Tuple Exercises
Digression on The Sigma Operator
14. Lists
List Semantics
List Literal Values
List Operations
List Comparison Operations
List Statements
List Built-in Functions
List Methods
List Exercises
15. Mappings and Dictionaries
Dictionary Semantics
Dictionary Literal Values
Dictionary Operations
Dictionary Comparison Operations
Dictionary Statements
Dictionary Built-in Functions
Dictionary Methods
Dictionary Exercises
Advanced Parameter Handling For Functions
Unlimited Number of Positional Argument Values
Unlimited Number of Keyword Argument Values
Using a Container Instead of Individual Arguments
Creating a print function
16. Sets
Set Semantics
Set Literal Values
Set Operations
Set Comparison Operators
Set Statements
Set Built-in Functions
Set Methods
Set Exercises
17. Exceptions
Exception Semantics
Basic Exception Handling
Raising Exceptions
An Exceptional Example
Complete Exception Handling and The finally Clause
Exception Functions
Exception Attributes
Built-in Exceptions
Exception Exercises
Style Notes
A Digression
18. Generators and the yield Statement
Generator Semantics
Defining a Generator
Generator Functions
Generator Statements
Generator Methods
Generator Example
Generator Exercises
Subroutines and Coroutines
19. Files
File Semantics
Additional Background
Built-in Functions
File Methods
Several Examples
Reading a Text File
Reading a File as a Sequence of Strings
Read, Sort and Write
Reading "Records"
File Exercises
20. Advanced Sequences
Lists of Tuples
List Comprehensions
Sequence Processing Functions: map, filter, reduce and zip
Advanced List Sorting
Multi-Dimensional Arrays or Matrices
The Lambda

  Published under the terms of the Open Publication License Design by Interspire