Python III

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

1st bite of Python

Printing

2nd bite of Python

Loops

3rd bite of Python

Dice

4th bite of Python

Stars

5th bite of Python

Strings

6th bite of Python

Simple maths

7th bite of Python

More loops

8th bite of Python

Circles

9th bite of Python

Statistics & histograms

10th bite of Python

Pretty nets

11th bite of Python

Medical scans

12th bite of Python

Handwriting recognition

13th bite of Python

Flower recognition

Conditionals & random numbers

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. The import keyword;
  2. module random and its method randint;
  3. conditionals: if-elif-else;
  4. relational operators;
  5. assignment vs comparison;
  6. booleans operators, and vs or;
  7. variable names: rules and conventions e.g. camel-case;
  8. output to screen (standard output) vs output to file;
  9. Linux commands and options: ls, wc, head, tail;
  10. basic probability theory;
  11. initialisation, counters and normalisation;
  12. dir;
  13. more flowcharting.

Exercise
  1. For the program on slide #19 mark out the:
    • variable names;
    • arguments;
    • functions;
    • keywords;
    • modules;
    • methods;
  2. Repeat Exercise 1 for the program on slide #23.
  3. Repeat Exercise 1 for the program on slide #35.
  4. Is each of the following a legal variable name in Python?
    • mickey-mouse;
    • mickey.mouse;
    • mickey mouse;
    • 2mickey mice.
  5. Refer to the program on slide #35. What difference would you expect if we change
    range(0, so_many_times) to
    • range(-100, so_many_times-100);
    • range(so_many_times+1000, 1000, -1);
    • range(0, so_many_times*1000, 1000)?
  6. Refer the program on slide #38. What happens if we change elif to if? Draw the revised flowchart.
  7. Write a program to generate two random numbers between 0 and 100. Print the larger number, and the sum, difference and product of the two numbers.
  8. Write a program to generate a random number between 0 and 10. Take the number as the radius of a circle. Print the diameter, circumference and area of this circle. You can get the value for π by first importing the math module; the value for π is then accessible as math.pi.
  9. Write a program to generate a random number between 0 and 100,
    • take the random number as a temperature in celsius, convert that to fahrenheit;
    • take the random number as a measurement in centimetres, convert to and inches;
    • take the random numner as a weight in kilograms, convert to pounds.
  10. Write a program simulating the draw of Magnum 4D first prize.

What we have learned so far
  1. Python keywords:
    • for
    • in
    • import
    • as
    • if
    • elif
    • else
    • and
    • or
  2. Python built-in functions:
    • print(…, end='…')
    • range(…, …, …)
    • help(…)
    • dir(…)
  3. Python modules and methods:
    • random.randint(…, …)
    • math.pi
  4. Linux commands (and options):
    • ls -l
    • wc -l
    • head
    • tail

1st bite of Python

Printing

2nd bite of Python

Loops

3rd bite of Python

Dice

4th bite of Python

Stars

5th bite of Python

Strings

6th bite of Python

Simple maths

7th bite of Python

More loops

8th bite of Python

Circles

9th bite of Python

Statistics & histograms

10th bite of Python

Pretty nets

11th bite of Python

Medical scans

12th bite of Python

Handwriting recognition

13th bite of Python

Flower recognition