Python X
Mary PW Chin 钱碧慧博士 PhD (Wales), MSc (Surrey) E-mail:
Drawing six faces of dice
Slides here are continuously being revised and updated. If you have been here before, your browser might have some old files cached. To ensure that you see the latest version, please follow the instructions given in the following link: ☞ how to clear browser cache .
Synopsis
Module matplotlib , its class Circle and its method subplots ;
module sys ;
module numpy and its methods: floor and ceil ;
colours;
saving plots to file;
more number sequences;
runtime input from keyboard (standard input):
by passing arguments;
by interactive input;
arrays;
while ;
storing values before they get overwritten.
Do not forget
For every program you encounter it is always an excellent exercise to change the variable names to names which are most meaningful to you, then make the program run. You will gain deeper understanding of the program. You will see the program in a new light. Everyone is different: variable names most intuitive to me differ from variable names most intuitive to you. My programs are written with variable names most intuitive to me, so you should change them to names most intuitive to you so that the program flow becomes more obvious.
For every program you encounter you should be able to
Identify parts you may change:
variables;
arrays;
arguments.
Identify parts you may not change:
imported modules and methods;
functions and keywords.
List the exact order line by line how Python executes the program.
Draw the flowchart.
Exercise
Refer to program version #1 from the slides. What happens if we move line #10 to
the line before line #3?
the line before line #19?
the line after line #19?
Refer to program version #1 from the slides. What happens if we move lines #12-#17 to the line before line #3?
Mark out the:
variable names;
arguments;
functions;
keywords;
modules;
methods.
on program versions #4, #5, #7, #8, #9, #10, #11, #12 and #13.
Draw the flowchart for program versions #8 and #12.
In this lesson we made 12 revisions to the program. Summarise the improvement in each version in a 3-column table:
version improvement in plain English technical modification in Python
#1 to #2
#2 to #3
#3 to #4
…
…
…
#12 to #13
Write a program that prints the following with as few repetitions as you can:
.o.o.o.o.o.o.o.o.o.o
.o.o.o.o.o.o.o.o.o.o
.o.o.o.o.o.o.o.o.o.o
.o.o.o.o.o.o.o.o.o.o
.o.o.o.o.o.o.o.o.o.o
.o.o.o.o.o.o.o.o.o.o
.o.o.o.o.o.o.o.o.o.o
.o.o.o.o.o.o.o.o.o.o
Write a program that prints the following with as few repetitions as you can:
.ox.ox.ox.ox.ox.ox.ox
.ox.ox.ox.ox.ox.ox.ox
.ox.ox.ox.ox.ox.ox.ox
.ox.ox.ox.ox.ox.ox.ox
.ox.ox.ox.ox.ox.ox.ox
.ox.ox.ox.ox.ox.ox.ox
.ox.ox.ox.ox.ox.ox.ox
.ox.ox.ox.ox.ox.ox.ox
Write a program that prints the following with as few repetitions as you can:
.ooxxx.ooxxx.ooxxx
.ooxxx.ooxxx.ooxxx
.ooxxx.ooxxx.ooxxx
.ooxxx.ooxxx.ooxxx
.ooxxx.ooxxx.ooxxx
.ooxxx.ooxxx.ooxxx
.ooxxx.ooxxx.ooxxx
.ooxxx.ooxxx.ooxxx
External goodies
☞ Pyplot tutorial