Python I

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

FIRST BYTE OF PYTHON

Preliminaraies
  1. PythonAnywhere is highly recommended if you are starting fresh without pre-installed Python, or if you feel unsure. PythonAnywhere is easy. The slides below will guide you step by step.
  2. If you are on Linux, or if you have pre-installed Anaconda or other Python environments, just skip slides #3 to #13. Proceed with the remaining slides. As usual, type out the programs (given in the slides) using your text editor and run them from either:
    • your Linux terminal: python3.5 filename_I_like.py
      or
    • your Python prompt: exec(open('filename_I_like.py').read())
    In the slides,
    • we call your (interactive) Python prompt tab [1];
    • we call your Linux terminal tab [2];
    • we call your text editor tab [3].
  3. Flexi-programmers at Penang Science Cluster should keep a green exercise book recording all solved exercises. We'd like to be as green as possible — save your answers as files in a designated directory/folder for easy retrieval anytime in the future. For exercises you find difficult to put in soft copy e.g. flowchart drawing, you may draw by hand and retain the hard copy for easy retrieval in the future.
  4. 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 here: ☞ how to clear browser cache.

Synopsis
  1. Cloud computing;
  2. Bash console vs Python console;
  3. The print function;
  4. Variables;
  5. Arithmetic operators;
  6. Running Python interactively vs running from saved .py files;
  7. Auto-completion (TAB key) and command history (UP and DOWN arrow keys);
  8. Loops:
    1. the for…in keywords;
    2. the range function;
  9. Sequence of numbers;
  10. Commenting;
  11. The help function.
Exercise
  1. Refer to the program on slide #44. What happens if we make five changes
    • the 'n' on line #15 to 'a';
    • the 'n' on line #20 to 'b';
    • the 'n' on line #25 to 'c';
    • the 'n' on line #30 to 'd';
    • the 'n' on line #35 to 'e',
    and then run? What changes on other lines do we need in order to make the program work?
  2. We have 6 examples in the program on slide #44. Summarise the difference in a 3-column table:
    exampledifference in plain Englishtechnical modification in Python
    #0 vs #1
    #1 vs #2
    #2 vs #3
    #3 vs #4
    #4 vs #5
  3. Write a program that prints the following with as few repetitions as you can:
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
              
  4. 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
              
  5. 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
              
  6. 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
              
  7. Write a program that prints the following with as few repetitions as you can:
              *...*****.......*********
              *********.......*****...*
              *...*****.......*********
              *********.......*****...*
              *...*****.......*********
              *********.......*****...*
              *...*****.......*********
              *********.......*****...*
              *...*****.......*********
              *********.......*****...*
              
  8. Write a program that prints a multiplication table for 3:
    133
    236
    339
    12336

  9. Write a program that prints the multiplication tables for 2, 3, 4, 5, 6, 7, 8 and 9:
    122
    224
    326
    12224
    133
    236
    339
    12336
    14 4
    24 8
    34 12
    124 48
    15 5
    25 10
    35 15
    125 60
    16 6
    26 12
    36 18
    126 72
    17 7
    27 14
    37 21
    127 84
    18 8
    28 16
    38 24
    128 96
    19 9
    29 18
    39 27
    129 108
  10. Write a program that prints a combined multiplication table for 2, 3, 4, 5, 6, 7, 8 and 9 as follows:
    n22*n33*n 44*n 55*n 66*n 7 7*n 8 8*n 9 9*n
    122334455667 7 8 8 9 9
    224 36 48 510 612 7 14 8 16 9 18
    326 39 412 515 618 7 21 8 24 9 27
    2 3 4 5 6 7 8 9
    2 3 4 5 6 7 8 9
    12224 336 448 560 672 7 84 8 96 9 108
What we learned so far
  1. Python keywords:
    • for
    • in
  2. Python built-in functions:
    • print(…, end='…')
    • range(…, …, …)
    • help(…)
  3. Linux command (and option):
    • ls -l

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