por vs para
Para points forward to a goal or recipient. Por looks back at cause, exchange, or movement through.
para
Destination, purpose, recipient, deadline, or a surprising contrast given the subject.
Lo hice para ti.
I did it for you (you'll benefit).
The action is aimed at you as its destination. Compare: 'lo hice por ti' = because of you / for your sake.
Para ser extranjero, hablas muy bien.
For a foreigner, you speak really well.
Sets up a contrast — given what you are, this is unexpected. A sideways compliment.
Lo necesito para el lunes.
I need it by Monday.
Monday is the deadline — the destination in time. Por lunes would mean 'around Monday.'
por
Cause, exchange, duration, movement through space, or on behalf of.
Lo hice por ti.
I did it because of you / for your sake.
You're the reason, not the recipient. The motivation, not the destination.
Caminamos por la playa.
We walked along the beach.
Movement through or along. Para la playa would mean you were heading to the beach as a destination.
Estuve enfermo por una semana.
I was ill for a week.
Duration. How long the state lasted. Para una semana would imply a deadline.
Tricky bit
'Gracias por todo' uses por (exchange — you're acknowledging what they gave). Never 'gracias para.' And 'por' in passive voice: 'fue escrito por Cervantes' = written by Cervantes.