How to Learn a New Programming Language
As I said in the last two posts, Google I/O was truly inspiring. It got me to thinking about how I program, what languages I chose to do it in, and what I can do to become a better software developer. Two things I will be doing are getting more proficient with Java (especially for my Master’s Thesis) and learning Python. I’ve begun going through and learning some of the syntax and nuances of Python and found I follow a pretty similar (and effective) pattern for each language I learn. (It’s worth noting that in the following, I assume the reader has at the very least read about programming and has some language to learn in mind.)
First, pick up a book. I usually go with the for the Absolute Beginner series. It taught me PHP/MySQL and did it in a very effective, modular way. The authors don’t assume you know how to program, but the books aren’t so slow that if you have, you’re bored. They also provide the tools you need to set up your environment, resources, and all of the book’s sample code on a CD. Each chapter takes you through 2 or 3 smaller programs, and builds a full one at the end. In my opinion, they have the perfect combo of code and text.
Then, build one program. Just one- and continue to expand it as you learn new syntax. I build a “Guess the number” game. It’s a simple problem with an easy solution that you can expand on. My plan of action for this program goes like this, following along with the book I’m using.
- Basic print statement. Something like
"I'm thinking of a number..." - Variable assignment. Hard code the first number you want the user to guess. That way you know the right number and can check your logic for the next step.
x= 3. - Get input from the user/store it in a variable.
guess= get_input("What's your guess"). - If statements.
If guess == x: print something, else: print something else. - While loop. Do something like prompt to user to see if they want to guess again, accepting yes or no.
while keepGoing != no: doing it again
These five basic steps show you the important basics- input, output, and flow of control. The next few steps could be language dependent, but you can also take some liberties and get creative.
- Change
xto a random value. If the user wants to guess again, the number will now change.x= rand(0, 10). - Allow the user to enter five guesses at a time. This will do a couple of things for you- make use of an array, and use a for loop.
while i <= 5:
guesses[i]= get_input("What's one of your guesses?")
i= i+1
for guess as guesses:
if guess == x: print something,
else: print something else
- Write a function/method to check if the guess is correct. It might be a trivial thing to do here, but you're really just doing it to learn the syntax.
- Create a 'Guesser' class. This of course assumes you're using an object oriented language. Write one class with functions to prompt the user, store the input and make the guess, etc.
- Finally, try storing the results in a file. Keep a counter for how many times the user, plays, and for each time, store each guess, the actual answer, and if they got it right or not.
With these steps, you're learning a new control structure, how to use arrays, write to files, and most importantly how to modularize your code through functions and classes. I think with that, you will know enough basic stuff about the language to go off and write some other less trivial programs.
After that, it's really up to you to dig deeper into the particular language you are learning to really make use of its power. The Absolute Beginner books take you beyond the above exercises and do some language specific things. With PHP, it was creating a web app and connecting to a MySQL database. With Python, it looks like you build a basic game. With Java, you should probably explore Generics or threads. Either way, once you learn the basic syntax, it's time to harness the true power of the language.
My last piece of advice is to pick up the O'Reilly cookbook for that particular language. They're usually for a more advanced user of the language, but are extremely helpful in doing specific things. The PHP and Java Cookbooks have helped me immensely.
