Hacker News new | past | comments | ask | show | jobs | submit login
Ask HN: How to get back to programming Python?
59 points by mlitwiniuk on Nov 14, 2023 | hide | past | favorite | 56 comments
I've used Python back on my university days (some numpy coding, some gui apps for both linux and windows) and later professionally, when doing webdev in Pylons and Django. Then I've switched to Ruby and for the last 15 years haven't touched Python at all. And now actually I'd like to get back to it, with some side projects. Ideally I'd love to do some image recognition / qualification apps. Question is - what's the best place to start? I remember some basics, but as I can see, A LOT has changed.



I stopped using Python professionally for a few years myself. The biggest changes I noticed or made myself:

- I used to do everything in vim, but now I adopted VSCode (usually with SSH-Remote) and it's been such an improvement to my productivity. Try to use the integrated test explorer and debugger.

- If you use type annotations (sparingly), your VSCode experience gets even better.

- Many projects use auto formatting with 'black' these days. Initially I was a bit grossed out by this, but I now love it. It frees me up from having to spend mental cycles on complex lines, and removes the friction when reviewing CRs. The one change I make is to have a slightly longer line length.

- pyproject.toml is a new project level config file. Try to only use that file for all your setup and tool configurations

- Since you said you last touched Python 15 years ago, we now have Python 3.x. Syntax is a lot cleaner. There are many smaller differences.

- async/await is new. It is pretty neat if you want to do async programming. Don't expect a major boost (or any boost) in performance over traditional threaded code, but you can expect much cleaner code.

- f-strings are pretty neat, I use them pervasively

- I'm not sure if these are already 15 years old, but "with" context managers are the primitive to use for cleaning up resources.


In case you're not already aware, GPT4 et al write usable Python and can execute some Python code themselves for trying things out.

I find it much more pleasant to ask GPT4's advice and have it write sample code than it is to use web search, Stack Overflow, etc. Even when I know exactly what code to write, it's often faster to ask GPT4 to write it a certain way and then make minimal edits myself.

The paid version of GPT4 used to be the best, but lately the VSCode Insider GitHub Copilot produces comparable or identical results, since it uses GPT4. I have only one custom instruction, "If writing Python code, always use context managers where appropriate."

EDIT: If you don't want to hassle with maintaining a development environment, try Replit[0]. Their AI is not nearly as good as GPT4 though.

[0] https://replit.com/


I'd strongly second the suggestion to use the paid (GPT-4 based) ChatGPT Code Interpreter. I've found it to be incredibly strong/useful for interactive Python coding and it does a very good job explaining itself. You can use the custom instructions like "I'm an experienced Ruby developer that last used Python in 2005" and it can provide much better results as well.

I've only poked briefly at it, but https://deepseekcoder.github.io/ evals as very strong at code and is probably the best open model available. You can chat with it for free w/ a signup (or run it yourself if you're looking for a project).

