মুখ্য Beginning Programming with Python for Dummies

# Beginning Programming with Python for Dummies

5.0 / 5.0
How much do you like this book?
What’s the quality of the file?
The easy way to learn programming fundamentals with Python

Python is a remarkably powerful and dynamic programming language that's used in a wide variety of application domains. Some of its key distinguishing features include a very clear, readable syntax, strong introspection capabilities, intuitive object orientation, and natural expression of procedural code. Plus, Python features full modularity, supporting hierarchical packages, exception-based error handling, and modules easily written in C, C++, Java, R, or .NET languages, such as C#. In addition, Python supports a number of coding styles that include: functional, imperative, object-oriented, and procedural.

Due to its ease of use and flexibility, Python is constantly growing in popularity—and now you can wear your programming hat with pride and join the ranks of the pros with the help of this guide. Inside, expert author John Paul Mueller gives a complete step-by-step overview of all there is to know about Python. From performing common and advanced tasks, to collecting data, to interacting with package—this book covers it all!

• Use Python to create and run your first application
• Find out how to troubleshoot and fix errors
• Learn to work with Anaconda and use Magic Functions
• Benefit from completely updated and revised information since the last edition

If you've never used Python or are new to programming in general, Beginning Programming with Python For Dummies is a helpful resource that will set you up for success.
ক্যাটাগোরিগুলো:
সাল:
2018
সংস্করণ:
Paperback
প্রকাশক:
John Wiley & Sons
ভাষা:
english
পৃষ্ঠা:
408 / 411
ISBN 10:
1119457890
ISBN 13:
9781119457893
বইয়ের সিরিজ:
For Dummies
ফাইল:
PDF, 8.61 MB

## প্রায়শই ব্যবহৃত পরিভাষা

Arnold samuel
i like these books because they have made me to know what i should follow
08 April 2019 (17:41)
very useful books for anyone wants to improve his skills
07 January 2020 (11:00)
Ed
I cant believe there is a trove of treasures on this site
02 February 2020 (12:48)
doctorwhythink
i love this website!
19 April 2020 (14:01)
Fishan
I am searching for a book entitled "Plant Physiology by Plant Physiology and Ross
and Salisbury
21 April 2020 (16:27)
tsk
I love visiting the website all the books are available for me
16 May 2020 (18:19)
Christian Leandro Diaz Martinez
Awesome help, Thank you very much, Greatest contributions. God Bless Rusia all the time.
23 May 2020 (16:35)
Dspuli
It's very useful, thanks alot
03 August 2020 (12:19)
Stanley Chidubem
My very own libary????. I love login in here to get books
08 September 2020 (00:57)
Paul
This is a very useful website. I love it!
23 September 2020 (13:51)
kyawkyaw
Thank you for your uploading so many books , i would like to express my thankfulness .
08 October 2020 (03:31)
Sly
This is the best ebook site ever tnx so so much for uploading so many useful books
13 October 2020 (23:41)
mateus
thanks to those who take care of this wonderful site, I am very grateful for the content that I am unable to obtain!
26 October 2020 (04:44)
Pooh
This is amazing and beneficial all of us who wanna learn python
10 November 2020 (06:07)
Evans
I am grateful for people who established this site it is indeed of great beneficial to me
15 January 2021 (09:29)
Malex
A big thank you to all maintaining this site
01 February 2021 (06:45)
Specter
thank youu
best site by all means many thanks
19 February 2021 (19:05)
kuwerdas
I am grateful for what you've made. The amount of gratitude I have for the developer of this site is endless. I hope you guys live a long, happy, and healthy life. God bless you all.
18 April 2021 (07:16)
Luis Núnez
Es un gusto entrar en esta biblioteca. infinitas gracias para todo el equipo.
23 April 2021 (05:17)

আপনি একটি বুক রিভিউ লিখতে পারেন এবং আপনার অভিজ্ঞতা শেয়ার করতে পারেন. অন্যান্য পাঠকরা আপনার পড়া বইগুলির বিষয়ে আপনার মতামত সম্পর্কে সর্বদা আগ্রহী হবে. বইটি আপনার পছন্দ হোক বা না হোক, আপনি যদি নিজের সৎ ও বিস্তারিত চিন্তাভাবনা ব্যক্ত করেন তাহলে অন্যরা তাদের জন্য উপযুক্ত নতুন বইগুলি খুঁজে পাবে.
1

Year:
1987
Language:
english
File:
PDF, 20.48 MB
0 / 0
2

### Begin to Code with Python

Year:
2017
Language:
english
File:
PDF, 14.17 MB
0 / 0
Beginning
Programming
with Python
®

2nd Edition

by John Paul Mueller

Beginning Programming with Python® For Dummies®, 2nd Edition
Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com
Copyright © 2018 by John Wiley & Sons, Inc., Hoboken, New Jersey
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any
means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections
107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher. Requests to
the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River
Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.
Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related
trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written
permission. Python is a registered trademark of Python Software Foundation Corporation. All other trademarks are the
property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in
this book.

LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO
REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS
OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES
OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR
PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR
EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED
IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS
REQUIRED, THE SERVI; CES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE
PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN
ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF
FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION
THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS
SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED
BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.

the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit
https://hub.wiley.com/community/support/dummies.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with
standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to
media such as a CD or DVD that is not included in the version you purchased, you may download this material at
Library of Congress Control Number: 2017964018
ISBN 978-1-119-45789-3; ISBN 978-1-119-45787-9 (ebk); ISBN 978-1-119-45790-9 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1

Contents at a Glance
Introduction.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 1
Part 1: Getting Started with Python.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 5
CHAPTER 1:
CHAPTER 2:
CHAPTER 3:
CHAPTER 4:
CHAPTER 5:

Talking to Your Computer.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 7
Getting Your Own Copy of Python. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 21
Interacting with Python.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 37
Writing Your First Application.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 55
Working with Anaconda. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 83

Part 2: Talking the Talk .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

101

Storing and Modifying Information.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
CHAPTER 7: Managing Information.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
CHAPTER 8: Making Decisions .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
CHAPTER 9: Performing Repetitive Tasks.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
CHAPTER 10: Dealing with Errors.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

103
113
135
151
165

Part 3: Performing Common Tasks.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

195

Interacting with Packages.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
CHAPTER 12: Working with Strings .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
CHAPTER 13: Managing Lists. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
CHAPTER 14: Collecting All Sorts of Data .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
CHAPTER 15: Creating and Using Classes.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

197
225
243
261
281

Part 4: Performing Advanced Tasks.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

303

CHAPTER 6:

CHAPTER 11:

Storing Data in Files.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 305
CHAPTER 17: Sending an Email. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 323
CHAPTER 16:

Part 5: The Part of Tens.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

341

Ten Amazing Programming Resources. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Ten Ways to Make a Living with Python.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
CHAPTER 20: Ten Tools That Enhance Your Python Experience. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
CHAPTER 21: Ten (Plus) Libraries You Need to Know About.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

343
353
361
371

Index.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

379

CHAPTER 18:
CHAPTER 19:

INTRODUCTION .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 1
About This Book. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Foolish Assumptions. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Icons Used in This Book.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Beyond the Book.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Where to Go from Here .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

1
2
3
3
4

PART 1: GETTING STARTED WITH PYTHON.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 5
CHAPTER 1:

Talking to Your Computer .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 7
Understanding Why You Want to Talk to Your Computer. .  .  .  .  .  .  .  .  .  .  . 8
Knowing that an Application is a Form of Communication. .  .  .  .  .  .  .  .  .  . 9
Thinking about procedures you use daily .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 9
Writing procedures down. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 10
Seeing applications as being like any other procedure.  .  .  .  .  .  .  .  .  . 11
Understanding that computers take things literally. .  .  .  .  .  .  .  .  .  .  .  . 11
Defining What an Application Is .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 11
Understanding that computers use a special language .  .  .  .  .  .  .  .  . 12
Helping humans speak to the computer.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 12
Understanding Why Python Is So Cool. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 14
Unearthing the reasons for using Python .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 14
Deciding how you can personally benefit from Python. .  .  .  .  .  .  .  .  . 15
Discovering which organizations use Python .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 16
Finding useful Python applications. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 17
Comparing Python to other languages .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 18

