Three specific situations that I can think of call for the use of a comma before "and."
1. The first is created when we have three or more items in a series (the serial comma.)
2. The second situation occurs when "and" is being used to coordinate (join) two independent clauses, like in:
John has taken guitar lessons for three years, and today he is an outstanding guitar-player.
(An independent clause is a group of words that has a subject and a verb and can stand alone as a sentence.)
3. to enclose phrases that interrupt a clause:
I warned him again, and I really meant it this time, to mind his own business.
As to 'but', it is just like 'and' a coordinating conjunction and it would also apply when it joins two independent clauses.
John was sure of success, but Mary wasn't. (here, 'but' joins two independent clauses).
Mary wanted to buy a book but could not afford to do it. (no comma before 'but')
(Here, the conjunction "but" merely connects the two verbs "wanted" and "could not afford", no independent clauses.)
Student or Learner