"Mientras Pepe jugaba a el baloncesto": el baloncesto has no life nor related to human beings, why put "a" in the sentence, why not say like "mientras Pepe jUgaba el baloncesto".
"Fue primero a la farmacia". What's the function of "a"? Why not use "en" en la farmacia?
"Por qué faltaste a clase" Why put "a" not "el" here?
Unlike the English verb to play, the Spanish verb jugar is intransitive. Therefore, you need a preposition to introduce its complements. In this case, to introduce the game you are playing, you use the preposition a. Why is it transitive in one language and intransitive in the other? Well, that's just the way each language has evolved.
In Spanish, fue can be the past indefinite of either the verb ser or ir. This sentence can mean that he was the first person to arrive in the pharmacy (in this case, it would be the verb ser) or that he went to the pharmacy before going somewhere else (in this case, it would be the verb ir). As written in the example, the preposition a indicates the second case; the verb ir is a movement verb, so it uses the preposition a to indicate the destination of the movement. If you wanted to use the preposition en, you would probably write Fue el primero en la farmacia, and in this case fue would be the verb ser (he was the first to arrive at the pharmacy).
There is no way you can use el in this sentence, since clase is feminine. Anyway, the verb faltar is intransitive too, so you need a preposition to introduce its complements. In this case, you are using the fourth meaning of the word in the DRAE (no acudir a una cita u obligación), and the preposition used to introduce this appointment or obligation is a.
Take into account that different languages have different grammar rules. You just have to learn how each word works in each language you learn.
The different a's that you're asking about all have different reasons which shows just how versatile the preposition is.
The verb jugar (not jagar) takes the preposition a. That's just the way it is. It's the same reason you cuidar de or tener que. It's not the a personal that's used with animate direct objects, though.
The verb ir generally uses prepositions indicating movement or motion, hence ir a (go to). While there's nothing strictly ungrammatically about ir en, in the example you give, entrar would work better.
faltar a (algo) is an expression summed up in one of the DRAE's definitions: "Dicho de una persona: No corresponder a lo que es, o no cumplir con lo que debe. Faltó a la lealtad, a la nobleza."
- "Mientras Pepe jugaba a el baloncesto" ¿What was Pepe playing? Al Baloncesto.
"a el"contracts to
"al"and you can remove it since
"Baloncesto"is a proper noun.
"Pepe jugaba el Baloncesto"doesn't sounds bad for me but I think is more proper say
"Pepe jugaba al baloncesto"or
"Pepe jugaba Baloncesto"I'd pick the last one.
- "Fue primero a la farmacia" ¿Where did he go first? A la farmacia [Not a proper noun
"A la"required]. ¿Where did he buy first? En la farmacia.
- "Por qué faltaste a clase" The only way of putting
"el"in there is talking about time. "por qué faltaste el Jueves/otro día/mes de mayo/". Clase is not a proper noun so needs
"a"but you can also say
"a la clase".
- Mientras Pepe jugaba a el baloncesto" , well I would never say "a el baloncesto" , but I suposse it is formal spanish from Spain.
"Fue primero a la farmacia". Here 'a' means to . I guess it is the same in english , in english you say " I went TO the pharmacy first " not 'IN THE PHARMARCY"
"Por qué faltaste a clase" , first of all clase is considered here as an 'event'. Faltar is the opposite of attending an event or going somewhere so just use the same structure , pay attention: No fui A la fiesta = falté A la fiesta , since that 'no fui' equals to 'falté' you use the same structure , so you use 'a'.