CHAPTER 2:

Getting Your Own Copy of Python. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 21
Downloading the Version You Need. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Installing Python .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Working with Windows.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Working with the Mac.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Working with Linux .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Accessing Python on Your Machine .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using Windows .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using the Mac .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using Linux.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Testing Your Installation. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

21
24
25
27
28
31
32
34
35
35

v

CHAPTER 3:

Interacting with Python.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 37
Opening the Command Line .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Starting Python .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using the command line to your advantage.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using Python environment variables to your advantage .  .  .  .  .  .  .  .
Typing a Command.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Telling the computer what to do.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Telling the computer you’re done.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Seeing the result .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using Help .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Getting into help mode. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Asking for help.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Leaving help mode .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Obtaining help directly.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Closing the Command Line .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

CHAPTER 4:

38
38
39
41
43
43
44
44
46
46
47
49
50
51

Writing Your First Application.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 55
Understanding Why IDEs Are Important.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 56
Creating better code.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 56
Debugging functionality.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 56
Defining why notebooks are useful .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 57
Obtaining Your Copy of Anaconda.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 58
Obtaining Analytics Anaconda. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 58
Installing Anaconda on Linux. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 59
Installing Anaconda on MacOS .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 60
Installing Anaconda on Windows .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 61
Downloading the Datasets and Example Code. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 64
Using Jupyter Notebook.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 64
Defining the code repository.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 65
Creating the Application.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 71
Understanding cells. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71
Adding documentation cells .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 74
Other cell content .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 75
Understanding the Use of Indentation .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 75
Adding Comments. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 77
Understanding comments.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 78
Using comments to leave yourself reminders .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 79
Using comments to keep code from executing .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 80
Closing Jupyter Notebook. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80

vi

Beginning Programming with Python For Dummies

CHAPTER 5:

Working with Anaconda. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 83
Downloading Your Code. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Working with Checkpoints.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Defining the uses of checkpoints .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Saving a checkpoint. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Restoring a checkpoint.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Manipulating Cells.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Adding various cell types .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Splitting and merging cells.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Moving cells around .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Running cells .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Toggling outputs .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Changing Jupyter Notebook’s Appearance .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Finding commands using the Command Palette .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Working with line numbers .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using the Cell Toolbar features.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Interacting with the Kernel. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Obtaining Help. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using the Magic Functions.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Viewing the Running Processes. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

84
85
85
86
86
86
87
87
88
88
90
90
91
92
93
94
95
97
99

PART 2: TALKING THE TALK .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

101

Storing and Modifying Information .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

103

Storing Information. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Seeing variables as storage boxes .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using the right box to store the data.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Defining the Essential Python Data Types.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Putting information into variables .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Understanding the numeric types .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Understanding Boolean values.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Understanding strings .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Working with Dates and Times .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

104
104
104
105
105
106
110
110
111

Managing Information. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

113

Controlling How Python Views Data.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Making comparisons. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Understanding how computers make comparisons .  .  .  .  .  .  .  .  .  .  .
Working with Operators.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Defining the operators.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Understanding operator precedence. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

114
114
115
115
116
122

CHAPTER 6:

CHAPTER 7:

vii

Creating and Using Functions .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 123
Viewing functions as code packages. . . . . . . . . . . . . . . . . . . . . . . . .124
Understanding code reusability .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 124
Defining a function .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 125
Accessing functions.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 126
Sending information to functions.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 127
Returning information from functions.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 131
Comparing function output.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 132
Getting User Input. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 132
CHAPTER 8:

CHAPTER 9:

CHAPTER 10:

viii

Making Decisions.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

135

Making Simple Decisions by Using the if Statement .  .  .  .  .  .  .  .  .  .  .  .  .  .
Understanding the if statement .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using the if statement in an application .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Choosing Alternatives by Using the if. . .else Statement.  .  .  .  .  .  .  .  .  .  .
Understanding the if. . .else statement .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using the if. . .else statement in an application .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using the if. . .elif statement in an application .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using Nested Decision Statements. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using multiple if or if. . .else statements .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Combining other types of decisions.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

136
136
137
141
141
142
143
146
146
148

Performing Repetitive Tasks .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

151

Processing Data Using the for Statement .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Understanding the for statement.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Creating a basic for loop. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Controlling execution with the break statement .  .  .  .  .  .  .  .  .  .  .  .  .  .
Controlling execution with the continue statement. .  .  .  .  .  .  .  .  .  .  .
Controlling execution with the pass clause.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Controlling execution with the else statement.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Processing Data by Using the while Statement .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Understanding the while statement.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using the while statement in an application.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Nesting Loop Statements.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

152
152
153
153
156
157
158
159
160
161
162

Dealing with Errors.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

165

Knowing Why Python Doesn’t Understand You .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Considering the Sources of Errors .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Classifying when errors occur.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Distinguishing error types .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Catching Exceptions .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Basic exception handling .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Handling more specific to less specific exceptions .  .  .  .  .  .  .  .  .  .  .  .
Nested exception handling .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

166
167
168
169
171
171
183
185

Beginning Programming with Python For Dummies

Raising Exceptions. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Raising exceptions during exceptional conditions.  .  .  .  .  .  .  .  .  .  .  .  .
Passing error information to the caller .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Creating and Using Custom Exceptions.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using the finally Clause .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

189
189
190
191
192

PART 3: PERFORMING COMMON TASKS.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

195

CHAPTER 11:

CHAPTER 12:

CHAPTER 13:

Interacting with Packages .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

197

Creating Code Groupings.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Understanding the package types .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Considering the package cache. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Importing Packages. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using the import statement. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using the from. . .import statement.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Finding Packages on Disk.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Downloading Packages from Other Sources. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Opening the Anaconda Prompt. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Working with conda packages.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Installing packages by using pip .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Viewing the Package Content .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Viewing Package Documentation .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Opening the Pydoc application.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using the quick-access links. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Typing a search term .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Viewing the results .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

198
200
201
202
203
205
207
209
210
210
215
216
219
219
220
221
222

Working with Strings. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

225

Understanding That Strings Are Different.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Defining a character by using numbers.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using characters to create strings .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Creating Stings with Special Characters. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Selecting Individual Characters.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Slicing and Dicing Strings.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Locating a Value in a String .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Formatting Strings. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

226
226
227
229
231
233
236
238

Managing Lists.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

243

Organizing Information in an Application .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Defining organization using lists.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Understanding how computers view lists.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Creating Lists.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Accessing Lists.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Looping through Lists.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

244
244
245
246
248
249

ix

CHAPTER 14:

CHAPTER 15:

Modifying Lists.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Searching Lists.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Sorting Lists .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Printing Lists. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Working with the Counter Object.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

250
254
255
257
259

Collecting All Sorts of Data. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

261

Understanding Collections. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Working with Tuples.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Working with Dictionaries .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Creating and using a dictionary. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Replacing the switch statement with a ­dictionary .  .  .  .  .  .  .  .  .  .  .  .  .
Creating Stacks Using Lists. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Working with queues .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Working with deques .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

262
263
266
267
270
273
275
278

Creating and Using Classes .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

281

Understanding the Class as a Packaging Method. . . . . . . . . . . . . . . . .282
Considering the Parts of a Class .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 284
Creating the class definition .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 284
Considering the built-in class attributes .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 285
Working with methods.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 286
Working with constructors.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 288
Working with variables.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 290
Using methods with variable argument lists. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 293
Overloading operators.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 294
Creating a Class.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 296
Defining the MyClass class.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 296
Saving a class to disk. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 297
Using the Class in an Application .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 298
Extending Classes to Make New Classes.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 299
Building the child class.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 299
Testing the class in an application .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 301

