100% found this document useful (10 votes)
40 views

PDF (Ebook) Learn Python Visually: Creative Coding with Processing.py by Tristan Bunn ISBN 9781718500969, 9781718500976, 1718500963, 1718500971 download

The document provides information about various ebooks available for download, including titles on Python programming and cooking. It lists several ebooks along with their ISBN numbers and links for purchase or download. Additionally, it includes details about the author and technical reviewer of one of the featured ebooks, 'Learn Python Visually: Creative Coding with Processing.py' by Tristan Bunn.

Uploaded by

ziadatasso7w
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (10 votes)
40 views

PDF (Ebook) Learn Python Visually: Creative Coding with Processing.py by Tristan Bunn ISBN 9781718500969, 9781718500976, 1718500963, 1718500971 download

The document provides information about various ebooks available for download, including titles on Python programming and cooking. It lists several ebooks along with their ISBN numbers and links for purchase or download. Additionally, it includes details about the author and technical reviewer of one of the featured ebooks, 'Learn Python Visually: Creative Coding with Processing.py' by Tristan Bunn.

Uploaded by

ziadatasso7w
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Download Full Version ebook - Visit ebooknice.

com

(Ebook) Learn Python Visually: Creative Coding


with Processing.py by Tristan Bunn ISBN
9781718500969, 9781718500976, 1718500963,
1718500971
https://ebooknice.com/product/learn-python-visually-
creative-coding-with-processing-py-23885702

Click the button below to download

DOWLOAD EBOOK

Discover More Ebook - Explore Now at ebooknice.com


Instant digital products (PDF, ePub, MOBI) ready for you
Download now and discover formats that fit your needs...

Start reading on any device today!

(Ebook) Biota Grow 2C gather 2C cook by Loucas, Jason;


Viles, James ISBN 9781459699816, 9781743365571,
9781925268492, 1459699815, 1743365578, 1925268497
https://ebooknice.com/product/biota-grow-2c-gather-2c-cook-6661374

ebooknice.com

(Ebook) Matematik 5000+ Kurs 2c Lärobok by Lena


Alfredsson, Hans Heikne, Sanna Bodemyr ISBN 9789127456600,
9127456609
https://ebooknice.com/product/matematik-5000-kurs-2c-larobok-23848312

ebooknice.com

(Ebook) SAT II Success MATH 1C and 2C 2002 (Peterson's SAT


II Success) by Peterson's ISBN 9780768906677, 0768906679

https://ebooknice.com/product/sat-ii-success-
math-1c-and-2c-2002-peterson-s-sat-ii-success-1722018

ebooknice.com

(Ebook) Master SAT II Math 1c and 2c 4th ed (Arco Master


the SAT Subject Test: Math Levels 1 & 2) by Arco ISBN
9780768923049, 0768923042
https://ebooknice.com/product/master-sat-ii-math-1c-and-2c-4th-ed-
arco-master-the-sat-subject-test-math-levels-1-2-2326094

ebooknice.com
(Ebook) Cambridge IGCSE and O Level History Workbook 2C -
Depth Study: the United States, 1919-41 2nd Edition by
Benjamin Harrison ISBN 9781398375147, 9781398375048,
1398375144, 1398375047
https://ebooknice.com/product/cambridge-igcse-and-o-level-history-
workbook-2c-depth-study-the-united-states-1919-41-2nd-edition-53538044

ebooknice.com

(Ebook) Learn Data Analysis with Python: Lessons in Coding


by A.J. Henley, Dave Wolf ISBN 9781484234853, 1484234855

https://ebooknice.com/product/learn-data-analysis-with-python-lessons-
in-coding-44859412

ebooknice.com

(Ebook) Tiny Python Projects: Learn coding and testing


with puzzles and games by Ken Youens-Clark ISBN
9781617297519, 1617297518
https://ebooknice.com/product/tiny-python-projects-learn-coding-and-
testing-with-puzzles-and-games-42879916

ebooknice.com

(Ebook) Tiny Python Projects: Learn coding and testing


with puzzles and games by Ken Youens-Clark ISBN
9781617297519, 1617297518
https://ebooknice.com/product/tiny-python-projects-learn-coding-and-
testing-with-puzzles-and-games-42970568

ebooknice.com

(Ebook) Learn Python By Coding Video Games (Intermediate):


A step-by-step guide to coding in Python fast by PATRICK
FELICIA ISBN 9798215956915, 8215956912
https://ebooknice.com/product/learn-python-by-coding-video-games-
intermediate-a-step-by-step-guide-to-coding-in-python-fast-51434196

ebooknice.com
FULL
COLOR

LE ARN PY THON
V I S U A L LY
C R E A T I V E C O D I N G W I T H
P R O C E S S I N G . P Y

TRISTAN BUNN
LEARN PYTHON VISUALLY
LEARN PYTHON
V I S U A L LY
C R E AT I V E CO D I N G W I T H P RO C E S S I N G .P Y

b y Tr is t a n B u nn

San Francisco
LEARN PYTHON VISUALLY. Copyright © 2021 by Tristan Bunn.

All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.

25 24 23 22 21 123456789

ISBN-13: 978-1-7185-0096-9 (print)


ISBN-13: 978-1-7185-0097-6 (ebook)

Publisher: William Pollock


Executive Editor: Barbara Yien
Production Editor: Katrina Taylor
Developmental Editors: Annie Choi and Jill Franklin
Cover Design: Gina Redman
Interior Design: Octopod Studios
Technical Reviewer: Paddy Gaunt
Copyeditor: Sharon Wilkey
Compositor: Craig Woods, Happenstance Type-O-Rama
Proofreader: Emelie Battaglia
Indexer: BIM Creatives, LLC

For information on book distributors or translations, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1-415-863-9900; [email protected]
www.nostarch.com

Library of Congress Control Number: 2020950273

No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other
product and company names mentioned herein may be the trademarks of their respective owners. Rather
than use a trademark symbol with every occurrence of a trademarked name, we are using the names only
in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of
the trademark.

The information in this book is distributed on an “As Is” basis, without warranty. While every precaution
has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any
liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly
or indirectly by the information contained in it.
About the Author
Tristan Bunn kicked off his web design career back in the days of
PlayStation 1, grunge music, and dial-up modems. Since then, he’s worked
on a diverse range of digital projects for varied clients. He’s currently
involved in lecturing, research, and work that blends code, interaction,
interface design, and creativity. Tristan has years of experience teaching
coding for art, games, web, and other creative technologies.

About the Tech Reviewer


Paddy Gaunt studied engineering at Cambridge University (UK), working
in the chemical and gas industries as well as textile manufacturing. Much
of the time, he had the responsibility of implementing IT systems as these
became a more significant part of management and marketing. Since its
launch in 2012, he has been the chief maintainer of the pi3d Python mod-
ule for fast 3D graphics on the Raspberry Pi microcomputer.
BRIEF CONTENTS

Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv

Chapter 1: Hello, World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Chapter 2: Drawing More Complicated Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

Chapter 3: Introduction to Strings and Working with Text . . . . . . . . . . . . . . . . . . . . . . . . . . 53

Chapter 4: Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

Chapter 5: Iteration and Randomness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

Chapter 6: Motion and Transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

Chapter 7: Working with Lists and Reading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133

Chapter 8: Dictionaries and JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

Chapter 9: Functions and Periodic Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175

Chapter 10: Object-Oriented Programming and PVector . . . . . . . . . . . . . . . . . . . . . . . . . 207

Chapter 11: Mouse and Keyboard Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239

Afterword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
CONTE NT S IN DE TA IL

ACKNOWLEDGMENTS XIII

INTRODUCTION XV
Who Is This Book For? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi
What Is Python Mode for Processing? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi
What Are Algorithms? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
What Is Creative Coding? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii
Where Can I Find Help? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx
Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Source Code and Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
What’s in This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Let’s Go! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii

1
HELLO, WORLD! 1
Processing Installation and Python Mode Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Your First Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Fills and Strokes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Background Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Color Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2D Primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
triangle() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
ellipse() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
quad() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
line() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Challenge #1: Rainbow Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Basic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Modulo Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Arcs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Challenge #2: Disk Usage Analyzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

2
DRAWING MORE COMPLICATED SHAPES 29
Displaying a Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Drawing Curves Using Catmull-Rom Splines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Curving Lines with curve() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Changing Curves with curveTightness() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Drawing Bézier Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Using the bezier() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Positioning Anchor and Control Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Drawing Shapes Using Vertices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Bézier Vertices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Using Vector Graphics Software for Generating Shapes . . . . . . . . . . . . . . . . . . . . . . . . 50
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

