NOTE: This web page was last modified on Tue May 31 06:00:27 2016 UTC.
Grading is done and has been entered into reg.buu.ac.th.
2016/05/08@10:00 ICT matplotlib output images added.
2016/05/09@15:15 ICT New rule prohibiting even watches and announcing eye glasses inspection for final exam added.
2016/05/13@13:51 ICT Improve demo007b.py, add demo007c.py file.
2016/05/14@14:31 ICT Add demo007d.py file.
2016/05/14@14:50 ICT Add demo005b.py file.
2016/05/14@19:56 ICT Add demo005c.py and demo005d.py files.
2016/05/27@18:05 ICT Add exam3 and its solutions.
2016/05/30@15:04 ICT Actually, five people skipped exam #3....
2016/05/30@15:41 ICT Add final exam.
2016/05/30@15:54 ICT Add final exam answers.
2016/05/31@13:00 ICT Add note that grades have been submitted.
Mr. Ham's Python 3.5 demos for BUU
The files you want are below. Since SourceForge changed ownership
recently, you might want to download everything in case this site
Always check the 'Last modified' date to see if you really have the
latest version or not. When I make fixes, I re-upload them and the
'Last modified' date will automatically be updated to show that the
file has been changed.
If you don't have it already, you might want the officially allowed
Cheat Sheet, but remember it must be one sheet of paper, prineted
on two sides, in color. Black and white, or two page versions will
not be permitted. You didn't have to buy any textbook - surely you
can go to a decent print shop that has a color laserjet printer and
print one piece of paper with two sides?
All files are text files and use UNIX®-style line endings.
If you want 0x0D 0x0A line endings for your Microsoft® operating
system then you need to do the conversion yourself.
Exam 1 files
There were two slightly different exams since the test was spread
over two different days. You should be able to recognize the one you
took. The solutions are not unique, since many lines in the source code
were duplicated. The robot grader uses your answers to unscramble the
original file, and then your unscrambled version is compared with the
original file to determinte your score. Any time you left a slot blank,
that is an incorrect answer.
- The exam1_one.pdf exam.
Last modified: Mon Feb 22 12:38:03 2016 UTC
- The unscrambled file exam1_one.original
used to create the one.pdf exam.
Last modified: Mon Feb 22 12:38:22 2016 UTC
- One exam1_one.solved.pdf exam solution.
Last modified: Mon Feb 22 12:37:48 2016 UTC
- The exam1_two.pdf exam.
Last modified: Mon Feb 22 12:38:08 2016 UTC
- The unscrambled file exam1_two.original
used to create the two.pdf exam.
Last modified: Mon Feb 22 12:38:34 2016 UTC
- One exam1_two.solved.pdf exam solution.
Last modified: Mon Feb 22 12:37:54 2016 UTC
Tech demo files from 2016/01/14
NOTE: You may have to force your browser to use UTF-8 encoding to see the non-English strings
- The utf8example.py simple utf8 output module.
Last modified: Wed Mar 2 04:23:18 2016 UTC
- The example2.py program that imports the entire simple utf8 output module.
Last modified: Wed Mar 2 04:23:22 2016 UTC
- The example3.py program that imports one function from the simple utf8 output module.
Last modified: Wed Mar 2 04:23:27 2016 UTC
- The readfile.py simple program to read a text file and print it on the screen.
Last modified: Wed Mar 2 04:16:31 2016 UTC
- The feet2meters.py simple GUI program to convert feet to meters.
Last modified: Wed Mar 2 04:34:10 2016 UTC
- The hellobuttons.py simple GUI program with buttons.
Last modified: Wed Mar 2 04:34:02 2016 UTC
Tech demo files from week of 2016/01/25
- The inv.dat menu module.
Last modified: Fri Jan 29 10:48:27 2016 UTC
- The inv2.dat menu module.
Last modified: Fri Jan 29 10:48:30 2016 UTC
- The readit.py menu module.
Last modified: Fri Jan 29 10:49:19 2016 UTC
- The readit2.py menu module.
Last modified: Fri Jan 29 10:49:22 2016 UTC
- The readit3.py menu module.
Last modified: Fri Jan 29 10:49:25 2016 UTC
The inventory application from week of 2016/01/25
- The product_master.dat menu module.
Last modified: Fri Jan 29 10:47:02 2016 UTC
- The current_inventory.dat menu module.
Last modified: Fri Jan 29 10:54:03 2016 UTC
- The mymenu.py menu module.
Last modified: Fri Feb 5 01:07:40 2016 UTC
Fixed self-test (oops!)
- The inventory.py menu module.
Last modified: Sat Jan 30 06:06:42 2016 UTC
OK, I finished the cleanups, added the dosctrings, etc.
Files from the lectures of 2016/02/01
- The csv.pdf documentation for CSV module.
Last modified: Mon Feb 1 04:36:35 2016 UTC
- The readit._csv.py menu module.
Last modified: Mon Feb 1 04:34:48 2016 UTC
- The readit2_csv.py menu module.
Last modified: Fri Feb 5 01:11:39 2016 UTC
Add docstring, fix function name (oops!)
- The readit3_csv.py menu module.
Last modified: Fri Feb 5 01:11:54 2016 UTC
- The tkinter.pdf documentation for tkinter module.
Last modified: Mon Feb 1 04:37:07 2016 UTC
Note that this is reference documentation, not a tutorial.
- The official tkinter
module docuemntation does not have many examples.
- The hello_world_gui.py menu module.
Last modified: Mon Feb 1 04:42:12 2016 UTC
- You probably want to look at a
tutorial that includes instructions
for python3 with tkinter. The tutorial also includes information for tcl, perl,
and ruby, but you can ignore those parts (they are clearly marked).
- For general help on python3, if you are patient this
python course has a lot
of examples and explanations.
Files from the lecture of 2016/02/04
- The oodemo.py menu module OOP version.
Last modified: Thu Feb 4 06:28:35 2016 UTC
- The mymenu.py menu module OOP version.
Last modified: Thu Feb 4 06:37:29 2016 UTC
- The inventory.py inventory module OOP version.
Last modified: Thu Feb 4 06:22:38 2016 UTC
- The demo_inv_app.py main program OOP version.
Last modified: Thu Feb 4 06:22:34 2016 UTC
Files from the lecture of 2016/02/05
Since I did the procedural to OOP conversion live in both lectures, the
results are slightly different. Wise students will examine both the
2016/02/04 and 2016/02/05 solutions.
- The mymenu.py menu module OOP version.
Last modified: Sat Feb 6 06:43:22 2016 UTC
- The inventory.py inventory module OOP version.
Last modified: Sat Feb 6 06:43:19 2016 UTC
- The invapp.py main program OOP version.
Last modified: Sat Feb 6 06:43:15 2016 UTC
Files from the start of lecture of 2016/02/08
- If you really cannot remember any SQL,
you should work through the excellent and free
Learn SQL The Hard Way book.
- The sqlite3_demo.py sqlite3 demo.
Last modified: Mon Feb 8 05:36:51 2016 UTC
Updated to handle file delete rules on Windows®.
Updated again to add check constraints, activate referential integrity, add timestamp
and required PRAGMA statements to generated backup SQL script, add function to
retrieve sqlite3 version.
- The myio.py menu module OOP version.
Last modified: Sun Feb 7 20:08:56 2016 UTC
- The inventory.py inventory module OOP version.
Last modified: Sun Feb 7 20:08:59 2016 UTC
- The invapp.py main program OOP version.
Last modified: Sun Feb 7 20:09:03 2016 UTC
Files from the end of lecture of 2016/02/08
- The inventory.py inventory module OOP/SQlite version.
Last modified: Mon Feb 8 12:30:35 2016 UTC
- The invapp.py main program updated for OOP/SQlite version.
Last modified: Mon Feb 8 12:30:34 2016 UTC
- The fibo.py answer to 13:00 class quiz.
Last modified: Mon Feb 8 12:30:33 2016 UTC
- The mra.py answer to 15:00 class quiz.
Last modified: Mon Feb 8 12:45:10 2016 UTC
Files from the lecture of 2016/02/11
- The boom.py custom exception and paramter passing demo.
Last modified: Thu Feb 11 02:53:27 2016 UTC
- The inventory.py version with quantity update.
Last modified: Fri Feb 12 08:57:25 2016 UTC
Add optimization for zero delta value.
- The invapp.py main program version with quantity update.
Last modified: Fri Feb 12 07:09:09 2016 UTC
- The myio.py version with support for quantity update.
Last modified: Fri Feb 12 07:09:18 2016 UTC
File from 2016/02/12
- The inventory.py much improved version using enum
to support constant values for the masks, better error handling, a move of the format
strings to class-wide variables, and a fix for the report for the case when the database
exists with the correct schema but the inventory table is empty. Docstrings have been
improved too. It also verfies the update worked by checking that the number of
modified rows is exactly one.
Last modified: Fri Feb 12 11:23:24 2016 UTC
- The quiz.py quiz solution.
Last modified: Fri Feb 12 11:34:26 2016 UTC
Files from 2016/02/15
- The inventory.py rough version from end of 1st lecture.
It does support price update and item delete, but it isn't pretty.
Last modified: Tue Feb 16 09:39:09 2016 UTC
- The inventory.py better version with improved docstrings.
Last modified: Tue Feb 16 12:54:10 2016 UTC
- The quiz.py quiz solution updated with
a few more of the many ways to do it.
Last modified: Tue Feb 16 13:08:20 2016 UTC
Attack of the psuedo-psycho zombie GUI mutants.....
Well, OK, not quite, but here is a GUI demo for you to study. I have not tested it
on Mac or Windows yet - have you?
- The gmenu.py proof-of-concept procedure GUI menu demo
Last modified: Wed Feb 17 13:59:55 2016 UTC
Files from 2016/02/18 & 2016/02/19
- The inventory.py full-featured version.
Last modified: Fri Feb 19 07:52:57 2016 UTC
- The invapp.py full-featured version.
Last modified: Fri Feb 19 07:52:54 2016 UTC
- The myio.py Updated to support 2 digit choices.
Last modified: Fri Feb 19 07:52:51 2016 UTC
File from 2016/03/14
- The lamtan.py My silly quiz solution
Last modified: Mon Mar 14 12:08:19 2016 UTC
Files from 2016/03/21
- The feet2meters.py Corrected feet to meters conversion program.
Last modified: Tue Mar 22 11:57:48 2016 UTC
- The thb2euro.py Convert Thai Baht to Euros.
Last modified: Tue Mar 22 11:57:34 2016 UTC
- The thb2euro2.py Hideous color version of convert Thai Baht to Euros.
Be sure to take note that this version only uses tk, not ttk.
Last modified: Tue Mar 22 11:57:38 2016 UTC
Files from 2016/03/22
- The app.py Simple text viewer
Last modified: Tue Mar 22 11:51:53 2016 UTC
- The app4.py Simple text editor and viewer
Last modified: Tue Mar 22 11:51:56 2016 UTC
- The Biohazard_symbol.svg original biohazard graphic
SVG is a vector-based format, so it scales to any size and still looks good.
Last modified: Tue Mar 22 21:24:26 2016 UTC
- The Biohazard_symbol.gif gif biohazard graphic
The tkinter tools apparently work better with gif (which at least supports
Last modified: Tue Mar 22 21:24:32 2016 UTC
- The barf.py tool to create inline graphic from gif
Last modified: Tue Mar 22 21:24:58 2016 UTC
- The app5.py Improved text editor and viewer
Check out the new about box and the support for BOM (byte order marker).
It also includes the fixes to ensure files are read and written with
Unicode UTF-8 encoding.
Last modified: Tue Mar 22 21:24:37 2016 UTC
Files from 2016/03/25
The bug was subtle. Since the menu choices for both Save and Save As
both called the savefile() function with no parameter to distingish which choice was used,
the function logic failed. It would say "If I know the file name, 'Save As' is just
'Save' the the file name I already know." That is not what we wanted. I used
lambda in the menu definitions (the command= part) so that I could send a boolean parameter
to let the function know if we were doing Save or Save As and now it
seems to work as intended. I was already doing something like that with the openfile()
function, but hadn't realized I needed it for the savefile() function too. The bug was a logic
bug, not a tkinter bug and not a python syntax bug. I'm sorry the bug fix took
so long. The changes were small, but it took me a while to figure out exactly
what it was doing. It was not possible for me to change what was being done
until I knew exactly what the problem was, but once I could reliably produce
the bug it was fairly simple to implement the fix. I also used the assert
statement which you may not have seen before. See the official
documentation for details on how it works.
- The app4.py Fixed version of app4
Last modified: Fri Mar 25 17:10:22 2016 UTC
- The app5.py Fixed version of app5
Last modified: Fri Mar 25 17:10:25 2016 UTC
Files from 2016/03/28
- The pinky.py Hyperlink text widget demo
Last modified: Sun Mar 27 19:13:26 2016 UTC
- The app6.py A version of app5 with interface language switching.
This was a bit tricky. I created two complete menu heirarchies, one for each language, and switch between
them. That was easy, but the state of some things like "Save As" and "Save" change (DISABLED or NORMAL),
and that state must be cloned when you switch languages (see Thai() and English() functions). This application
also shows how to include both an image and text on a menu item.
To totally localize the application, however, would be a lot more work since the built-in dialogs don't
support Thai language (file dialogs, message boxes) and to make it truly portable we'll have to create our own
replacements for those. We'll take a look at what it would take to do that after Songkran.
Last modified: Mon Mar 28 04:21:30 2016 UTC
- The Thailand.gif The source of the small Thai flag used in app6.py (used barf.py to create inline python version)
Last modified: Mon Mar 28 04:24:54 2016 UTC
- The United_Kingdom.gif The source of the small UK flag used in app6.py (used barf.py to create inline python version)
Last modified: Mon Mar 28 04:24:59 2016 UTC
Files from 2016/03/29
- The app7.py More font fixes, Thai strings translated, bug where
status bar was not updated on language switching is fixed.
Last modified: Tue Mar 29 07:12:30 2016 UTC
- The app8.py First rough cut at find/find next implementation.
Last modified: Tue Mar 29 14:57:50 2016 UTC
Files from 2016/03/31
- The app9.py Find/Find next implemented, but menu
for find next is always active.
Last modified: Fri Apr 1 00:31:23 2016 UTC
- The app9b.py Hopefully fixed the find next to only
be active if a previous find has been successful. However, this was done at the very
last minute in the class and is not very well tested yet.
Last modified: Fri Apr 1 00:31:06 2016 UTC
- The app9c.py Fixed bug where add_cascade() was used instead of
add_command() for the searchmenu, causing the funny triangle to sometimes show up in "Find Next"
Note that "Find Next" always starts from the last find instead of
from the current insert cursor position, which is a bug that has not
yet been fixed. Various ways of fixing it were discussed in the
2016/04/01 lecture, but none has been implemented yet.
Last modified: Fri Apr 1 05:23:06 2016 UTC
Files from 2016/04/01
Well, I think I got it working. But it needs more testing. Are you up to the
- The app10.py Find & Replace implemented, but
it is buggy. It broke "Find Next" and also there is no way to repeat a
Find & Replace. The python3 and tkinter parts work, but our application logic
is not good yet. This is what I plan to fix before midnight.
Last modified: Fri Apr 1 05:31:41 2016 UTC
- The app10b.py Ok, it seems to work for me now.
Please let me know if you find a bug (and get some quiz points if you are
the first person to report it and it is a real
bug in my program (not the operating system, etc.)). The missing Thai translations
do not count as a bug, since you know I cannot translate them. 825 lines of joy!
Last modified: Fri Apr 1 10:21:18 2016 UTC
Files from 2016/04/04
- The rb.py Simple Radiobutton demo program.
Last modified: Sun Apr 3 17:20:59 2016 UTC
- The rb2.py Radiobutton demo program that updates
visible interface based on current radio choice.
Last modified: Sun Apr 3 17:35:27 2016 UTC
Files from 2016/04/2
These programs use the matplotlib package. Like all things with
python3, matplotlib works well but the documentation doesn't have enough examples. I will provide
examples of the things you need to know, and I expect you to:
The point of this course was specifically to help you get started with using python3 with matplotlib
so you can use that in your future projects for BUU (and later on the job or when you study for
a master's degree). This semester had too many holidays, and the final exams are in only a few
weeks, so don't be lazy - get serious and please learn this stuff now!
- write the examples down in your paper notebook
- run & study the example until you think you understand them
- play with the examples by making small changes and verify your understanding
Normally you would use matplotlib to generate a graph in some format required by your word processing
software, and then include that plot in your document. Here I show how to generate an on-screen plot
which is useful while you debug your data and/or plot, then three different output formats (PDF, PNG, and
SVG) each of which has it's strengths and weaknesses. If you need some other format (PS, JPG, etc.) you
would need to tweak the examples a bit, or use image converter software with the output of one of these
There are basically two general types of plots: plots from a set of data points you have, or plots from
an equation that generates data points. In a real experiement, you probably have your actual results data
as a set of data points, and your theoretical or expected output as an equation, so you need to learn
both types of plots. Fortunately matplotlib has excellent support for both types of plots, so learning
how to do things like setup your axes, label the graph or points on it, etc. is the same for both types
- The official matplot PDF documentation (36.3 MB)
Very poorly formatted I'm sorry to say, but it does have lots of details we need to know.
- Anaconda Downloads
Anaconda provides a ready-to-go python installation with matplotlib
for 64 bit windows and 64 bit OSX. You want the Anaconda3 4.0 version
for your platform. If you use Linux and you keep your machine up-to-date,
you should use the distribution package management system to get what
you need instead. You must un-install any
existing python-3.5.1 version before you install this Anaconda.
Data file for demo001.py.
Data file for demo002.py.
Data file for demo003.py.
Data file for demo004.py.
Display a line graph from data in a file on screen. It requires a GUI
- demo002.py demo002.pdf
Generate a line graph from data in a file and store in a PDF file.
This is a standard document type that can be displayed or printed on almost
any modern computer. This works from the command line.
Generate a line graph from data in a file and store in a PNG file.
This is a rasterized format that doesn't scale well but works with web pages.
This works from the command line.
Generate a line graph from data in a file and store in a SVG file.
This is a vector format that scales well (and Firefox can display it).
This works from the command line.
Generate a line graph from an equation and display it.
Generate a vertical bar chart from hard-coded data and display it.
Generate a horizontal bar chart from hard-coded data and display it.
Generate a scatter plot from hard-coded data and display it.
Generate a pie chart plot from hard-coded data and display it.
This includes a lot of code to set font properties & colors, and
show how to write arbitrary text on your plot.
Files from 2016/05/01
These files were created for the special make-up class.
Improved function graph with legend to the right, and
this shows discrete points instead of line segments.
Fancy vertical bar chart with special control over
axes tick marks, labels, bar colors, etc.
Fancy vertical bar chart for black & white printing
or transparency, uses hatching instead of colors.
- demo014.py demo014.ps
Fancy pie chart for black & white printing or
transparency, creates and A4 Postscript file in batch
These files were created `live' during the lecture.
Files from 2016/05/02
These examples are for simple curve fitting.
They all use hard-coded data points, but you could
merge in the csv code from demo001.py or even adapt
to use sqlite with code from the sqlite3_demo.py program.
These are our first examples that use the numpy module.
Files from 2016/05/13
Files from 2016/05/14
Left‐to‐right, Black & white version.
A version of demo005 that displays the legend in
a better way.
Black & White version of demo005b with legend below plot.
An improved version of demo005c with higher resolution (notice
curve near x=1 no longer has that ugly angle), and many subtle
tweaks to make it look better. This version outputs a PNG image.
The midterm exam was held on March 10, 2016, and covered python3
text mode programming.
Exam 3 was held on April 4, 2016, and covered python3 and tkinter, but
not matplotlib or numpy. I could not post this
or the answers before the final exam because five people failed to take
exam 3 before the final exam. You can ask Dilok, Atthawud,
Pasuta, Nattanicha, and Saranyu about that.
Postscript format of actual exam paper. The spoken instructions said
answer any one of the three questions. People who took the exam late
were required to answer question 2 or question 3.
A solution to the first question.
A solution to the second question.
A solution to the third question.
The final exam was held on May 19, 2016, and covered matplotlib and a tiny
bit of numpy for curve fitting.
Postscript format of actual exam paper.
- Answers are 1, 4, 2, 6, 3, 4, 10, 8, 9, 6 in that order.
You might want to support language switching in your application, or currency
conversion. In that case, some images of national flags
might be helpful.
Hey, vi is not your enemy...
Many people had horrible problems with even exiting vim. When you are ready to
exit, press the 'esc' key twice (it's in the upper left-hand corner of all
normal keyboards). This will ensure you exit insert mode. What you do next
depends on which vi you are using.
- If you use the vim version of vi.
Then hold the shift key down and type ZQ to lose the
changes, or ZZ to save the changes.
- If you really are running some other vi (most older real UNIX® systems
like Solaris®, IRIX®, AIX®, etc.), you should instead use :q!
to lose the changes and :x! to save the changes.
basic vi commands
if you need help with vi.
But if you still hate vi....
One tool I have used with python on both Windows® and Linux® is
Geany which is a very
lightweight IDE that works very well with Python. It has syntax highlighting
that most people here like, it does not require modes like vi,
and it has menus, can display Thai, etc. I used it when I taught a seminar
and most students liked it. If you really don't like vi, try geany. Other
features that it has include code folding, auto-completion, plugins, and
more. It is free and GPL.
Stack overflow solutions to do the conversion. VIM can work with the
files without conversion, and so can python, but if you use some other
editor you probably need to do the conversion.