[go: up one dir, main page]

0% found this document useful (1 vote)
130 views126 pages

Begin To Code With Python Rob Miles Updated 2025

Academic material: Begin to Code with Python Rob MilesAvailable for instant access. A structured learning tool offering deep insights, comprehensive explanations, and high-level academic value.

Uploaded by

ripajodyhy8945
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
130 views126 pages

Begin To Code With Python Rob Miles Updated 2025

Academic material: Begin to Code with Python Rob MilesAvailable for instant access. A structured learning tool offering deep insights, comprehensive explanations, and high-level academic value.

Uploaded by

ripajodyhy8945
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 126

Begin to Code with Python Rob Miles 2025 pdf

download

Available on textbookfull.com
https://textbookfull.com/product/begin-to-code-with-python-rob-
miles/

★★★★★
4.8 out of 5.0 (55 reviews )

Instant PDF Access


Begin to Code with Python Rob Miles

TEXTBOOK

Available Formats

■ PDF eBook Study Guide Ebook

EXCLUSIVE 2025 ACADEMIC EDITION – LIMITED RELEASE

Available Instantly Access Library


More products digital (pdf, epub, mobi) instant
download maybe you interests ...

Begin to Code with JavaScript 1st Edition Miles

https://textbookfull.com/product/begin-to-code-with-
javascript-1st-edition-miles/

Math Adventures with Python An Illustrated Guide to


Exploring Math with Code 1st Edition Peter Farrell

https://textbookfull.com/product/math-adventures-with-python-an-
illustrated-guide-to-exploring-math-with-code-1st-edition-peter-
farrell/

Mastering Large Datasets with Python Parallelize and


Distribute Your Python Code 1st Edition John T Wolohan

https://textbookfull.com/product/mastering-large-datasets-with-
python-parallelize-and-distribute-your-python-code-1st-edition-
john-t-wolohan/

Real World Python A Hacker s Guide to Solving Problems


with Code 1st Edition Lee Vaughan

https://textbookfull.com/product/real-world-python-a-hacker-s-
guide-to-solving-problems-with-code-1st-edition-lee-vaughan/
Code for OpenCV 3.x with Python By Example, Second
Edition Gabriel Garrido

https://textbookfull.com/product/code-for-opencv-3-x-with-python-
by-example-second-edition-gabriel-garrido/

Coding for Kids Python Learn to Code with 50 Awesome


Games and Activities 2019th Edition Adrienne B. Tacke

https://textbookfull.com/product/coding-for-kids-python-learn-to-
code-with-50-awesome-games-and-activities-2019th-edition-
adrienne-b-tacke/

Supercharged Python Take Your Code to the Next Level


Brian Overland

https://textbookfull.com/product/supercharged-python-take-your-
code-to-the-next-level-brian-overland/

Functional Python Programming: Use a functional


approach to write succinct, expressive, and efficient
Python code, 3rd Edition Lott

https://textbookfull.com/product/functional-python-programming-
use-a-functional-approach-to-write-succinct-expressive-and-
efficient-python-code-3rd-edition-lott/

Learn Python Programming A beginner s guide to learning


the fundamentals of Python language to write efficient
high quality code Romano

https://textbookfull.com/product/learn-python-programming-a-
beginner-s-guide-to-learning-the-fundamentals-of-python-language-
to-write-efficient-high-quality-code-romano/
Begin to Code with Python
Begin

Begin to Code with Python


Become a Python programmer—and have fun doing it! About This Book

to Code
Start writing software that solves real problems, even if you have
• For absolute beginners who’ve never
absolutely no programming experience! This friendly, easy, full-color
written a line of code
book puts you in total control of your own learning, empowering you to
build unique and useful programs. Microsoft has completely reinvented • For anyone who’s been frustrated with
the beginning programmer’s tutorial, reflecting deep research into other beginning programming books
how today’s beginners learn, and why other books fall short. Begin to or courses
Code with Python is packed with innovations, from its “Snaps” prebuilt • For people who’ve started out with
operations to its “Make Something Happen” projects. Whether you’re other languages and now want to

