logo资料库

Think Julia.pdf

第1页 / 共412页
第2页 / 共412页
第3页 / 共412页
第4页 / 共412页
第5页 / 共412页
第6页 / 共412页
第7页 / 共412页
第8页 / 共412页
资料共412页,剩余部分请下载后查看
Preface
Why Julia?
Who Is This Book For?
Conventions Used in This Book
Using Code Examples
O’Reilly Online Learning
How to Contact Us
Acknowledgments
Contributor List
1. The Way of the Program
What Is a Program?
Running Julia
The First Program
Arithmetic Operators
Values and Types
Formal and Natural Languages
Debugging
Glossary
Exercises
2. Variables, Expressions, and Statements
Assignment Statements
Variable Names
Expressions and Statements
Script Mode
Operator Precedence
String Operations
Comments
Debugging
Glossary
Exercises
3. Functions
Function Calls
Math Functions
Composition
Adding New Functions
Definitions and Uses
Flow of Execution
Parameters and Arguments
Variables and Parameters Are Local
Stack Diagrams
Fruitful Functions and Void Functions
Why Functions?
Debugging
Glossary
Exercises
4. Case Study: Interface Design
Turtles
Simple Repetition
Exercises
Encapsulation
Generalization
Interface Design
Refactoring
A Development Plan
Docstring
Debugging
Glossary
Exercises
5. Conditionals and Recursion
Floor Division and Modulus
Boolean Expressions
Logical Operators
Conditional Execution
Alternative Execution
Chained Conditionals
Nested Conditionals
Recursion
Stack Diagrams for Recursive Functions
Infinite Recursion
Keyboard Input
Debugging
Glossary
Exercises
6. Fruitful Functions
Return Values
Incremental Development
Composition
Boolean Functions
More Recursion
Leap of Faith
One More Example
Checking Types
Debugging
Glossary
Exercises
7. Iteration
Reassignment
Updating Variables
The while Statement
break
continue
Square Roots
Algorithms
Debugging
Glossary
Exercises
8. Strings
Characters
A String Is a Sequence
length
Traversal
String Slices
Strings Are Immutable
String Interpolation
Searching
Looping and Counting
String Library
The ∈ Operator
String Comparison
Debugging
Glossary
Exercises
9. Case Study: Word Play
Reading Word Lists
Exercises
Search
Looping with Indices
Debugging
Glossary
Exercises
10. Arrays
An Array Is a Sequence
Arrays Are Mutable
Traversing an Array
Array Slices
Array Library
Map, Filter, and Reduce
Dot Syntax
Deleting (Inserting) Elements
Arrays and Strings
Objects and Values
Aliasing
Array Arguments
Debugging
Glossary
Exercises
11. Dictionaries
A Dictionary Is a Mapping
Dictionaries as Collections of Counters
Looping and Dictionaries
Reverse Lookup
Dictionaries and Arrays
Memos
Global Variables
Debugging
Glossary
Exercises
12. Tuples
Tuples Are Immutable
Tuple Assignment
Tuples as Return Values
Variable-Length Argument Tuples
Arrays and Tuples
Dictionaries and Tuples
Sequences of Sequences
Debugging
Glossary
Exercises
13. Case Study: Data Structure Selection
Word Frequency Analysis
Random Numbers
Word Histogram
Most Common Words
Optional Parameters
Dictionary Subtraction
Random Words
Markov Analysis
Data Structures
Debugging
Glossary
Exercises
14. Files
Persistence
Reading and Writing
Formatting
Filenames and Paths
Catching Exceptions
Databases
Serialization
Command Objects
Modules
Debugging
Glossary
Exercises
15. Structs and Objects
Composite Types
Structs Are Immutable
Mutable Structs
Rectangles
Instances as Arguments
Instances as Return Values
Copying
Debugging
Glossary
Exercises
16. Structs and Functions
Time
Pure Functions
Modifiers
Prototyping Versus Planning
Debugging
Glossary
Exercises
17. Multiple Dispatch
Type Declarations
Methods
Additional Examples
Constructors
show
Operator Overloading
Multiple Dispatch
Generic Programming
Interface and Implementation
Debugging
Glossary
Exercises
18. Subtyping
Cards
Global Variables
Comparing Cards
Unit Testing
Decks
Add, Remove, Shuffle, and Sort
Abstract Types and Subtyping
Abstract Types and Functions
Type Diagrams
Debugging
Data Encapsulation
Glossary
Exercises
19. The Goodies: Syntax
Named Tuples
Functions
Blocks
Control Flow
Types
Methods
Constructors
Conversion and Promotion
Metaprogramming
Missing Values
Calling C and Fortran Code
Glossary
20. The Goodies: Base and Standard Library
Measuring Performance
Collections and Data Structures
Mathematics
Strings
Arrays
Interfaces
Interactive Utilities
Debugging
Glossary
21. Debugging
Syntax Errors
Runtime Errors
Semantic Errors
A. Unicode Input
B. JuliaBox
Index
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
分享到:
收藏