Skip to content

Instantly share code, notes, and snippets.

@tjjjwxzq
Last active July 1, 2017 15:46
Show Gist options
  • Save tjjjwxzq/0c439790c1cca65dba5d to your computer and use it in GitHub Desktop.
Save tjjjwxzq/0c439790c1cca65dba5d to your computer and use it in GitHub Desktop.
A Novice's Advice to Beginners in Progamming

A Novice's Advice to Beginners in Programming

This is just a compilation of some advice that I might have liked to have had when I started programming, and hopefully you'll find this helpful and encouraging :)

(Though I'm sure some of you have already have a background in programming and are thinking "meh I ain't no beginner I don't need to read this")

Your profs/ TAs are not there to handhold you in learning syntax. There are tons of free resources out there that will help you do so and might in fact be more effective in teaching you the basics (what is probably more challenging is learning how to sift through all of them and not get drawn down rabbit holes).

Using the provided online textbook should be good enough to teach you all the basics, so you should really really go through the relevant chapters before the cohorts and use cohort time to ask questions about things that you are struggling to solve or understand by yourself.

A really good habit and skill to develop is learning how to read the docs. If you don't know what exactly a particular feature of the language does, head to the docs. Sometimes the docs aren't that clear, maybe even confusing to beginners, in which case feel free to ask your profs/TAs (though they may well need to check against the docs themselves), or...

This kinda falls under the previous point, and this is probably a really trite statement by now, but the Internet is your friend (I don't want to be partial to be any search engine in particular, though only one has really entered the popular lexicon; you can use something like duckduckgo if you really want to)

Oftentimes you will be really stuck on something, and your profs and TAs aren't free to help you. Or even better (or worse), they don't even know how help you. Learning to search for and sift through answers on the Interwebz is actually a really key skill and something you will have to hone (which search terms should you use to get the kind of answers you're looking for? How do you persist when it seems you're not getting the kind of answers you need? How do you extract information from people who seem to have similar problems but with slightly different setups?). If you have a question, stack overflow probably has an answer.

And there are times when you will probably get really frustrated in the process, which brings me to...

Yes, programming can get really frustrating really quick, when your program refuses to do what you want it to do. At this stage, it's probably because you don't have a correct mental model of what's going on (whether it's with your program or with the API of some library or framework that you're using). If you've exhausted your Google-fu (and maybe the time of your profs/TAs) you should probably just take a break. But the feeling of frustration will dog you throughout your programming journey, and yes, I deal with it all the time. Especially when you're learning things yourself, oftentimes your profs/TAs won't have quick answers or might not be familiar or even know about the language/framework/technologies that you're trying to learn (if you happen to ask them about stuff outside the curriculum). But don't let your frustration or the lack of easy answers undermine your motivation.

Also kind of a subpoint of learning to learn, but try to grow more and more independent in your problem solving and debugging. Sometimes trying to get at a single bug may take you several hours or a whole day, and you will invariably get really really really frustrated (actually if you have the level of persistence to get to that stage already, you should congratulate yourself). You might be tempted to give in at the first opportunity to the easier option of getting your friends/profs/TAs do your debugging and problem solving for you. Try to train yourself to resist this and perservere, and you will learn a lot of intangibles in the process (such as the very trait of perserverance itself and the skill of debugging), and maybe a few hours and a facepalm later you will taste the sweetness of having independently solved something that originally had you stumped.

Of course, this isn't too say that you should be too proud to ask for help, and stupidly chug away at a problem for hours when you could have actually done it in maybe five minutes if someone more experienced had pointed out to you a certain key piece of information or knowledge which you lacked, which brings me to...

For sure, there are people out there, friends sitting around you, your profs/TAs who at this point can program better than you. Learn from them. By which I mean not just asking questions during cohorts and blankly staring when they help you with debugging your program. You should be actively noting how they are doing things differently from you. They may not always be doing it better, but its likely at this stage that they are. If they are helping you debug your program, ask them to walk you through their thought process. Why did they choose to put the breakpoint or the print statement where they chose to put it? If you are confused, just ask. Beyond cohort sessions, if you want to get better at programming, read code written by others. As a beginner you will be able to learn alot and improve your own code by reading the code of more experienced programmers or experts in the language. Look at the answers on Tutor after you submit yours and think about how your code could have been better. Seek out classmates who you know are good programmers and ask to look at their code.

Some people out there like to think that there are two types of programming students: those who can program and those who fundamentally cannot. I say throw that kind thinking out of the window. There's no denying that there will always be some people who are able to pick up programming quicker than others. There's no denying that there will always be some people who are able to pick up [fill in your skill of choice here] quicker than others.

So, some of you might find this a real challenge, and you'll have to work much harder than some of your friends who seem to be able to breeze through all their problemsets. Or maybe some of you actually are really enjoying programming, but are worried that you'll always be slower than others. Maybe you're thinking "oh but I know I just can't program..."

Firstly, you won't know until you try. And secondly, programming, like any art, is really about practice. Even if you haven't got any innate brilliance, you can always get good by practising. For instance, I don't consider myself to have any particularly high level of innate potential in programming. I'm kind of slow when it comes to thinking about algorithms. Sometimes I don't think I'll ever be any good of a programmer or software developer. But I remind myself that practice and hard work count much more than innate skill in almost anything. So if you find yourself thinking, "I think I'll never get good at xxxxx", maybe you should reassess your mindset. Maybe your friends are blazing ahead only because they've had prior experience and have had more practice than you.

Also, maybe you don't think you'll get very good because you haven't had much prior experience. Well, I and quite a lot of my fellow ISTDians came into this without much background either. All I had was some embarrassing attempt at ActionScript some lovely 5 years ago, which probably didn't count for much. You can't change the past. But you can do stuff now.

So don't let these things get you down. Just keep programming. You will get better!

Fair enough, you're doing this because it's just a course requirement; we all have those kinds of subjects. Nonetheless, if this is something new to you, seize the opportunity to learn as much as you can, even if you don't forsee yourself taking it very much further. Programming will teach you to think differently, and I think it will be quite a refreshing change from all the math/physics/bio/chem that you guys have been doing so far.

And of course, you probably won't be able to escape some sort of programming during your pillar years:

  • ASD: if I'm not wrong there's a computational architecture module, and you guys will have to learn to use C# with Grasshopper
  • ESD: I think you'll need to know software like Matlab, and there might be projects where you need to do some basic Python (I thiiink?)
  • EPD: Arrrrduuuiino (and other microcontrollers, and maybe other stuff I don't know about)
  • ISTD: Our pillar shirt says it all

This famous essay by Peter Norvig nails it, but like I've said programming is a craft that takes practice. I deliberately titled this the way its titled, because I'm still very much aware of the long long long way I have to go as a programmer. You won't get proficient after one introductory course. You won't get proficient without ever having worked on a full scale software development project (which really goes beyond more than just programming). But maybe what you'll get out of this course that you didn't have going in is a true appreciation of how much there is that you don't know, how much there is to learn, and how to go about actually learning it.

Be humble, but have confidence in yourself at the same time. There's a tension there. Deal with it positively.

If you're already pretty good, help your friends. Teaching others will teach you as well. And don't be dismissive of others if they're slower than you. No one likes assholes. No one likes people who put them down. Encourage your friends. I think it's generally better to be a nice guy than to be a smart one.

Yay you actually reached the end of sorts! Even if you just skimmed, or just jumped straight to this section, I hope you guys learn to enjoy programming! Being able to program really empowers you to be able to do all sorts of cool stuff. And did I mention the job market??? Yup, there are plenty of employers who will love you if you can ship code. Singapore needs the kind of people that I hope some of you will become :)

Okay, you've got access to your course materials, but you want to go further. Here are some resources you can dig into.

TBH with the level of programming that you guys will be doing for this course, I really don't think you need the amount of bloat that comes with installing the whole Canopy distribution (what with its IDE and the 100+ extra libraries that you are never going to use). So I'm actually all for you totally ditching Canopy and learning to roll from your commandline (but don't quote me on that to your profs). You'll have to learn to use a non-graphical shell someday (hint hint aspiring ISTDians). Might as well start early.

####1. Install Python If you're on windows, simply download the standard Python installer for the appropriate version (2.7.11, but you can play around with 3x if you're feeling adventurous. Just be sure to call the correct executable and be aware of which version the default python command symlinks to. But thankfully python on windows now ships with a launcher that makes things easy) In the installation setup, select the option to install with the default libraries (in particular, Tk/Tcl and pip should be installed)

If you're on *nix(ie. Mac OS or Linux), python often comes shipped with your OS. If not you can install python with your package manager. On Mac OS using Homebrew (and if you haven't got homebrew you should get it; package managers make installing stuff that much easier) open your commandline (Terminal) and run:

brew install python

That's it. With that command Homebrew will install pip and setuptools for you as well (again if you choose to try out python 3, be aware of which python and pip versions the default executables are calling)

If your on a Linux distro, I presume that you are already familiar with your package manager. You should be able to sudo apt-get install or sudo yum install or sudo pacman -S python2x or python3x depending on distro. You might have to install pip separately as well.

####2. Wait so what exactly is pip and setuptools?? I'm glad you asked. pip is python's package manager, meaning it helps you install, update, and keep track of all your python packages. What exactly is a package? It's basically a set of python modules (ie. files of python code) that other smart people have written, all bundled together nicely for you to use to make your life easier. For example you'll be learning to use Kivy, a GUI programming library which you can install as a python package. Libraries like Kivy provide a nice application programming interface (API) for you to use so you don't have to worry about low-level stuff like spawning windows in your desktop environment (in a word, abstraction). For more see here

####3. How do I run python from the commandline? Fire up your command line interface (CLI) program, whether it's the Command Prompt (cmd.exe) on Windows, or (most likely) Bash on *nix. You should see a prompt, waiting for you to type in something nice and shiny.

python

will do the trick. Press enter to tell the shell to run your command, and you should have entered the python interactive shell! (So now you're running a shell inside a shell; ain't that fancy?) It's actually the same python interactive shell that runs in your Canopy IDE. (If you're on Windows and you're getting an error when you try to run that command, read on and you'll understand why)

on command prompt
The python interactive shell running on my Windows Command Prompt on bash in a terminal emulator
The python interactive shell running on Bash in GNOME Terminal on my Arch Linux box

You can do all the stuff here that you do in the Canopy interactive shell. And if you want to exit the shell, you can type quit() or just Ctrl-C (in the Windows command prompt) or Ctrl-D (in a *nix shell).

Great! You can run python from the commandline. But what was really happening when you typed python?

Installing python should have put a python executable somewhere in your filesystem. On Windows with a system wide install, its typically something like C:\PythonX\python.exe, X being the version number (eg. 27 for Python2.7x) On *nix, you'll probably find it as /usr/bin/python

When you tell your shell (cmd or Bash) to execute a program like python, it has to know where to find the python executable (your python.exe in Windows; *nix system executables don't have file extensions, so its just plain old python). But if you just typed python you didn't specify the full path. How does your shell know which executable you're referring to? It boils down to your PATH environment variable: this tells your shell where executable files are located in your filesystem. If the path to your executable isn't stored in your PATH variable, your shell will complain (as you might have experienced if you tried running python on the Windows command prompt)

cmd complains
Command Prompt complains that it can't find my python executable

So how do I know which paths I have specified in my PATH? In the Windows Command Prompt you can run echo %PATH% to see what your PATH is set to:

windows PATH

On *nix do echo $PATH

nix PATH

Note that in Windows, the paths stored in PATH are separated by a semicolon ; while on *nix they're separated by a colon :.

What you'll notice if you bothered to scrutinize my Windows PATH (if you didn't I forgive you) is that I don't have the path C:\PythonX\ in my PATH variable, which is why Command Prompt complains about there not being a python executable. On *nix the /usr/bin directory stores all the systemwide executables and is included in PATH by default, so no problems there.

To rectify the missing path on Window, run this in your command prompt:

SET PATH=%PATH%;C:\PythonX

If you want a way to do it through the GUI, see here

And you should be able to run python fine now!

####4. No IDE?? But now where am I going to write my python scripts? Now that you've taken your first steps towards freeing yourself from Canopy and a bloated hard disk, a trusty text editor will be indispensable.

Sublime text is a popular choice, or you can try TextMate if you're on Mac OS. Atom is another nice one, or you could go the route of Vim or Emacs is you're so inclined.

vim vim >> [insert your not-so-favourite text editor here]

Text editors typically come built in with syntax highlighting for popular languages (such as python), or you can install them as plugins for those languages that don't come out of the box. There will also be a few default themes you can choose from, or you can install a theme you really like as a plugin. Now you will be able to show off your more prettily syntax highlighted code to all your friends who are still stuck with Canopy and make them jealous. Surely that's enough of a motivation?

pretty syntax highlighting
A python script in vim with syntax highlighting in the molokai theme. Sooo preeettty

####5. How do I install python packages? I think one reason they want you to use Canopy is because it comes preinstalled with some of the libraries that you'll need (and did I mention the 100+ others that you will NEVER NEED???) But who needs that when you've got pip!

pip as I've mentioned before is a package manager for python. Most of the packages you can find on PyPI, the Python Package Index can be installed using pip. Installation is as simple as a pip install *packagename*

So for instance, if you need to install numpy (which you will probably need to use the eBot API), simply run

pip install numpy

That's it! To see some of the other stuff you can do with pip, just run pip and some documentation should come out. For example, you may want to list the packages you have installed:

pip list

####6. So where are my python packages installed? On Windows it will probably in a directory something like C:\PythonX\Lib\site-packages. On *nix you'll find it in /usr/lib/pythonX/site-packages. Again X stands for your version number.

here are my python packages
Python packages under /usr/lib/python2.7/site-packages on my Linux box

####7. How do I prevent dependency hell? virtualenv is your friend. Google-fu your way to it.

At this stage the debugger is probably not so much useful for actually debugging but more as tool for you guys to understand the flow of program execution. Running your program in debugging mode allows you to set breakpoints and step through your program line by line, or to the next statement to be executed (those two are different things, which you will realize when you learn flow control and functions).

On Canopy you can start the debugger by clicking on the little bug icon at the top. Next to it you'll see some arrows which basically allow you to step through your program (hover your cursor to find out which exactly does what). That's pretty much all there is to it. There's something in the online textbook called Codelens which helps you do pretty much the same thing. I encourage you to play around with it a bit and try to understand the way your program is being executed.

I've also noticed that some you have your debugging capabilities disabled in Canopy because you haven't registered your Enthought account, or something. So you should really go register your account, or just ditch Canopy (it's practically screaming for you to do so, ain't it)

But wait! If I ditch canopy and do everything from the commandline, can I still use a debugger? Yes! Python comes with a nifty little built-in module called pdb which is an interactive source code debugger. Using it is as simple as importing it into your script with import pdb and placing a pdb.set_trace() at wherever you want a break point. Then you can step through your programs using commands at the prompt, such as n (for stepping to the next line) or c (for continuing with running the program as normal). See here for more.

Use the debugger to help you learn, but not as a crutch. Once you get comfortable thinking like a programmer, you should be capable to follow the flow of your program in your mind, and for the types of things you'll be programming for this course, you'll rarely need a debugger to actually debug stuff. A few well-placed print statements, and your own logical thinking, is often enough to do the trick.

Good for you! The CLI might seem pretty intimidating or even primitive at first, but it's a really powerful tool once you know how to harness it (if I speak as if I have already been able to harness it, let me not kid you or myself). At this stage it's probably enough to know how to move about your filesystem and run simple commands like mkdir, ls, man etc etc. If you're serious about getting into programming (and going to ISTD) I recommend you start learning some basics. Here's a great crash course for both Windows and *nix systems.

If you're on Windows, you might want to consider changing to a *nix environment, because UNIX shells just come built-in with a ton more useful utilities (and you don't have to do some weird hack to enable straightforward Ctrl-Shift-C and Ctrl-Shift-V copying and pasting like you have to on the Windows Command Prompt). If you really want to stick with Windows and want to hack around more with your CLI, you can look into PowerShell. But really, unless you are a true blue Windows aficionado and are certain of wanting to work with the .NET framework, may I suggest giving a UNIX system a spin? It just feels so much easier and lots of software jobs require you to be familiar with a *nix environment anyway.