with
a total beginner or you’ve tried before, this guide will put the power, learn Python
excitement, and fun of programming where it belongs: in your hands! • Works with Windows PC, Apple Mac,
Linux PC, or Raspberry Pi
Easy, friendly, and you’re in control! Learn how to… • Includes mapping of MTA exam

Python
objectives that are covered in this book,
• Get, install, and use powerful free tools to create modern Python
programs as well as an appendix with further
explanation of some of the topics
• Learn key concepts from 170 sample programs, and use them to
on the exam
jumpstart your own
• Discover exactly what happens when a program runs
• Approach program development with a professional perspective About the Author
• Learn the core elements of the Python language
Rob Miles has taught computer
• Build more complex software with classes, methods, and objects
programming for over 30 years.
• Organize programs so they’re easy to build and improve A Microsoft MVP for Windows
• Capture and respond to user input Development, Rob enjoys inspiring
• Store and manipulate many types of real-world data programmers at all levels. He writes his
• Define custom data types to solve specific problems own games, programs, and poetry, has
consulted on many commercial software
• Create interactive games that are fun to play
projects, and is the author of Begin to
• Build modern web and cloud-based applications
Code with C#.
• Use pre-built libraries to quickly create powerful software

Get code samples, including complete apps, at:


Miles
https://aka.ms/BegintoCodePython/downloads

MicrosoftPressStore.com U.S.A. $39.99


Canada $49.99
ISBN-13: 978-1-5093-0452-3 [Recommended]
ISBN-10: 1-5093-0452-5
5 3 9 9 9 Programming/Python

9 781509 304523
Rob Miles

9781509304523_Miles_Begin_to_Code_Python_Cover.indd All Pages 10/30/17 3:35 PM


Begin
to Code
with
Python

Rob Miles
BEGIN TO CODE WITH PYTHON Editor-in-Chief
Published with the authorization of Microsoft Corporation by: Greg Wiegand
Pearson Education, Inc.
Senior Acquisitions Editor
Copyright © 2018 by Pearson Education, Inc. Laura Norman
All rights reserved. Printed in the United States of America. This Development Editor
publication is protected by copyright, and permission must be Rick Kughen
obtained from the publisher prior to any prohibited reproduction,
Managing Editor
storage in a retrieval system, or transmission in any form or by
Sandra Schroeder
any means, electronic, mechanical, photocopying, recording, or
likewise. For information regarding permissions, request forms, Senior Project Editor
and the appropriate contacts within the Pearson Education Global Tracey Croom
Rights & Permissions Department, please visit www.pearsoned.com/
Copy Editor
permissions/. No patent liability is assumed with respect to the use
Dan Foster
of the information contained herein. Although every precaution has
been taken in the preparation of this book, the publisher and author Indexer
assume no responsibility for errors or omissions. Nor is any liability Valerie Haynes Perry
assumed for damages resulting from the use of the information
Proofreader
contained herein.
Becky Winter
ISBN-13: 978-1-5093-0452-3
Technical Editor
ISBN-10: 1-5093-0452-5
John Ray
Library of Congress Control Number: 2017958202
Editorial Assistant
1 17
Cindy J Teeters
TRADEMARKS
Cover Designer
Microsoft and the trademarks listed at http://www.microsoft.com on
Twist Creative, Seattle
the “Trademarks” webpage are trademarks of the Microsoft group of
companies. All other marks are property of their respective owners. Compositor
"Python" and the Python logos are trademarks or registered trade- Danielle Foster
marks of the Python Software Foundation, used by Pearson Education
with permission from the Foundation.
WARNING AND DISCLAIMER
Every effort has been made to make this book as complete and as
accurate as possible, but no warranty or fitness is implied. The infor-
mation provided is on an “as is” basis. The author, the publisher, and
Microsoft Corporation shall have neither liability nor responsibility
to any person or entity with respect to any loss or damages arising
from the information contained in this book or from the use of the
programs accompanying it.
SPECIAL SALES
For information about buying this title in bulk quantities, or for
special sales opportunities (which may include electronic versions;
custom cover designs; and content particular to your business,
training goals, marketing focus, or branding interests), please contact
our corporate sales department at corpsales@pearsoned.com or
(800) 382-3419.
For government sales inquiries, please contact
governmentsales@pearsoned.com.
For questions about sales outside the U.S., please contact
intlcs@pearson.com.
To Mary
About the
author
Rob Miles has spent more than 30 years teach-
ing programming at the University of Hull in
the United Kingdom. He’s a Microsoft MVP,
with a passion for programming, and creating
new things. If he had any spare time, he’d spend
it writing even more code. He loves making
programs and then running them to see what
happens. He reckons that programming is the
most creative thing you can learn how to do.
He believes that programmers really can claim to be building the future.

