Ask HN: Which book would you recommend for learning Python in detail?
Hi, I know some programming. I know C and some python as well. I have built projects with python but every time I work with python, I feel like python is made on the simple basic structure and I don't know that. I find myself constantly looking for small things as well, I feel like I really don't know the internals of pythons. Which book would you recommend that you go beyond the syntax and shows how things works on a basic level so that I can get build up some intuition while working with python. Forgive me if I failed to express myself.
64 comments
[ 5.6 ms ] story [ 118 ms ] threadWhen you're ready to go beyond the basics, I recommend Effective Python [4] and Serious Python [5]. Both go deeper on classes, metaclasses, how objects are represented internally, and Serious Python covers a bit of functional programming, scaling, etc.
[1] https://www.fluentpython.com/
[2] http://www.dabeaz.com/python-distilled/
[3] https://automatetheboringstuff.com/
[4] https://effectivepython.com/
[5] https://nostarch.com/seriouspython
Automate the boring stuff is a good beginner book, I like the coverage in “Introducing Python” by Lubanovic because it covers Modules and Packages for a whole chapter whereas in other books distributing your code is often a footnote. Even Fluent Python (my favorite python book) doesn’t have that great coverage of that.
Also perhaps Python Cookbook.
A major part of any language is knowing the standard library(-ies).
* Practices of the Python Pro (https://www.manning.com/books/practices-of-the-python-pro) — learn to design professional-level, clean, easily maintainable software at scale, includes examples for software development best practices
* Intuitive Python (https://pragprog.com/titles/dmpython/intuitive-python/) — productive development for projects that last
This teaches 90 specific ways to be more Pythonic
* Robust Python
This teaches some architectural / implementation patterns for code bases larger than a script
* High Performance Python
How to get the most juice out of Python, including compiling
For learning python (if you already know some programming) I always recommend “Introudcing Python” by Lubanovic it gives a coverage of all the major python features.
Once you are able to code basic python I use the book “Fluent Python” by Ramalho (I still use it to this day even as an advanced python programmer). This book has an amazing coverage of every single advanced feature in python. Anytime you’re on the job and you know you could use some fancy classes or some async stuff or maybe multi threading you can visit a chapter in this book and get up to speed on it. I probably open this book once a month just to refresh my knowledge on some particular python feature I want to use.
https://www.youtube.com/playlist?list=PLV7iu6GiDKwQWZbapY5mP...
Like you I want to know more about CPython behind the scenes, not how to use Python itself. I like the deeper understanding
[1] https://github.com/anthonywritescode/explains
Python Crash Course, 2nd Edition: A Hands-On, Project-Based Introduction to Programming, by Eric Matthes is my favourite for self study the basics.
It is so easy to go to SO and copy and paste code and not know what is really going on. I think understanding Python at a deeper level will be even more important now that chatGPT will spit out the code for you and happily lie about some things.
[0] https://www.amazon.com/Learn-Python-Hard-Way-Introduction/dp...
It's great that people like him are spending a lot of time helping others learn but imo he has done more harm than good.
[0] https://learnpythonthehardway.org/book/nopython3.html
That seems like an extraordinary amount of influence to ascribe to one guy who wrote a textbook. Python 3 adoption was mostly driven by large projects. I can't think of any sizable Python project that would base their decision to migrate on what some introductory CS students are learning.
https://pymotw.com/3
The advantage of this is that you learn by looking at examples, and at the same time you learn the standard library. In my opinion it's much more valuable to learn the standard library of a language once you know a little bit of the language itself, because it helps you write useful programs and learn the language in that way.
I also recommend reading the official Python documentation, especially when working on things, and looking at functions within whatever module you are using that you are not using. Over time you will discover functions / methods you had no idea were in there. I also suggest just exploring other modules you have yet to use.
OK, __slots__ and pdb, but the rest are "as needed" and don't have much to do with everyday Python programming.
But then I re-read the OP question, and he asks indeed about learning Python 'in detail' - what's going on under the hood.
Then I spend some time researching those questions, and it's pretty interesting !
> I feel like python is made on the simple basic structure and I don't know that ...<snip>... I feel like I really don't know the internals of python
I think i took what you said a bit too literally :-) You definitely don't need this stuff to be a solid, competent Python developer.
I was suggesting these topics because i feel like understanding these were what unlocked the ability for me to handle new things - things new to me, not necessarily new to python - and be able to intuit what they meant or were doing behind the scenes. This knowledge made it so that i stopped feeling like "i don't know exactly how that works" when i saw new things in Python.
Don't get me wrong, it's not magic. E.g. when the walrus operator was introduced, i could guess how this was done but i didn't know just by looking at it.
https://tenthousandmeters.com/tag/cpython/
https://rushter.com/blog/tags/cpython/
https://eli.thegreenplace.net/tag/python-internals
https://realpython.com/cpython-source-code-guide/
I'm not aware of any good books about Python's internals.
You can also explore the CPython dev guides: https://devguide.python.org/internals/.
On the other hand, if you want to be a productive Python programmer, don't feel like you need to know anything about C++ to do so. there may eventually come a time where it becomes relevant for you, but you can get very far before it comes up.
I am a Deep Learning Research Engineer who works 100% in Python.
I had just started second year in college with only basic knowledge in imperative C. Didn't even know how to write a C function other than main().
Then I started learning Python with Python Crash Course by Eric Matthes.
I finished the entire book in five days. I really enjoyed it and learned a lot.
This is the ONLY programming book I read cover-to-cover, and the only Python book I have ever read.
This book served me greatly.
When anyone asks for Python book recommendation, I recommend this book only.
You don't need any other.
This book has the basics taught, then goes through three projects. One of them is a game. I remember.
All the exercises are great, self-contained, doable without outside help, and help you grow.
And the book's design has all that No Starch Press goodness to it.
Go for it.
It mainly started in Humanities classes in middle school. I had great History/Geography/Bengali teachers there.
The gist of what they taught me was to optimize writing for one thing and one thing only- clear communication of information. No need to impress or bloat. So that the person grading your test copy finds it the most convenient to read your answer (hence bullet points, smaller paragraphs, etc.), and finds the answer so easily, that they don't dare to deduct marks. India is big on standardized tests, and a total stranger grades your copy.
I used to optimize for grades, and now I do for communication of information. I employ the same things when writing tutorials, documentations, and reports.
I am a reader, and some advices from one/two memoirs of authors I read stuck with me.
I also used to spend 10-20 hours per week on Quora during college, and was in the eyes of admins there, and worked on all sorts of pilot projects. I also had real recognition and multiple thousand followers in one non-English language version of Quora. Practice comes handy.
I also think communication is very important- in teaching, presenting, and writing. So writing is only a part of a bigger part of myself. And communication not in the smelly MBA/LinkedIn kind of way. Communication in its true sense.
I also think that I shouldn’t try to impress or flex while writing. I can comfortably do that because I have plenty other ways to wow people in life. So no word or sentence I use is engineered to impress anyone.
And I always keep in mind that I am selling something. (I have no ties to the publisher or the author of the book here) I really benefited from the book, so I spread the word whenever I can. So, if I can convice 10 people to buy/borrow, and 2 of them read it in full, I honestly believe that they will benefit.
I don't think I am a particularly good writer. However, if you find it good, this is how I do it.
No. It's bloody awful.
And reading and working with large codebases is very important for practical work. And it has no substitution.
I will recommend this path:
Read and do all exercises from Python Crash Course ---> Solve coding problems from Codewars. Choose Codeforces or Project Euler as well, if you have the corresponding bend ---> Make your own projects, solve your own problems ---> Contribute to Open Source projects --> Get hired. Unique growth happens after using a language in job. Or use Python in your job
In the intermediate space somewhere, learn something where you can use Python. It may be webdev, gamedev, cybersecurity, or ML/AI. Using Python is the key here.
Different people want different things when they want to learn a language. So, choose based on your goals.
The books just collect dust - and yet, every few years I try this experiment again with the same result.
Kinda interesting aside: I’ve been told I write great code in a particular language, while eschewing near universal patterns - patterns I’d probably have picked up if I’d gotten to Chapter 3 of any of these books. (It’s hard to independently discover cargo cult habits).
My take on the question: no book necessary.
Recently, I needed to ramp up quickly on an embedded C project (without any past MCU programming experience), and turned to a book for the first time in ~15 years. It was a refreshing experience - it was a short book, and I read it cover to cover over a weekend, but it really enabled me to be successful in the project I had to take on.
I like to think of real-world experience as building a sponge: you fill in the areas that you need to as you go, creating a sparse structure of knowledge. This complements formal training (whether a book or a class) - I think of these more "complete" approaches as pouring water into the sponge. Having hands-on experience lets you take more advantage of a class or book, and the class or book fills in the gaps that you simply didn't have to learn as you built things.
[1] https://www.youtube.com/watch?v=cKPlPJyQrt4&ab_channel=PyDat...
Its lessons are free, open source, and extremely accessible. Nice and short, too.
And, once you're proficient, you can contribute back to it!
It's not a book, but I loved it and it is really helpful.
I would recommend compiling a debug build of Python and stepping through it while it executes your script, even if it takes you an entire day for a simple script. Python is a big dictionary sandwich. Everything is a dictionary, even dictionaries.
What does
A.b.c() do?
You do not need to know the Python internals. The semantics are good enough, but the leak through everywhere. You might enjoy using https://micropython.org/ to learn instead.
All three are quite well written, kept more reliably up to date than any other resource, and provide links to a wealth of other material.
A particularly useful point is the link to the source code found at the top of most of the Standard Library pages… Python’s source code is really literate, and often the best way of understanding what’s happening “under the hood” is to actually look, rather than rely on someone else’s summary or intuition of what maybe only _was_ under the hood.