PART 4: PERFORMING ADVANCED TASKS .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

303

Storing Data in Files.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

305

CHAPTER 16:

Understanding How Permanent Storage Works. . . . . . . . . . . . . . . . . .306
Creating Content for Permanent Storage .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 308
Creating a File .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 311
Reading File Content.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 314
Updating File Content.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 317
Deleting a File. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 321

x

Beginning Programming with Python For Dummies

Sending an Email .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

323

Understanding What Happens When You Send Email .  .  .  .  .  .  .  .  .  .  .  .
Viewing email as you do a letter.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Defining the parts of the envelope.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Defining the parts of the letter .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Creating the Email Message. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Working with a text message. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Working with an HTML message. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Seeing the Email Output. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

324
325
326
331
335
335
337
338

PART 5: THE PART OF TENS.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

341

CHAPTER 17:

CHAPTER 18:

CHAPTER 19:

Ten Amazing Programming Resources .  .  .  .  .  .  .  .  .  .  .  .  .

343

Working with the Python Documentation Online. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using the LearnPython.org Tutorial.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Performing Web Programming by Using Python.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Getting Additional Libraries.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Creating Applications Faster by Using an IDE.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Checking Your Syntax with Greater Ease.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Using XML to Your Advantage.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Getting Past the Common Python Newbie Errors .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Understanding Unicode.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Making Your Python Application Fast. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

344
345
346
346
348
348
349
350
351
352

Ten Ways to Make a Living with Python. .  .  .  .  .  .  .  .  .  .  .

353

Working in QA .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 354
Becoming the IT Staff for a Smaller Organization .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 355
Performing Specialty Scripting for Applications.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 355
Administering a Network.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 356
Teaching Programming Skills. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .357
Helping People Decide on Location .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 357
Performing Data Mining.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 358
Interacting with Embedded Systems .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 358
Carrying Out Scientific Tasks.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 359
Performing Real-Time Analysis of Data .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 359
CHAPTER 20:

Ten Tools That Enhance Your
Python Experience .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

361

Tracking Bugs with Roundup Issue Tracker. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Creating a Virtual Environment by Using VirtualEnv .  .  .  .  .  .  .  .  .  .  .  .  .  .
Installing Your Application by Using PyInstaller .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Building Developer Documentation by Using pdoc.  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Developing Application Code by Using Komodo Edit. .  .  .  .  .  .  .  .  .  .  .  .  .

362
363
364
365
366

xi

Debugging Your Application by Using pydbgr. .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Entering an Interactive Environment by Using IPython.  .  .  .  .  .  .  .  .  .  .  .
Testing Python Applications by Using PyUnit .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Tidying Your Code by Using Isort .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Providing Version Control by Using Mercurial .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

367
368
368
369
370

Ten (Plus) Libraries You Need to Know About.  .  .  .  .

371

Developing a Secure Environment by Using PyCrypto .  .  .  .  .  .  .  .  .  .  .  .
Interacting with Databases by Using SQLAlchemy.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Seeing the World by Using Google Maps.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Adding a Graphical User Interface by Using TkInter .  .  .  .  .  .  .  .  .  .  .  .  .  .
Providing a Nice Tabular Data Presentation by
Using PrettyTable .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Enhancing Your Application with Sound by Using PyAudio .  .  .  .  .  .  .  .
Manipulating Images by Using PyQtGraph .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Locating Your Information by Using IRLib.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Creating an Interoperable Java Environment by Using JPype.  .  .  .  .  .  .
Accessing Local Network Resources by Using Twisted Matrix.  .  .  .  .  .
Accessing Internet Resources by Using Libraries.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

372
372
373
373

INDEX.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

379

CHAPTER 21:

xii

Beginning Programming with Python For Dummies

374
374
375
376
377
378
378

Introduction

P

ython is an example of a language that does everything right within the
domain of things that it’s designed to do. This isn’t just me saying it, either:
Programmers have voted by using Python enough that it’s now the
fifth-ranked language in the world (see https://www.tiobe.com/tiobe-index/
for details). The amazing thing about Python is that you really can write an
­application on one platform and use it on every other platform that you need to
support. In contrast to other programming languages that promised to provide
platform independence, Python really does make that independence possible. In
this case, the promise is as good as the result you get.
Python emphasizes code readability and a concise syntax that lets you write
­applications using fewer lines of code than other programming languages require.
You can also use a coding style that meets your needs, given that Python supports
the functional, imperative, object-oriented, and procedural coding styles (see
­Chapter 3 for details). In addition, because of the way Python works, you find it
used in all sorts of fields that are filled with nonprogrammers. Beginning
Programming with Python for Dummies, 2nd Edition is designed to help everyone,
including nonprogrammers, get up and running with Python quickly.
Some people view Python as a scripted language, but it really is so much more.
(Chapter 18 gives you just an inkling of the occupations that rely on Python to
make things work.) However, Python it does lend itself to educational and other
uses for which other programming languages can fall short. In fact, this book uses
Jupypter Notebook for examples, which relies on the highly readable literate programming paradigm advanced by Stanford computer scientist Donald Knuth (see
Chapter 4 for details). Your examples end up looking like highly readable reports
that almost anyone can understand with ease.

Beginning Programming with Python For Dummies, 2nd Edition is all about getting up
and running with Python quickly. You want to learn the language fast so that you
can become productive in using it to perform your real job, which could be anything. Unlike most books on the topic, this one starts you right at the beginning
by showing you what makes Python different from other languages and how it can
help you perform useful work in a job other than programming. As a result, you

Introduction

1

gain an understanding of what you need to do from the start, using hands-on
examples and spending a good deal of time performing actually useful tasks. You
even get help with installing Python on your particular system.
When you have a good installation on whatever platform you’re using, you start
with the basics and work your way up. By the time you finish working through the
examples in this book, you’ll be writing simple programs and performing tasks
such as sending an email using Python. No, you won’t be an expert, but you will
be able to use Python to meet specific needs in the job environment. To make
absorbing the concepts even easier, this book uses the following conventions:

»» Text that you’re meant to type just as it appears in the book is bold. The

exception is when you’re working through a step list: Because each step is
bold, the text to type is not bold.

»» When you see words in italics as part of a typing sequence, you need to replace

that value with something that works for you. For example, if you see “Type Your
Name and press Enter,” you need to replace Your Name with your actual name.

»» Web addresses and programming code appear in monofont. If you’re reading

a digital version of this book on a device connected to the Internet, note that
you can click the web address to visit that website, like this: www.dummies.com.

»» When you need to type command sequences, you see them separated by a
special arrow, like this: File ➪   New File. In this case, you go to the File menu
first and then select the New File entry on that menu. The result is that you
see a new file created.

Foolish Assumptions
You might find it difficult to believe that I’ve assumed anything about you — after
all, I haven’t even met you yet! Although most assumptions are indeed foolish,
I made these assumptions to provide a starting point for the book.
Familiarity with the platform you want to use is important because the book
doesn’t provide any guidance in this regard. (Chapter 2 does provide Python installation instructions for various platforms, and Chapter 4 tells you how to install
Anaconda, which includes Jupyter Notebook — the Integrated ­Development
­Environment, or IDE, used for this book.) To provide you with maximum
­information about Python, this book doesn’t discuss any platform-specific issues.
You really do need to know how to install applications, use applications, and
­generally work with your chosen platform before you begin working with this book.
This book also assumes that you can locate information on the Internet. Sprinkled
throughout are numerous references to online material that will enhance your

2

Beginning Programming with Python For Dummies

learning experience. However, these added sources are useful only if you actually
find and use them.

