Well, *that's* screwed up. You should complain to your local network manager about it. "localhost" ought to resolve to 127.0.0.1, or ::1/128 in IPv6-land, not something else. It's possible that 10.31.101.168 is your Mac's address, but that still doesn't make this correct behavior. So for the moment, don't use "-h localhost".
Before I go and break b.., can you guess the reason for this?
If you want the connection to go to the database named "lecture", you need to say "psql lecture", or some more verbose form of that such as "psql -h 127.0.0.1 -U ozan lecture".
This seems to work for accessing the database on the command line. The Python code seems to work as well.
Though, I must admit that I am still pretty much confused about what is going on. So, the problem seems to be the localhost that is somehow messed up. Is that right?