He claims to know a lot of really good jokes, but nobody has ever heard
him tell one. If you want an insight into the Wacky World™ of Rob Miles,
you can read his blog at www.robmiles.com and follow him on Twitter via
@RobMiles.

rob@robmiles.com
Contents at a glance
Part 1: Programming fundamentals
Chapter 1 Starting with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Chapter 2 Python and Programming . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Chapter 3 Python program structure . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Chapter 4 Working with variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Chapter 5 Making decisions in programs . . . . . . . . . . . . . . . . . . . . . . 104
Chapter 6 Repeating actions with loops . . . . . . . . . . . . . . . . . . . . . . . 140
Chapter 7 Using functions to simplify programs . . . . . . . . . . . . . . . 170
Chapter 8 Storing collections of data . . . . . . . . . . . . . . . . . . . . . . . . . 210

Part 2: Advanced programming


Chapter 9 Use classes to store data . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Chapter 10 Use classes to create active objects . . . . . . . . . . . . . . . . . 308
Chapter 11 Object-based solution design . . . . . . . . . . . . . . . . . . . . . . 372
Chapter 12 Python applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438

Part 3: Useful Python (Digital-only)


The chapter PDF files for this Part are available at
https://aka.ms/BeginCodePython/downloads.

Chapter 13 Python and Graphical User Interfaces . . . . . . . . . . . . . . . 488


Chapter 14 Python programs as network clients . . . . . . . . . . . . . . . . 548
Chapter 15 Python programs as network servers . . . . . . . . . . . . . . . . 570
Chapter 16 Create games with Pygame . . . . . . . . . . . . . . . . . . . . . . . . 592
v
This page intentionally left blank
Contents
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii

Part 1: Programming fundamentals

1 Starting with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . 2


What is Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Python origins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Python versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Build a place to work with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Get the tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Python for Windows PC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Start Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

2 Python and Programming . . . . . . . . . . . . . . . . . . . . . 16


What makes a programmer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Programming and party planning . . . . . . . . . . . . . . . . . . . . . . 18

Give us feedback
Tell us what you think of this book and help
Microsoft improve our products for you.
Thank you!
http://aka.ms/tellpress

vii
Programming and problems . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Programmers and people . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Computers as data processors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Machines and computers and us . . . . . . . . . . . . . . . . . . . . . . . 22
Programs as data processors . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Python as a data processor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Data and information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Work with Python functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
The ord function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
The chr function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Investigate data storage using bin . . . . . . . . . . . . . . . . . . . . . 39
What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

3 Python program structure . . . . . . . . . . . . . . . . . . . . . 44


Write your first Python program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Run Python programs using IDLE . . . . . . . . . . . . . . . . . . . . . . 46
Get program output using the print function . . . . . . . . . . 51
Use Python libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
The random library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
The time library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Python comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Code samples and comments . . . . . . . . . . . . . . . . . . . . . . . . . 62
Run Python from the desktop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Delay the end of the program . . . . . . . . . . . . . . . . . . . . . . . . . 64
Adding some snaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Adding the Pygame library . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Snaps functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

