Mike McGrath
Python
In easy steps is an imprint of In Easy Steps Limited
16 Hamilton Terrace Holly Walk Leamington Spa
Warwickshire CV32 4LY
www.ineasysteps.com
Copyright 2014 by In Easy Steps Limited. All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without prior written permission from the publisher.
Notice of Liability
Every effort has been made to ensure that this book contains accurate and current information. However, In Easy Steps Limited and the author shall not be liable for any loss or damage suffered by readers as a result of any information contained herein.
Trademarks
All trademarks are acknowledged as belonging to their respective companies.
Contents
Preface
The creation of this book has been for me, Mike McGrath, an exciting personal journey in discovering how Python can be used today for procedural and object-oriented programming, to develop applications and to provide online functionality. Example code listed in this book describes how to produce Python programs in easy steps and the screenshots illustrate the actual results. I sincerely hope you enjoy discovering the exciting possibilities of Python and have as much fun with it as I did in writing this book.
In order to clarify the code listed in the steps given in each example I have adopted certain colorization conventions. Components of the Python programming language are colored blue, programmer-specified names are red, numeric and string data values are black, and comments are green, like this:
# Write the traditional greeting.
greeting = Hello World!
print( greeting )
Additionally, in order to identify each source code file described in the steps, a colored icon and file name appears in the margin alongside the steps:
script.py
page.html
image.gif
For convenience I have placed source code files from the examples featured in this book into a single ZIP archive. You can obtain the complete archive by following these easy steps:
Open the web browser and navigate to www.ineasysteps.com then navigate to the Resources tab and choose the Downloads section
Find Python in easy steps in the list, then click on the hyperlink entitled All Code Examples to download the archive
Next, extract the MyScripts and MyProjects folders to your home directory (such as C:\ ) and copy all contents of the htdocs folder to your web servers documents directory
Now, follow the steps to call upon the Python interpreter and see the output
Getting started
Welcome to the exciting world of the Python programming language. This chapter demonstrates how to install Python and create your first program.
Introducing Python
Python is a high-level (human-readable) programming language that is processed by the Python interpreter to produce results. Python includes a comprehensive standard library of tested code modules that can be easily incorporated into your own programs.
The Python language was developed by Guido van Rossum in the late eighties and early nineties at the National Research Institute for Mathematics and Computer Science in the Netherlands. Python is derived from many other languages, including C, C++, the Unix shell and other programming languages. Today, Python is maintained by a core development team at the Institute, although Guido van Rossum still holds a vital role in directing its progress.
Discover all the latest Python news online at www.python.org
The basic philosophy of the Python language is readability, which makes it particularly well-suited for beginners in computer programming, and it can be summarized by these principles:
Beautiful is better than ugly
Explicit is better than implicit
Simple is better than complex
Complex is better than complicated
Readability counts
As Python is intended to be highly readable it uses English keywords frequently where other languages may use punctuation. Most significantly, it uses indentation to group together statements into code blocks whereas other languages may use keywords or punctuation for this purpose. For example, in the Pascal programming language blocks start with the keyword begin and end with the keyword end , and in the C programming language blocks are enclosed within curly brackets ( { } braces). Grouping blocks of statements by indentation is sometimes criticized by programmers familiar with languages that group by punctuation but the use of indentation in Python certainly produces code that has an uncluttered visual layout.
Programming languages that group blocks by indentation are said to adhere to the offside rule a pun on the offside rule in soccer.
Some of Pythons key distinguishing features that make it an attractive choice of language for the beginner include:
Python is free is open source distributable software
Python is easy to learn has a simple language syntax
Python is easy to read is uncluttered by punctuation
Python is easy to maintain is modular for simplicity
Python is batteries included provides a large standard library for easy integration into your own programs
Python is interactive has a terminal for debugging and testing snippets of code
Python is portable runs on a wide variety of hardware platforms and has the same interface on all platforms
Python is interpreted there is no compilation required
Python is high-level has automatic memory management
Python is extensible allows the addition of low-level modules to the interpreter for customization
Python is versatile supports both procedure-orientated programming and object-orientated programming (OOP)
Python is flexible can create console programs, windowed GUI (Graphical User Interface) applications, and CGI (Common Gateway Interface) scripts to process web data
Python is named after the British television comedy series Monty Pythons Flying Circus you may encounter references to this in the Python documentation.
As development of Python continues newer versions are released as with most software. Currently, the final 2.7 version is out, with a statement of extended support for this end-of-life release. The 2.x branch will see no new major releases after that.
Next page