My question involves real estate located in the State of: CA

My understanding is that when a married couple sells a home, can be exempt up to $500k in capital gains. If they live in 2 out of the last 5 years. Do they have to purchase the property as a married couple? Or can one person buy it as a single person, live in it, get married, then sell it as a couple later to get the $500k tax exemption?