Python VIII

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

1st bite of Python

Printing

2nd bite of Python

Loops

3rd bite of Python

If-elif-else

4th bite of Python

Nested loops

5th bite of Python

Input, while, tables

6th bite of Python

Strings

7th bite of Python

Simple maths

8th bite of Python

More loops

9th bite of Python

Dictionaries

10th bite of Python

Drawing

11th bite of Python

Statistics & histograms

12th bite of Python

Pretty nets

13th bite of Python

Medical scans

14th bite of Python

Handwriting recognition

15th bite of Python

Flower recognition

More loops & recursive functions

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.

Gentle reminder

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.

Synopsis
  1. Arrays;
  2. further drills on logic and program design;
  3. Fibonacci series;
  4. calculating Factorials with and without math.factorial;
  5. module calendar and its Calendar, month_abbr, day_abbr classes, weekday and itermonthdays methods;
  6. module dateutil and its relativedelta method.

Exercise
  1. Write a program that prints the following with as few repetitions as you can:
               *
               **
               ***
               ****
               *****
               ******
               *******
               ********
               *********
               **********
               *********
               ********
               *******
               ******
               *****
               ****
               ***
               **
               *
              
  2. 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.
  3. Write a program that prints the following with as few repetitions as you can:
                      *               *
                     * *             *
                    *   *           *
                   *     *         *
                  *       *       *
                 *         *     *
                *           *   *
               *             * *
              *               *
               *             * *
                *           *   *
                 *         *     *
                  *       *       *
                   *     *         *
                    *   *           *
                     * *             *
                      *               *
              

External goodies

Algorithms

1st bite of Python

Printing

2nd bite of Python

Loops

3rd bite of Python

If-elif-else

4th bite of Python

Nested loops

5th bite of Python

Input, while, tables

6th bite of Python

Strings

7th bite of Python

Simple maths

8th bite of Python

More loops

9th bite of Python

Dictionaries

10th bite of Python

Drawing

11th bite of Python

Statistics & histograms

12th bite of Python

Pretty nets

13th bite of Python

Medical scans

14th bite of Python

Handwriting recognition

15th bite of Python

Flower recognition