Not Python itself, but Qt has a way. You probably don't want to run a loop, but use Qt's event handler system. To do this, you create an instance of a custom Qt class and "install" it as an "event handler" on the main Maya window (or on a viewport). Then in the eventFilter() method of your class, you look for QKeyEvent, check the key, and if it's the one you want, you run your code.
There are a lot of links in that chain, so try something really simple first, like printing the event you get. Just be aware that if you print for every event (rather than only for QKeyEvent) you will hang Maya because there are so many events it will clog up the Script Editor.
This post:
https://groups.google.com/d/msg/python_inside_maya/Auiy06xVqLo/61sqMp_2AQAJshow an example of putting an event handler on the main Maya window, in this case to detect Drop events. You could adjust it to detect keystrokes. It was written for Maya 2015 (where the version of Qt was PySide) so you may need to adjust it to use PySide2 as well.