3
INTRODUCTION TO STRINGS AND WORKING WITH TEXT 53
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Creating Strings in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Using Concatenation and String Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Working with String Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
String Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Slice Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Typography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Text Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

4
CONDITIONAL STATEMENTS 69
Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
The Boolean Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
elif Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Challenge #3: Four-Square Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

5
ITERATION AND RANDOMNESS 85
Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Using Iteration to Draw Concentric Circles . . . . . . . . . . . . . . . . . . . . . . . . . . 86
while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Challenge #4: Create Line Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
break and continue Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Randomness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
random() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Random Seed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Truchet Tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

x Contents in Detail
6
MOTION AND TRANSFORMATION 105
Perceiving Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Adding Motion to Processing Sketches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
The draw() and setup() Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Saving Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Challenge #5: DVD Screensaver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Processing Transformation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
translate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
rotate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
scale() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
shearX() and shareY() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
pushMatrix() and popMatrix() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Challenge #6: Analog Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

7
WORKING WITH LISTS AND READING DATA 133
Introducing Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Creating and Accessing Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Modifying Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Combining Loops and Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Drawing Shapes by Using a List of Color Values . . . . . . . . . . . . . . . . . . . . . 140
Looping with enumerate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Creating Lists of Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Challenge #7: Breakout Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Reading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
File Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
CSV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Challenge #8: Games Sales Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

8
DICTIONARIES AND JSON 159
Introducing Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Accessing Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Modifying Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Nesting Dictionaries and Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Combining Loops and Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Iterating Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Iterating Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Iterating Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Working with JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Understanding JSON Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Using Web APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Reading in JSON Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Challenge #9: Coffee Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

Contents in Detail xi
9
FUNCTIONS AND PERIODIC MOTION 175
Defining Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Creating a Simple Speech Bubble Function . . . . . . . . . . . . . . . . . . . . . . . . . 176
Drawing Compound Shapes Using a Function . . . . . . . . . . . . . . . . . . . . . . . 179
Adding Arguments and Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Using Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Setting Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Mixing Positional and Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 186
Returning Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Defining Functions for Periodic Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
An Introduction to Trigonometric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Circular and Elliptical Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Sine Waves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Lissajous Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Creating Screensaver-Like Patterns with Lissajous Curves . . . . . . . . . . . . . . . . 203
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206

10
OBJECT-ORIENTED PROGRAMMING AND PVECTOR 207
Working with Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Defining a New Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Creating an Instance from a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Adding Attributes to a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Adding Methods to a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Splitting Your Python Code into Multiple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Programming Movement with Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
The PVector Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Moving an Amoeba with PVector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Adding Many Amoebas to the Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Challenge #10: Collision Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

11
MOUSE AND KEYBOARD INTERACTION 239
Mouse Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Mouse Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Creating a Paint App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Keyboard Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Adding Keyboard Shortcuts to the Paint App . . . . . . . . . . . . . . . . . . . . . . . . 251
Challenge #11: Adding Paint App Features . . . . . . . . . . . . . . . . . . . . . . 252
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253

AFTERWORD 255

INDEX 259

xii 
ACKNOWLEDGMENTS

I’ve been a fan of No Starch Press books for years, and I’m thrilled to have
them publish my first book. I’d like to thank everybody there, in particular
my editors, Jill Franklin and Annie Choi, for their invaluable feedback and
direction. Thanks to Paddy Gaunt, the technical reviewer, for checking over
all my code and offering some excellent suggestions to improve it.
Additionally, I’d like to thank the creators, maintainers, and community
surrounding Processing and its related projects, and also the developers of
the Python programming language. You’ve inspired my students and me, and
it’s a privilege to share your hard work with everybody who reads this book.
INTRODUCTION

When I first encountered programming


code, I stared, amazed and bewildered, at
a screen of obscure commands and symbols
and wondered how anybody could understand
it, let alone write it. I’d hit the F5 key, and the program
would magically spawn a cityscape in which two play-
ers, depicted as gorillas, could hurl explosive bananas
at each other. I tried changing a few lines to see what
would happen, and on occasion, it was something pre-
dictable or cool. More often than not, the game would
simply fail to run. In a futile attempt to be helpful, the computer would diag-
nose my errors, rambling on about syntax and “illegal” operations of varying
description.
For some years thereafter, I was content to avoid learning to program.
That began to change when I became interested in making my creative
work more interactive. You may already have encountered a few of the same
barriers that frustrated me. Maybe you were getting by just fine with visual
tools but then hit a wall. Or to your disappointment (and horror?), you dis-
covered that what you sought to accomplish required delving into code.
Software applications, with all of their graphical widgets, make us feel
like we’re in control. The illusion, however, soon fades when you discover
that the tool you desire is missing. Through learning to program, you gain
a true mastery of your computer.

Who Is This Book For?


This book assumes no prior programming experience. It strives to make
the process of learning to program as visual and entertaining as possible.
The content is based on my extensive experience teaching first-time coders,
designers, and interactive media students. The skills and knowledge you’ll
gain are fundamental to programming for an ever-expanding horizon of
creative technologies, such as games, the web, augmented/virtual reality,
and even visual effects for films.
If you’re an artist, student, designer, researcher, or just somebody keen
on picking up coding skills, Python Mode for Processing is excellent for
learning to program in a visual context.
For anybody with prior programming experience, this book would be
useful for learning Python, Python Mode for Processing, or creative coding
techniques.
You may have experience with another visual programming language—
something like Scratch, where you connect together graphical elements like
boxes, icons, and arrows. Python is not such a language—rather, it is a tex-
tual programming language that requires you to type code. To make learn-
ing visual, though, you’ll focus on writing code that produces drawings,
patterns, animations, data visualizations, user interfaces, and simulations.
This approach not only makes for cool-looking graphics, but also helps you
visualize the underlying concepts of programming.

What Is Python Mode for Processing?


Python Mode for Processing combines the Python programming language and
Processing, a development environment for interactive and graphics program-
ming. You’ll also see Python Mode for Processing referred to as Processing.py.
The project started as a command line tool named Processing.py, but its
developer decided to label it Python Mode when it was made available for
the Processing development environment. In this book, you can consider the
terms largely interchangeable.
Python is one of the most popular programming languages in use
today. There are many good reasons for this, but here’s why you should
care. First, Python is a beginner-friendly language. It’s more approachable
than languages like Java or C++, so you’ll find it easier to read, write, and

xvi Introduction
understand. Second, it’s a general-purpose language, suitable for program-
ming artificial intelligence (AI), games, simulations, web applications, and
just about everything in between.
Processing, which has been around since the early 2000s, is composed
of a programming language and an editor for writing and compiling code.
It provides a collection of special commands that allow you to draw, animate,
and handle user input by using code. The creators, Casey Reas and Ben Fry,
developed Processing to make programming more accessible for designers
and artists, although its thriving user base has grown to include researchers,
hobbyists, and educators.
Java is the basis for the original Processing programming language,
but other variants have since appeared, including JavaScript (p5.js) and
Ruby (JRubyArt) versions. In 2010, Jonathan Feinberg created Processing.py,
which you can think of as a sort of extension for Processing that allows you
to write Python instead of Java-esque code.
Both Python and Processing are open source and won’t cost you a cent.
What’s more, you can use them on just about any platform, including Linux,
macOS, and Microsoft Windows.

What Are Algorithms?


You’ll encounter the term algorithm frequently in the domain of pro-
gramming. You can think of an algorithm as a set of rules a computer or
machine must follow to achieve a particular goal. As an example, an algo-
rithm for making a cup of instant coffee would read as follows:

1. Place one teaspoon of coffee granules in a mug.


2. Fill the kettle with water.
3. Switch on the kettle.
4. Once the water has boiled, add 240 ml boiling water to the mug.
5. Add one level teaspoon of sugar to the same mug.
6. Stir the contents.
7. Serve.

However, this set of steps is insufficient for programming a real-life


coffee-making robot. Should the sizes of the mugs vary, smaller ones would
overflow. Furthermore, the robot would ignore any requests for milk or
extra sugar. Computers cannot make any assumptions, and require explicit
and unambiguous direction, communicated in a language that machines
understand—like Python. Learning the Python language may be the hur-
dle you face initially, but as you grow more fluent, the challenge will shift
toward the mastery of algorithmic thinking.

Introduction xvii
What Is Creative Coding?
Creative coding is computer programming for creative output. This broad term
encompasses, but is not limited to, computer-generated audio and visual art,
interactive installations, experimental games, and data visualizations.
Take, for example, Frederic Brodbeck’s Cinemetrics project. Using Python,
Brodbeck developed a program that analyzes DVD movie data to generate
visual fingerprints of films. The fingerprint is an open ring formed from many
segments; a single segment represents a span of 10 shots, and the concentric
bands show the color breakdown for each of those segments. The diagonal
length of each segment indicates the amount of motion. Figure 1 is a
Cinemetrics fingerprint for the film Quantum of Solace (2008).