If you're looking to poke around with local models more easily, you can give KillianLucas/open-interpreter a try (in conjunction with LM Studio or w/ an OpenAI or Anthropic API key), it's pretty neat (be very careful with code execution, I'd recommend doing it in a sandbox lest you accidentally trash your system).


I like open-interpreter, but be careful using GPT-4 API, you can quickly run up your bill using it.


i cant tell if this is free/uses GPT-4 for something else?


You can use it if you are running your own model at home (eg code llama) or use it with gpt 3.5 or 4


whats a context manager



thanks! have been using the with statement didn't know that's what it was called.


I felt the same way and recently did the whole 92 questions of CodeSignal's Python Arcade section (https://app.codesignal.com/arcade/python-arcade). This made me completely fluent on modern Python syntax and the questions themselves are very easy so can be done quickly. You are really only re-learning Python's syntax as opposed to doing hard algorithmic problems or anything like that (although I do recommend doing this as you'll be able to touch on Python's standard library more).

I guess the one thing that you don't get to do with that approach is build something interesting or use `async-await` but it gets you fluent with the syntax again which is an important first step.


(re)learn the tooling first; especially virtual environments [1], python 3 features. Wow at f-strings, smile at structural matching, enjoy the feel of tools like PyCharm.

Find a project to start from scratch, or find one to which to contribute. Read as much Python 3 code as you can to get your neurons firing.

[1] stick to venv, pip install, and pip freeze (time spent down the rabbit hole of packaging is time not spent actually coding)


re: virtual environments - Getting back to learning Python myself, started with `Pipenv`, but recently stumbled across Poetry. Recommendations on one vs the other?


Unless I'm making a "project" that I want to eventually package into an application or library, I prefer to just use pyenv with venv. Pyenv lets me pick my Python interpreter which I then use in conjunction with venv to create a virtual environment. For example, creating a venv with Python 3.9:

    pyenv local 3.9
    python -m venv my-env
    source my-env/bin/activate
    pip install [whatever I need]


Jinx you owe me a coke.


Funny that we both formatted the commands and everything xD


And we joined at nearly the same time, have a similar HN "score" and have a similarly dorky handle. We complete us.


Find the "just right" packaging system will get someone nerdsniped, you can do that. Or you can do the commands below and get something running in 20 seconds using the built in tools. Nerdsnipe yourself later as an exercise.

    python -m venv myenv.env
    source myenv.env/bin/activate
    pip install -U pip setuptools wheel
    pip install ipython
    ipython<return> now repl away!


the ".venv/bin/activate" script is rarely useful outside blog posts. Instead, just have a "bin/run-main" script set PYTHONPATH to point to the source files in your project, then call '.venv/bin/python3 src/python/some/where/file/name.py "${@}"' to run it. YMMV. HAND. IANAL.


Definitely poetry; pipenv is not actively maintained and poetry is it's spiritual successor.


Definitely not, pdm is better behaved and less prone to random breaking changes


Poetry. Pipenv has a very bad reputation and most new projects that needs a more robust tooling than a simple “requirements.txt” use it.

This is also confirmed by my own experience.


Poetry is not stable at all. pip-tools all the way, if not…pdm please


I'm a pyenv + virtualenv person and that's worked well for many years. But it sounds like I'll need to have ChatGPT explain Poetry to me...

https://fathomtech.io/blog/python-environments-with-pyenv-an...


Personally, I like Poetry over pipenv, but preferences will vary. There are some other alternatives currently fighting for mindshare, too. It will be interesting to see where the dust settles.


Thanks for the input everyone. Clearly Python needs more tools lol



I've tried both and actually prefer PDM the most by far


Poetry is the way to go.


Thanks. Any examples of Python 3 code worth looking at?


Don't know if its worth looking at, but you can look at my code: https://github.com/Vrroom/vectorrvnn :)


I can't speak highly enough of Miguel Grinberg's work with Python/Flask (https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial...) and the community he's created around it, for both beginners and advanced folks.

Racing through his mega tutorial was a great refresher for me on the fundamentals, and it's easy to plug in computer vision & related libraries/extensions/packages.


If you sign up for ChatGPT Plus ($20/month) you get access to Code Interpreter, which lets GPT-4 write Python code and then execute it in a sandbox environment and show you the results.

It's an incredible tool for learning Python, because it means you can explore all kinds of new tricks and see the results instantly - directly from your browser (or even on your phone).

I have 20+ years of Python experience and I use Code Interpreter mode to try things out several times a day. I think it's an incredible tool for learning.

I wrote a bit more about it here: https://simonwillison.net/2023/Sep/29/llms-podcast/#code-int...


Create a small program, and iterate, iterate, iterate.

I took the entire pandemic off from programming, and when I had to go back to find a job, I needed to level up quickly to be able to pass programming interviews.

I wrote a program to start downloading stock quotes, then added writing to a database, then added code to graph it with flask, etc. If you choose something you're interested in and keep expanding the scope of the project, it's the best way to learn.

And functionally, Python 3 isn't a huge difference over Python 2, especially if you're starting from scratch again. For me the biggest change is adding parentheses around my print statements. Everything else is pretty similar.


Advent of Code [1] is coming up soon and its a great way to learn a language. The past years problems are still available and there are a lot of solutions all over reddit, github. This is a big plus because you can compare your code with them and find idiomatic ways to do things or libraries you might not have known about etc.

[1] https://adventofcode.com/


"Please don't use AI to get on the global leaderboard"


Grab Python 3.12, install IPython.

Install PyCharm

Forget everything you know and work through https://www.dabeaz.com/python-distilled/



I recently switched from Ruby->Python.

I liked this guide as an overview for more experienced programmers: https://docs.python-guide.org/

I thought the "Learn Enough Python to be Dangerous" book was a good intro but like many books/guides it's kind of redundant for experienced developers.

Mostly, the big revelation has been AI tools like ChatGPT/CoPilot helping with specific syntactic drudgery like "how do i iterate over this thing" as well as bigger questions.


> https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial...

Wow I practiced programming with that tutorial years ago, it's amazing people still reference it.


If you're looking for non-project experience, I find Project Euler[1][2] to be a low stakes way of (re)learning a language.

Advent of Code[3] is also coming up in about two weeks. It's another low-stakes way of engaging with a language.

1. https://projecteuler.net/

2. betrayed by "project", but it's just solving math problems with your language

3. https://adventofcode.com/


This may sound stupid, but do read the tutorial. And then the original docs - Language, and (beginning of) Library parts. Like (x and y or z) vs (y if x else z). As a plain novel if you want.. the interesting bits will stick.

What's there has been there looong time.

Most of stuff / fluff added last 10 years isn't really essential.

have fun

p.s. uh yes. There are python programmers, and there are django/.../"lego" programmers. Seems recently the lego ones are in more demand.. and availability


You can approach this in two stages:

1) Reacclimating yourself with the language. When I'm trying to pick up a a new language or re-engage with a language, I like to work on bite sized problems to understand the syntax and get somewhat comfortable with writing in it. I like to focus on the language and not worry too much about environment setup, best practices, frameworks, etc. You can easily do this by solving some Leetcode/Project Euler problems and running the code in Repl.it.

