{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Python Dev - Scripts, Modules and Packages\n", "\n", "## Foreword\n", "\n", "### ACSE 1 Lecture One - 12th October 2020 - Version 3.0.5\n", "____________________" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "
@Percival, James R
in acse1
or General
, or DM me.\n",
"\n",
"Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) \n",
"Type 'copyright', 'credits' or 'license' for more information\n",
"IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.\n",
"\n",
"In [1]:\n",
"
\n",
"\n",
"Unlike the vanilla python interpreter (what you get by typing `python` in a terminal/command prompt), it contains useful features like tab autocompletions, a richer browsable history (using the arrow keys, additional access to the inbuilt documentation system and the easy ability to call out to the underlying operating system.\n",
"\n",
"Many of the features available to you should be familiar even to those of you who have only used Jupyter notebooks before, since they are also available inside Jupyter notebook code cells. In fact \"under the hood\" Jupyter is running an IPython console (the Python \"kernel\") to process Python3 code."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise One: Running Python Code\n",
"Using VS Code (or your own prefered data entry method), write a Python script to output the first 20 prime numbers. If you answered lecture 2 in the introductory exercises, you can start from the code you wrote ther, or start from fresh.
\n", "\n", "Tips:\n", "a
divides b
exactly, then a%b==0
, which gives a quick test. When testing your code, you should expect the output for the first 5 primes to be [2, 3, 5, 7, 11]
.
Try to convert the script into a routine to calculate all prime numbers smaller than an input, \\(n\\) using `sys.argv`.
\n", "\n", "A [model solution](https://msc-acse.github.io/ACSE-1/lectures/lecture7-solutions.html#exercise1) for the script is available.\n", "Write a script to calculate the mean of a sequence of numbers. As an extension, tru make it take extra options (using the `argparse` module) -b
, -o
and -x
to work with with binary (i.e. base 2, with 101 == 5
decimal), octal (i.e. base 8, with 31 == 25
decimal) and hexadecimal (i.e. base 16 2A == 42
decimal) numbers.
Test your basic script on the following sequences: 1
(mean 1) 1 5 7 13 8
(mean 6.8), 2.5, 4 ,-3.2, 9.3
(mean 3.15).
Also try feeding it no input.
\n", "\n", "_Tips_:\n", "\n", "For the longer version you can use the 2 argument version of the int
function to change the base of numbers. For example int('11',2)==3
and int('3A', 16)==58
.
Write a script to plot the functions $y=\\sin(x)$, $y=\\cos(x)$ and $y=\\tan(x)$ to screen over the range [0,$2\\pi$] and then run it in a terminal/prompt.
\n", "\n", "Make sure to include labels on your axes.
\n", "\n", "Change the script to output a .png
file to disk.
Next do the same to write a .pdf
.
Model answers are available.
\n", "\n", "A model answer is available.
\n", "\n", "\n", "\n", "The moment that code is going to be read a second time (including by you in two months time) then it becomes unacceptable to write it as though it is disposable. Functions need docstrings, and variables should have names which make sense (and not just to you personally right now).\n", "\n", "Similarly, **when you've tested your code**, and you know that a specific function takes 90% of the runtime, it may make sense to rewrite it in a faster way, even if that is harder to maintain (more `numpy`, using `numba`, writing your own `C` extension modules, and so on).\n", " \n", "Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
\n", "\n", "Find the sum of all the multiples of 3 or 5 below 1000.
\n", " \n", "2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
\n", "\n", "What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
\n", "\n", "73167176531330624919225119674426574742355349194934\n",
"96983520312774506326239578318016984801869478851843\n",
"85861560789112949495459501737958331952853208805511\n",
"12540698747158523863050715693290963295227443043557\n",
"66896648950445244523161731856403098711121722383113\n",
"62229893423380308135336276614282806444486645238749\n",
"30358907296290491560440772390713810515859307960866\n",
"70172427121883998797908792274921901699720888093776\n",
"65727333001053367881220235421809751254540594752243\n",
"52584907711670556013604839586446706324415722155397\n",
"53697817977846174064955149290862569321978468622482\n",
"83972241375657056057490261407972968652414535100474\n",
"82166370484403199890008895243450658541227588666881\n",
"16427171479924442928230863465674813919123162824586\n",
"17866458359124566529476545682848912883142607690042\n",
"24219022671055626321111109370544217506941658960408\n",
"07198403850962455444362981230987879927244284909188\n",
"84580156166097919133875499200524063689912560717606\n",
"05886116467109405077541002256983155200055935729725\n",
"71636269561882670428252483600823257530420752963450\n",
"
\n",
"\n",
"The four adjacent digits in this number that have the greatest product are 9 × 9 × 8 × 9 = 5832. Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
\n", "\n", "In this lecture we learned:\n", "\n", "
Tomorrow:\n", "