beginning
end
amount of
motion
1 segment
= 10 shots

chapter
color palette

Figure 1: Quantum of Solace fingerprint, created by Frederic Brodbeck. Screenshot from http://cinemetrics.site/.

The fingerprints can also be animated, in which case motion is instead


visualized using pulsating segments. An interactive interface provides a
selection of presets and filters so that you can arrange fingerprints along-
side one another and make comparisons—for example, between originals
and remakes, different genres, the works of a single director, and so forth.
Figure 2 compares (from left to right) 2001: A Space Odyssey (1968), The
Simpsons Movie (2007), and a soccer match.

xviii Introduction
Figure 2: Fingerprints comparing (left to right) 2001: A Space Odyssey, The Simpsons Movie, and a soccer
match. Screenshots from http://cinemetrics.site/.

Many creative coding projects employ a similar underlying approach, in


which data is fed into a program to influence how it controls output. Music
visualizations with real-time audio synchronization—like those featured
in popular media player software—are a good example. However, you can
experiment with plenty of other data sources, such as web feeds, fitness track-
ers, environmental sensors, and a plethora of public datasets.
In some instances, it’s useful to opt for randomized data values.
Consider procedurally generated game content. As opposed to construct-
ing levels manually, you can program games to generate dungeon layouts,
terrain, narrative elements, and enemy spawn locations automatically. Of
course, such games should include sensible constraints; for instance, a cap
on the total number of enemies that can appear at once, and algorithms
for ensuring that stage layouts are not impossible to traverse.
Game characters may be composed using a random selection of modu-
lar components, or generated entirely from shapes and formulas. As an
example, I’ve written a Processing Python program that generates the
randomized microbial beasties displayed in Figure 3. The code—an adap-
tation of Lieven Menschaert’s NodeBox script Aquatics!—spawns a creature
with a random fill color, shape (defined by something named the superfor-
mula), and no fewer than three eyes. There’s a 70 percent chance that hair
will grow along the creature’s edges, which can be swayed by the force of a
randomly directed current.

Introduction xix
Figure 3: A Processing.py adaption of Lieven Menschaert’s NodeBox script Aquatics!

Countless examples of cool, creative coding projects exist—from


robots that doodle and write poetry, to evolutionary simulators, and even
a program that pores over satellite imagery in search of architecture or
infrastructure that resembles letters (The Aerial Bold Project by Benedikt
Groß and Joey Lee, 2016).
Perhaps this creative coding thing sounds a bit too artsy for you?
Processing also isn’t ideal for that race car sim you’ve always dreamed of
building, and it’s definitely no good for backend web development. That’s
okay. Creative coding with Processing’s Python Mode need not be the ulti-
mate goal of reading this book. Think of it as a starting point for exploring
Python, other frameworks, creative applications for coding, and program-
ming in general.

Where Can I Find Help?


Programming is rewarding, in part because it’s challenging. If you find
yourself struggling with something, do not stress; that’s normal! With a
little perseverance, you’ll soon grasp whatever has you snagged.

xx Introduction
Online Resources
If you’re getting nowhere, reach out to online communities. You’ll find a
dedicated category for Processing.py in the official Processing forum at
https://discourse.processing.org/. You’ll often discover that somebody has already
encountered and received a solution for the challenge you’re facing; if not, go
ahead and create a new topic. Incidentally, the author of this book is known
to lurk about in this friendly and welcoming corner of the internet.
The official Python Mode reference is available at https://py.processing
.org/reference/. Each entry includes a description and brief code example. It’s
handy to keep this web page open while you work in the Processing develop-
ment environment.

Source Code and Solutions


You’ll be typing a lot of code. This is a good thing because the best way
to learn is by doing. At times, however, you might mistype something or
be unable to figure why your code refuses to work. In such instances, it
can be helpful to have access to a complete, working version of the file.
You can access all of the code in this book, as well as solutions to the chal-
lenges in each chapter, at https://github.com/tabreturn/processing.py-book/.
You can also find any updates to this book at https://www.nostarch.com/
Learn-Python-Visually/.

What’s in This Book?


This book begins with the basics and builds up toward more advanced
topics as you progress. Each chapter, therefore, requires a grasp of the
concepts introduced in the chapters preceding it. You’ll be working, step
by step, through a series of practical tasks. You’ll also find some theory,
plenty of visuals, and challenges to consolidate what you’ve learned.
The following outline provides a brief overview of the contents in
each chapter:
Chapter 1: Hello, World!   This chapter covers the installation and
setup procedure for the book and introduces the basics of drawing with
code. You’ll also learn how computers manage color, how you can store
and reuse values (using variables), and how to perform basic arithmetic
operations using Python.
Chapter 2: Drawing More Complicated Shapes   Having covered some
drawing essentials in the first chapter, you’ll move on to drawing more
organic shapes, as opposed to geometric ones. You’ll learn to define
shapes by using points (or vertices) and curves, which enable you to
draw just about any shape with code.
Chapter 3: Introduction to Strings and Working with Text   In this
chapter, you’ll learn how to use Python’s string features to manipulate
text. You’ll also learn how to use Processing functions to draw text to the
display window, in different styles and colors, and in different fonts.

Introduction xxi
Chapter 4: Conditional Statements   This is where you really begin to
think like a programmer. In this chapter, you’ll introduce control flow
to your programs. In other words, you’ll learn how to write programs
that can make decisions, executing different actions to respond to dif-
ferent situations.
Chapter 5: Iteration and Randomness   In this chapter, you’ll learn how
to write programs that can repeat an operation a specified number of
times or until a certain requirement is met. Toward the end of the chap-
ter, you’ll experiment with randomness and creating tiled patterns.
Chapter 6: Motion and Transformation   This chapter focuses primar-
ily on adding motion to your Processing programs and transforming the
drawing space. You’ll also learn how to save frames as images and how to
get time values from your computer. You’ll use these skills to create an
animated screensaver and analog clock.
Chapter 7: Working with Lists and Reading Data   Python lists will
unlock powerful ways to manage and manipulate values in collections.
You’ll explore techniques for data visualization. You’ll also learn to read
in list data from external files. For the final task, you’ll render a chart by
using a CSV file.
Chapter 8: Dictionaries and JSON   Dictionaries are similar to lists
in that they store collections of items. With dictionaries, however, you
access items by using a key (usually a word) instead of referring to the
item position. Once again, you’ll get to use your new dictionary skills
for data visualization. You’ll also learn to work with JSON data.
Chapter 9: Functions and Periodic Motion   You’ll use functions to
divide a program into named sections of reusable code. This will make
your code more modular, and easier to read and modify. You’ll also delve
into some trigonometry for generating elliptical and wave-type motions.
Chapter 10: Object-Oriented Programming and PVector   You can use
object-oriented programming to structure programs by modeling real-
world objects. In this chapter, you’ll employ an object-oriented approach
to building an amoeba simulation. You’ll also learn to program the
amoebas’ motion by using Processing’s PVector class.
Chapter 11: Mouse and Keyboard Interaction   In this chapter, you’ll
add interactivity to your programs. Processing can handle input from
various devices, but here you’ll focus on mouse and keyboard input to
build a paint app. In the process, you’ll learn about event functions and
how to control Processing’s draw loop behavior.

Let’s Go!
The speed at which you progress through these chapters is likely to be influ-
enced by your prior experience in similar areas. If you’ve done any type of
programming before, Python or otherwise, you’ll encounter some familiar
concepts. That said, it’s not a race! Enjoy the ride, stop for breaks, and if
you’re feeling really inspired, feel free to head off-road.

xxii Introduction
1
H E L L O, W O R L D !

When learning a new programming lan­


guage, it’s a long-standing tradition that
the first code you write is to display the
message ‘Hello, World!’ In keeping with that
tradition, you’ll do that here too—but that’s not
all. This chapter introduces everything you need to
understand the fundamentals of Processing, and
you’ll quickly move on from a simple ‘Hello, World!’
to drawing with code.
To get started, you’ll set up Python Mode for Processing so you can create
your own sketches. Along the way, you’ll learn the basic rules of writing code
in Processing, as well as how to deal with errors, use variables, and perform
arithmetic operations. You’ll also learn about how Processing handles color
and how to measure angles using radians. By the end of this chapter, you’ll be
comfortable drawing colorful geometric shapes by using various Processing
functions. Let’s get started.
Processing Installation and Python Mode Setup
Before writing any code, you first need to set up Python Mode for Processing.
Head over to the Processing downloads web page (https://processing.org/
download/) and grab the version of Processing appropriate for your system
(Windows, Linux, or macOS). As of January 2021, Processing 3.5.4 is the lat­
est stable release.
Processing does not employ an installation program. Instead, you simply
extract the file you have downloaded (usually a .zip archive) and run the
application. The exact process varies slightly between operating systems:

• On Windows, unzip all of the contents by right-clicking the file and


selecting Extract All, and then follow the instructions. Extract or move
the folder to any location on your computer, including your Program
Files folder or Desktop.
• On macOS, unzip the file by double-clicking it, and then move the
extracted app to any location on your computer, including your
Applications folder or Desktop.
• The Linux version of Processing is a .tar archive. Extract or move the
folder to any location on your computer, including your home folder
or desktop.

Once you’re finished, open the newly extracted folder. Figure 1-1 shows
an abridged listing of what you can expect to see in your file manager. Next,
locate and run the executable file named processing. On macOS, you’ll just
have a single file named processing.

processing-x.x.x

core
java
lib
modes
tools
processing
... ...

Figure 1-1: The contents of a freshly


extracted processing folder for Windows
or Linux

The application layout may vary slightly among systems and Processing
versions, but the key elements are outlined in Figure 1-2. If you’re a Mac user,
you’ll find the menu bar in its usual position at the top of your screen. Note
that the upper right button in the Processing interface is labeled Java. This is
because Processing comes bundled with Java mode as the default.

2 Chapter 1
Processing

File Edit Sketch Tools Help Menu bar

Java Toolbar

Filename Tabs

1 code goes here ...

Code editor

Message area

Console area

Console

Figure 1-2: The Processing interface

Next, activate Python Mode. Click the Java button and select Add Mode
from the drop-down menu; then, from the Contribution Manager window
that appears, choose Python Mode for Processing. Finally, click Install. You
can now change between Python and Java mode by using the drop-down
menu. Switch to Python (Figure 1-3).

Processing

File Edit Sketch Tools Help

Python

Figure 1-3: The button to the right indicates that Python Mode is activated.

You’re now ready to write your first lines of code!

NOTE For a demonstration of what Processing can do, take a look at the Python
examples (FileExamples) included with Processing. To run any example,
use the play () button.

Hello, World! 3
Your First Sketch
Processing refers to programs as sketches. Given the visual and artistic nature
of what you are likely to produce, it’s a fitting term. Select FileNew to cre­
ate a new sketch, or use the associated keyboard shortcut (listed alongside
the menu entry).
Enter the following lines of code:

size(500, 500)
print('Hello, World!')

I’ll go through the specifics of this code in a bit. For now, save the
sketch by using FileSave As and name it hello_world.
You will notice that Processing creates a new folder named hello_world;
within it, there are two files: hello_world.pyde and sketch.properties (Figure 1-4).
Depending on your system’s configuration, you may or may not see the file
extension (.pyde). To reopen any sketch, locate and open the .pyde file.

hello_world

hello_world.pyde
sketch.properties

Figure 1-4: The contents of your hello_world


sketch folder

You may want to add other assets to your sketch folders, such as images
and fonts, but more on that later.
Next, click the play () button to execute the code. Better yet, use the
associated keyboard shortcut: CTRL-R for Windows and Linux, or -R for
macOS. A gray 500 × 500 pixel display window should appear. In the console,
which is the black area at the bottom of the editor, Processing should dis­
play Hello, World! (Figure 1-5).
Now let’s get back to the code you entered in this file; it uses two
Processing functions: size() and print(). Functions are named instructions,
sort of like dog commands for computers. Some commands are simple, like
“sit,” but something like “fetch” may involve specifying what it is that Fido
should retrieve.
Python functions consist of a function name followed by opening and
closing parentheses, which is where you provide arguments. In the case of my
dog-command analogy, “ball” could be an argument for “fetch.” The size()
function (Figure 1-6) takes two arguments: the first represents the width of
your sketch, and the second is the height.
In this case, the display window is 500 pixels wide by 500 pixels high.

4 Chapter 1
hello_world
File Edit Sketch Tools He

hello_world

1 size(500, 500)
2 print('Hello, World!')

Hello, World!
Console

Figure 1-5: Display window (left) and an editor with ′Hello, World!′ displayed in
the console (right)

size(500, 500)

Function width height


name

Arguments

Figure 1-6: Anatomy of a size() function

The print() function writes to the console. This function takes a single
argument: the phrase 'Hello, World!' Because this is text—or technically,
string data—you need to wrap it in quotation marks. You can use single or
double quotes, but be sure to use the same type for both opening and clos­
ing quotes.
Python classifies every value according to a data type, which deter­
mines how the value is handled and the operations you can perform on
it. For example, you can perform arithmetic operations—like division or
subtraction—on numeric data types but not on strings. In this chapter,
you’ll deal with three data types:
String  Text data, like ‘Hello, World!’
Integer  Numbers without decimal points, such as 1, –27, or 422
Floating-point  Numbers that include a decimal point, such as 1.618

Hello, World! 5
What separates Processing code from standard Python code are some
of its functions; the size() function, for example, is Processing-specific. In
other words, it won’t work outside the Processing environment. The print()
function, on the other hand, is a built-in element of the standard Python
programming language. It works in Processing’s Python Mode and any
other Python program.
Throughout this book, I usually refer to Processing-exclusive features in
the context of Processing, and standard Python features with the term Python.
If this is confusing, think of them as one and the same. At this point, distin­
guishing Processing from Python isn’t crucial; you will understand the dif­
ferences in time.

HOW DO I KNOW W HAT ARGUMENT S TO PROV IDE?

The type, number, and order of arguments vary according to the function,
and some functions don’t require arguments. For a complete list of Processing
functions and the arguments they require, as well as many standard Python
elements, refer to the Processing.py reference at https://py.processing.org/
reference/.
This book covers a lot of that online content, but the reference should be
your go-to source for in-depth descriptions of each function.

Comments
If you want Python to ignore any part of your code, you can comment it
out. This feature is useful for leaving notes, in plain English, to yourself
or anybody else editing your code. Let’s add a few comments to your
hello_world file:

1 # dimensions of the display window measured in pixels


size(500, 500)
print('Hello, World!') # writes hello world to the console area
2 '''
This is a multiline comment.
Any code between the opening and closing triple-quotes is ignored.
'''
print('How are you?')

Comments come in two types: single- and multiline. As shown here,


use a # character for single-line comments 1 and ''' (or """) for multiline
comments 2.