Icons Used in This Book
As you read this book, you see icons in the margins that indicate material of interest
(or not, as the case may be). This section briefly describes each icon in this book.
of extra work. The tips in this book are time-saving techniques or pointers to
resources that you should try in order to get the maximum benefit from Python.
I don’t want to sound like an angry parent or some kind of maniac, but you should
avoid doing anything marked with a Warning icon. Otherwise, you could find that
your program only serves to confuse users, who will then refuse to work with it.
Whenever you see this icon, think advanced tip or technique. You might find these
tidbits of useful information just too boring for words, or they could contain the
solution you need to get a program running. Skip these bits of information whenever you like.
If you don’t get anything else out of a particular chapter or section, remember the
material marked by this icon. This text usually contains an essential process or a
bit of information that you must know to write Python programs successfully.

Beyond the Book
This book isn’t the end of your Python programming experience — it’s really just
the beginning. I provide online content to make this book more flexible and better
able to meet your needs. That way, as I receive email from you, I can do things like
address questions and tell you how updates to either Python or its associated

»» Cheat sheet: You remember using crib notes in school to make a better mark
on a test, don’t you? You do? Well, a cheat sheet is sort of like that. It provides
you with some special notes about tasks that you can do with Python that not
every other developer knows. You can find the cheat sheet for this book by
going to www.dummies.com and searching Beginning Programming For
Dummies, 2nd Edition Cheat Sheet. It contains really neat information like the
top ten mistakes developers make when working with Python and some of
the Python syntax that gives most developers problems.

Introduction

3

»» Updates: Sometimes changes happen. For example, I might not have seen

an upcoming change when I looked into my crystal ball during the writing of
this book. In the past, that simply meant the book would become outdated
and less useful, but you can now find updates to the book at by going to
www.dummies.com and searching this book’s title.

questions and demonstrations of useful book-related techniques at http://
blog.johnmuellerbooks.com/.

»» Companion files: Hey! Who really wants to type all the code in the book? Most

readers would prefer to spend their time actually working through coding
examples, rather than typing. Fortunately for you, the source code is available for
download, so all you need to do is read the book to learn Python coding techniques. Each of the book examples even tells you precisely which example project
to use. You can find these files at going to www.dummies.com and searching this
book’s title. On the page that appears, scroll down to the graphic of the book’s
the page that appears.

Where to Go from Here
It’s time to start your Programming with Python adventure! If you’re a complete
programming novice, you should start with Chapter 1 and progress through the
book at a pace that allows you to absorb as much of the material as possible.
If you’re a novice who’s in an absolute rush to get going with Python as quickly as
possible, you could skip to Chapter 2 with the understanding that you may find
some topics a bit confusing later. Skipping to Chapter 3 is possible if you already
have Python installed, but be sure to at least skim Chapter 2 so that you know
what assumptions were made when writing this book.
Readers who have some exposure to Python can save time by moving directly to
Chapter 4. It’s essential to install Anaconda to gain access to Jupyter Notebook,
which is the IDE used for this book. Otherwise, you won’t be able to use the downloadable source easily. Anaconda is free, so there is no cost involved.
Assuming that you already have Jupyter Notebook installed and know how to use
it, you can move directly to Chapter 6. You can always go back to earlier chapters
as necessary when you have questions. However, it’s important that you understand how each example works before moving to the next one. Every example has
important lessons for you, and you could miss vital content if you start skipping
too much information.

4

Beginning Programming with Python For Dummies

1

Getting Started
with Python

IN THIS PART . . .

Install Python on your Linux, Mac, or Windows system.
Interact with the Python-supplied tools.
Install and use Anaconda to write your first application.
Use Anaconda to perform useful work.

IN THIS CHAPTER

»» Creating programs to talk to your
computer
»» Understanding programs and their
creation
»» Considering why you want to use
Python

Chapter

1

Talking to Your
Computer

H

