Assorted tips on Python (and programming in general)
Stephen D. Boyles, Avinash Unnikrishnan
Many students new to programming find it challenging. This document is
designed to complement our
Python tutorial
by addressing some of these challenges. The tutorial explains
the "nuts and bolts" of Python; here we provide an assortment of tips and
advice.
I can't get Python to install or run on my machine.
Every operating system and configuration is slightly different. If you are
unable to get Python installed on your machine, or are unable to get code to
run, there are several websites which provide interactive Python environments
through your browser.
One such environment is here.
You can type code directly in your browser (in the middle window), then click
on the green "run" button to execute the code. Output appears in the window to
the right.
You can save projects, and upload files using the window pane on the left.
For assignments involving multiple python and test files, you can upload them
all. However, this website will only run the file named
main.py . So, if you need to run another file (such as
grader.py ), copy that into main.py before pressing
Run.
It is preferable to have Python running on your own machine, but as a
fallback you can write and run code through your browser.
I need more information than what is in the tutorial. I've gone through
it, but I don't know how to get started writing my own code.
There are other, more comprehensive tutorials (some of which are linked to
in our tutorial). But all a tutorial can do is tell you what the components of
the language do, they cannot tell you how to design and write a program.
It is like learning to use a tool in a machine shop. Instruction manuals
and tutorials will show you how to use a drill press, say, or a table saw.
They will not tell you how to build something from scratch -- that knowledge
really only comes from experience, trial and error, and seeking out answers to
specific questions once you get into design and building.
Here are some tips to gain that experience in writing code, as quickly as
possible:
- Try to write code as often as you can, even for things that may not
necessarily require it. In your research, in assignments for other
classes, in your personal life (e.g. can you write a simple program to
calculate tips or split expenses among roommates?), and so on.
- Read other people's code, and take the time to understand how it is
structured and how it works. If you don't know what something is doing,
look it up in a tutorial or reference, or ask someone more experienced.
- Don't be afraid to try things out. If you aren't sure if
you can use a
list or dict in a certain way, or
if a particular loop statement will do what you want, give it a try. The
worst that happens is that it won't work (and perhaps some debugging can
tell you why, and help you figure out why.
- You can also experiment using small programs separate from what you're
actually trying to write. It's often easier to test things in simpler
environments.
- It's a good idea to do this experimenting on copies of your code,
so that you can easily revert things if they don't work out.
- Google and other search engines are your friend, especially when you have
specific questions. Python is a very commonly-used language today, and
you are almost certainly not the first person to have whatever question you
are thinking of. Asking good search queries (precise, with the right
keywords) will often lead you directly to your answer.
- Develop some skill with debugging. See more below on this.
- When you learn a new feature of the language, ask yourself: why is this
useful? In what circumstances would I want to do this? Is this just a
shorter way of doing something I could do with other language features, or
is this something completely new?
You should not feel ashamed to do any of these things. This is how everyone
learns.
OK, that's good general advice. But I need to write some code right away
for an assignment due tomorrow.
Fair enough. This advice assumes that you are familiar with the syntax and
common data structures of the language, and the more you've done the above
steps, the easier this will be...
- First decide exactly what your code needs to do. You don't have to be at
the computer to do this, you can do this in plain English (or mixed
English/math) on a piece of paper if you want. If you are implementing an
algorithm, look for pseudocode from your instructor or textbook. If it is
a new method, think carefully about what steps need to be followed and what
variables you will need to refer to. Write it out clearly and precisely.
- Now, map those steps onto Python features. What kind of loops will you use
to implement those steps? Here is where familiarity with the language
helps (not just what the syntax does, but when it is useful). For
instance, if you need to loop a definite number of times, a
for loop is the logical choice, whereas if you don't know how
many times you need to loop (you need to loop until something happens), try
a while loop. What kind of data structures will you need to
represent your variables? Which things can use simple variables, which
things might be better suited to a list, dict, etc.
You still don't have to be at the computer to do this.
- Now you can begin writing code. Take your description from the first step,
and your "translation table" from the second step, and write the code. In
doing this, you may realize that you need to change your design or data
structures. That's fine.
- Now get your code running. The first step is that it runs without errors
or crashes; fix any mistakes needed until this is done. (If you don't know
what an error message means, google it.)
- Now your code runs, but you still need to check if it is running correctly.
Give it some test inputs (where you already know what the answer should
be), and check to see that it is giving the right answer. It's good to
check corner cases too.
- At some point, you have to stop testing (it's not really possible to prove
that your code is completely bug-free). But most people stop too soon,
rather than too late. Rather than being satisfied with working correctly
on a few inputs, try to be more rigorous with your testing. Do the results
change in logical ways when you vary the inputs? Does your code correctly
handle strange inputs? Have you checked all of the logical paths in the
code (both cases for each
if statement)?
My code isn't giving the right answer, and I don't know why.
Learning to efficiently debug your code is extremely important. Debugging
involves several processes: (1) discovering that your code is not doing what it
should; (2) identifying where the problem lies; and (3) fixing it. A bug at one
point in the code may trigger several other errors later on, so the first thing
that appears wrong may not actually be the problem.
There are special software programs called debuggers, which have tools to
help with this (letting you step through your code one line at a time, examine
the values of all the variables, set stopping points to suspend your code so
you can check things, and so forth). If you will be doing serious software
development, it is worth learning to use a debugger. For small projects or
assignments, you can often debug by using print statements to
output the values of variables or data structures as your code runs.
Start with an instance where you know what the answer should be, and determine
what the values of the variables should be at each step of the algorithm (you
can do this on paper or in a spreadsheet, for instance.) Compare the output
from your print statements to what the answers should be, and see
where they are going wrong.
It is better to err on the side of too much output, rather than too little
(you can always "comment out" these statements if they are too much). Start at
a point where you are sure you code is giving correct values (the start of the
program, if need be), and try to pinpoint exactly where the first wrong value
is occurring over several executions of the program. (If the error is
happening between two subsequent print s, but you aren't sure
where, add something in between and run again.)
Once you see where the problem is, you have to fix it somehow. Some bugs
are relatively easy to fix once found (typos, not properly updating a
variable), and others may require deeper changes to the program structure or
logic.
In time, you will become more efficient at interpreting error messages and
unexpected output, determining where the problem lies, and learning to fix
them.
Back to teaching or the
main page.
|