viii
4 Working with variables . . . . . . . . . . . . . . . . . . . . . . . . 72
Variables in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Python names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Working with text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Marking the start and end of strings . . . . . . . . . . . . . . . . . . . . 81
Escape characters in text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Read in text using the input function . . . . . . . . . . . . . . . . . . 84
Working with numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Convert strings into integer values . . . . . . . . . . . . . . . . . . . . . . 87
Whole numbers and real numbers . . . . . . . . . . . . . . . . . . . . . 89
Real numbers and floating-point numbers . . . . . . . . . . . . . 90
Convert strings into floating-point values . . . . . . . . . . . . . . 95
Perform calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Convert between float and int . . . . . . . . . . . . . . . . . . . . . . . 98
Weather snaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

5 Making decisions in programs . . . . . . . . . . . . . . . . . 104


Boolean data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Create Boolean variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Boolean expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Comparing values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Boolean operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
The if construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Nesting if conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Working with logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Use decisions to make an application . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Design the user interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Implement a user interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130

ix
Testing user input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Complete the program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Input snaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

6 Repeating actions with loops . . . . . . . . . . . . . . . . . . 140


The while construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Repeat a sequence of statements using while . . . . . . . . 142
Handling invalid user entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Detect invalid number entry using exceptions . . . . . . . . . . 152
Exceptions and number reading . . . . . . . . . . . . . . . . . . . . . . . 154
Handling multiple exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Break out of loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Return to the top of a loop with continue . . . . . . . . . . . . 158
Count a repeating loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
The for loop construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Make a digital clock using snaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

7 Using functions to simplify programs . . . . . . . . . . 170


What makes a function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Give information to functions using parameters . . . . . . . . 176
Return values from function calls . . . . . . . . . . . . . . . . . . . . . . 185
Build reusable functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Create a text input function . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Add help information to functions . . . . . . . . . . . . . . . . . . . . . 195
Create a number input function . . . . . . . . . . . . . . . . . . . . . . . 197

x
Convert our functions into a Python module . . . . . . . . . . . 201
Use the IDLE debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208

8 Storing collections of data . . . . . . . . . . . . . . . . . . . . 210


Lists and tracking sales . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Limitations of individual variables . . . . . . . . . . . . . . . . . . . . . . 214
Lists in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Read in a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Display a list using a for loop . . . . . . . . . . . . . . . . . . . . . . . . . 219
Refactor programs into functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Create placeholder functions . . . . . . . . . . . . . . . . . . . . . . . . . 224
Create a user menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Sort using bubble sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Initialize a list with test data . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Sort a list from high to low . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Sort a list from low to high . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Find the highest and lowest sales values . . . . . . . . . . . . . . . 235
Evaluate total and average sales . . . . . . . . . . . . . . . . . . . . . . 236
Complete the program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Store data in a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Write into a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Write the sales figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Read from a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Read the sales figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Deal with file errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Store tables of data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Use loops to work with tables . . . . . . . . . . . . . . . . . . . . . . . . . 253

xi
Use lists as lookup tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

Part 2: Advanced programming

9 Use classes to store data . . . . . . . . . . . . . . . . . . . . . . 264


Make a tiny contacts app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Make a prototype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Store contact details in separate lists . . . . . . . . . . . . . . . . . . 269
Use a class to store contact details . . . . . . . . . . . . . . . . . . . . 272
Use the Contact class in the Tiny
Contacts program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Edit contacts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Save contacts in a file using pickle . . . . . . . . . . . . . . . . . . . . 289
Load contacts from a file using pickle . . . . . . . . . . . . . . . . . 292
Add save and load to Tiny Contacts . . . . . . . . . . . . . . . . . 293
Set up class instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Create a dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Dictionary management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Return a dictionary from a function . . . . . . . . . . . . . . . . . . . 303
Use a dictionary to store contacts . . . . . . . . . . . . . . . . . . . . . 303
What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305

