Dealing with a friend who always leans on me I study Mathematics and Computer Science. I have a classmate who  excessively depends on me when it comes to coursework . He wants me to explain definitions, proofs, share code, check his proofs and bugs in programs. It seems to me he does not pull enough efforts when he doesn't get things in the first try and approaches for help.

I have tried to advice by saying "you should work on this for more time" as most of his questions are trivial. I think that he finds it easier to get help from somebody rather than reading again or spending more time. I also think that I am spoiling him by helping. This is also ruining me as this eats up my time. 

What is a good solution to my problem? How can I convince him to put more effort from his part? I tried to avoid him especially when exams get near, but he ends up finding me.
He's a postgraduate, and so are you. A postgraduate degree is self-guided study. And while there's scope for group working and collective problem solving, hand-holding him is actually making his situation worse, not better.

In other words, by doing his work for him, you are hindering him. If he truly is a friend, then it's time for you to stop hindering him.

Explain to him that you have been preventing him becoming a fully-fledged researcher, and apologise for that; remind him that the purpose of the degree is to build research skills, self-discipline, and independent thought, and that when he asks you to do work for him, you're both complicit in preventing him from achieving these goals. And so it's got to stop, for his sake, and for your sake in not being complicit with what's going on.