First, make sure your system is fully updated:
sudo apt update
sudo apt upgrade
Next, install the default GCC toolchain with:
sudo apt install build-essential
Next, we need to install a few prerequisites for building Python:
sudo apt install libssl-dev zlib1g-dev libncurses5-dev libncursesw5-dev libreadline-dev libsqlite3-dev
sudo apt install libgdbm-dev libdb5.3-dev libbz2-dev libexpat1-dev liblzma-dev libffi-dev
At the time of this writing, the latest stable version of Python is 3.7.1, if you want to use a newer version change the next instructions accordingly:
wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz
tar xf Python-3.7.1.tar.xz
cd Python-3.7.1
./configure --enable-optimizations
make -j 8
sudo make altinstall