
Coding in Python and Elements of Discrete Mathematics Maria Litvin Phillips Academy, Andover, Massachusetts Gary Litvin Skylight Software, Inc. Skylight Publishing Andover, Massachusetts Skylight Publishing 9 Bartlet Street, Suite 70 Andover, MA 01810 web: http://www.skylit.com e-mail: [email protected] [email protected] Copyright © 2019 by Maria Litvin, Gary Litvin, and Skylight Publishing All rights reserved. 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, or otherwise, without the prior written permission of the authors and Skylight Publishing. Library of Congress Control Number: 2019905086 ISBN 978-0-9972528-4-2 The names of commercially available software and products mentioned in this book are used for identification purposes only and may be trademarks or registered trademarks owned by corporations and other commercial entities. Skylight Publishing and the authors have no affiliation with and disclaim any sponsorship or endorsement by any of these products’ manufacturers or trademarks’ owners. 1 2 3 4 5 6 7 23 22 21 20 19 Printed in the United States of America To Henry and Esther, digital natives Brief Contents Preface xi Chapter 1. An Introduction to Computers and Coding in Python 1 Chapter 2. Variables and Arithmetic 17 Chapter 3. Sets and Functions 35 Chapter 4. Algorithms and while and for Loops 63 Chapter 5. Strings, Lists, Dictionaries, and Files 77 Chapter 6. Number Systems 109 Chapter 7. Boolean Algebra and if-else Statements 127 Chapter 8. Digital Circuits and Bitwise Operators 149 Chapter 9. Turtle Graphics 167 Chapter 10. Sequences and Sums 191 Chapter 11. Parity, Invariants, and Finite Strategy Games 213 Chapter 12. Counting 235 Chapter 13. Probabilities 253 Chapter 14. Vectors and Matrices 275 Chapter 15. Polynomials 295 Chapter 16. Recurrence Relations and Recursion 311 Chapter 17. Graphs 327 Chapter 18. Number Theory and Cryptology 361 Appendices 387 Index 399 About the Authors Maria Litvin has taught computer science and mathematics at Phillips Academy in Andover, Massachusetts, since 1987. Prior to joining Phillips Academy, Maria taught computer science at Boston University. Maria has co-authored several popular computer science textbooks — C++ for You++: An Introduction to Programming and Computer Science (1998), Java Methods: Object-Oriented Programming and Data Structures (2001-2015), Be Prepared for the AP Computer Science Exam in Java, and 250 Multiple-Choice Computer Science Questions — as well as Continental Mathematics League (CML) computer science contests for elementary and middle school students. As a consultant for the College Board, Maria provides training for high school AP Computer Science teachers, and, since 2014, as a Code.org facilitator, Maria has trained hundreds of New England elementary school teachers in teaching computer science to children in grades K-5. Maria is the recipient of the 1999 Siemens Award for Advanced Placement for Mathematics, Science, and Technology for New England and the 2003 RadioShack National Teacher Award. Gary Litvin is the co-author of C++ for You++, Java Methods, Be Prepared for the AP Computer Science Exam in Java, 250 MC Questions, and CML computer science contests. Gary has worked in many areas of software development, including artificial intelligence, pattern recognition, computer graphics, and neural networks. As the founder of Skylight Software, Inc., he developed SKYLIGHTS/GX, one of the first visual programming tools for C and C++ programmers. Gary led the development of several state-of-the-art software products, including interactive touch screen development tools, OCR and handwritten character recognition systems, and credit card fraud detection software. Contents Preface xi Chapter 1. An Introduction to Computers and Coding in Python 1 1.1 Prologue 2 1.2 CPU and Memory 2 1.3 Python Interpreter 6 1.4 Using IDLE 12 1.5 Review 15 Chapter 2. Variables and Arithmetic 17 2.1 Prologue 18 2.2 Python Code Structure 18 2.3 Variables 24 2.4 Arithmetic Operators 30 2.5 Review 33 Chapter 3. Sets and Functions 35 3.1 Prologue 36 3.2 Sets in Math and in Python 37 3.3 Ways to Define a Function in Math 43 3.4 Functions in Python 46 3.5 Function Arguments 53 3.6 Python’s Built-In Functions 57 3.7 Review 62 Chapter 4. Algorithms and while and for Loops 63 4.1 Prologue 64 4.2 Algorithms 64 4.3 while and for Loops 67 4.4 Review 75 vii viii CONTENTS Chapter 5. Strings, Lists, Dictionaries, and Files 77 5.1 Prologue 78 5.2 Indices, Slices, and the in Operator 79 5.3 Strings 83 5.4 Lists and Tuples 90 5.5 Dictionaries 97 5.6 Files 101 5.7 Review 107 Chapter 6. Number Systems 109 6.1 Prologue 110 6.2 Positional Number Systems 111 6.3 The Binary, Octal, and Hexadecimal Systems 114 6.4 Representation of Numbers in Computers 119 6.5 Irrational Numbers 122 6.6 Review 125 Chapter 7. Boolean Algebra and if-else Statements 127 7.1 Prologue 128 7.2 Operations in Boolean Algebra 129 7.3 Logic and Sets 133 7.4 if-else Statements in Python 138 7.5 Review 147 Chapter 8. Digital Circuits and Bitwise Operators 149 8.1 Prologue 150 8.2 Gates 153 8.3 Bitwise Logical Operators 158 8.4 Review 166 Chapter 9. Turtle Graphics 167 9.1 Prologue 168 9.2 The turtle Module Basics 170 9.3 Coordinates and Text 179 9.4 Colors 185 9.5 Review 190 CONTENTS ix Chapter 10. Sequences and Sums 191 10.1 Prologue 192 10.2 Arithmetic and Geometric Sequences 193 10.3 Sums 196 10.4 Infinite Sums 200 10.5 Fibonacci Numbers 204 10.6 Review 212 Chapter 11. Parity, Invariants, and Finite Strategy Games 213 11.1 Prologue 214 11.2 Parity and Checksums 215 11.3 Invariants 220 11.4 Finite Strategy Games 225 11.5 Review 234 Chapter 12. Counting 235 12.1 Prologue 236 12.2 The Multiplication Rule 236 12.3 Permutations 239 12.4 Using Division 242 12.5 Combinations 245 12.6 Using Addition and Subtraction 249 12.7 Review 252 Chapter 13. Probabilities 253 13.1 Prologue 254 13.2 Calculating Probabilities by Counting 254 13.3 More Probabilities by Counting 259 13.4 Multiplication, Addition, and Subtraction 263 13.5 Pseudorandom Numbers 268 13.6 Review 274 Chapter 14. Vectors and Matrices 275 14.1 Prologue 276 14.2 Operations on Vectors 278 14.3 Matrices 285 14.4 Review 294 x CONTENTS Chapter 15. Polynomials 295 15.1 Prologue 296 15.2 Addition and Subtraction 297 15.3 Multiplication, Division, and Roots 300 15.4 Binomial Coefficients 305 15.5 Review 310 Chapter 16. Recurrence Relations and Recursion 311 16.1 Prologue 312 16.2 Recurrence Relations 312 16.3 Recursion in Programs 314 16.4 Mathematical Induction 321 16.5 Review 326 Chapter 17. Graphs 327 17.1 Prologue 328 17.2 Types of Graphs 331 17.3 Isomorphism of Graphs 335 17.4 Degree of a Vertex 338 17.5 Directed and Weighted Graphs 342 17.6 Adjacency Matrices 346 17.7 Coloring Maps and Graphs 350 17.8 The Four Color Theorem 353 17.9 Review 360 Chapter 18. Number Theory and Cryptology 361 18.1 Prologue 362 18.2 Euclid’s Algorithm 362 18.3 The Fundamental Theorem of Arithmetic 368 18.4 Arithmetic of Remainders 372 18.5 Ciphers 379 18.6 Review 386 Appendix A. Getting Started with Python www.skylit.com/python Appendix B. Selected Built-In, math, and random Functions 389 Appendix C. String Operations and Methods 391 Appendix D. List, Set, and Dictionary Operations and Methods 395 Index 399 Preface This book is a “Python early” remake of our earlier book Mathematics for the Digital Age and Programming in Python. We introduce more Python features earlier, giving the reader the necessary tools to start writing Python code sooner and in a more “pythonic” (idiomatic) manner. We have added two chapters — “Turtle Graphics” and “Vectors and Matrices” — and a separate section on Fibonacci numbers (in the “Sums and Sequences” chapter). We have updated many examples, exercises, and solutions. And we have changed the title to better match the sequence of topics and the fast-changing technological landscape and vocabulary. But the main idea of the book remains the same: to introduce the discrete mathematics concepts that we consider essential knowledge for any literate coder. This kind of math is very accessible, yet most K-12 math curricula in the United States do not yet include it. The math segments of this book include many hands-on coding exercises, which reinforce students’ learning of both coding and math. “So, is this a math book or a computer programming book?” This is probably the first question on the impatient reader’s mind. But why should it be? It is a librarian’s dilemma: “Does it go on the math shelf or on the computer shelf?” There is a simple solution: put a copy on each. The purpose of this book is to teach a particular way of thinking — precision thinking — and how to solve problems that require this way of thinking. Both mathematics and computer programming nourish the ability to think with precision and to solve problems that call for exact solutions. Mathematics teaches us to appreciate the beauty of a rigorous argument. In the long run, this is more valuable than a lesson on solving today’s practical problems. Still, mathematics does not exist in a vacuum — its abstractions are rooted in practical knowledge accumulated over centuries. The teaching of mathematics draws on examples and analogies from the world around us.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages14 Page
-
File Size-