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' keyword;
    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 between versions 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 print() functions as you can:
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
              
  4. Write a program that prints the following with as few print() functions 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 print() functions 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 print() functions 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 print() functions as you can:
              *...*****.......*********
              *********.......*****...*
              *...*****.......*********
              *********.......*****...*
              *...*****.......*********
              *********.......*****...*
              *...*****.......*********
              *********.......*****...*
              *...*****.......*********
              *********.......*****...*
              

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