But how are you going to get a UNIX environment on Windows?? Enter Cygwin which is a UNIX-like environment and CLI for windows. Or you could consider setting up a Linux virtual machine using something like Virtualbox. If you've already gotten this far, I presume you're interested enough to be able to figure this out yourself with your Google-fu, so all the best trying out Cygwin/Linux!

side note: last year for Systems World, they actually made us install a Linux virtual machine - Linux Lite to be precise - to do linear programming in a spreadsheet program. Why exactly we had to go out of our way to install a full blown VM just to use a spreadsheet program to do stuff that Excel and Numbers could probably have done just as well, is beyond me, but I suspect one of the profs was a true Linux enthusiast.

In any case, that was one of the few times I actually welcomed people telling me to install 'this whole other software thing that you really need for your course', and I now live in my Arch Linux box on my Windows host most of the time.

THE POINT BEING: if they're going to make you do it for Math, anyway, might as well do it now, right?

cygwin and cmd Cygwin and Command Prompt. Cygwin obviously

And if simple stuff like cd and mkdir are, well, too simple for you, you can try your hand at bash scripting! (ahh I'll get there someday)

If the problem sets are really not enough for you, here are some resources you can look to:

  • Python Quiz: test your knowledge of python syntax and concepts!
  • PythonKoans: a port of the famous Ruby Koans - will teach you a thing or two about idiomatic python, some cool features of the language (and invariably general programming concepts along the way), and also introduces you to the idea of unit testing. Definitely worth a shot if you have the time. The GitHub repo should be more than enough to get you started.
  • Learn Python the Hard Way: another interactive textbook, if you really want. Or you can just try out the exercises for practice

Maybe you're not too impressed with the kind of stuff you're coding at this moment. You thought programming was supposed to be way cooler than this, like, allowing you to build games and apps and stuff. Look no further:

There's something Python programmers call idiomatic python, which you could roughly translate as python code that reads well. Just like how the phrase "you sit la" isn't idiomatic in standard English (but it is idiomatic Singlish) some code written in python will read better, will be more idiomatic than others. This is territory I'm not too familiar with either, since I haven't even been programming in Python (or programming for that matter) for that long to say that I'm able to write idiomatic Python code, but I'm learning, and I'll just toss a few resources out for you guys to get a feel for what this means:

The concept of writing idiomatic code doesn't lie entirely within the domain of just the python language. Each language and its community has its own set of idioms, style guides, whether written or unwritten, and conventions that one should learn if one hopes attain any degree of fluency. Have fun digging around.

Me too! Maybe you'll find some of this stuff helpful:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment