Can bad gas make the check engine light go on the oxygen sensor code p0040 came on and i cleared the codes hasn't come on yet?

I would not think so but I have learned that nothing is impossible. P0040 is not an O2 sensor code, it is a EVAP system leak. A loose gas cap will set this code for sure as the code indicates a leak in the EVAP system. If it does not come back on then forget it. If it does replace the gas car first and clear the codes. If it comes back on then check the EVAP system hoses for cuts or leaks, check the purge valve. If none of these are leaking you need to see a pro and have a smoke test run. These can be very tricky to find at times.