MAS115 Mathematical Investigation Skills: Python

Dr Alexander Fletcher
Autumn 2021/22

Lecture 1: Intended learning outcomes

By the end of this lecture, you will be able to:

0  Introduction and overview

0.1  How the Python part of the module works

0.2  The module webpage

Visit http://mas115.group.shef.ac.uk/ regularly. There you can

You can also find these via the module page on Blackboard.

0.3  Why programming?

0.4  Why Python?

0.5  Some etymology

Python is named after "Monty Python", a British comedy group from the 60s, 70s and 80s. Tradition dictates the use of terms from their sketches in examples. The use of the word `spam' that you are familiar with comes from that Monty Python as well.


1  Programming

1.1  What is this?

INGREDIENTS
3 eggs, as fresh as possible
2 knobs unsalted butter
Salt and pepper

INSTRUCTIONS
Crack the eggs into a small bowl and whisk.
Add some salt and pepper.
Heat the butter in a 9-inch non-stick frying pan.
Pour in the eggs.
In the first 30 seconds, create 6-10 small cuts.
When the top is nearly set turn off the heat.
Don't worry if some of the egg in the centre isn't quite set.
Use your spatula to flip one half over the other.
Serve immediately.

1.2  What is this?

GLOSSARY
C12B = sl next 6 sts onto a CN and leave at back of work.
(K1. P1) 3 times then (K1. P1) 3 times from CN.

INSTRUCTIONS
CO 36 sts.
1st row: (RS). *K1. P1. Rep from * to end of row.
Rep last row 5 times more.
**Next row: (RS). (K1. P1)
6 times. C12B. (K1. P1) 6 times.
Next 5 rows: *K1. P1. Rep from * to end of row.**
Rep from ** to ** until work from beg measures 60 ins.
Cast off in pat.

1.3  What is this?

1 ccaatcaggt ccctccctac ctcagatcgc agctataata cataggagta aagaggcttc
61 tcgcattaag tggctgtggc ttgaagtaac gttgtgattt cgaggtcagt cttacctttc
121 gcatccccgc cgcaaacctc cgatgcgtta tcagtcgcac gtttccgcac ctgtcacggt
181 cggggcttgg cgctgctgag ggacacgcgt gaaccgagga gacggcaagg acatcgccgg
241 agatccgcgc ctcgacaacg agaaaccctg ctagacagac cgctcgagaa caccgcagcg
301 agattcagcg tgcggcaaaa tgcggctttt gacgagagtg ctgctggtgt ctcttctcac
361 tctgtccttg gtggtgtccg gactggcctg cggtcctggc agaggctacg gcagaagaag
421 acatccgaag aagctgacac ctctcgccta caagcagttc atacctaatg tcgcggagaa
481 gaccttaggg gccagcggca gatacgaggg caagataacg cgcaattcgg agagatttaa
541 agaacttact ccaaattata atcccgacat tatctttaag gatgaggaga acacgggagc
601 ggacaggctc atgacacaga gatgcaaaga caagctgaac tcgctggcca tctctgtaat
661 gaaccactgg ccaggggtta agctgcgtgt gacagagggc tgggatgagg acggtcacca
721 ttttgaagaa tcactccact acgagggaag agctgttgat attaccacct ctgaccgaga
781 caagagcaaa tacgggacac tgtctcgcct agctgtggag gctggatttg actgggtcta
841 ttacgagtcc aaagcccaca ttcattgctc tgtcaaagca gaaaattcgg ttgctgcgaa
901 atctgggggc tgtttcccag gttcggctct ggtctcgctc caggacggag gacagaaggc
961 cgtgaaggac ctgaaccccg gagacaaggt gctggcggca gacagcgcgg gaaacctggt
1021 gttcagcgac ttcatcatgt tcacagaccg agactccacg acgcgacgtg tgttttacgt
1081 catagaaacg caagaacccg ttgaaaagat caccctcacc gccgctcacc tcctttttgt
1141 cctcgacaac tcaacggaag atctccacac catgaccgcc gcgtatgcca gcagtgtcag
1201 agccggacaa aaggtgatgg ttgttgatga tagcggccag cttaaatctg tcatcgtgca
1261 gcggatatac acggaggagc agcggggctc gttcgcacca gtgactgcac atgggaccat
1321 tgtggtcgac agaatactgg cgtcctgtta cgccgtaata gaggaccagg ggcttgcgca

1.4  What is a computer program?

A computer program is a precise set of instructions to perform a task written in a language that a computer can understand. The two examples were precise sets of instructions written in specific languages for humans to understand.

Writing a computer program can be very roughly split into two parts:

The first part is very mathematical and requires a careful, precise understanding of what the task requires. [In general, once you have written some code you will analyse the task again and so on.]

1.5  Calculating a factorial

  1. What is 4!?
  2. How is n! defined? (What is n?)
  3. How do you calculate it?
  4. How do you tell someone else to calculate it?
  5. How do you tell a computer how to calculate it?

1.6  First attempt at a program

# A program to calculate 10!


factorial = 1
i = 1
while i <= 10:
    factorial = factorial * i
    i = i + 1
print("10! = ", factorial)

Line 1 is a comment.
Line 4 sets the variable factorial to 1.
Line 5 sets the variable i to 1.
Line 6 starts up the loop. It repeats the following lines until i > 10.
Line 7 sets the variable factorial to i times its old value.
Line 8 sets the variable i to be its old value plus 1.
Line 9 prints out the result to the screen.

1.7  Adding some user input

# A program to calculate factorial of number entered
n = int(input("Enter the number you want "
              "to calculate the factorial of: "))
factorial = 1
i = 1
while i <= n:
    factorial = factorial * i
    i = i + 1
print(n, "! = ", factorial)

We have added lines 2 and 3 and changed 10 to n on lines 6 and 9. (Lines 2 and 3 are one line of code but won't fit on screen.) This sets n to be the integer that the user inputs after the prompt. Note: we get the wrong answer if we enter a negative number!

1.8  Some terminology

What we have written is variously referred to as:

Two other terms are the following:

Writing programs is referred to as either:

These two terms mean the same thing.