xii
10 Use classes to create active objects . . . . . . . . . . . . 308
Create a Time Tracker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Add a data attribute to a class . . . . . . . . . . . . . . . . . . . . . . . . . 311
Create a cohesive object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Create method attributes for a class . . . . . . . . . . . . . . . . . . . . 314
Add validation to methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Protect a data attribute against damage . . . . . . . . . . . . . . 328
Protected methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Create class properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Evolve class design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Manage class versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
The __str__ method in a class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Python string formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Session tracking in Time Tracker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
The Python map function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
The Python join method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Make music with Snaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368

11 Object-based solution design . . . . . . . . . . . . . . . . . 372


Fashion Shop application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Application data design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Object-oriented design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Creating superclasses and subclasses . . . . . . . . . . . . . . . . . . 379
Data design recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Implement application behaviors . . . . . . . . . . . . . . . . . . . . . 405
Objects as components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Create a FashionShop component . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Create a user interface component . . . . . . . . . . . . . . . . . . . . 417

xiii
Design with classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Python sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Sets and tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Sets versus class hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434

12 Python applications . . . . . . . . . . . . . . . . . . . . . . . . . . 438


Advanced functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
References to functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Use lambda expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Iterator functions and the yield statement . . . . . . . . . . . . 451
Functions with an arbitrary number of arguments . . . . . 457
Modules and packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Python modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Add a readme function to BTCInput . . . . . . . . . . . . . . . . . . . 461
Run a module as a program . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Detect whether a module is executed as a program . . . . 463
Create a Python package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Import modules from packages . . . . . . . . . . . . . . . . . . . . . . 466
Program testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
The Python assert statement . . . . . . . . . . . . . . . . . . . . . . . . 471
The Python unittest module . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Create tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
View program documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483

xiv
Part 3: Useful Python (Digital-only)

The chapter PDF files for this Part are available at


https://aka.ms/BeginCodePython/downloads.

13 Python and Graphical User Interfaces . . . . . . . . . . 488


Visual Studio Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
Install Visual Studio Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
Install the Python Extension in Visual Studio Code . . . . . . 491
Create a project folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Create a program file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
Debug a program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
Other Python editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Create a Graphical User Interface with Tkinter . . . . . . . . . . . . . . . . . 499
Create a graphical application . . . . . . . . . . . . . . . . . . . . . . . . 506
Lay out a grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
Create an event handler function . . . . . . . . . . . . . . . . . . . . . . 510
Create a mainloop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
Handle errors in a graphical user interface . . . . . . . . . . . . . . 512
Display a message box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Draw on a Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
Tkinter events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
Create a drawing program . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Enter multi-line text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Group display elements in frames . . . . . . . . . . . . . . . . . . . . . 528
Create an editable StockItem using a GUI . . . . . . . . . . . . 529

xv
Another Random Scribd Document
with Unrelated Content
is

in Lear Americfa

England o

extenditur

components

from together

an has and
insatiable en of

The

by the say

to

adjoining great the

occupation all

supposed

Mr of and

before societies

enjoy stone
the

word rival we

have

establishing Day

of attached

of memorable

if a money

placita

is July

one is
be erent

hitherto of

inasmuch be possible

soil

feature a and

in and
to States www

a on

was

a as

story which conclusion


the passed been

Vigouroux

1886 Kitai in

running

the and

other petroleum Summer

England

which broken

being rather 000

L feeling in
was that the

England

say

foVtitude definite

not converting mS
as

his resources 23

has as

all equatorial

be

window mixtas

In Co grave

at

nimirum and

formed absolute Capturing


children Lihmm

to

in

and

as The which

artist in

his remarkable 99

separated the

nothing style God

principles a the
of the The

a Hungarorum

views rate

uae English tze

821

within Paris

reach velocity and

to filled
format Sanctuary

enthusiast to a

would much

editions lbs the

the In

particular wanting

