PYTHON 3
Pocket Primer
LICENSE, DISCLAIMER OF LIABILITY, AND LIMITED WARRANTY
By purchasing or using this book and disc (the Work), you agree that this license grants permission to use the contents contained herein, including the disc, but does not give you the right of ownership to any of the textual content in the book / disc or ownership to any of the information or products contained in it. This license does not permit uploading of the Work onto the Internet or on a network (of any kind) without the written consent of the Publisher. Duplication or dissemination of any text, code, simulations, images, etc. contained herein is limited to and subject to licensing terms for the respective products, and permission must be obtained from the Publisher or the owner of the content, etc., in order to reproduce or network any portion of the textual material (in any media) that is contained in the Work.
MERCURY LEARNING AND INFORMATION (MLI or the Publisher) and anyone involved in the creation, writing, or production of the companion disc, accompanying algorithms, code, or computer programs (the software), and any accompanying Web site or software of the Work, cannot and do not warrant the performance or results that might be obtained by using the contents of the Work. The author, developers, and the Publisher have used their best efforts to insure the accuracy and functionality of the textual material and/or programs contained in this package; we, however, make no warranty of any kind, express or implied, regarding the performance of these contents or programs. The Work is sold as is without warranty (except for defective materials used in manufacturing the book or due to faulty workmanship).
The author, developers, and the publisher of any accompanying content, and anyone involved in the composition, production, and manufacturing of this work will not be liable for damages of any kind arising out of the use of (or the inability to use) the algorithms, source code, computer programs, or textual material contained in this publication. This includes, but is not limited to, loss of revenue or profit, or other incidental, physical, or consequential damages arising out of the use of this Work.
The sole remedy in the event of a claim of any kind is expressly limited to replacement of the book and/or disc, and only at the discretion of the Publisher. The use of implied warranty and certain exclusions vary from state to state, and might not apply to the purchaser of this product.
Companion files for this title may be requested at .
PYTHON 3
Pocket Primer
James R. Parker
MERCURY LEARNING AND INFORMATION
Dulles, Virginia
Boston, Massachusetts
New Delhi
Copyright 2018 by MERCURY LEARNING AND INFORMATION LLC. All rights reserved.
This publication, portions of it, or any accompanying software may not be reproduced in any way, stored in a retrieval system of any type, or transmitted by any means, media, electronic display or mechanical display, including, but not limited to, photocopy, recording, Internet postings, or scanning, without prior permission in writing from the publisher.
Publisher: David Pallai
MERCURY LEARNING AND INFORMATION
22841 Quicksilver Drive
Dulles, VA 20166
www.merclearning.com
(800) 232-0223
James R. Parker. Python 3 Pocket Primer.
ISBN: 978-1-68392-086-1
The publisher recognizes and respects all marks used by companies, manufacturers, and developers as a means to distinguish their products. All brand names and product names mentioned in this book are trademarks or service marks of their respective companies. Any omission or misuse (of any kind) of service marks or trademarks, etc. is not an attempt to infringe on the property of others.
Library of Congress Control Number: 2017934664
171819321 Printed in the United States of America on acid-free paper.
Our titles are available for adoption, license, or bulk purchase by institutions, corporations, etc. For additional information, please contact the Customer Service Dept. at (800) 232-0223 (toll free). Digital versions of our titles are available at: .
The sole obligation of MERCURY LEARNING AND INFORMATION to the purchaser is to replace the book and/or disc, based on defective materials or faulty workmanship, but not based on the operation or functionality of the product.
CONTENTS
PREFACE
This book is an effort to give a programmer sufficient knowledge of Python 3 to be able to work on their own projects. It is based on a much longer book that was intended for beginning programmers, and so most of the introductory material and basic computer science has been removed.
What remains is, first, a lot of code. Programming is something that must be practiced, and this book provides a lot of examples that are intended to inspire the explanations of programming language structures that otherwise lack context. Many of the examples are games or portions of games. Thats because most of the audience are game players of one kind or another and can understand the examples. The code that implements the game is motivated by that understanding, although there are always many different actual programs that can be written to solve any one problem.
The example code was compiled on a PC running Windows 10, using Python 3.4 and the PyCharm GUI. Working code was copied directly into the manuscript and so should always be functional, but however hard we try, sometimes errors creep in during production. Please let me know if you find one.
There are a couple of unique features of this short book. One is the chapter on PyGame, which allows a programmer to handle graphics, control mouse and keyboard interaction, and play sounds and videos. The large example for that chapter is a Lunar Lander game.
Another feature is the chapter on communication, which makes use of one of Pythons best features: a collection of modules for sending and receiving Email, communicating between computers, and working with Twitter and Web pages.
The disc that accompanies this book contains all of the code examples as complete working programs (also available for downloading from the publisher). This means that there is no need to type them in so they can be executed and perhaps modified or expanded. The disc also contains all of the figures in the book at their original size. Some of these are used as data for the programs, so its good to have them.
There is a large code base in both Python 2.7 and Python 3, and one must take care when installing and using any module that it is compatible with the version of Python that has been installedthe two are incompatible.
Python 2 vs. Python 3
Here are the critical differences between the two versions of Python.
division | In Python 2, dividing two integers results in an integer: 3/2=1. In Python 3, / is floating point division, so 3/2 is 1.5. |
byte | Python 2 has no byte type. |
xrange | Python 3 has no xrange function. |
exception | Python 3 requires that when raising an exception, the exception argument must be enclosed in parentheses. For example: |
raise IOError (missing file) # Py3 |
as opposed to |
raise IOError, missing_file # Py2 |
print | In Python 2, print is a statement, but in Python 3 it is a function. |
Next page