6 Chapter 1
Exploring the Variety of Random
Documents with Different Content
place. I was convinced that the greatest calamity that ever befell the
benighted nations of the ancient world was in their having passed
away without a knowledge of the actual existence of Duluth; that
their fabled Atlantis, never seen save by the hallowed vision of the
inspired poesy, was, in fact, but another name for Duluth; that the
golden orchard of the Hesperides, was but a poetical synonym for the
beer-gardens in the vicinity of Duluth. I was certain that Herodotus
had died a miserable death, because in all his travels and with all his
geographical research he had never heard of Duluth. I knew that if
the immortal spirit of Homer could look down from another heaven
than that created by his own celestial genius upon the long lines of
pilgrims from every nation of the earth to the gushing fountain of
poesy opened by the touch of his magic wand, if he could be
permitted to behold the vast assemblage of grand and glorious
productions of the lyric art called into being by his own inspired
strains, he would weep tears of bitter anguish that, instead of
lavishing all the stores of his mighty genius upon the fall of Illion, it
had not been his more blessed lot to crystalize in deathless song the
rising glories of Duluth. Yes, sir, had it not been for this map, kindly
furnished me by the legislature of Minnesota, I might have gone
down to my obscure and humble grave in an agony of despair,
because I could nowhere find Duluth. Had such been my melancholy
fate, I have no doubt that with the last feeble pulsation of my
breaking heart, with the last faint exhalation of my fleeting breath, I
should have whispered, “Where is Duluth?”
But, thanks to the beneficence of that band of ministering angels
who have their bright abodes in the far-off capital of Minnesota, just
as the agony of my anxiety was about to culminate in the frenzy of
despair, this blessed map was placed in my hands; and as I unfolded
it a resplendent scene of ineffable glory opened before me, such as I
imagined burst upon the enraptured vision of the wandering peri
through the opening gates of Paradise. There, there, for the first
time, my enchanted eye rested upon the ravishing word, “Duluth!”
This map, sir, is intended, as it appears from its title, to illustrate the
position of Duluth in the United States; but if gentlemen will
examine it, I think they will concur with me in the opinion, that it is
far too modest in its pretensions. It not only illustrates the position
of Duluth in the United States, but exhibits its relations with all
created things. It even goes further than this. It hits the shadowy vale
of futurity, and affords us a view of the golden prospects of Duluth
far along the dim vista of ages yet to come.
If gentlemen will examine it, they will find Duluth not only in the
center of the map, but represented in the center of a series of
concentric circles one hundred miles apart, and some of them as
much as four thousand miles in diameter, embracing alike, in their
tremendous sweep the fragrant savannas of the sunlit South and the
eternal solitudes of snow that mantle the ice-bound North. How
these circles were produced is perhaps one of those primordial
mysteries that the most skilled paleologist will never be able to
explain. But the fact is, sir, Duluth is pre-eminently a central point,
for I am told by gentlemen who have been so reckless of their own
personal safety as to venture away into those awful regions where
Duluth is supposed to be, that it is so exactly in the center of the
visible universe that the sky comes down at precisely the same
distance all around it.
I find, by reference to this map, that Duluth is situated somewhere
near the western end of Lake Superior, but as there is no dot or other
mark indicating its exact location, I am unable to say whether it is
actually confined to any particular spot, or whether “it is just lying
around there loose.” I really cannot tell whether it is one of those
ethereal creations of intellectual frostwork, more intangible than the
rose-tinted clouds of a summer sunset; one of those airy exhalations
of the speculator’s brain which, I am told, are very flitting in the form
of towns and cities along those lines of railroad, built with
government subsidies, luring the unwary settler as the mirage of the
desert lures the famishing traveler on, and ever on, until it fades
away in the darkening horizon; or whether it is a real, bona fide,
substantial city, all “staked off,” with the lots marked with their
owners’ names, like that proud commercial metropolis recently
discovered on the desirable shores of San Domingo. But, however
that may be, I am satisfied Duluth is there, or thereabouts, for I see it
stated here on the map that it is exactly thirty-nine hundred and
ninety miles from Liverpool, though I have no doubt, for the sake of
convenience, it will be moved back ten miles, so as to make the
distance an even four thousand.
Then, sir, there is the climate of Duluth, unquestionably the most
salubrious and delightful to be found anywhere on the Lord’s earth.
Now, I have always been under the impression, as I presume other
gentlemen have, that in the region around Lake Superior it was cold
enough for at least nine months in the year to freeze the smoke-stack
off a locomotive. But I see it represented on this map that Duluth is
situated exactly half way between the latitudes of Paris and Venice,
so that gentlemen who have inhaled the exhilarating air of the one,
or basked in the golden sunlight of the other, may see at a glance that
Duluth must be the place of untold delight, a terrestrial paradise,
fanned by the balmy zephyrs of an eternal spring, clothed in the
gorgeous sheen of ever blooming flowers, and vocal with the silvery
melody of nature’s choicest songsters. In fact sir, since I have seen
this map, I have no doubt that Byron was vainly endeavoring to
convey some faint conception of the delicious charms of Duluth
when his poetic soul gushed forth, in the rippling strains of that
beautiful rhapsody—
“Know ye the land of the cedar and the vine,
Whence the flowers ever blossom, the beams ever shine;
Where the light wings of Zephyr, oppressed with perfume,
Wax faint o’er the gardens of Gul in her bloom;
Where the citron and olive are fairest of fruit,
And the voice of the nightingale never is mute;
Where the tints of the earth and the hues of the sky,
In color though varied, in beauty may vie?”

As to the commercial resources of Duluth, sir, they are simply


illimitable and inexhaustible, as is shown by this map. I see it stated
here that there is a vast scope of territory, embracing an area of over
two millions of square miles, rich in every element of material wealth
and commercial prosperity, all tributary to Duluth. Look at it, sir,
(pointing to the map.) Here are inexhaustible mines of gold,
immeasurable veins of silver, impenetrable depths of boundless
forest, vast coal measures, wide extended plains of richest pasturage
—all, all embraced in this vast territory—which must, in the very
nature of things, empty the untold treasures of its commerce into the
lap of Duluth. Look at it, sir, (pointing to the map); do not you see
from these broad, brown lines drawn around this immense territory,
that the enterprising inhabitants of Duluth intend some day to
inclose it all in one vast corral, so that its commerce will be bound to
go there whether it would or not? And here, sir, (still pointing to the
map), I find within a convenient distance the Piegan Indians, which,
of all the many accessories to the glory of Duluth, I consider by far
the most inestimable. For, sir, I have been told that when the small-
pox breaks out among the women and children of the famous tribe,
as it sometimes does, they afford the finest subjects in the world for
the strategical experiments of any enterprising military hero who
desires to improve himself in the noble art of war, especially for any
valiant lieutenant-general whose
“Trenchant blade, Toledo trusty,
For want of fighting has grown rusty,
And eats into itself for lack,
Of somebody to hew and hack.”

Sir, the great conflict now raging in the Old World has presented a
phenomenon in military science unprecedented in the annals of
mankind, a phenomenon that has reversed all the traditions of the
past as it has disappointed all the expectations of the present. A great
and warlike people, renowned alike for their skill and valor, have
been swept away before the triumphant advance of an inferior foe,
like autumn stubble before a hurricane of fire. For aught I know the
next flash of electric fire that simmers along the ocean cable may tell
us that Paris, with every fibre quivering with the agony of impotent
despair, writhes beneath the conquering heel of her loathed invader.
Ere another moon shall wax and wane, the brightest star in the
galaxy of nations may fall from the zenith of her glory never to rise
again. Ere the modest violets of early spring shall ope their
beauteous eyes, the genius of civilization may chant the wailing
requiem of the proudest nationality the world has ever seen, as she
scatters her withered and tear-moistened lilies o’er the bloody tomb
of butchered France. But, sir, I wish to ask if you honestly and
candidly believe that the Dutch would have overrun the French in
that kind of style if General Sheridan had not gone over there, and
told King William and Von Moltke how he had managed to whip the
Piegan Indians.
And here, sir, recurring to this map, I find in the immediate
vicinity of the Piegans “vast herds of buffalo” and “immense fields of
rich wheat lands.” [Here the hammer fell.]
[Many cries: “Go on!” “go on!”]
The Speaker—Is there any objection to the gentleman from
Kentucky continuing his remarks? The chair hears none. The
gentleman will proceed.
Mr. Knott—I was remarking, sir, upon these vast “wheat fields”
represented on this map in the immediate neighborhood of the
buffaloes and Piegans, and was about to say that the idea of there
being these immense wheat fields in the very heart of a wilderness,
hundreds and hundreds of miles beyond the utmost verge of
civilization, may appear to some gentlemen as rather incongruous, as
rather too great a strain on the “blankets” of veracity. But to my mind
there is no difficulty in the matter whatever. The phenomenon is very
easily accounted for. It is evident, sir, that the Piegans sowed that
wheat there and ploughed it in with buffalo bulls. Now, sir, this
fortunate combination of buffaloes and Piegans, considering their
relative positions to each other and to Duluth, as they are arranged
on this map, satisfies me that Duluth is destined to be the best
market of the world. Here, you will observe, (pointing to the map),
are the buffaloes, directly between the Piegans and Duluth; and here,
right on the road to Duluth, are the Creeks. Now, sir, when the
buffaloes are sufficiently fat from grazing on those immense wheat
fields, you see it will be the easiest thing in the world for the Piegans
to drive them on down, stay all night with their friends, the Creeks,
and go into Duluth in the morning. I think I see them, now, sir, a vast
herd of buffaloes, with their heads down, their eyes glaring, their
nostrils dilated, their tongues out, and their tails curled over their
backs, tearing along toward Duluth, with about a thousand Piegans
on their grass-bellied ponies, yelling at their heels! On they come!
And as they sweep past the Creeks, they join in the chase, and away
they all go, yelling, bellowing, ripping and tearing along, amid clouds
of dust, until the last buffalo is safely penned in the stock-yards at
Duluth.
Sir, I might stand here for hours and hours, and expatiate with
rapture upon the gorgeous prospects of Duluth, as depicted upon this
map. But human life is too short, and the time of this house far too
valuable to allow me to linger longer upon this delightful theme. I
think every gentleman upon this floor is as well satisfied as I am that
Duluth is destined to become the commercial metropolis of the
universe and that this road should be built at once. I am fully
persuaded that no patriotic representative of the American people,
who has a proper appreciation of the associated glories of Duluth and
the St. Croix, will hesitate a moment that every able-bodied female in
the land, between the ages of eighteen and forty-five, who is in favor
of “woman’s rights,” should be drafted and set to work upon this
great work without delay. Nevertheless, sir, it grieves my very soul to
be compelled to say that I cannot vote for the grant of lands provided
for in this bill.
Ah, sir, you can have no conception of the poignancy of my
anguish that I am deprived of that blessed privilege! There are two
insuperable obstacles in the way. In the first place my constituents,
for whom I am acting here, have no more interest in this road than
they have in the great question of culinary taste now, perhaps,
agitating the public mind of Dominica, as to whether the illustrious
commissioners, who recently left this capital for that free and
enlightened republic, would be better fricasseed, boiled, or roasted,
and, in the second place, these lands, which I am asked to give away,
alas, are not mine to bestow! My relation to them is simply that of
trustee to an express trust. And shall I ever betray that trust? Never,
sir! Rather perish Duluth! Perish the paragon of cities! Rather let the
freezing cyclones of the bleak northwest bury it forever beneath the
eddying sands of the raging St. Croix.
Henry Carey’s Speech on the Rates of
Interest.