2) Working through a project. You're going to get the most value out of the time if you try to actually build something. The issue with step 1 above is that it doesn't teach you how things like package management, environment setup, testing, etc. work. It's going to be very slow at first, and you might end up restarting the project several times based on updated learning (which is good).

As it related to Python specifically, fortunately there's loads of very solid information available online. I'd pick one of the well-known frameworks (Django or Flask) and just start trying to build something with it.


I made more or less this journey a year ago, I’d dabbled in Python in the past, then done a decade or so of Ruby before accepting a job at a Python shop. Advanced Python Development by Matthew Wilkes was great for giving a jumpstart from “I can read Python and throw together some quick and dirty code” to “I can write Python to a professional standard”.

Coming over from Ruby I was very pleasantly surprised by just how good all the tooling is. Type checking is really well supported and an absolute godsend when working on large applications, and VSCode has deep support for the language.


I remember the Pylon days! I'd recommend Michael Kennedy's FastAPI course on MongoDB and Beanie. It's a quick introduction to how things can be done these days, and a great springboard:

https://training.talkpython.fm/courses/mongodb-with-async-py...

FastAPI plays well with OpenAI's APIs and ML APIs in general. There are some great examples on GitHub for that.


How does a low effort post like this make to the front page of HN? You have been programming for 15 years in Ruby, and you are confused about how to learn a another language?

Here's your answer: start programming a project that interests you in the domains you listed. Learn something, ask question, iterate.

Seriously, what kind of answer are you expecting here? Are you expecting someone to give you a brand new way of learning a subject? Something outside the bounds of tutorials/videos/documentation/hands-on?


If you want to segment images, classify objects, and extract metrics from the objects, you'll want to learn several deep learning methods (like YOLO, Mask R-CNN, or Segment Anything). Such tools are almost always implemented in Python, but most of your work in that space will be preparing and feeding data into the model, evaluating the output, then tuning/debugging the model and data, which is not really the same as coding in Python.


One of the things that has changed is typing - you can and should type check your Python code with tools like mypy or pyright. Even better if you use a strict mode for mypy which forces you to type hint everything.

Also check out dataclasses, Pydantic (most of my code these days is gluing together dataclasses and Pydantic models), FastAPI, PyTorch if you're interested in ML.


As a suggestion, you could start by writing a simulation for a N elevator system. This will jump start your experience with real-world problems... multi-threading, priority queues, shared state, and much more


You mention image recognition, I can recommend this website that does a good job with python calling OpenCV:

https://pyimagesearch.com/



a lot has changed and 15 years and also python's traditional slow pace of change is less true (Guido van Rossum liked it that way and the BDFL has moved on).

to just get a refresh of the basics (then search for the rest) the tutorial is pretty good: https://docs.python.org/3/tutorial/index.html

if you want to get more depth a good book is Fluent Python by Luciano Ramalho.


I found the book “Python Distilled” got me up to speed quickly after not doing a ton of Python for about a decade.


Type python, hit enter, proceed to program...

Of course, if there's no meme for that, it doesn't actually exist does it...


Try finding someone who knows python, and watching them for a few hours. Take notes in a notebook.


If you have background in Rails, maybe doing a project using Django would be a good start.


ChatGPT

Start with ChatGPT




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: