Programming and Mathematical Thinking

Programming and Mathematical

Thinking

A Gentle Introduction to Discrete Math

Featuring Python

Allan M. Stavely

The New Mexico Tech Press

Socorro, New Mexico, USA

Programming and Mathematical Thinking

A Gentle Introduction to Discrete Math Featuring Python

Allan M. Stavely

Copyright ? 2014 Allan M. Stavely

First Edition

Content of this book available under the Creative Commons Attribution-Noncommercial-ShareAlike License. See

for details.

Publisher's Cataloguing-in-Publication Data

Stavely, Allan M

Programming and mathematical thinking: a gentle introduction to discrete

math featuring Python / Allan M. Stavely.

xii, 246 p.: ill. ; 28 cm

ISBN 978-1-938159-00-8 (pbk.) ¡ª 978-1-938159-01-5 (ebook)

1. Computer science ¡ª Mathematics. 2. Mathematics ¡ª Discrete

Mathematics. 3. Python (Computer program language).

QA 76.9 .M35 .S79 2014

004-dc22

OCLC Number: 863653804

Published by The New Mexico Tech Press, a New Mexico nonprofit corporation

The New Mexico Tech Press

Socorro, New Mexico, USA



Once more, to my parents, Earl and Ann

i

ii

Table of Contents

Preface ........................................................................................................ vii

1. Introduction ............................................................................................. 1

1.1. Programs, data, and mathematical objects ..................................... 1

1.2. A first look at Python .................................................................... 3

1.3. A little mathematical terminology ................................................ 10

2. An overview of Python ........................................................................... 17

2.1. Introduction ................................................................................. 17

2.2. Values, types, and names ............................................................. 18

2.3. Integers ........................................................................................ 19

2.4. Floating-point numbers ................................................................ 23

2.5. Strings .......................................................................................... 25

3. Python programs .................................................................................... 29

3.1. Statements ................................................................................... 29

3.2. Conditionals ................................................................................ 31

3.3. Iterations ..................................................................................... 35

4. Python functions ..................................................................................... 41

4.1. Function definitions ..................................................................... 41

4.2. Recursive functions ...................................................................... 43

4.3. Functions as values ...................................................................... 45

4.4. Lambda expressions ..................................................................... 48

5. Tuples ..................................................................................................... 51

5.1. Ordered pairs and n-tuples .......................................................... 51

5.2. Tuples in Python .......................................................................... 52

5.3. Files and databases ...................................................................... 54

6. Sequences ............................................................................................... 57

6.1. Properties of sequences ................................................................ 57

6.2. Monoids ...................................................................................... 59

6.3. Sequences in Python ..................................................................... 64

6.4. Higher-order sequence functions .................................................. 67

6.5. Comprehensions .......................................................................... 73

6.6. Parallel processing ....................................................................... 74

7. Streams ................................................................................................... 83

7.1. Dynamically-generated sequences ................................................ 83

7.2. Generator functions ..................................................................... 85

iii

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download