In the Pennsylvania Constitutional Convention, 1873.


In the Constitutional Convention, in Committee of the Whole on
the article reported from the Committee on Agriculture, Mining,
Manufactures, and Commerce, the first section being as follows:—“In
the absence of special contracts the legal rate of interest and discount
shall be seven per centum per annum, but special contracts for
higher or lower rates shall be lawful. All national and other banks of
issue shall be restricted to the rate of seven per centum per annum.”
Mr. H. C. Carey made an address in favor of striking out the section.
The following is an abstract of his remarks:—
Precisely a century and a half since, in 1723, the General Assembly
of Pennsylvania reduced the legal charge for the use of money from
eight to six per cent. per annum. This was a great step in the
direction of civilization, proving, as it did, that the labor of the
present was obtaining increased power over accumulations of the
past, the laborer approaching toward equality with the capitalist. At
that point it has since remained, with, however, some change in the
penalties which had been then prescribed for violations of the law.
Throughout the recent war the financial policy of the National
Government so greatly favored the money-borrower and the laborer
as to have afforded reason for believing that the actual rate of
interest was about to fall permanently below the legal one, with the
effect of speedily causing usury laws to fall into entire disuse. Since
its close, however, under a mistaken idea that such was the real road
to resumption, all the Treasury operation of favoring the money-
lender; the result exhibiting itself in the facts that combinations are
being everywhere formed for raising the price of money; that the
long loans of the past are being daily more and more superseded by
the call loans of the present; that manufacturer and merchant are
more and more fleeced by Shylocks who would gladly take “the
pound of flesh nearest the heart” from all over whom they are
enabled to obtain control.
Anxious for the perpetuation of this unhappy state of things, these
latter now invite their victims to give their aid towards leveling the
barriers by which they themselves are even yet to a considerable
extent protected, assuring them that further grant of power will be
followed by greater moderation in its exercise. Misled thereby,
money borrowers, traders, and manufacturers are seen uniting, year
after year with their common enemy in the effort at obtaining a
repeal of the laws in regard to money, under which the State has so
greatly prospered. Happily our working men, farmers, mechanics,
and laborers fail to see that advantage is likely to accrue to them
from a change whose obvious tendency is that of increasing the
power of the few who have money to lend over the many who need to
borrow; and hence it is that their Representatives at Harrisburg have
so steadily closed their ears against the siren song by which it is
sought to lead their constituents to give their aid to the work of their
own destruction.
Under these circumstances is it that we are now asked to give place
in the organic law to a provision by means of which this deplorable
system is to be made permanent, the Legislature being thereby
prohibited, be the necessity what it may, from placing any restraint
upon the few who now control the supply of the most important of all
the machinery of commerce, as against the many whose existence,
and that of their wives and children, is dependent upon the obtaining
the use thereof on such terms as shall not from year to year cause
them to become more and more mere tools in the hands of the
already rich. This being the first time in the world’s history that any
such idea has been suggested, it may be well, before determining on
its adoption, to study what has been elsewhere done in this direction,
and what has been the result.
Mr. Carey then proceeded to quote at great length from recent and
able writers the results that had followed in England from the
adoption of the proposition now before the convention. These may
be summed up as the charging of enormous rates of interest, the
London joint stock banks making dividends among their
stockholders to the extent of twenty, thirty, and almost forty per
cent., the whole of which has ultimately to be taken from the wages
of labor employed in manufactures, or in agriculture. At no time, said
Mr. Carey, in Britain’s history, have pauperism and usury traveled so
closely hand in hand together; the rich growing rich to an extent that,
till now, would have been regarded as fabulous, and the
wretchedness of the poor having grown in like proportion.
After discussing the effects of the repeal of the usury laws in some
of the American States, Mr. Carey continued:—
“We may be told, however, that at times money is abundant, and
that even so late as last summer it was difficult to obtain legal
interest. Such certainly was the case with those who desired to put it
out on call; but at that very moment those who needed to obtain the
use of money for long periods were being taxed, even on securities of
unexceptionable character, at double, or more than double, the legal
rates. The whole tendency of the existing system is in the direction of
annihilating the disposition for making those permanent loans of
money by means of which the people of other countries are enabled
to carry into effect operations tending to secure to themselves control
of the world’s commerce. Under that system there is, and there can
be, none of that stability in the price of money required for carrying
out such operations.
Leaving out of view the recent great combination for the
maintenance and perpetuation of slavery, there has been none so
powerful, none so dangerous as that which now exists among those
who, having obtained a complete control of the money power, are
laboring to obtain legal recognition of the right of capital to perfect
freedom as regards all the measures to which it may be pleased to
resort for the purpose of obtaining more perfect control over labor.
Already several of the States have to some extent yielded to the
pressure that has been brought to bear upon them. Chief among
these is Massachusetts, the usury laws having there been totally
repealed, and with the effect, says a distinguished citizen of that
State, that “all the savings institutions of the city at once raised the
rate from six to seven per cent.; those out of the city to seven and a
half and eight per cent. and there was no rate too high for the greedy.
The consequence,” as he continues, “has been disastrous to industrial
pursuits. Of farming towns in my county, more than one quarter
have diminished in population.” Rates per day have now to a great
extent, as I am assured, superseded the old rates per month or year;
two cents per day, or $7.30 per annum, having become the charge for
securities of the highest order. What, under such circumstances,
must be the rate for paper of those who, sound and solvent as they
may be, cannot furnish such security, may readily be imagined. Let
the monopoly system be maintained and the rate, even at its
headquarters, New England, will attain a far higher point than any
that has yet been reached; this, too, in despite of the fact that her
people had so promptly secured to themselves a third of the whole
circulation allowed to the 40,000,000 of the population of the Union
scattered throughout almost a continent. How greatly they value the
power that has been thus obtained is proved by the fact that to every
effort at inducing them to surrender, for advantage of the West or
South, any portion thereof, has met with resistance so determined
that nothing has been yet accomplished.
Abandonment of our present policy is strongly urged upon us for
the reason that mortgages bear in New York a higher rate of interest.
A Pennsylvanian in any of the northern counties has, as we are told,
but to cross the line to obtain the best security at seven per cent.
Why, however, is it that his neighbors find themselves compelled to
go abroad when desirous of obtaining money on such security? The
answer to this question is found in the fact that the taxation of
mortgages is there so great as to absorb from half to two-thirds of the
interest promised to be paid.
Again, we are told that Ohio legalizes “special contracts” up to
eight per cent. and, that if we would prevent the efflux of capital we
must follow in the same direction. Is there, however, in the exhibit
now made by that State, anything to warrant us in so doing? Like
Pennsylvania, she has abundant coal and ore. She has two large
cities, the one fronting on the Ohio, and the other on the lakes, giving
her more natural facilities for maintaining commerce than are
possessed by Pennsylvania; and yet, while the addition to her
population in the last decade was but 306,000, that of Pennsylvania
was 615,000. In that time she added 900 to her railroad mileage,
Pennsylvania meantime adding 2,500. While her capital engaged in
manufactures rose from 57 to 141 millions, that of Pennsylvania grew
from 109 to 406, the mere increase of the one being more than fifty
per cent. in excess of the total of the other. May we find in these
figures any evidence that capital has been attracted to Ohio by a
higher rate of interest, or repelled from our State by a lower one?
Assuredly not!
What in this direction is proposed to be done among ourselves is
shown in the section now presented for our consideration. By it the
legal rate in the absence of “special contracts” is to be raised to seven
per cent., such “contracts,” however ruinous in their character, and
whatsoever the nature of the security, are to be legalized; the only
exception to these sweeping changes being that national banks,
issuing circulating notes are to be limited to seven per cent. Shylock
asked only “the due and forfeit of his bond.” Let this section be
adopted, let him then present himself in any of our courts, can its
judge do other than decide that “the law allows it and the court
awards it,” monstrous as may have been the usury, and discreditable
as may have been the arts by means of which the unfortunate debtor
may have been entrapped? Assuredly not. Shylock, happily, was
outwitted, the bond having made no provision for taking even “one
jot of blood.” Here, the unfortunate debtor, forced by his flinty-
hearted creditor into a “special contract” utterly ruinous, may, in
view of the destruction of all hope for the future of his wife and
children, shed almost tears of blood, but they will be of no avail; yet
do we claim to live under a system whose foundation-stone exhibits
itself in the great precept from which we learn that duty requires of
us to do to others as we would that others should do unto ourselves.
By the English law the little landowner, the mechanic who owns
the house in which he lives, is protected against his wealthy
mortgagee. Here, on the contrary, the farmer, suffering under the
effects of blight or drought, and thus deprived of power to meet with
punctuality the demands of his mortgagee, is to have no protection
whatsoever. So, too, with the poor mechanic suffering temporarily by
reason of accidental incapacity for work, and, with the sheriff full in
view before him, compelled to enter into a “special contract”
doubling if not trebling, the previous rate of interest. Infamous as
may be its extortion the court may not deny the aid required for its
enforcement.
The amount now loaned on mortgage security in this State at six
per cent. is certainly not less than $400,000,000, and probably
extends to $500,000,000, a large portion of which is liable to be
called for at any moment. Let this section be adopted and we shall
almost at once witness a combined movement among mortgagees for
raising the rate of interest. Notices demanding payment will fly thick
as hail throughout the State, every holder of such security knowing
well that the greater the alarm that can be produced and the more
utter the impossibility of obtaining other moneys the larger may be
made the future rate of interest. The unfortunate mortgagor must
then accept the terms, hard as they may be, dictated to him, be they
8, 10, 12, or 20 per cent. Such, as I am assured has been the course of
things in Connecticut, where distress the most severe has been
produced by a recent abandonment by the State of the policy under
which it has in the past so greatly prospered. At this moment her
savings’ banks are engaged in compelling mortgagers to accept eight
per cent. as the present rate. How long it will be before they will carry
it up to ten or twelve, or what will be the effect, remains to be seen.
Already among ourselves the effects of the sad blunders of our great
financiers exhibit themselves in the very unpleasant fact that sheriffs’
sales are six times more numerous than they were in the period from
1861 to 1867, when the country was so severely suffering under the
waste of property, labor, and life, which had but then occurred. Let
this section be adopted, giving perfect freedom to the Shylocks of the
day, and the next half dozen years will witness the transfer, under the
sheriff’s hammer, of the larger portion of the real property of both
the city and the State. Of all the devices yet invented for the
subjugation of labor by capital, there is none that can claim to be
entitled to take precedence of that which has been now proposed for
our consideration.
Rightly styled the Keystone of the Union, one duty yet remains to
her to be performed, to wit: that of bringing about equality in the
distribution of power over that machinery for whose use men pay
interest, which is known as money. New England, being rich and
having her people concentrated within very narrow limits, has been
allowed to absorb a portion of that power fully equal to her needs,
while this State, richer still, has been so “cabined, cribbed, confined,”
that her mine and furnace operators find it difficult to obtain that
circulating medium by whose aid alone can they distribute among
their workmen their shares of the things produced.—New York,
already rich, has been allowed to absorb a fourth of the permitted
circulation, to the almost entire exclusion of the States south of
Pennsylvania and west of the Mississippi; and hence it is that her
people are enabled to levy upon those of all these latter such
enormous taxes. To the work of correcting this enormous evil
Pennsylvania should now address herself. Instead of following in the
wake of New Jersey and Connecticut, thereby giving to the monopoly
an increase of strength, let her place herself side by side with the
suffering States of the West, the South, and the Southwest,
demanding that what has been made free to New York and New
England shall be made equally free to her and them. Let her do this,
and the remedy will be secured, with such increase in the general
power for developing the wonderful resources of the Union as will
speedily make of it an iron and cloth exporting State, with such
power for retaining and controlling the precious metals as will place
it on a surer footing in that respect than any of the powers of the
Eastern world. The more rapid the societary circulation, and the
greater the facility of making exchanges from hand to hand, and
from place to place, the greater is the tendency toward reduction in
the rate of interest, toward equality in the condition of laborer and
employer, and toward growth and power to command the services of
all the metals, gold and silver included.
It will be said, however, that adoption of such measures as have
been indicated would tend to produce a general rise of prices; or, in
the words of our self-styled economists, would cause “inflation.” The
vulgar error here involved was examined some thirty years since by
an eminent British economist, and with a thoroughness never before
exhibited in reference to any other economic question whatsoever,
the result exhibiting itself in the following brief words of a highly
distinguished American one, published some twelve or fifteen years
since, to wit:
“Among the innumerable influences which go to determine the general rate of
prices, the quantity of money, or currency, is one of the least effective.”
Since then we have had a great war, in the course of which there
have been numerous and extensive changes in the price of
commodities, every one of which is clearly traceable to causes widely
different from those to which they so generally are attributed. Be
that, however, as it may, the question now before us is one of right
and justice, and not of mere expediency. North and east of
Pennsylvania eight millions of people have been allowed a greater
share of the most important of all powers, the money one, than has
been allotted to the thirty-two millions south and west of New York,
and have thus been granted a power of taxation that should be no
longer tolerated. The basis of our whole system is to be found in
equality before the law, each and every man, each and every State,
being entitled to exercise the same powers that are permitted to our
people, or other States. If the Union is to be maintained, it can be so
on no terms other than those of recognition of the existence of the
equality that has here been indicated. To the work of compelling that
recognition Pennsylvania should give herself, inscribing on her
shield the brief words fiat justitia, ruat cœlum—let justice be done
though the heavens fall!
Speech of Gen. Simon Cameron.

On the benefits derived by Pennsylvania from the Policy of Internal


Improvements.
Any one will see, who will take the trouble to read the debates on
the location of the National Capital, that the decision of that question
seems to have been made solely with reference to a connection of the
East with the then great wilderness of the West. All the sagacious
men then in public life looked to the time when the West, with its
wonderful productive soil brought under subjection by industry,
would exercise a controlling influence on the destiny of the country.
Columbia, in the State of Pennsylvania, was at one time within one
vote of becoming the site of the Capital; and Germantown, near, and
now a part of, Philadelphia, was actually decided on as the proper
location by a majority of one. The first of these was favored because
it was believed to be a favorable point from which to begin a slack
water route to the west. Germantown near the Schuylkill, was chosen
for the same reason. All looked forward to a system of canals which
would accomplish this desirable object, and experience has fully
demonstrated their wisdom in that great design. About 1790, General
Washington and the great financier Robert Morris, traveled on
horseback from Philadelphia to the Susquehanna river, with a view
of deciding whether a canal could be built over that route.
Shortly after this, some gentlemen near Philadelphia actually
began building a canal to the west, did some work on its eastern end,
built one or two locks on the dividing ridge near Lebanon, and for
want of sufficient funds and knowledge of the subject the work was
stopped. The money expended on the enterprise was lost.
But the progressive men of the country, keeping their minds on the
subject, continued to agitate the popular mind on it until 1820, when
the Legislature of Pennsylvania chartered the Union Canal Company,
and appropriated one million dollars to aid its construction. In a few
years the canal was completed between the Schuylkill and
Susquehanna. Although very small, this improvement did a great
deal of good. And the most remarkable thing about it was its
unpopularity with the masses. Not only the members of General
Assembly who passed the bill, but Governor Heister, who signed the
act of incorporation, were driven from office at the first opportunity
legally presented for testing public opinion, and the party to which
they belonged went into a minority. I remember well what a mighty
sum a million dollars seemed to be; and the political revolution
caused by this appropriation showed me that the idea of its vastness
was not confined by any means to myself.
Our system of canals was completed, and the benefits derived from
them were incalculable. When they were commenced our State was
poor. Industry languished. The interchange of her products was
difficult. Population was sparse. Intelligence was not generally
diffused. Manufactures struggled weakly along. Work was not
plentiful. Wages were low. When they were finished the busy hum of
industry was heard on every hand. Our population had grown until
we numbered millions. Our iron ore beds were yielding their
precious hoards for human use. Coal mines, unknown or useless
until means were provided for transporting their wealth to market,
now sent millions of tons in every direction. Progress in every walk of
advanced civilization was realized, and we were on the high road to
permanent prosperity. But in the meantime a new and better means
of communication had been discovered, and the building of railroads
quickly reduced the value of canals, and the works we had completed
at so much cost, and with such infinite labor, were suddenly
superseded. We lost nearly all the money they had cost us, but this
investment was wisely made. The return to our State was many times
greater than the outlay.
Like all great projects intended for the public good, that of Internal
Improvement progressed. In 1823, the New York canal—which had
been pushed through against the prejudiced opposition of the
people, by the genius of De Witt Clinton—was opened. Its success
caused a revolution in the public mind all over the country. The
effect was so marked in the State, that in 1825 a convention was
called to consider the subject. Every county in the State was
represented, I believe. That body pronounced in favor of a grand
system of public works, which should not only connect the East and
West, but also the waters of the Susquehanna with the great lakes,
the West and the Northwest. Appropriations were recommended to
the amount of three millions of dollars, and in 1826, I think the work
began. This sum seemed to be enormous, and the estimates of the
engineers reached a total of six millions of dollars. Meeting an ardent
friend of the system one day, he declared that a sum of that
magnitude could never be expended on these works. I ventured to
reply, with great deference to his age and experience, that I thought
it would be insufficient, and before they were completed I would not
be surprised if ten millions would be found necessary. Looking at me
steadily for a few moments, he closed the conversation by
exclaiming, “Young man, you are a d——d fool!” I was thus left in full
possession of his opinion of me. But after we had spent
$41,698,594.74 in the construction of these works, I found my
estimate of his judgment was singularly in harmony with my opinion
of his politeness. His candor I never doubted.
In the convention of 1825, there were two gentlemen who voted for
railways instead of canals. One was professor Vethake of Dickinson
College, Carlisle; and the other was Jacob Alter, a man of very little
education, but of strong understanding. The professor was looked
upon as a dreamer, and was supposed to have led his colleague
astray in his vagaries. But they both lived to see railroads extended
over the whole world. As a part of our system of public works, we
built a railroad from the Delaware to the Susquehanna, from
Philadelphia to Columbia, and one from the eastern base of the
Allegheny mountains to their western base. They were originally
intended to be used with horse power. In the meantime the railroad
system had been commenced, and the Pennsylvania Railroad, under
the charge of a man of extraordinary ability, John Edgar Thompson,
was rapidly pushed to completion. Another great railway, the
Philadelphia and Reading, was built to carry anthracite coal from the
Schuylkill mines to the market. A railroad was built each side of the
Lehigh river, that another part of our coal territory might find a
market in New York. Another was built from the north branch of the
Susquehanna, connecting with the New York roads, and leading to
the northern coal field. And yet another was built along the
Susquehanna, through the southern coal basin, to the city of
Baltimore. The total cost of these roads, independent of the
Pennsylvania railroad, was $95,250,410.10, as shown by official
reports. Their earnings last year are officially given at
$24,753,065.32. Each of these was forced to contend with difficulty
and prejudice. All were unpopular, and all were looked upon with
suspicion until they actually forced their usefulness on the public
mind. Those who made the fight for canals were forced to go over the
whole ground again for railroads, and their double victory is greater
than the success generally vouchsafed to the pioneers in any cause.
These roads, with the Pennsylvania railroad and the lesser lines of
improvements running through the coal region cost over
$207,000,000.
The Reading Railroad will serve to illustrate the struggle of these
great schemes. Its stock, now worth over par, once sold for twenty
cents on the dollar; and at one time it was forced to sell its bonds at
forty cents on the dollar to pay operating expenses. The vindication
of the sagacity of the pioneers in these great enterprises is complete.
All these lines are now profitable, and it has been demonstrated
everywhere in the United States, that every new railroad creates the
business from which its stockholders receive their dividends. It
seems, therefore, scarcely possible to fix a limit to our profitable
railroad expansion. They open new fields of enterprise, and this
enterprise in turn, makes the traffic which fills the coffers of the
companies.
I cannot now look back to the struggle to impress the people with
the advantages of railways, without a feeling of weariness at the
seeming hopeless struggle, and one of merriment at the general
unbelief in our new-fangled project. Once at Elizabethtown in this
State a public meeting had been called for the purpose of securing
subscriptions to the stock of the Harrisburg and Lancaster Railroad.
This road was intended to complete the railway between
Philadelphia and Harrisburg, one hundred and five miles. A large
concourse had gathered. Ovid F. Johnson, Attorney-General of our
State, and a brilliant orator, made an excellent speech; but the effect
was not in proportion to the effort. I determined to make an appeal,
and I gave such arguments as I could. In closing I predicted that
those now listening to me would see the day when a man could
breakfast in Harrisburg, go to Philadelphia, transact a fair day’s
business there, and returning, eat his supper at home. Great
applause followed this, and some additional subscriptions. Abram
Harnly, a friend of the road, and one of the most intelligent of his
class, worked his way to me, and taking me aside whispered, “That
was a good idea about going to Philadelphia and back to Harrisburg
the same day;” and then, bursting with laughter, he added,—“But you
and I know better than that!” We lived to see the road built; and now
people can come and go over the distance twice a day, which Abram
seemed to consider impossible for a single daily trip.
The peculiar condition of the States then known as “the West” was
the subject of anxiety to many. They had attracted a large population,
but the people were exclusively devoted to agriculture. Lacking
diversified industry, they were without accumulated wealth to enable
them to build railways; nor were the States in condition to undertake
such an onerous duty, although several of them made a feeble
attempt to do so. At one time the bonds of Illinois, issued to build her
canals, sold as low as thirty cents on the dollar. So with Indiana. Both
States were supposed to be bankrupt. It became, therefore, an
important problem as to how means of communication should be
supplied to the people of the West. Congress, in 1846, gave a grant of
land to aid in building a railroad in Illinois. Every alternate section
was given to the Company, and each alternate section was reserved
by the Government. The road was built; and the one-half of the land
retained by the government sold for a great deal more than all was
worth before the road was constructed. This idea was original, I
think, with Mr. Whitney of Mass., who spent two winters in
Washington, about 1845, endeavoring to induce Congress to adopt
that plan for the construction of a Trans-Continental Railway.
He died before seeing his scheme succeed. Others have built a road
across the continent on the Central route. Another on the Northern
route is now progressing, and the wealth and enterprise of those
having it in charge renders its completion certain. And it yet remains
for us to give the people of the Southern route a road to the Pacific
which shall develop the magnificent region through which it will
pass, and give the country one route to the great ocean protected
from the ordinary difficulty of climate with which railroads must
contend over so large a part of our territory. But I am admonished by
the value of your space to confine myself to the limits of my own
State.
I have said that the outlay we have made in building our public
works was of great benefit to us even when the canals had been
rendered almost valueless through the competition of railroads. This
is paradoxical, but it is true nevertheless. That expenditure gave our
people a needed knowledge of our vast resources. It familiarized
them with large expenditures when made for the public good. And it
showed them how a great debt may be beneficially incurred, and yet
not break down the enterprise of the people. We at one time owed
$41,698,595.74. By a steady attention to our finances, it is now
reduced to $31,000,000, with resources,—the proceeds of the sale of
public works—on hand amounting to $10,000,000. And while we
have been steadily reducing our State debt, we have built 5,384 miles
of railway on the surface of the earth, and 500 miles underground in
our mines, at a cost of not less than $350,000,000, for a mile of
railroad in Pennsylvania means something. We sent 368,000 men to
the Federal Army. And our credit stands high on every stock
exchange. Gratifying as this progress is, it is only a fair beginning.
There is a large part of our territory rich in timber and full of iron,
coal, and all kinds of mineral wealth, so entirely undeveloped by
railroads that we call it “the Wilderness.” To open it up is the
business of to-day, and I sincerely hope to see it done soon.
Forty years ago George Shoemaker, a young tavern-keeper of more
vigor and enterprise than his neighbors, came to the conclusion that
anthracite coal could be used as fuel. He went to the expense of
taking a wagon load of it to Philadelphia, a hundred miles away, and,
after peddling it about the streets for some days, was forced to give it
away, and lose his time, his labor and his coal. He afterwards saw a
great railway built to carry the same article to the same point, and
enriching thousands from the profits of the traffic. But his experience
did not end there. He saw a thousand dollars paid eagerly for an acre
of coal land, which at the time of his venture to Philadelphia, no one
would have, and he could not give away.
I have thought that a retrospective survey of our wonderful
development might point plainly to the duty of the future. For if the
experience of what has gone before is not useful to cast light on what
is yet to come, then it will be difficult indeed to discover wherein its
value lies. It teaches me to devote time and labor for the
advancement of all Public Improvements, and I trust it may have a
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebooknice.com

You might also like