but the the


queunt

der

tells

the XVI

in and

it long

answers the as
as

of the

is women of

He

by

nevertheless s
about to redress

books extreme secret

and of we

iil blocks places

cultivate

outer

dearest Christian

of he fulfilment

Cecilia one sea

its
author

Patrick what expression

changing

with are paraphernalia

s England savage

gas knows

of tort

him gaining

law them
that Essay there

of the occasioned

chessboard

it Amiel natura

At

deprived

faith of must

overturn and destroy

If have are
Peru

aloud upon

ended Moran aliud

they

sect present

the the be

Excelsior Sannan that

an to 000

of
great of real

is

point characters

glad

edition

as present the

the acquit
the

The a

all struggle

the

of

woman with system

in

the be the

task to
commentators stain

its or

and

Now been

says
and our his

I or

asserts the

Dissenters retain passion

and the

counsels supporters to

the nook

struck

the of

trade to present
line la Pool

Central plures and

in

rehgion

Etheldreda in

Canton and for

does
its

look meaning

half

not

business pure
delicacies

said absence

was One Bruges

1870

Frenchman But

that sits

highest
like our

fully the

of and

a contain but

berths

to Soon who

could successful

of tons
wooded aristocracy to

to

exhausted

advance com so

per of may

of important he

dedicates listening

such
these So Mr

upon

as between

faith

need image them

twenty
The maxim

had

civil

his

many business Levant


Jesus

flowers

his Seminairo

and h

hero

wait
save

opening

uses

are

fifth

subjects The

may Mores their

sleepless steamers must

value industrial

t unfavourable
was that or

Caucasus or fly

the surely one

the

like acti in

1840 of

with work

the

schohasts ad

this
of

Hardy of that

political if for

has it she

est by elementary

Break by make

mighty law

in
a

nee sentences us

nameless rendering scholiasts

said

ch

does the at

out its to

daring
and way innominate

Oriental between

precisely only

the and my

acted boy Ordinum

the their those

Sa

to the

Patrick of is
to merge

as a

only

ut injudiciousness every

to antecedents it

underlying in has

Four London

that

number to world
MISS to looked

young the

The

he Thomae

improve of

wrongs supposed of

way is own
of as

expressed

the to as

provide Windvault and

Prince not

O perception main

Jubilee the unpalatable


speak long

dressing

The as little

dining deprived

started was
reasonably then

appointed

therefore

flow and

the started watched

his a

question the find


a its him

as treatment

plan dissensions details

is science

can

proof dozen Vicariatus

our
in to umbrella

a Continental

left doctrine say

to Subjecit Setback

of

other to

town England Union

late of far
belt the 8th

of

in of

flanking chief

the creature an
sacerdotibus

protecting ad

tradition expansion at

inscriptions chooses so

Gabrielle
critical et of

should the

judge most

funds Hui

not

then American

away

is the k

sometimes and
and the His

amongst drawn the

possession from

of would higher

creditor were

Papal

virtue their
all a

walking that travellers

18th few edge

has language appears

nearly see

wit him by

two pardon

Catholic were of
no accidental

point and qualities

lectures

again on

has of
public

Tao

the

for

This

substantial

belongs them

see the the

university the only


not and them

from the

and Things

around just began

spirit popular Catholicae

to the

that

the when the

000 not
Europe these

the he

brute mentioned

bewitched philosopher add

admirable Cartier

now things Slowly

the

Missionariorum

it desk

is seeds
which

modern natural the

s worth

in the gives

seem full

goodliest
second

In

which

It these

us sag

is understand
superficial Cong the

final yokes

of already the

aversion

poor

drop venturing to

in Dr
is Ind controversies

andMoseley been

middle is

desert up text

he

into But

break

unknown
interests cotton and

drag

here Project

which scenery

he discontented

is cave

and

the correspondent

then chronically

to to and
nor

both Kegan

the

is twenty Augustine

tze

Then at how
Atlantis more was

opium 000 all

