[HN Gopher] Think Python, 3rd Edition
___________________________________________________________________
Think Python, 3rd Edition
Author : beefman
Score : 413 points
Date : 2024-02-16 03:32 UTC (19 hours ago)
(HTM) web link (allendowney.github.io)
(TXT) w3m dump (allendowney.github.io)
| jph00 wrote:
| So excited to see this coming! When we used Jupyter Notebooks to
| publish Practical Deep Learning for Coders, we discussed with
| Allen the idea of doing the same for some of his books.
|
| Now it's actually happening. :D Even better, he's taken it
| further by adding cool tools such as a Jupyter-based turtle that
| shows inline graphics in the notebooks. I strongly suspect this
| will turn out to be the best way to learn Python programming when
| it's released.
|
| Oh and I just remembered, we even showed a proof-of-concept of
| converting some of the 2nd edition of this book into nbdev
| notebooks:
| https://github.com/fastai/nbdev_cards/blob/master/01_deck.ip... .
| That notebook is rendered as this HTML:
| https://fastai.github.io/nbdev_cards/deck.html
| stuaxo wrote:
| Having code you can hide like this looks great foe
| documentation, I've been looking for something like this for a
| while.
| fifilura wrote:
| I am proud of you!
|
| I loved Think Bayes and Think Stats, but it felt a bit off when
| everyone else were using notebooks.
|
| When I learn a new language with e.g. AdventOfCode, my first
| task is building a jupyter image for it.
| fbdab103 wrote:
| What luck! I just started mentoring someone in Python, and I am a
| huge fan of Downey. Truly an outstanding educator and renaissance
| man.
|
| Will definitely keep my eye on this.
| asicsp wrote:
| I had read the second edition when I was still relatively new to
| Python. I even spent a few weeks trying to translate the book to
| Ruby. Found it a great experience, especially the friendly prose
| and excellent exercises.
| BadHumans wrote:
| This book gets overlooked in favor of other ones such as Python
| Crash Course but I really enjoyed reading through Think Python 2e
| and will read through this version as well. Check out the rest of
| his books on Green Tea Press.
| wyclif wrote:
| I also like PCC. What do you think the pros and cons are of PCC
| v. TP3e? They are really two different types of books in terms
| of pedagogy.
| BadHumans wrote:
| Python Crash Course teaches Python while Think Python
| teachers computer science using Python. You are right that
| they are two different books but people recommend Python
| Crash Course for beginners when Think Python is a much better
| recommendation for them in opinion. If you just need to learn
| Python then PCC is a great book.
| aitchnyu wrote:
| Tangential, is there a book to learn typing? I feel I need a
| hefty book to learn typing. Don't mind if you recommend a
| Typescript book or other language with similar concepts.
| h4ch1 wrote:
| https://www.cis.upenn.edu/~bcpierce/tapl/
|
| This is a comprehensive resource that I use as a reference.
| Happy reading!
| quickthrower2 wrote:
| That is a big topic. Learning typing in Go will be easier than
| Typescript which is orders more sophisticated. In Typescript
| you can solve Sudoku in the type system! Then there is the
| theory of type systems. But if it is the day to day just use
| it, it will make sense. You are just describing what the type
| of something will be in code rather than let it be figured out
| at runtime.
| NegativeLatency wrote:
| Haskell might be a fun if indirect way to learn more about a
| type system. It's a neat middle between academic typing systems
| and a language you can do real stuff with.
|
| Typescript might be nice because of how it's a gradual typing
| system, just try and do a bit more and more with it as you go.
| asicsp wrote:
| Not a book, but this might help:
|
| "Python Type Challenges" (https://github.com/laike9m/Python-
| Type-Challenges) -- Master Python typing (type hints) with
| interactive online exercises
| magnio wrote:
| I can recommend Total Typescript by Matt Pocock, a course and a
| collection of tutorials. He is writing a book in public at [0]
| as well.
|
| A bit more advanced, and geared towards library authors, is
| Type-level Typescript.[1]
|
| [0]: https://github.com/total-typescript/total-typescript-book
|
| [1]: https://type-level-typescript.com/
| lysecret wrote:
| I learned it through Domain Modeling Made Functional (it uses
| F# though which looks the same as Ocaml).
| NlightNFotis wrote:
| Programming with types by Vlad Riscutia fits your bill exactly.
|
| Examples in typescript (so syntax should be familiar compared
| to e.g OCaml) and teaches you how to model a domain in types
| and how to think in terms of a type system, instead of diving
| into the details of how to implement one.
| noelwelsh wrote:
| You mean types, right? Typing is what you do on a keyboard.
|
| If you want to understand type systems, Types and Programming
| Languages (https://www.cis.upenn.edu/~bcpierce/tapl/) is the
| book most people start with. If that is too advanced for you,
| PLAI (https://www.plai.org/) is a gentle introduction to
| programming language theory which includes type systems.
| eru wrote:
| Those books are great; though I'm not sure whether they are
| good for a beginner? Getting your feet wet by programming in
| a few reasonably typed languages might be better, before you
| tackle TAPL?
|
| (By 'reasonably typed' I mean something like Haskell, OCaml,
| even TypeScript or Facebook's hack. But not Go, C++ or Java.)
| jdeisenberg wrote:
| If you want something JavaScript-ish with strong type
| inference, take a look at ReScript (https://rescript-
| lang.org/)
| jjgreen wrote:
| The source of several jokes of course
| https://stackoverflow.com/questions/430182/is-c-strongly-
| typ...
| elbear wrote:
| This is the book that got me started with programming, so I'm
| grateful to Allen for it.
| dang wrote:
| Related:
|
| _Think Python 2e_ -
| https://news.ycombinator.com/item?id=35421096 - April 2023 (30
| comments)
|
| _Think Python: How to Think Like a Computer Scientist_ -
| https://news.ycombinator.com/item?id=1586000 - Aug 2010 (9
| comments)
| bikingbismuth wrote:
| Think Python 2e changed the trajectory of my life. I took a
| single Java class and hated it so much I gave up on programming.
| A few years later as a network engineer I had a problem that
| seemed like it could be scripted and ended up picking up Think
| Python and fell in love with the language and programming in
| general.
| ambrose2 wrote:
| Similar for me - I had learned some Java, Matlab, C, Perl here
| and there but it wasn't until Think Python 2e that I was
| gripped and from there read many other books and changed my
| career to software.
| el_oni wrote:
| What other books did you read that you would recommend?
| ambrose2 wrote:
| I enjoyed "Data Structures and Algorithms in Python"!
| rmbyrro wrote:
| Couldn't this result be more attributable to Python itself
| than the book?
| eru wrote:
| In the abstract, yes. But I also know that Think Python is
| a great book.
|
| (I came across Think Python when I was trying to help other
| people learn how to program. So I did not learn programming
| from Think Python, and Python is also not my favourite
| language. (It's also not my least favourite language,
| either. Far from it.))
| ambrose2 wrote:
| Maybe, but I think the book really was different from any
| book I had read before. Most books teach the language, but
| Think Python showed me some of the why, what each data
| structure might be used for, it would quickly dip into some
| interesting implementation details to give me a more solid
| foundation and then zoom out and give me perspective on why
| something is useful and related software writing skills and
| tips. It was thorough, but also concise, and the exercises
| were engaging, so I found myself for the first time really
| getting sucked into the zone with the problems and time was
| flying by. This hadn't happened to me with Java or Perl.
| bemusedthrow75 wrote:
| This is just the sort of thing I needed to read.
|
| I am considering changing the trajectory of my own life,
| towards a more community/maker/teacher role, and I have a
| freelance/small business idea about teaching but I need sort of
| "soft syllabus" materials.
|
| I am learning Python myself, having just never had a need for
| it in all of my professional web development life (I've written
| apps in just about every other web-focussed programming
| language, including Perl and Ruby).
|
| It looks like the right language to teach general concepts in,
| and having a book I can draw from will help.
| eru wrote:
| Think Python is also just a really great book, even for
| people who don't want to stick with Python long term.
| macintux wrote:
| Python is challenging as a production language, given its
| tooling inconsistencies, but it's a great pseudocode
| language.
| cjohnson318 wrote:
| Think Python helped me finally understand OOP for the first
| time, after a C++ class, a Java class, etc.
| dod9er wrote:
| I love the Jupyter-/Colab-approach and would like to join the
| journey... and support the project by buying the book (although I
| don't need it to follow the Colab?). But without subscribing to
| the OReilly-learning platform it seems that buying it on amazon
| is the only option, right ?
| bemusedthrow75 wrote:
| It's a pre-order even on Amazon. The publication date for the
| book isn't until later in the year (at least for the edition
| available in the UK, which will probably be the same):
|
| https://www.whsmith.co.uk/products/think-python-how-to-think...
|
| https://blackwells.co.uk/bookshop/product/Think-Python-by-Al...
|
| (Blackwells think September, incidentally, whereas WHS think
| the end of July)
|
| Amazon just list books way, way earlier in pre-order than a lot
| of places.
| wjnc wrote:
| Green Tea Press (by Prof. Downey) has a donate button. That's
| probably a more efficient means of supporting the project than
| buying the book, considering all the middlemen.
| dod9er wrote:
| Oh my ..., was skipping too fast through the page and didn't
| see this, and also didn't realize what he exactly is doing
| with his Textbook Manifesto etc. Ok, the Paypal-Link is a no-
| brainer for me now. Thanks for pointing out the obvious.
| librasteve wrote:
| I love these books ... in my case Think Perl6 (now Think Raku)
| was a real eye opener. A quick scan of the new Python 3rd edition
| looks like it is quite dumbed down - which is probably
| appropriate for Python.
|
| I highly recommend the intro to Functional Programming in Raku
| (chapter 14). https://greenteapress.com/wp/think-perl-6/
| andai wrote:
| Cool, I didn't know about this book! I enjoyed Think Python,
| but this book surprises me. I'd think there are very few people
| learning Perl / Raku as their first language nowadays, so a
| book targeting more experienced programmers seems like it would
| make a lot more sense.
| skotobaza wrote:
| What are some books for mid to advanced programming in Python? I
| already know Puthon and programming in general but want to
| improve my Python skills.
|
| I know only Fluent Python which I'm currently reading, and
| CPython Internals.
| nickelpro wrote:
| I mean, the docs? And the source code?
|
| At a certain point of expertise, everything after basic
| journeyman familiarity, there's nothing left but to read code
| and write code.
| stefanos82 wrote:
| `Fluent Python` is more than enough; the rest of the language
| features you will understand them by reading the official
| reference manual.
| eru wrote:
| You could also look into more general algorithm and data
| structure books, or into design books.
|
| https://www.redblobgames.com/ has lots of really nifty
| articles, too.
| __mharrison__ wrote:
| You might want to consider books that show application of
| techniques in real world practical code.
|
| For example, Effective Pandas 2 illustrates common patterns for
| dealing with tabular data. Along the way, it uses
| comprehensions, lambdas, unpacking, etc. Shows how to use
| pytest to refactor. Leverage visualization to understand data.
|
| (Disclaimer: I'm the author)
| brunooliv wrote:
| This book ALSO changed my life when I was first learning
| programming!! Not so much in terms of a pivot but of the way the
| book was written itself and how some concepts all of a sudden
| just made sense as a total freshman. Now, ~10-15 years later,
| seeing this third edition is such a nostalgia and I can't
| recommend this book enough!!!
| caturopath wrote:
| Love Think Python, I have recommended it to so many learners: it
| balances the various concerns of a new programmer book really
| well. Allen Downey has a bunch of other books with somewhat
| similar approaches too https://greenteapress.com/wp/ -- some I do
| think he might have gone too far in the low-rigor side, but all
| the ones I've reviewed have been pretty good.
|
| (I was sharing a table at a conference with Allen some time ago
| and told him how many times I'd recommended or bought people his
| books, and I think he thought I was bullshitting him.)
| bmitc wrote:
| How does one do proper concurrency in Python? As in separate
| "processes" running with encapsulation, fault tolerance, etc. For
| example, how does one bundle up a TCP client in a "process" such
| that the process handles failed connections, broken connections,
| retrieving data at periodic intervals (relatively fast),
| receiving "messages" from other "processes", etc.?
|
| There's Ray, Pyro, Pykka, Celery, multiprocessing, asyncio,
| threads, Qt, and more, but all of them have issues. And a lot of
| it boils down to the GIL, although "processes" that are doing I/O
| such as TCP and other network communication should ideally reduce
| the GIL effect, to my understanding (is that right?).
|
| From what I can tell, it's basically one of the worst language
| choices for systems of this nature, but I am trying to figure out
| how to do it because I need to.
| andai wrote:
| Something something libuv?
| da39a3ee wrote:
| Can you give an example of what you consider a good solution in
| a different language? That will help us see exactly what you're
| looking for and how/whether it might be achieved in Python.
| bmitc wrote:
| Erlang and Elixir. :)
| staticautomatic wrote:
| Well RabbitMQ is built in Erlang so there you go :)
| bmitc wrote:
| But RabbitMQ is just a messaging bus, isn't it? That
| doesn't handle the processes (operating system processes
| or virtual processes) actually processing the messages
| and those processes handling fault-tolerance for the
| things they're connected to.
| staticautomatic wrote:
| That is correct. Honestly I'm not sure this is a good use
| case for Python but it's definitely possible. I've used
| the RabbitMQ + gevent + multiprocessing pattern in the
| past and it works but I find the code extremely hard to
| reason about. If I were doing it again from scratch I'd
| probably choose another language with better concurrency
| primitives.
| bmitc wrote:
| > Honestly I'm not sure this is a good use case for
| Python but it's definitely possible
|
| It definitely isn't, but this is a new role with a rushed
| timeline in a place dominated by Python.
|
| > and it works but I find the code extremely hard to
| reason about
|
| This is also a major concern of mine.
| staticautomatic wrote:
| Rather than handling the multiprocessing and message
| passing yourself it would be much easier to use celery +
| gevent and let celery do the work of spinning up
| processes for executing the tasks.
|
| It may feel limiting but my advice would be to keep all
| the celery job queue stuff isolated from your server,
| especially if you are using an async web framework. Have
| your web server just put all the jobs in the celery queue
| and let it handle executing them, regardless of whether
| they're cpu or io-bound. If you try to optimize too much
| by doing something like leaning on celery for cpu-bound
| tasks but letting your web server handle the io-bound
| ones you're going to be in for a world of hurt when it
| comes to both debugging and enforcing the order of
| execution. Celery has its warts but you'll at least know
| where in the system your problem is and have reasonably
| good control over the pipeline.
| bmitc wrote:
| Can Celery help handle state internal to the workers?
|
| What about using Pkykka alongside Pyro such that each
| Pyro remote object is actually a Pykka actor? Such that
| Pyro allows splitting workers across separate Python OS
| processes and the "messaging" while Pykka handles the
| internal state.
| staticautomatic wrote:
| Could you give an example of state internal to the
| worker?
| bmitc wrote:
| Similar to how one can pass around state inside an OTP
| GenServer in Erlang.
|
| In Python, it could be a class managing a session type of
| object, like a TCP socket connection, or managing some
| piece of hardware that is doing something independently
| of the other parts of the system, or a database writer,
| etc.
| Siecje wrote:
| Use asyncio. Bind to a socket, set blocking False, then
| asyncio.run(on_new_connection(sock)).
|
| Inside that coroutine get the loop and await
| loop.sock_accept(sock)
|
| And then asyncio.create_task(on_connection_data(connection)).
|
| The only gotcha is you need to keep a reference to that task so
| it doesn't get garbage collected.
| staticautomatic wrote:
| Get garbage collected by what? Doesn't Python use reference
| counting?
| d0mine wrote:
| asyncio doesn't store strong references to tasks. It is
| your responsibility to keep the ref:
| https://docs.python.org/3/library/asyncio-
| task.html#asyncio....
|
| Consider organizing the code using TaskGroup.
| jph00 wrote:
| Python has a GC.
| foresto wrote:
| The reference implementation (CPython) does use reference
| counting, but that is not its only approach to garbage
| collection.
|
| "The default build implementation is a generational
| collector. The free-threaded build is non-generational;
| each collection scans the entire heap."
|
| https://devguide.python.org/internals/garbage-collector/
|
| https://docs.python.org/3/library/gc.html
|
| (And as someone else pointed out, asyncio's event loop
| keeps only weak references to tasks, so the GC
| implementation doesn't really matter here.)
| jononor wrote:
| If you are I/O bound then asyncio or good old fashioned gevent
| will do great. If you are CPU bound, then use multiprocessing.
| If you need to accept "jobs" from elsewhere, use RabbitMQ (with
| or without Celery). If you have mixed CPU/IO workload that fits
| the worker pattern, then you would do all 3. At the top level
| you have a RabbitMQ consumer, fetching jobs from a remote queue
| and then putting these into a multiprocessing queue processed
| by N=~cpucount processes. And each of these use asyncio/gevent
| to do their work.
| bmitc wrote:
| So do you recommend a single Python process running asyncio
| or multiprocessing or both? Or do people normally split these
| things amongst several Python processes?
|
| My understanding is that multiprocessing creates multiple
| interpreters but that it still comes across some GIL issues
| if all under the same Python process.
|
| I am in general quite comfortable with the actor model, and I
| would ideally use Erlang/Elixir here, but I can't for various
| reasons.
| varispeed wrote:
| Why some people are extremely averse to RabbitMQ?
|
| I saw that at one company having RabbitMQ / Celery setup -
| every time a new software engineer comes in, they complain
| about RabbitMQ and ask why would company use it. The
| infrastructure was running like this without hiccups for
| years. At one point company has let go of many experienced
| engineers and this time one developer found some issue with
| the code and blamed it on rabbit as it was locking the queue.
| There were no more senior developers to contest it, so he
| convinced manager to swap it out for Redis. He took about two
| months to rewrite it. Surprise, the same issue existed on
| Redis. The Redis solution works fine, but has its own
| limitations...
| F00Fbug wrote:
| This has been a great resource - I love this book! For the last 5
| years, I've taught an intro to programming class at the college
| level and I always recommend that my students augment their
| resources with this book.
|
| Glad to see it evolving!
| irrational wrote:
| Does anyone know how to report issues with the first chapter? I
| don't see a way to leave feedback.
| hoppyhoppy2 wrote:
| Maybe https://github.com/AllenDowney/ThinkPython/issues
| irrational wrote:
| Thanks!
| xoxxala wrote:
| "If you have comments, corrections or suggestions, please send
| me email at feedback{at}thinkpython{dot}com."
| peruvian wrote:
| Apologies for changing the subject, but aside from real world
| experience (which I have and am getting at work), is there a
| resource of similar quality for more intermediate/advanced Python
| programmers? I always feel like there's a big chunk of the
| language or stdlib I do not know.
| jjice wrote:
| I enjoyed Effective Python. It's a "tips" style book with a
| good handful of recommendations with use cases and
| applications.
| bikingbismuth wrote:
| It's a bit older, but I learned a lot from "Writing idiomatic
| Python". Honorable mention to "the little book of Python
| antipatterns" as well.
| leetrout wrote:
| Links for anyone else interested as I was
|
| https://jeffknupp.com/writing-idiomatic-python-ebook/
|
| https://docs.quantifiedcode.com/python-anti-patterns/
| zerkten wrote:
| Fluent Python and Effective Python are good books. The former
| is huge and is really multiple books in one.
| theptip wrote:
| I like https://effectivepython.com/
|
| Also just reading Norvig's annual Advent of Code
| implementations usually provides some insight on how to write
| elegant and concise Python code.
| billbrown wrote:
| Looks like there's a new edition coming in March.
| https://www.amazon.com/Effective-Python-Specific-Software-
| De...
| js2 wrote:
| I learned Python starting with 1.5.2 from the official
| documentation and think it's a good resource.
|
| https://docs.python.org/3/tutorial/index.html
|
| https://docs.python.org/3/library/index.html
|
| Whenever a new version is released, I read its What's New
| documentation.
|
| Beyond that, I like to read source code, both for the stdlib
| and popular third-party packages. This advice generally applies
| when I'm learning any new language or re-familiarizing myself
| with one, not just Python.
| sireat wrote:
| I really enjoyed Fluent Python a while back as an
| intermediate book.
|
| Python official docs are not completely horrible, but
| compared to most other popular languages (Kotlin, Scala,
| Rust, Go at least), the Python official docs are kind of meh.
|
| I suppose Python docs beat C and C++ which do not have
| official docs besides the spec. (not counting K&R and
| Bjarne's books).
|
| Also I guess Javascript does not have official docs (ie MDN
| is not official)
| pid-1 wrote:
| Reading the docs proactively (not just when you need
| something).
| billfruit wrote:
| Fluent Python is good book at that level, and works as a good
| reference book while working too.
| hobs wrote:
| I have bought this book for every friend learning python for
| work purposes, really fleshes a lot out that's not taught
| implicitly. The data model stuff is really useful.
| rmk wrote:
| Python Modules of the Week (PYMOTW). Great resource to learn
| the stdlib.
|
| https://pymotw.com/3/
| nickpsecurity wrote:
| Humble Bundle had some nice collections on Python for many
| uses. For in general, I remember that Serious Python and
| Automate the Boring Stuff with Python were both good.
| febeling wrote:
| What is a good book for learning Python when you already now
| some/many other languages?
| d0mine wrote:
| Just follow the tutorial (yes, really)
| https://docs.python.org/3/tutorial/index.html
| autoexec wrote:
| It's what I started with, but honestly it's not great. It
| also heavily overemphasizes the interactive interpreter
| orzig wrote:
| The author also blogs his latest Python projects (mostly Bayesian
| data analysis) on https://www.allendowney.com/blog/ , which I
| have really enjoyed.
| lynguist wrote:
| I use the 2nd edition when I teach intro to programming. The
| students uniformly love this book and prefer it over any
| university provided material.
| mharig wrote:
| Have a look at https://realpython.com/
| nullptr_deref wrote:
| Hey genuine question,
|
| Why would anyone bother with learning anymore? When is learning
| enough to get started?
|
| How will someone get started with work? What qualifies someone to
| start a new position?
|
| Pretty sure only smart people will be hired for the roles.
|
| No matter what I do, I am forever unqualified, even from junior
| roles.
|
| 1. Have post grad degree
|
| 2. Have internships
|
| 3. Have only part time or short tenure roles.
|
| But can't get into any industrial role. Why?
|
| Means, I die? What qualifies for a job?
|
| Well one option is to go back to shithole country I came from and
| just stay there earning pennies. With the money I make there
| affording a new mac will take 2 years salary. So that is the kind
| of shithole I am talking about.
|
| Seriously, why?
| itsoktocry wrote:
| > _Pretty sure only smart western people will be hired for the
| roles._
|
| Everywhere I've ever worked in tech, in my 20 year career, has
| had people from all backgrounds.
| andai wrote:
| I particularly enjoyed this passage from an older edition of
| Think Java: https://files.catbox.moe/v1vgdc.jpg
|
| Also neat:
|
| > What happened next is the cool part. Jeff Elkner, a high school
| teacher in Virginia, adopted my book [Think Java] and translated
| it into Python. He sent me a copy of his translation, and I had
| the unusual experience of learning Python by reading my own book.
| ListeningPie wrote:
| Is it really so difficult to learn loops and objects? It's always
| been syntax and not knowing how to use library that tripped me
| up.
___________________________________________________________________
(page generated 2024-02-16 23:01 UTC)