aving a conversation with your computer might sound like the script of a
science fiction movie. After all, the members of the Enterprise on Star Trek
regularly talked with their computer. In fact, the computer often talked
back. However, with the rise of Apple’s Siri (http://www.apple.com/ios/siri/),
Amazon’s Echo (https://www.amazon.com/dp/B00X4WHP5E/), and other ­interactive
software, perhaps you really don’t find a conversation so unbelievable.
Asking the computer for information is one thing, but providing it with instructions is quite another. This chapter considers why you want to instruct your computer about anything and what benefit you gain from it. You also discover the need
for a special language when performing this kind of communication and why you
want to use Python to accomplish it. However, the main thing to get out of this
chapter is that programming is simply a kind of communication that is akin to

CHAPTER 1 Talking to Your Computer

7

Understanding Why You Want to Talk
Talking to a machine may seem quite odd at first, but it’s necessary because a
it would still be communicating with you. Nothing can occur without an exchange
of information between the machine and you. Activities such as

»» Finding the greatest gift in the world
are all examples of communication that occurs between a computer and you. That
the computer further communicates with other machines or people to address
requests that you make simply extends the basic idea that communication is necessary to produce any result.
In most cases, the communication takes place in a manner that is nearly invisible
to you unless you really think about it. For example, when you visit a chat room
online, you might think that you’re communicating with another person. However,
the other person’s computer through the chat room (whatever it consists of), and
the other person’s computer is communicating with that person. Figure 1-1 gives
you an idea of what is actually taking place.
FIGURE 1-1:

Communication
with your
computer may be
invisible unless
you really think

Notice the cloud in the center of Figure 1-1. The cloud could contain anything, but
you know that it at least contains other computers running other applications.
These computers make it possible for your friend and you to chat. Now, think
about how easy the whole process seems when you’re using the chat application.
Even though all these things are going on in the background, it seems as if you’re
simply chatting with your friend, and the process itself is invisible.

8

PART 1 Getting Started with Python

Knowing that an Application is a Form
of Communication
Computer communication occurs through the use of applications. You use one
application to answer your email, another to purchase goods, and still another to
create a presentation. An application (sometimes called an app) provides the means
to express human ideas to the computer in a manner the computer can understand and defines the tools needed to shape the data used for the communication
in ­specific ways. Data used to express the content of a presentation is different from
data used to purchase a present for your mother. The way you view, use, and understand the data is ­different for each task, so you must use different applications to
interact with the data in a manner that both the computer and you can understand.
You can obtain applications to meet just about any general need you can conceive
of today. In fact, you probably have access to applications for which you haven’t
even thought about a purpose yet. Programmers have been busy creating millions
of applications of all types for many years now, so it may be hard to understand
what you can accomplish by creating some new method for talking with your
computer through an application. The answer comes down to thinking about the
data and how you want to interact with it. Some data simply isn’t common enough
to have attracted the attention of a programmer, or you may need the data in a
format that no application currently supports, so you don’t have any way to tell
the computer about it unless you create a custom application to do it.
The following sections describe applications from the perspective of working with
unique data in a manner that is special in some way. For example, you might have
access to a video library database but no method to access it in a way that makes
sense to you. The data is unique and your access needs are special, so you may
want to create an application that addresses both the data and your needs.

Thinking about procedures you use daily
A procedure is simply a set of steps you follow to perform a task. For example,
when making toast, you might use a procedure like this:

1.
2.
3.
4.
5.

Get the bread and butter from the refrigerator.
Open the bread bag and take out two pieces of toast.
Remove the cover from the toaster.
Place each piece of bread in its own slot.
Push the toaster lever down to start toasting the bread.

CHAPTER 1 Talking to Your Computer

9

6.
7.
8.
9.

Wait for the toasting process to complete.
Remove toast from the toaster.
Place toast on a plate.
Butter the toast.

Your procedure might vary from the one presented here, but it’s unlikely that
you’d butter the toast before placing it in the toaster. Of course, you do actually
have to remove the bread from the wrapper before you toast it (placing the bread,
wrapper and all, into the toaster would likely produce undesirable results). Most
people never actually think about the procedure for making toast. However, you
use a procedure like this one even though you don’t think about it.
Computers can’t perform tasks without a procedure. You must tell the computer
which steps to perform, the order in which to perform them, and any exceptions
to the rule that could cause failure. All this information (and more) appears within
an application. In short, an application is simply a written procedure that you use
to tell the computer what to do, when to do it, and how to do it. Because you’ve
been using procedures all your life, all you really need to do is apply the knowledge you already possess to what a computer needs to know about specific tasks.

Writing procedures down
toast. After we turned in our papers, she brought in a toaster and some loaves of
bread. Each paper was read and demonstrated. None of our procedures worked as
expected, but they all produced humorous results. In my case, I forgot to tell the
teacher to remove the bread from the wrapper, so she dutifully tried to stuff the
piece of bread, wrapper and all, into the toaster. The lesson stuck with me. Writing
about procedures can be quite hard because we know precisely want we want to
do, but often we leave steps out — we assume that the other person also knows
precisely what we want to do.
Many experiences in life revolve around procedures. Think about the checklist
used by pilots before a plane takes off. Without a good procedure, the plane could
crash. Learning to write a great procedure takes time, but it’s doable. You may
have to try several times before you get a procedure that works completely, but
eventually you can create one. Writing procedures down isn’t really sufficient,
though — you also need to test the procedure by using someone who isn’t familiar
with the task involved. When working with computers, the computer is your perfect test subject.

10

PART 1 Getting Started with Python

Seeing applications as being
like any other procedure
A computer acts like the grade school teacher in my example in the previous section. When you write an application, you’re writing a procedure that defines a
series of steps that the computer should perform to accomplish whatever task you
have in mind. If you leave out a step, the results won’t be what you expected. The
computer won’t know what you mean or that you intended for it to perform certain tasks automatically. The only thing the computer knows is that you have
provided it with a specific procedure and it needs to perform that procedure.

Understanding that computers
take things literally
People eventually get used to the procedures you create. They automatically compensate for deficiencies in your procedure or make notes about things that you left out.
In other words, people compensate for problems with the procedures that you write.
When you begin writing computer programs, you’ll get frustrated because computers perform tasks precisely and read your instructions literally. For example, if
you tell the computer that a certain value should equal 5, the computer will look
for a value of exactly 5. A human might see 4.9 and know that the value is good
enough, but a computer doesn’t see things that way. It sees a value of 4.9 and
decides that it doesn’t equal 5 exactly. In short, computers are inflexible, unintuitive, and unimaginative. When you write a procedure for a computer, the computer will do precisely as you ask absolutely every time and never modify your
procedure or decide that you really meant for it to do something else.

Defining What an Application Is
As previously mentioned, applications provide the means to define express human
ideas in a manner that a computer can understand. To accomplish this goal, the
application relies on one or more procedures that tell the computer how to perform the tasks related to the manipulation of data and its presentation. What you
see onscreen is the text from your word processor, but to see that information, the
computer requires procedures for retrieving the data from disk, putting it into a
form you can understand, and then presenting it to you. The following sections
define the specifics of an application in more detail.

CHAPTER 1 Talking to Your Computer

11

Understanding that computers
use a special language
Human language is complex and difficult to understand. Even applications such as
Siri and Alexa have serious limits in understanding what you’re saying. Over the
years, computers have gained the capability to input human speech as data and to
understand certain spoken words as commands, but computers still don’t quite
understand human speech to any significant degree. The difficulty of human
speech is exemplified in the way lawyers work. When you read legalese, it appears
as a gibberish of sorts. However, the goal is to state ideas and concepts in a way
that isn’t open to interpretation. Lawyers seldom succeed in meeting their objective precisely because human speech is imprecise.
Given what you know from previous sections of this chapter, computers could
never rely on human speech to understand the procedures you write. Computers
always take things literally, so you’d end up with completely unpredictable results
if you were to use human language to write applications. That’s why humans use
special languages, called programming languages, to communicate with computers.
These special languages make it possible to write procedures that are both specific
and completely understandable by both humans and computers.
Computers don’t actually speak any language. They use binary codes to flip
switches internally and to perform math calculations. Computers don’t even
understand letters — they understand only numbers. A special application turns
the computer-specific language you use to write a procedure into binary codes. For
the purposes of this book, you really don’t need to worry too much about the lowlevel specifics of how computers work at the binary level. However, it’s interesting
to know that computers speak math and numbers, not really a language at all.

Helping humans speak to the computer
It’s important to keep the purpose of an application in mind as you write it. An
application is there to help humans speak to the computer in a certain way. Every
application works with some type of data that is input, stored, manipulated, and
output so that the humans using the application obtain a desired result. Whether
the application is a game or a spreadsheet, the basic idea is the same. Computers
work with data provided by humans to obtain a desired result.
When you create an application, you’re providing a new method for humans to
speak to the computer. The new approach you create will make it possible for other
humans to view data in new ways. The communication between human and computer should be easy enough that the application actually disappears from view.
Think about the kinds of applications you’ve used in the past. The best applications are the ones that let you focus on whatever data you’re interacting with.

12

PART 1 Getting Started with Python

For example, a game application is considered immersive only if you can focus on
the planet you’re trying to save or the ship you’re trying to fly, rather than the
application that lets you do these things.
One of the best ways to start thinking about how you want to create an application
is to look at the way other people create applications. Writing down what you like
and dislike about other applications is a useful way to start discovering how you
want your applications to look and work. Here are some questions you can ask
yourself as you work with the applications:

»» What do I find distracting about the application?
»» Which features were easy to use?
»» Which features were hard to use?
»» How did the application make it easy to interact with my data?
»» How would I make the data easier to work with?
»» What do I hope to achieve with my application that this application doesn’t
provide?

Professional developers ask many other questions as part of creating an application, but these are good starter questions because they begin to help you think
about applications as a means to help humans speak with computers. If you’ve
ever found yourself frustrated by an application you used, you already know how
other people will feel if you don’t ask the appropriate questions when you create
your application. Communication is the most important element of any application you create.
You can also start to think about the ways in which you work. Start writing procedures for the things you do. It’s a good idea to take the process one step at a
time and write everything you can think of about that step. When you get finished, ask someone else to try your procedure to see how it actually works. You
might be surprised to learn that even with a lot of effort, you can easily forget to
include steps.
The world’s worst application usually begins with a programmer who doesn’t
know what the application is supposed to do, why it’s special, what need it
addresses, or whom it is for. When you decide to create an application, make sure
that you know why you’re creating it and what you hope to achieve. Just having a
plan in place really helps make programming fun. You can work on your new
application and see your goals accomplished one at a time until you have a completed application to use and show off to your friends (all of whom will think
you’re really cool for creating it).

CHAPTER 1 Talking to Your Computer

13

Understanding Why Python is So Cool
Many programming languages are available today. In fact, a student can spend an
entire semester in college studying computer languages and still not hear about
them all. (I did just that during my college days.) You’d think that programmers
would be happy with all these programming languages and just choose one to talk
to the computer, but they keep inventing more.
Programmers keep creating new languages for good reason. Each language has
something special to offer — something it does exceptionally well. In addition, as
computer technology evolves, so do the programming languages in order to keep
up. Because creating an application is all about efficient communication, many programmers know multiple programming languages so that they can choose just the
right language for a particular task. One language might work better to obtain data
from a database, and another might create user interface elements especially well.
As with every other programming language, Python does some things exceptionally well, and you need to know what they are before you begin using it. You might
be amazed by the really cool things you can do with Python. Knowing a programming language’s strengths and weaknesses helps you use it better as well as avoid
frustration by not using the language for things it doesn’t do well. The following

Unearthing the reasons for using Python
Most programming languages are created with specific goals in mind. These goals
help define the language characteristics and determine what you can do with the
language. There really isn’t any way to create a programming language that
does everything because people have competing goals and needs when creating
­applications. When it comes to Python, the main objective was to create a
­programming language that would make programmers efficient and productive.
With that in mind, here are the reasons that you want to use Python when creating
an application:

»» Less application development time: Python code is usually 2–10 times

shorter than comparable code written in languages like C/C++ and Java, which
means that you spend less time writing your application and more time using it.

»» Ease of reading: A programming language is like any other language — you

need to be able to read it to understand what it does. Python code tends to be
easier to read than the code written in other languages, which means you
spend less time interpreting it and more time making essential changes.

14

PART 1 Getting Started with Python

»» Reduced learning time: The creators of Python wanted to make a programming language with fewer odd rules that make the language hard to learn.
After all, programmers want to create applications, not learn obscure and
difficult languages.

Although Python is a popular language, it’s not always the most popular language
out there (depending on the site you use for comparison). In fact, it currently ranks
fifth on sites such as TIOBE (http://www.tiobe.com/index.php/content/
paperinfo/tpci/index.html), an organization that tracks usage statistics (among
other things). However, if you look at sites such as IEEE Spectrum (https://

spectrum.ieee.org/computing/software/the-2017-top-programminglanguages), you see that Python is actually the number-one language from that
site’s perspective. Tech Rapidly has it as the number-three language (see http://
techrapidly.com/top-10-best-programming-languages-learn-2018/).
If you’re looking for a language solely for the purpose of obtaining a job, Python
is a great choice, but Java, C/C++, or C# might be better choices, depending on the
kind of job you want to get. Visual Basic is also a great choice, even if it isn’t currently quite as popular as Python. Make sure to choose a language you like and one
basis of what you intend to accomplish. Python was the language of the year in
both 2007 and 2010 and has ranked as high as the fourth most popular language
in February 2011. So it truly is a good choice if you’re looking for a job, but not
necessarily the best choice. However, you may be surprised to learn that many
colleges now use Python to teach coding, and it has become the most popular language in that venue. Check out my blog post at http://blog.johnmuellerbooks.
com/2014/07/14/python-as-a-learning-tool for details.

Deciding how you can personally benefit
from Python
Ultimately, you can use any programming language to write any sort of application you want. If you use the wrong programming language for the job, the process will be slow, error prone, bug ridden, and you’ll absolutely hate it — but you
can get the job done. Of course, most of us would rather avoid horribly painful
experiences, so you need to know what sorts of applications people typically use
Python to create. Here’s a list of the most common uses for Python (although
people do use it for other purposes):

»» Creating rough application examples: Developers often need to create a

prototype, a rough example of an application, before getting the resources to
create the actual application. Python emphasizes productivity, so you can use
it to create prototypes of an application quickly.

CHAPTER 1 Talking to Your Computer

15

»» Scripting browser-based applications: Even though JavaScript is probably

the most popular language used for browser-based application scripting,
Python is a close second. Python offers functionality that JavaScript doesn’t
provide (see the comparison at https://blog.glyphobet.net/essay/2557
for details) and its high efficiency makes it possible to create browser-based
applications faster (a real plus in today’s fast-paced world).

»» Designing mathematic, scientific, and engineering applications: Interestingly
enough, Python provides access to some really cool libraries that make it easier
to create math, scientific, and engineering applications. The two most popular
libraries are NumPy (http://www.numpy.org/) and SciPy (http://www.scipy.
org/). These libraries greatly reduce the time you spend writing specialized code
to perform common math, scientific, and engineering tasks.

»» Working with XML: The eXtensible Markup Language (XML) is the basis of

most data storage needs on the Internet and many desktop applications
today. Unlike most languages, where XML is just sort of bolted on, Python
makes it a first-class citizen. If you need to work with a Web service, the main
method for exchanging information on the Internet (or any other XMLintensive application), Python is a great choice.

»» Interacting with databases: Business relies heavily on databases. Python
isn’t quite a query language, like the Structured Query Language (SQL) or
Language INtegrated Query (LINQ), but it does do a great job of interacting
with databases. It makes creating connections and manipulating data
relatively painless.

»» Developing user interfaces: Python isn’t like some languages like C# where

you have a built-in designer and can drag and drop items from a toolbox onto
the user interface. However, it does have an extensive array of graphical user
interface (GUI) frameworks — extensions that make graphics a lot easier to
create (see https://wiki.python.org/moin/GuiProgramming for details).
Some of these frameworks do come with designers that make the user
interface creation process easier. The point is that Python isn’t devoted to just
one method of creating a user interface — you can use the method that best

Discovering which organizations
use Python
Python really is quite good at the tasks that it was designed to perform. In fact,
that’s why a lot of large organizations use Python to perform at least some
application-creation (development) tasks. You want a programming language
that has good support from these large organizations because these organizations
tend to spend money to make the language better. Table 1-1 lists the large organizations that use Python the most.

16

PART 1 Getting Started with Python

TABLE 1-1

Large Organizations That Use Python

Vendor

URL

Application Type

Alice Educational Software –
Carnegie Mellon University

(https://www.alice.org/)

Educational applications

Fermilab

(https://www.fnal.gov/)

Scientific applications

Go.com

(http://go.com/)

Browser-based applications

Search engine

Industrial Light & Magic

(http://www.ilm.com/)

programming need

Lawrence Livermore
National Library

(https://www.llnl.gov/)

Scientific applications

National Space and Aeronautics

(http://www.nasa.gov/)

Scientific applications

New York Stock Exchange

(https://nyse.nyx.com/)

Browser-based applications

Redhat

(http://www.redhat.com/)

Linux installation tools

Yahoo!

(https://www.yahoo.com/)

Parts of Yahoo! mail

Graphics engine

Zope – Digital Creations

(http://www.zope.org/en/latest/)

Publishing application

These are just a few of the many organizations that use Python extensively. You
can find a more complete list of organizations at http://www.python.org/about/
success/. The number of success stories has become so large that even this list
probably isn’t complete and the people supporting it have had to create categories
to better organize it.

Finding useful Python applications
You might have an application written in Python sitting on your machine right
now and not even know it. Python is used in a vast array of applications on the
market today. The applications range from utilities that run at the console to
full-fledged CAD/CAM suites. Some applications run on mobile devices, while
others run on the large services employed by enterprises. In short, there is no
limit to what you can do with Python, but it really does help to see what others
have done. You can find a number of places online that list applications written
in Python, but the best place to look is https://wiki.python.org/moin/
Applications.

CHAPTER 1 Talking to Your Computer

17

As a Python programmer, you’ll also want to know that Python development tools
are available to make your life easier. A development tool provides some level of
automation in writing the procedures needed to tell the computer what to do.
Having more development tools means that you have to perform less work in
order to obtain a working application. Developers love to share their lists of favorite tools, but you can find a great list of tools broken into categories at http://
Of course, this chapter describes a number of tools as well, such as NumPy and
SciPy (two scientific libraries). The remainder of the book lists a few other tools;
make sure that you copy down your favorite tools for later.

Comparing Python to other languages
Comparing one language to another is somewhat dangerous because the selection
of a language is just as much a matter of taste and personal preference as it is any
sort of quantifiable scientific fact. So before I’m attacked by the rabid protectors
of the languages that follow, it’s important to realize that I also use a number of
languages and find at least some level of overlap among them all. There is no best
language in the world, simply the language that works best for a particular application. With this idea in mind, the following sections provide an overview comparison of Python to other languages. (You can find comparisons to other
languages at https://wiki.python.org/moin/LanguageComparisons.)

C#
A lot of people claim that Microsoft simply copied Java to create C#. That said, C#
main (undisputed) intent behind C# is to create a better kind of C/C++ language —
one that is easier to learn and use. However, we’re here to talk about C# and
Python. When compared to C#, Python has these advantages:

»» Significantly easier to learn
»» Smaller (more concise) code
»» Supported fully as open source
»» Better multiplatform support
»» Easily allows use of multiple development environments
»» Easier to extend using Java and C/C++
»» Enhanced scientific and engineering support

18

PART 1 Getting Started with Python

Java
For years, programmers looked for a language that they could use to write an
application just once and have it run anywhere. Java is designed to work well on
any platform. It relies on some tricks that you’ll discover later in the book to
accomplish this magic. For now, all you really need to know is that Java was so
successful at running well everywhere that other languages have sought to emulate it (with varying levels of success). Even so, Python has some important
advantages over Java, as shown in the following list:

»» Significantly easier to learn
»» Smaller (more concise) code
»» Enhanced variables (storage boxes in computer memory) that can hold
different kinds of data based on the application’s needs while running
(dynamic typing)

»» Faster development times
Perl
Perl was originally an acronym for Practical Extraction and Report Language.
Today, people simply call it Perl and let it go at that. However, Perl still shows its
roots in that it excels at obtaining data from a database and presenting it in report
format. Of course, Perl has been extended to do a lot more than that — you can use
it to write all sorts of applications. (I’ve even used it for a Web service application.)
In a comparison with Python, you’ll find that Python has these advantages over
Perl:

»» Simpler to learn
»» Enhanced protection for data
»» Better Java integration
»» Fewer platform-specific biases
R
Data scientists often have a tough time choosing between R and Python because
both languages are adept at statistical analysis and the sorts of graphing that data
scientists need to understand data patterns. Both languages are also open source

CHAPTER 1 Talking to Your Computer

19

and support a large range of platforms. However, R is a bit more specialized than
Python and tends to cater to the academic market. Consequently, Python has these
advantages over R in that Python:

»» Emphasizes productivity and code readability
»» Is designed for use by enterprises
»» Offers easier debugging
»» Uses consistent coding techniques
»» Has greater flexibility
»» Is easier to learn

20

PART 1 Getting Started with Python

IN THIS CHAPTER

»» Obtaining a copy of Python for
»» Performing the Python installation
»» Finding and using Python on your
system
as planned

Chapter

2

Copy of Python

C

reating applications requires that you have another application, unless you
really want to get low level and write applications in machine code — a
decidedly difficult experience that even true programmers avoid if at all
possible. If you want to write an application using the Python programming language, you need the applications required to do so. These applications help you
work with Python by creating Python code, providing help information as you
need it, and letting you run the code you write. This chapter helps you obtain a
copy of the Python application, install it on your hard drive, locate the installed
applications so that you can use them, and test your installation so that you can
see how it works.

Every platform (combination of computer hardware and operating system
­software) has special rules that it follows when running applications. The Python
application hides these details from you. You type code that runs on any platform
that Python supports, and the Python applications translate that code into something the platform can understand. However, in order for the translation to take

CHAPTER 2 Getting Your Own Copy of Python

21

place, you must have a version of Python that works on your particular platform.
Python supports these platforms (and possibly others):

»» Android
»» BeOS
»» Berkeley Software Distribution (BSD)/FreeBSD
»» Hewlett-Packard Unix (HP-UX)
»» IBM i (formerly Application System 400 or AS/400, iSeries, and System i)
»» iPhone Operating System (iOS)
»» Linux
»» Mac OS X (comes pre-installed with the OS)
»» Microsoft Disk Operating System (MS-DOS)
»» MorphOS
»» Operating System 2 (OS/2)
»» Operating System 390 (OS/390) and z/OS
»» PalmOS
»» PlayStation
»» Psion
»» QNX
»» RISC OS (originally Acorn)
»» Series 60
»» Solaris
»» Virtual Memory System (VMS)
»» Windows 32-bit (XP and later)
»» Windows 64-bit
»» Windows CE/Pocket PC
Wow, that’s a lot of different platforms! This book is tested with the Windows,
Mac OS X, and Linux platforms. However, the examples could very well work with
these other platforms, too, because the examples don’t rely on any platformspecific code. Let me know if it works on your non-Windows, Mac, or Linux

22

PART 1 Getting Started with Python

platform at John@JohnMuellerBooks.com. The current version of Python at the
time of this writing is 3.6.2. I’ll talk about any Python updates on my blog at
book-specific questions there, too.
To get the right version for your platform, you need to go to https://www.python.
from view, so you need to scroll halfway down the page. You see a page similar to
the one shown in Figure 2-1. The main part of the page contains links for
default setup that is used in this book. The platform-specific links on the left side
of the page show you alternative Python configurations that you can use when the
need arises. For example, you may want to use a more advanced editor than the
one provided with the default Python package, and these alternative configurations can provide one for you.

FIGURE 2-1:

The Python
all sorts of
versions.

If you want to work with another platform, go to https://www.python.org/
as shown in Figure 2-2. Many of these installations are maintained by volunteers
rather than by the people who create the versions of Python for Windows, Mac OS
X, and Linux. Make sure you contact these individuals when you have installation
questions because they know how best to help you get a good installation on your
platform.

CHAPTER 2 Getting Your Own Copy of Python

23

FIGURE 2-2:

Volunteers have
available on
all sorts of
­platforms.

Installing Python

»» Python interpreter
»» Help files (documentation)
»» Command-line access
»» Integrated DeveLopment Environment (IDLE) application
»» Preferred Installer Program (pip)
»» Uninstaller (only on platforms that require it)
This book assumes that you’re using one of the default Python setups found at
https://www.python.org/downloads/release/python-362/. If you use a version other than 3.6.2, some of the examples won’t work as anticipated. The following sections describe how to install Python on the three platforms directly
supported by this book: Windows, Mac OS X, and Linux.

24

PART 1 Getting Started with Python

Working with Windows
The installation process on a Windows system follows the same procedure that
you use for other application types. The main difference is in finding the file you
downloaded so that you can begin the installation process. The following procedure should work fine on any Windows system, whether you use the 32-bit or the
64-bit version of Python.

1.

The name of this file varies, but normally it appears as python-3.6.2.exe for
both 32-bit systems and python-3.6.2-amd64.exe for 64-bit systems. The
version number is embedded as part of the filename. In this case, the filename
refers to version 3.6.2, which is the version used for this book.

2.

Double-click the installation file.
(You may see an Open File – Security Warning dialog box that asks whether
you want to run this file. Click Run if you see this dialog box pop up.) You see a
Python Setup dialog box similar to the one shown in Figure 2-3. The exact
dialog box you see depends on which version of the Python installation

FIGURE 2-3:

The setup
process begins by
should have

3.

Choose a user installation option (the book uses the default setting of
Install for All Users).
Using a personalized installation can make it easier to manage systems that
have multiple users. In some cases, the personalized installation also reduces
the number of Security Warning dialog boxes you see.

CHAPTER 2 Getting Your Own Copy of Python

25

4.

Select Add Python 3.6 to PATH.
Adding this setting enables you to access Python from anywhere on your hard
drive. If you don’t select this setting, you must manually add Python to the path
later.

5.

Click Customize Installation.
Install asks you to choose which features to use with your copy of Python, as
shown in Figure 2-4. Keep all the features selected for this book. However, for
your own installation, you may find that you don’t actually require all the
Python features.

FIGURE 2-4:

Choose the
Python features
you want to
install.

6.

Click Next.
You see the Advanced Options dialog box, shown in Figure 2-5. Note that Install
for All Users isn’t selected, despite your having requested that feature earlier.
Install also asks you to provide the name of an installation directory for Python.
Using the default destination will save you time and effort later. However, you
can install Python anywhere you desire.

FIGURE 2-5:

Decide on an
installation
location for your
copy of Python.

26

PART 1 Getting Started with Python

Using the Windows \Program Files or \Program Files (x86) folder is
problematic for two reasons. First, the folder name has a space in it, which
makes it hard to access from within the application. Second, the folder usually
requires administrator access, so you’ll constantly battle the User Account
Control (UAC) feature of Windows if you install Python in either folder.

7. Select the Install for All Users option to ensure that the installer makes
Python accessible to everyone.

Note that selecting this option automatically selects the Precompile Standard
Library option, which you should keep selected.

8. Type a destination folder name, if necessary.
This book uses an installation folder of C:\Python36.

9. Click Install.
You see the installation process start. At some point, you might see a User
Account Control dialog box asking whether you want to perform the install. If
you see this dialog box, click Yes. The installation continues and you see an
Installation Complete dialog box.

10. Click Close.

Working with the Mac
Your Mac system likely already has Python installed on it. However, this installation is normally a few years old — or whatever the age of your system happens to
be. For the purposes of this book, the installation will likely work fine. You won’t
be testing the limits of Python programming technology — just getting a great
start using Python.
The Leopard version of OS X (10.5) uses a really old version of Python 2.5.1. This
particular version lacks direct access to the IDLE application. As a result, you may
find that some book exercises won’t work properly. The article at https://wiki.
python.org/moin/MacPython/Leopard tells you more about how to overcome
this particular issue. The code in this book is tested with OS X version 10.12 that
comes with Python 2.7.10, which is just fine for working through the examples in
the book. Later versions of OS X and Python will also likely work fine, but you may
see warnings about library use or other potential compatibility issues.”
Depending on how you use Python, you might want to update your installation at
some point. Part of this process involves installing the GNU Compiler Collection
(GCC) tools so that Python has access to the low-level resources it needs. The following steps get you started with installing a new version of Python on your Mac
OS X 10.6 or above system.

CHAPTER 2 Getting Your Own Copy of Python

27

1.

You see information regarding the latest version of Python, as shown previously
in Figure 2-1.

2.

Click the Mac OS X 64-bit/32-bit installer link.
monitoring the download process so that you can easily see how long the

3.

4.

Click Continue three times.
The installation program displays late-breaking notes about Python, licensing
finally, a destination dialog box.

5.

Click Install.
name and password, if required, into the dialog box and click OK. You see an
Installing Python dialog box. The contents of this dialog box will change as the
installation process proceeds so that you know what part of Python the installer
is working with.
After the installation is completed, you see an Install Succeeded dialog box.

6.

Click Close.
Python is ready to use. (You can close the disk image at this point and remove

Working with Linux
Some versions of Linux come with Python installed. For example, if you have a
Red Hat Package Manager (RPM)-based distribution (such as SUSE, Red Hat,
­Yellow Dog, Fedora Core, and CentOS), you likely already have Python on your
system and don’t need to do anything else.
Depending on which version of Linux you use, the version of Python varies and
some systems don’t include the Interactive DeveLopment Environment (IDLE)
application. If you have an older version of Python (2.5.1 or earlier), you might
want to install a newer version so that you have access to IDLE. Many of the book
exercises require use of IDLE.

28

PART 1 Getting Started with Python

You actually have two techniques to use to install Python on Linux. The following
sections discuss both techniques. The first technique works on any Linux distribution; the second technique has special criteria that you must meet.

Using the standard Linux installation
The standard Linux installation works on any system. However, it requires you to
work at the Terminal and type commands to complete it. Some of the actual commands may vary by version of Linux. The information at http://docs.python.
org/3/install/ provides some helpful tips that you can use in addition to the
procedure that follows.

1.

You see information regarding the latest version of Python, as shown previously in Figure 2-1.

2.

a. Gzipped source tarball (any version of Linux)

3.

When asked whether you want to open or save the file, choose Save.

4.

The Archive Manager window opens. After the files are extracted, you see the
Python 3.6.2 folder in the Archive Manager window.

5.

Double-click the Python 3.6.2 folder.
The Archive Manager extracts the files to the Python 3.6.2 subfolder of your
home folder.

6.

Open a copy of Terminal.
The Terminal window appears. If you have never built any software on your
system before, you must install the build essentials, SQLite, and bzip2 or the
Python installation will fail. Otherwise, you can skip to Step 10 to begin working
with Python immediately.

7.

Type sudo apt-get install build-essential and press Enter.
Linux installs the Build Essential support required to build packages (see
https://packages.debian.org/squeeze/build-essential for details).

CHAPTER 2 Getting Your Own Copy of Python

29

8. Type sudo apt-get install libsqlite3-dev and press Enter.
Linux installs the SQLite support required by Python for database manipulation
(see https://packages.debian.org/squeeze/libsqlite3-dev for details).

9. Type sudo apt-get install libbz2-dev and press Enter.
Linux installs the bzip2 support required by Python for archive manipulation
(see https://packages.debian.org/sid/libbz2-dev for details).

10. Type CD Python 3.6.2 in the Terminal window and press Enter.
Terminal changes directories to the Python 3.6.2 folder on your system.

11. Type ./configure and press Enter.
The script begins by checking the system build type and then performs a series
of tasks based on the system you’re using. This process can require a minute
or two because there is a large list of items to check.

12. Type make and press Enter.
Linux executes the make script to create the Python application software.
The make process can require up to a minute — it depends on the processing

13. Type sudo make altinstall and press Enter.
and press Enter. At this point, a number of tasks take place as the system

Using the graphical Linux installation
All versions of Linux support the standard installation discussed in the “Using the
standard Linux installation” section of this chapter. However, a few versions of
Debian-based Linux distributions, such as Ubuntu 12.x and later, provide a graphical installation technique as well. You need the administrator group (sudo) password to use this procedure, so having it handy will save you time. The following
steps outline the graphical installation technique for Ubuntu, but the technique is
similar for other Linux installations:

1.

Open the Ubuntu Software Center folder. (The folder may be named
Synaptics on other platforms.)
You see a listing of the most popular software available for download and
installation.

2.

Select Developer Tools (or Development) from the All Software dropdown list box.
You see a listing of developer tools, including Python.

30

PART 1 Getting Started with Python

3.

Double-click the Python 3.6.2 entry.
The Ubuntu Software Center provides details about the Python 3.6.2 entry and
offers to install it for you.

4.

Click Install.
Ubuntu begins the process of installing Python. A progress bar shows the
button changes to a Remove button.

5.

Close the Ubuntu Software Center folder.
You see a Python icon added to the desktop. Python is ready for use.

After you have Python installed on your system, you need to know where to find
it. In some respects, Python does everything it can to make this process easy by
performing certain tasks, such as adding the Python path to the machine’s path
information during installation. Even so, you need to know how to access the
installation, which the following sections describe.

As you work your way through the book, you’ll use either IDLE or the Python commandline shell to work with Python in the beginning. Later, you use Anaconda because it
­provides a significantly enhanced and easier-to-use method of interacting with Python.
The name of the graphical (GUI) environment, IDLE or Anaconda, is precisely the same
across all three platforms, and you won’t even see any significant difference in the
­presentation. The differences you do see are minor, and you should ignore them as
you work through the book. With this in mind, the book does rely heavily on Windows
screenshots — all the screenshots you see were obtained from a Windows system for
the sake of consistency.
The command-line shell also works precisely the same across all three platforms. The
presentation may vary a little more than IDLE or Anaconda does simply because the
shell used for each platform varies slightly. However, the commands you type for one
platform are precisely the same on another platform. The output is the same as well.
When viewing the screenshot, look at the content rather than for specific differences
in the presentation of the shell.

CHAPTER 2 Getting Your Own Copy of Python

31

Using Windows
A Windows installation creates a new folder in the Start menu that contains your
Python installation. You can access it by choosing Start ➪ All Programs ➪ Python
3.6. The two items of interest in the folder when creating new applications are
IDLE (Python GUI) and Python (command line). (Chapter 4 helps you ins