Think Julia
How to Think Like a Computer Scientist
Ben Lauwens and Allen B. Downey
Think Julia
by Ben Lauwens and Allen B. Downey
Copyright © 2019 Ben Lauwens and Allen Downey. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North,
Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales
promotional use. Online editions are also available for most titles
(http://oreilly.com). For more information, contact our
corporate/institutional sales department: 800-998-9938 or
corporate@oreilly.com.
Editors: Jonathan Hassell and Melissa Potter
Production Editor: Christopher Faucher
Copyeditor: Rachel Head
Proofreader: Christina Edwards
Indexer: Angela Howard
Interior Designer: David Futato
Cover Designer: Karen Montgomery
Illustrator: Rebecca Demarest
April 2019: First Edition
Revision History for the First Edition
2019-04-05: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781492045038 for release
details.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Think
Julia, the cover image, and related trade dress are trademarks of O’Reilly
Media, Inc.
The views expressed in this work are those of the authors, and do not
represent the publisher’s views. While the publisher and the authors have
used good faith efforts to ensure that the information and instructions
contained in this work are accurate, the publisher and the authors disclaim
all responsibility for errors or omissions, including without limitation
responsibility for damages resulting from the use of or reliance on this
work. Use of the information and instructions contained in this work is at
your own risk. If any code samples or other technology this work contains
or describes is subject to open source licenses or the intellectual property
rights of others, it is your responsibility to ensure that your use thereof
complies with such licenses and/or rights.
978-1-492-04503-8
[GP]
Dedication
For Emeline, Arnaud, and Tibo.
Preface
In January 2018 I started the preparation of a programming course
targeting students without programming experience. I wanted to use Julia,
but I found that there existed no book with the purpose of learning to
program with Julia as the first programming language. There are
wonderful tutorials that explain Julia’s key concepts, but none of them pay
sufficient attention to learning how to think like a programmer.
I knew the book Think Python by Allen Downey, which contains all the key
ingredients to learn to program properly. However, this book was based on
the Python programming language. My first draft of the course notes was a
melting pot of all kinds of reference works, but the longer I worked on it,
the more the content started to resemble the chapters of Think Python.
Soon, the idea of developing my course notes as a port of that book to
Julia came to fruition.
All the material was available as Jupyter notebooks in a GitHub repository.
After I posted a message on the Julia Discourse site about the progress of
my course, the feedback was overwhelming. A book about basic
programming concepts with Julia as the first programming language was
apparently a missing link in the Julia universe. I contacted Allen to ask if I
could start an official port of Think Python to Julia, and his answer was
immediate: “Go for it!” He put me in touch with his editor at O’Reilly
Media, and a year later I was putting the finishing touches on this book.
It was a bumpy ride. In August 2018 Julia v1.0 was released, and like all
my fellow Julia programmers I had to do a migration of the code. All the
examples in the book were tested during the conversion of the source files
to O’Reilly-compatible AsciiDoc files. Both the toolchain and the example
code had to be made Julia v1.0–compliant. Luckily, there are no lectures to
give in August….
I hope you enjoy working with this book, and that it helps you learn to
program and think like a computer scientist, at least a little bit.
Ben Lauwens
Why Julia?
Julia was originally released in 2012 by Alan Edelman, Stefan Karpinski,
Jeff Bezanson, and Viral Shah. It is a free and open source programming
language.
Choosing a programming language is always subjective. For me, the
following characteristics of Julia are decisive:
Julia is developed as a high-performance programming language.
Julia uses multiple dispatch, which allows the programmer to
choose from different programming patterns adapted to the
application.
Julia is a dynamically typed language that can easily be used
interactively.
Julia has a nice high-level syntax that is easy to learn.
Julia is an optionally typed programming language whose (user-
defined) data types make the code clearer and more robust.
Julia has an extended standard library and numerous third-party
packages are available.
Julia is a unique programming language because it solves the so-called
“two languages problem.” No other programming language is needed to
write high-performance code. This does not mean it happens
automatically. It is the responsibility of the programmer to optimize the
code that forms a bottleneck, but this can done in Julia itself.
Who Is This Book For?
This book is for anyone who wants to learn to program. No formal prior
knowledge is required.
New concepts are introduced gradually and more advanced topics are
described in later chapters.
Think Julia can be used for a one-semester course at the high school or
college level.
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, email addresses, filenames, and file
extensions.
Used for program listings, as well as within paragraphs to refer to
program elements such as variable or function names, databases, data
types, environment variables, statements, and keywords.
Shows commands or other text that should be typed literally by the
user.
Shows text that should be replaced with user-supplied values or by
values determined by context.
This element signifies a tip or suggestion.
TIP
C
o
n
s
t
a
n
t
w
i
d
t
h
C
o
n
s
t
a
n
t
w
i
d
t
h
b
o
l
d
C
o
n
s
t
a
n
t
w
i
d
t
h
i
t
a
l
i
c