Dazbo's Advent of Code solutions, written in Python
Python Beginner's GuideThe Python HandbookThe Official Python TutorialGetting started with Python in VS Code TutorialGitVirtual EnvironmentConda
If you’re new to Python, then you’ll want to start here. If you’re already comfortable using Python, then feel free to dive straight into the AoC walkthroughts, using the links above.
If you’re a newbie to programming, then Python is a great language to start with. If you have a lot of programming experience, then guess what? Python is an amazing language to learn, and you’ll love it.
Many years ago, I used to write software for a living. Back then, my preferred languages were Java and C#. These days, I don’t write code in my job, though I do still need to know my way around. Now I write code as a hobby. But I can honestly say: of all the programming languages I’ve ever used, Python is my favourite.
For those of you coming from a background with languages like C, C#, Java, Javascript… You may find Python’s syntax and structure a bit jarring. But trust me: once you get your head around the differences, you’ll love it, just like I do.
Here are some of Python’s strengths:
I recommend working through the following, to get started:
To execute Python programs, you need to install Python on your machine. It’s possible you already have Python installed. To check, open your command prompt (or shell) and run:
> python --version
If you get a response that shows you have any Python 3.x version installed, then you’re good to go. E.g.
If you haven’t got Python installed, you’ll want the latest available Python 3 version, which you can download from here. It’s really quick and easy to install. It should only take a couple of minutes. Take a look here if you need any guidance on installing Python.
Once you’ve installed it, check the installation as described above.
You can write and run Python programs without a development environment. But it’s a much better - and more effective - experience, if you have one.
A development environment adds a wealth of features, such as:
My favourite development environment is Visual Studio Code - aka VS Code - which can be downloaded and installed from here. (Note: VS Code is not the same as Visual Studio, which is a different product.)
Some benefits of VS Code:
Check out the following useful links:
Now you’ll want to install some extensions that will make your Python - and general development - experience better. In VS Code, click on the Extensions button in the left hand panel of buttons. I’d recommend installing the following:
Note: if you’ve followed the Getting started with Python in VS Code Tutorial, this will have guided you through installing some of these extensions.
Git is an extremely popular version control system (VCS). A VCS provides capabilities like these:
Git is an open source VCS, created by the legendary Linus Torvalds. You know… The guy that created Linux.
Git is:
Finally, if you’re completely new to Python, you might want to learn some basics before you begin with AoC. Here’s a list of some decent learning material to help you get started:
Virtual environments are isolated Python contexts in which we can install Python packages. This allows specific and explicit combinations of Python runtimes, packages and frameworks, that may be different from other versions installed in the host OS. Basically, it gives us the ability to install packages in one environment without impacting another environment.
In Python, you should ALWAYS work inside a virtual environment (venv). Always install your packages into a venv, rather than at the global OS level.
You need to do this once, per project, per development machine you’re working with.
# From the root of your project folder
python -m venv .venv # where .venv is the name of the virtual env you are creating
Note that the .venv folder should NOT be checked-in to source control. You will always create the venv on any given development machine.
A venv must be activated. VS Code will usually detect any virtual environments installed within the project folder that has been opened in VS Code. But outside of VS Code, you activate a venv like this:
source .venv/bin/activate # Linux
.venv\Scripts\activate.ps1 # Windows
To install packages to an environment for the first time:
python -m pip install some-package
But once you’ve installed packages, you can “freeze” your current package installation, like this:
python -m pip freeze > path/to/requirements.txt
This file WILL be checked-in to source control. So we can use this file to repeat the same package installations on any other machine, i.e.
python3 -m pip install -r requirements.txt # install application dependencies
Conda is a powerful environment and package manager. It has a number of advantages over using pip and Python virtual environments:
A Conda environment is a separate Python installation with a specific set of packages; much like a Python virtual environment. We can work with Conda environments like this:
conda env list # see environments and current active env
export AOC_ENV="aoc-conda-env"
# Create and activate an environment
conda create --name $AOC_ENV
conda activate $AOC_ENV
# Install some core packages
# Before trying to run any Python code or Jupyter cells in this env
conda install -y -c conda-forge python jupyter jupyterlab
# Additional packages - or we could install these from (say) a Jupyter notebook
conda install pandas hvplot mathjax matplotlib networkx numpy plotly scipy
# Export the current environment config - for source control
conda env export > $AOC_ENV.yml
# To delete a Conda env
conda remove --name $AOC_ENV --all