Learning to Program with Art and Python

If you want to build a ship, don’t drum up the people to gather wood, divide the work, and give orders.
Instead, teach them to yearn for the vast and endless sea.

-Antoine De Saint-Exupery, The Little Prince

Face it, computers have taken over humanity.

Everyone’s face is buried in their phone, oblivious to the world. We’ve collectively fallen in love with the worlds of the machines.

Because of this, the ability to write new software gives programmers unimaginable power over the collective minds of humanity. Through their programs, they magnify and multiply ideas, which can quickly spread to millions of people in days, weeks, months.

But the process of learning to program computers is treacherous. To the layperson, it seems as if only a mathematically minded people can ever hope to learn to program, and truly control a computer.

And sure, everyone who learns to program must deal with broken code libraries, finding an appropriate programming language, deciding what to make, and so on. There are so many decisions to make along the journey, and so many false starts, that most people give up before they even really begin.

But programming is especially rewarding for the type of people who are drawn to the humanities. They bring a unique perspective and experience to the world of computers we inhabit. They enrich the world of computation by bringing fun and stories and color to our virtual worlds.

And honestly, most great programmers are more like painters than technicians, beginning their software with sketches, tiny little programs that just do one thing, that let them see their ideas before beginning. They’ll often make mistakes, write things that don’t run properly the first time. Eventually, through sheer force and trial and error, things pushed through to a clear idea.

And writing new programs isn’t just for obedient rule followers. Anyone motivated enough can write their own programs, given the proper instruction and patience. Once you understand the fundamental concepts, playing with software becomes an opportunity to play with the flow of thought itself.

This book gives creative, intelligent people with ideas the mental tools to start programming computers and getting their ideas into the world. If you’ve never thought of yourself as the sort of person who could write a program, this is the book for you.

Almost everyone explores their ideas and shares their creativity through computers now. Why are so few people exploring new ideas with programming?

And why are even less people creating programs that are fun?

Why Art

In this book, we’ll focus on learning to program through art and the Python programming language.

To an outsider, art and programming couldn’t be more different. One seems rigid, with precise instructions that are read by an unyielding machine, and the other is open to interpretation by error prone, emotional humans.

But the process of writing software is the same thing as the process of creating art. In each, we experiment, copy, explore, and then share what we’ve made. Art and programming are both mediums for exploring our ideas, and then sharing those ideas for other people to play with.

This book, however, isn’t about art with a capital “A”. We’re not making artwork fit for prestigious museums. Instead, this book is more like art with tiny “a”, as in arts and crafts, or better yet, finger painting. We’re just here to enjoy ourselves, and the act of putting paint on paper. Or in this case, code on the computer.

Step by step, we’ll make tiny, understandable changes to the code we’ve written, and reshape and mix ideas like Legos into new programs.

We’ll write programs for drawing, editing photos, and making infinitely repeating shapes.

By manually writing out and running each of these programs, you’ll get a feel for how to start exploring and solving problems on your own.

We’re just using drawing and art as a medium, because with art we’re allowing ourselves to make mistakes and not be perfect.

Why Python

To create our programs, we’ll use the open-source Python interpreter.

Open source is a software movement created by a global community of programmers around the world. Each contributes their time and mind to create software, and then gives it away for free.

Each person may contribute just a little bit of a program, but collectively, the resource of open source software is what powers most of the web, from Facebook to Google, to all the servers in between.

Python is a programming language, but also a program that reads and runs programs. Python can load and run programs from files, or you can write code line by line, directly into it. Because Python reads your program line by line, it’s called an interpreter.

In this book, we’ll use the main Python interpreter, also called Cython.

As an interpreter, Python reads our programs line by line executing each instruction in order from the files we write. Indeed, we can also type out our programs directly into Python, and have our programs run as we type. For a beginner, this makes starting with Python less frustrating, and makes making mistakes less costly.

Python has also been around for a long time, receiving decades of continuous improvements from some of the best programmers in the world. Because of this, it’s also got over a hundred thousand “libraries”, or programs which extend the capabilities of the Python program itself.

Libraries can connect our Python programs to the internet, write AI, draw images, control 3D printers, direct robots, and more. Each of them just gives us a new set of tools to incorporate to our programs.

Finally, Python is a very easy programming language to read, compared to most languages. At first, looking at Python code might seem confusing, but there are very simple rules for Python code relative to other languages. It uses a lot of white spaces to designate what parts of the code belong to each other. This makes reading other people’s code easier, and gives plenty of other, open source code to look at if we get stuck.

The Creative Process of Programming

As you progress through this book, you may encounter some points where you might not really understand what’s happening, or why things must be a certain way.

With so much to learn as a new programmer, it can be very intimidating. I’ve tried to anticipate all the places where you may get tripped up, or not understand fully what’s happening.

Maintaining the balance between understanding concepts completely and not understanding them at all can drive you crazy as a beginner.

As we go along, I’ll call out the most important parts you should fully understand. Most everything else should be fine to skip over at first. Doing things repeatedly is the only way to really absorb all the ideas of programming.

Keep in mind, whenever we learn something new, our first efforts are often terrible. It can take us hours to type out and run our very first, simple program. The errors we get when we try running things make no sense, and the tiniest things seem to matter in ways they couldn’t possibly.

Go easy on yourself if you feel stuck. Just trust the process, that things will get better. Slowly, piece after piece, we begin to find our way. Soon we can successfully manipulate our medium.

The Journey of Becoming A Programmer

By the end of this book, you will be a programmer.

This means you will be able to come up with new ideas, know where to begin after a bit of research, and then maintain the resolve to keep trying until your ideas come to life.

Along the way, if you get lost, it pays to read other people’s code. This is one of the most important things you can do when first beginning. Focus on reading other people’s code and understanding the ways in which they have approached their own problems.

Initially, you may wonder whether the code you’re reading is good or bad. What if you’re learning the wrong ideas from “bad code”? Developing a taste for “good code” comes from reading a lot of code, and making the judgement for yourself. Usually it also means writing a lot of bad code yourself. Some people love filing their ideas into classes. Other people like making everything into a function that returns a value.

Part of becoming a programmer is deciding which approach resonates with you. Try a few different approaches out, even if other people call it “bad”. You’ve got to start somewhere.

No “Right Way”

As you progress, you will inevitably worry that you’re programming the “wrong way”—as if every program you write is written in the most wrong, convoluted way. You’ll want somebody to tell you the right way to write code, and to show you how to organize ideas. But here’s a secret: Nobody is coming to save you. Instead, just remember that if you create solutions to your problems, you are doing a good job. If you’ve learned something new, you’re doing a good job.

You may wonder about code that you can maintain, or about code that’s fast, but for now, the only important thing is that your code is understandable to yourself. If you can write and read and understand what each line of your code is doing, then you are already a great programmer.

Enough talk, let’s begin coding!

Chapter One - A Tutorial Introduction