Python III

Mary PW Chin 钱碧慧博士
PhD (Wales), MSc (Surrey)
E-mail:

1st bite of Python

Getting started

2nd bite of Python

Dice

3rd bite of Python

Circles

4th bite of Python

Statistics & histograms

5th bite of Python

More loops

6th bite of Python

Pretty nets

7th bite of Python

Jumping man

8th bite of Python

Medical scans

9th bite of Python

Handwriting recognition

10th bite of Python

Flower recognition

THIRD BYTE OF PYTHON
a slideshow

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
  1. Module 'matplotlib', its class 'Circle' and its method 'subplots';
  2. Module 'sys';
  3. Module 'numpy' and its methods: 'floor' and 'ceil';
  4. Colours;
  5. Saving plots to file;
  6. More number sequences;
  7. Nested loops;
  8. Taking input during runtime from keyboard (standard input);
  9. Arrays;
  10. Functions and returns;
  11. While;
  12. Remembering 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

  1. Identify parts you may change:
    • variables;
    • arrays;
    • arguments.
  2. Identify parts you may not change:
    • imported modules and methods;
    • functions and keywords.
  3. List the exact order line by line how Python executes the program.
  4. Draw the flowchart.
Exercise
  1. 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?
  2. Refer to program version #1 from the slides. What happens if we move lines #12-#17 to the line before line #3?
  3. For program version #4, mark out the:
    • variable names;
    • arguments;
    • functions;
    • keywords;
    • modules;
    • methods.
  4. Repeat Exercise 3 with program version #5.
  5. Repeat Exercise 3 with program version #7.
  6. Repeat Exercise 3 with program version #8.
  7. Repeat Exercise 3 with program version #9.
  8. Repeat Exercise 3 with program version #10.
  9. Repeat Exercise 3 with program version #11.
  10. Repeat Exercise 3 with program version #12.
  11. Repeat Exercise 3 with program version #13.
  12. Draw the flowchart for program version #8.
  13. Draw the flowchart for program version #12.
  14. In this lesson we made 12 revisions to the program. Summarise the improvement in each version in a 3-column table:
    versionimprovement in plain Englishtechnical modification in Python
    #1 to #2
    #2 to #3
    #3 to #4
    #12 to #13
  15. Write a program that prints the following with as few repetitions as you can:
               *..*..*..*..*
                *..*..*..*..*
                 *..*..*..*..*
               *..*..*..*..*
                *..*..*..*..*
                 *..*..*..*..*
               *..*..*..*..*
                *..*..*..*..*
                 *..*..*..*..*
              
  16. Write a program that prints the following with as few repetitions as you can:
               *
               **
               ***
               ****
               *****
               ******
               *******
               ********
               *********
               **********
               *********
               ********
               *******
               ******
               *****
               ****
               ***
               **
               *
              
  17. Write a program that prints the following with as few repetitions as you can:
                      *
                     * *
                    *   *
                   *     *
                  *       *
                 *         *
                *           *
               *             *
              *               *
               *             *
                *           *
                 *         *
                  *       *
                   *     *
                    *   *
                     * *
                      *
              

    Hints:

    • Identify what is varying by a periodic trend: is it the number of asterisks or the number of spaces?
    • Build a two-column table, each row of your table corresponding to each row from the diamond above:
      • 1st column: with the number of spaces before the first asterisk on each line;
      • 2nd column: with the number of spaces between the asterisks on each line.
    • Let the 1st column be x, let the 2nd column be y, we can see that they have a linear relation and will fit into an equation y = mx + c, where m is the gradient and c is the offset/constant.
    • Select any pair of rows from your table, use the x and y values to solve the simultaneous equations to derive m and c.
    • Apply the formula you have just derived for the nested loops in your program.
  18. Write a program that prints the following with as few repetitions as you can:
                      *               *
                     * *             *
                    *   *           *
                   *     *         *
                  *       *       *
                 *         *     *
                *           *   *
               *             * *
              *               *
               *             * *
                *           *   *
                 *         *     *
                  *       *       *
                   *     *         *
                    *   *           *
                     * *             *
                      *               *
              

What we have learned so far
  1. Python keywords:
    • for
    • in
    • import
    • as
    • if
    • elif
    • else
    • and
    • or
    • with
    • as
  2. Python keywords:
    • for … in …:
    • import
    • if … and/or …:
    • if …: … elif …: … else:
    • with … as …:
  3. Python built-in functions:
    • print(…, end='…')
    • range(…, …, …)
    • help(…)
    • dir(…)
    • int(…)
    • str(…)
    • range(…)
  4. Python modules, methods and classes:
    • random.randint(…, …)
    • matplotlib.pyplot.Circle(…, …)
    • fig, ax = matplotlib.pyplot.subplots(); ax.add_artist(…); fig.savefig(…),
    • numpy.floor(…), numpy.ceil(…)
    • numpy.ndarray.astype(…)
  5. Linux commands (and options):
    • ls -l
    • wc -l
    • head
    • tail

1st bite of Python

Getting started

2nd bite of Python

Dice

3rd bite of Python

Circles

4th bite of Python

Statistics & histograms

5th bite of Python

More loops

6th bite of Python

Pretty nets

7th bite of Python

Jumping man

8th bite of Python

Medical scans

9th bite of Python

Handwriting recognition

10th bite of Python

Flower recognition