Python is often recommended as a first programming language to beginners because of how fast and easy it is to write and execute code. Simplicity is literally built into it, and it isn’t uncommon to be able to write a 10-15 line function from other languages in 5 lines or less with Python. In addition to this Python comes with a basic integrated development environment called IDLE. This is what I would like to special attention to, specifically IDLE’s interactive shell, where you can type in variable assignments, statements, function definitions, and so on. The shell will execute the code immediately and return an object or value if there is one, or give you red error text if the code has syntax errors. Here’s a video a little over 1 minute in length that shows some ways you can use the interactive shell:
As you can see you get immediate feedback, which is great! Learning is strengthened by the immediacy of feedback. I find myself writing the bulk of my code in Notepad++, then running the .py file I’m working on from the command line simply by typing:
Did the code compile? Or was there a syntax error? It will tell you the line where the code failed if so. Go to that line and try to guess what the issue is. Copy the line(s) or function, paste it into the shell, and start hacking away at it from there – testing your hypothesis with each modification of your code. Common errors, such as type errors, can be quickly diagnosed – “String literal cannot be implicitly casted to int” – or something that sounds like that, tells me that I need to fix how I’m using variables together and probably need to make them the same type. Less common errors that happen as your code becomes more complex may need to be scrutinized using a debugger, but that is an entire topic in and of itself.
I found this to be a very helpful method for writing my own code with only myself as my teacher. Of course there are tons of free online resources to help you learn, the Google Python class¹ being one that many people recommend. This is actually how I got into the habit of learning Python quickly through using the interpreter. Here is the video lecture, done by Nick Parlante (also a Stanford lecturer), that introduces the interpreter and how to use it a little more in depth:
Note: He is working on a Mac, while my examples and guides have been on Windows. It should be more or less the same after everything is installed. In the video, Nick is executing Python scripts in a bash shell² (Linux) by typing either:
- python exampleScript.py
To work with the interpreter on Linux or Mac, type “python” into the terminal. “>>>” should appear in the command prompt. You can now use the terminal the same way I use IDLE in the first embedded video.
Here is a really useful table³ for understanding the different aspects of several scripting languages. The columns are the different languages, and the rows are different concepts (from basic to more complex). For example, the second section covers what I just explained above, e.g., “python foo.py”. You should be able to paste the example code from the third section and on to see how it works, like so:
Taking classes in person, or watching quality video lectures online like this will help you get a firm understanding of basic (and universal) programming principles, as well as reinforce good programming habits. If you are just starting out, you will hear lots of terminology that will fly right over your head. And that’s expected, just don’t get hung up on understanding every word your teacher uses, that will come with time. The important thing is to start writing code that executes without any issues (syntactically correct) and seeing what it outputs. If your code didn’t work, type in letter for letter the example code from the instructor or book and compare the differences. Getting the output immediately through the interpreter/interactive shell is the best way to do just that. This way, you will be better equipped to make it past the steep learning curve that is typical of learning a first programming language.