The Practice of Programming

Andy Turner

[Fullscreen]

Running Python

  • To run / execute Python code we need a Python interpreter, the virtual machine, and the standard library, which contains commonly used code ancillary to the core language.
  • In addition, we may need to install additional libraries or packages. The official Python Package Index ('PyPI') lists thousands of third party optional extras.
  • You can manually download both the core language and additional libraries and manually install them. However, it can prove quite complicated to do so.

Anaconda

  • It is easier to use Anaconda: a distribution that includes all the core language elements, plus the optional extras that we are likely to want.
  • The Individual edition is best for starters: https://www.continuum.io/downloads

Writing and running Python

  • You can write Python in any text editor that will produce text in the format UTF-8.
  • On Windows, both Notepad and Notepad++ can be used. Notepad++ is slightly better as it helpfully displays the code in colour where each different language element is given a different default colour.

Integrated Development Environments

  • Generally, programmers use an IDE when coding. This is text editing software that adds coding functionality, including:
    • Syntax colouring
    • Line numbers
    • Push button running and debugging
    • Error identification and help
    • Autocompletion of keywords and procedure names
    • GUI building help
  • Anaconda comes with Spyder (Scientific PYthon Development EnviRonment), a popular IDE.

Read–Eval–Print loop (REPL)

  • REPL capable languages have an associated prompt that sets up an environment recording variable values, and allow you to type one line at a time to do jobs. These are read, evaluated, and the answers printed to the screen.
    • If you type python at the command prompt (which is itself a REPL environment), you open the Python REPL prompt. It looks like this:
      >>>
    • From now on, if you see this, it means the command is to be typed in a Python REPL prompt.
    • To end the Python REPL session type and enter:
      >>> quit()

Jupyter

  • Jupyter Notebooks integrate a REPL into a document so that you can integrate text, images, hyperlinks and code that runs.
  • These were formerly called iPython Notebooks – the name was changed when they added the option for other REPL languages (including R).

Debugging

  • However you run the code, there may be issues with it that the interpreter will flag. We can distinguish between
    • Compilation time errors: errors associated with problems with syntax.
    • Import time errors: errors associated with finding other code the software uses
    • Runtime errors: errors associated with the logic of the program.
  • The interpreter will give you a description of what has gone wrong and where. Your job is then to fix the issue. You then need to run the interpreter again. You repeat this process until the program will run.
  • This isn't because you can't program; this is programming.
  • Programming is 50% writing code and 50% fixing it.

Computational Thinking

  • Your program syntax needs to be valid/correct. Capital letters and punctuation and spacing are all important.
  • Computers are not yet very good at guessing what you want to do and automatically fixing syntactical incorrect code.
  • Approach coding logically by building up the components you need to do a job and breaking the solution down into manageable tasks.

Computational Thinking:
Algorithms

  • The initial outline for the processing done is called an 'algorithm', it's a bit like a cooking recipe.

How to calculate the mean of three numbers

  1. Get 3 numbers.
  2. Sum the numbers.
  3. Divide the sum by 3.
  4. Print the result.

Computational Thinking: How to code

  • First, write the algorithm into your text file as comments (these are lines starting # that the computer doesn't process - blocks of lines can start and end """).
  • Next, pick a line to work up as code.
  • Write the code a line, or couple of lines, at a time. Compile and test the code every couple of lines.
  • Baby steps mean you won't end up with 100 errors at once and you'll know where the errors are.
  • Think 'how can I test this is working properly?' as each line goes in.

Computational Thinking: How not to code

  • Don’t ignore errors – they won’t go away, and they’ll just make everything wrong.
  • If you encounter an error and can’t obviously figure out what is causing it, then try commenting out chunks of code until you have something simple that works, then add it back in, a line at a time.
  • Sometimes the error is caused by a bracket not being closed or in the right place and this can be particularly difficult to spot.

The Coding Community

  • Usually, the first thing to do when you have to code something is to look to see if there is already existing code that you can leverage or learn from.
  • But as you are just learning to code we ask that for this module, you mostly build on what we give and just use what we teach you to use, although feel free to explore the Python documentation to learn more and use other parts of the language.
  • There are many helpful forums where people post useful examples, ask and answer questions (Stack Overflow is especially good – but please don't ask questions on forums without talking to us first).
    https://stackoverflow.com/
  • It's not called a coding community for nothing.

Real World Programming: Open Source Software

  • One way people make code available for others is by making it "Open Source".
  • Open Source means the core code is available for people to study, adapt, and distribute. The originator maintains copyright and ownership, but licences others to use the code in this way. The details of what these generalities mean in practice is controlled by a licence.
  • Python was built on a solid Open Source basis from version 2.

Some key licences

  • GNU General Public License (GPL): Permission to copy, adapt, distribute
  • Artistic License 2: Permission to copy, adapt, distribute, provided it is clear where the original resides and you allow changes to be rolled into the original.
  • Attribution Assurance License: Assures attribution.
  • Full list:
    https://opensource.org/licenses/alphabetical

Using OSS

  • Open Source Licenses make code freely available (in the sense of putting it out there for others to use) and freely available (in the sense of not costing anything).
  • Different licenses have different requirements and protection. You must make sure you match the licensing requirements.
  • Moreover, for our courses, we want to see that you can write code yourself, so a) we have limits on the amount of others' code you can use and b) we have limits on automated code production (code produced by other software). Please see the assessments pages for more details. At the end of the day, you're here to learn to code, not learn to give the appearance of being able to code.

Repositories and versioning

  • Code repositories are places for storing code and providing access to the code to others. They are particularly helpful for a team that are all working on the same code.
  • Repository software helps to resolve conflicts where two or more people have tried to edit the same file simultaneously.
  • Repositories usually have version control (allowing you to roll back to previous versions).
  • A number are free to use.

GitHub

  • GitHub is a well known repository and collaboration platform.
  • As well as a repository, it also allows you to build your own Web site.

Further info