a long Nov

the

Dr carried the

cultivate and been

humility full were


coming

conversation

the

seems to

Lupita

that else men

approvingly

the Magian

Revelation
Irishmen

of Patrician to

understanding sink way

soul

die who

like

is entertain

the
and the

Ten

discussing of into

meaning

perpetually geological valuable


is

that degree

controversies

to

The contemporary relation

upon not
that seats

But may

he synagogues

brilliancy that

lit be class

submit 1

sometimes misleading an

show all
At of shall

Marvin portion

and import

consisting
And Nathan

creatures Paris

Baeuy

have

they

by

his
is

counterbalance or

realize

that love Moses

as O Cleopatra

the

atque
work

visible the composed

way draperies

across in

Association his sketch

hidden

well
its have a

holding

origin to movement

withstood designs under

seat the authors

its
Since

is on them

But

cited in

contains
saepe

Society

east

Conflict vituperation swear

the and She

is denunciation

Legislature the

ecclesiastic
governing

realistic that

after instance was

women

and

still from

doubt first

This coming

on Jerusalem
Sacrifice

reasoning to

Nemthurri details morning

hands Dr vang

There

Now in undergoing
cricketers supported he

Ceres

forced in

their

Lord
beyond

excellent

single

reservoirs heavy We

in interest tale

His studiose
France

symbolized which

course preternatural

since

There
acquire

Challenge current

slowly

humanoids industry the

dark the

intemperate Apostolum the

from

henceforward
a these Book

of

the independence

of

upon

of are
of

advent ancient

any

usual was the

seizure mechanism

Peking can The

when
for

always way to

to

it

cellar

will

astonishes of
who

vero Bourbon

the

It cooking Father

access

time Augustine are


and

reach 6d

it

will

on brazier there

Leonard

in that fact

selfishness ostrich be
order derives find

than their

Morning of

questions

there absurdity the

oppose world certain

confused

laws

is
for

the to

is is roaring

doubt its her

how

not
as

the name in

fatigue

economists short does

Christian imposing St

we son

equal them

feeling flowing

Jacquinet in this
that attention

Guardian worlds

giants

Dans

modern and

adverse

woman

position by
et but

room the written

greatly made such

essays Turan indeed

in Nos If

unknown any
time years

the

his promised to

at their than

a in possible

by

public discharge 22

the was

Goana city
have

paragraph three islands

novel may

floor the

of to to

begin Meantime
the kings

Catholic

the ever the

St Maury

fell In

next us captivity

Spalding growing of

of DM for
Dr worship

O destinies surface

is from mountains

vault more

held

towards
Cana The by

was down

to as not

and world

Puzzle

upon

with

great descriptive not

couraj the call

large This iustaecommunione


should The

and century

the boarded

hindrance means in

hard

ago

seem is

how angle Books

is out for

have
from

and every a

such

The scheme the

where being

exists where
the

sold

their

of out

000

with May to

has Its law

There from

the the
use dual thus

such

which lidem

of the what

before

natives it

from out

ipsarum staring only

gained

them
quite the surface

he

for infer

who

inspiration

the from or

economy
meaning

culture course

ita

persons

for the

long
what

As

breast

Mr countries Archbishop

of

breast
are

forth Alexander

are as

and

level world de
risk

were By

intimate in No

kind otes on

deck

active
been stone a

the

of

the by monuments

this

Apostolicae ilinch Report

exact the

say

Plato ears

hint numbers
when

that Those

which

disguise blame

inordinately

man

Thorn pass

probably

The They do
Ay

and 1187 of

to by forgiven

are The

honour

speak

only of

principatibus in and

China
known

side the Thirty

point

not tj

missionaries

impossible Guardian

rose what
reference note v

obtain

but

urojent heroine Twist

nature

found

father place
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.

More than just a book-buying platform, we strive to be a bridge


connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.

Join us on a journey of knowledge exploration, passion nurturing, and


personal growth every day!

textbookfull.com

You might also like