Copyright Anne Dawson 2005 - 2025
This file is: https://www.annedawson.net/PythonHints.html
Last updated: Monday 18th November 2024, 9:40 PT
-------------------------------
To learn how to program in any language
you must practice, practice, practice.
You must learn gradually but thoroughly.
If you're struggling to understand
one part of the language,
ask your instructor for an explanation,
either in class, in the office hour,
or by email. anne.dawson@gmail.com
Use any resource you can
to get information.
Python Useful Links
Understand how the different
numbering systems work -
decimal, binary, hexadecimal.
Understand binary logic:
AND, OR, NOT.
Know how to use
the Python development environment (IDLE).
Make use of the
Python's online help.
Find a book
which really explains
programming to you -
if it's a hard-copy book,
make useful notes in it.
Don't be afraid to ask questions.
There are no stupid questions.
Plan your program on paper first
(the algorithm).
If your program involves
calculations, break down
a long calculation into a
sequence of simple steps.
Test your sequence of calculations
on paper first. Write down test values
and calculate test results.
You can use this same test data
when you test your running program.
Use a "Top Down" approach -
break down what you're trying to do
into little simple parts.
Plan to implement each little part
as a mini program or function.
functions are a
feature of the Python language
covered in all programming courses.
Give the function a name
which reflects what the function
is going to do.
Write down your plan on paper.
Use any method you like which
helps you to picture what
your program should be doing.
Some people use flowcharts -
some people use pseudocode
you MUST use a method to
describe your program on paper.
Make sure the program works
on paper before you type it in
- a program which works on paper
will work on the computer.
Make sure you understand how
spaces are used in a Python code.
Put comments in your code.
You should be able to read
a program like a book.
It should tell its own story.
The first page of the code
should include:
the file name with version number
( eg Calc01.py ),
your name,
the date the code was started,
date completed (if complete),
the current status -
(COMPLETED or INCOMPLETE),
a brief description of the
purpose of the code,
a brief description of what
was in each previous version,
and any other pertinent details
- interpreter version (Python 4.3.2),
OS platform (Ubuntu Linux) etc.
Use lower case for
variable names eg "salary",
and uppercase for constants
such as "TAXRATE"
- that way, you can see immediately
which values may change,
and which may not.
Experiment
- you don't learn how to program
by reading books alone
- you must be prepared
to make lots of mistakes
- and to learn from them.
Keep your code simple
- a program that looks confusing
IS confusing.
Keep it simple
- a simple program
is more likely to work
and continue to work
because it is easy to understand,
hence easy to change
and easy to debug.
Initialize your variables.
This means giving a variable
a starting value, e.g.
count = 0
Use meaningful variable names
- "lunar_miles_per_second"
is much clearer than "lumps".
Always use names which
accurately reflect the content
- like "dollars_per_hour".
Use the appropriate data type
- if you are counting people,
use the integer type.
People don't come as fractions.
Use the appropriate control structure
for the job (selection, repetition).
Know when should you use a for loop
as opposed to a while loop.
If you don't know - find out.
Do not have multiple copies
of the same piece of code
- if this happens,
convert them into a single function.
Make use of "function stubs"
when you're developing a program
- a function stub is a function
with just the bare bones inside it
- usually just a print statement
to report that the function
has been called successfully.
Put a comment line of #'s
at the start and end of every function
to show where one function ends
and the next starts:
##############################
Do not have functions
larger than one screen full
- functions longer than
a screen full are hard to follow.
Break large functions down
into smaller ones.
A function should ideally
perform just one task.
Don't write the next line of code
until you fully understand
the current line of code
- if you don't understand it,
don't use it!
Develop your program gradually.
Using function stubs allows
you to develop your code
with all the functions in place.
The stubs are then completed
one by one, with full testing
before starting to code
the next function.
This gradual development
of a program leads to
fewer errors, and generally
shorter development times.
DEVELOP YOUR CODE IN STAGES.
Always have a working version
to fall back on.
Copy prog01.py to prog02.py,
then add more functionality
(well commented) to prog02.py
a few lines at a time -
save the code and test every few lines
- if you do get an error,
you know it is the last few lines
entered where the problem lies.
Run and test your program
after every time you have added
another few lines of code.
That way, any errors can be traced
to those last few lines added.
Save, save, save!!!!
Save your code at regular intervals.
You never know when there will be
a power outage. Make duplicate copies
of your code just in case.
Use a print statement to print out
the value of a variable that
you're not sure about.
Don't borrow code from another programmer
- you may inherit their bugs.
Don't borrow code, borrow ideas!
Find someone who is struggling on
the same problem - work together
- a problem shared is a problem halved.
Don't be afraid to help others
- explaining to others
makes things clearer to yourself.
Test your program in as
many different ways you can think of.
After you've tested your code,
get somebody else to test it for you.