Changing python version is going to cause major problems because many components of Ubuntu depend on it.
First, install the package ubuntu-desktop to restore the necessary packages for GUI.
sudo apt install ubuntu-desktop^
In case you cannot connect to the internet without GUI, then you will have to reinstall Ubuntu. In the extreme case, you can connect to internet by USB tethering with your phone.
Reboot, and the GUI should be restored.
Afterward (either after installing GUI or reinstalling Ubuntu), to safely install python3.9 use the deadsnakes ppa.
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.9
To run Python 3.9, run the command python3.9. Alternatively, set up a python virtual environment (google how to).
Do not change the default python version.