|
| 1 | +## Code Templates in Python - Level 2 |
| 2 | + |
| 3 | +These code templates may be useful for anyone who is learning the Python programming language. The templates use the procedural programming style. These templates are numbered and they are associated with the same numbered programs in the [snippets folder](https://github.com/HamPUG/examples/tree/master/ncea_level2/snippets). |
| 4 | + |
| 5 | +The top and bottom of these template programs remains much the same for each program. At the top of each program the template provides: shebang, importing modules, program description, copywrite, defining constants and variables. At the bottom of each program the template provides: `if __name__ == '__main__':`, help information function, python version check function, log file append function, command line argument parsing including debugging and help flags, and calls the main() function. |
| 6 | + |
| 7 | +The middle section of these template programs is what changes to give each program its unique features. It contains a main() function which includes calls to other functions. |
| 8 | + |
| 9 | +These template programs highlight the use of function code blocks, but do not utilise classes. They also assume the program is written as one file, and importing of modules is restricted to a small set of the builtin python modules. |
| 10 | + |
| 11 | +The programs are all console terminal based and there is no use of GUI windowing. |
| 12 | + |
| 13 | +These templates were developed using Python 3.4. They have been tested on Ubuntu Linux and Windows10 desktop environments. |
| 14 | + |
| 15 | +New Zealand secondary school teachers and students may find these template useful for the Digital Technologies, Level 2, Achievement Standards [91372](http://www.nzqa.govt.nz/nqfdocs/ncea-resource/achievements/2017/as91372.pdf) and [91373](http://www.nzqa.govt.nz/nqfdocs/ncea-resource/achievements/2017/as91373.pdf). |
| 16 | + |
| 17 | +In a classroom computer lab that uses Windows10 desktop computers, the console terminal window applications (CMD and Powershell) may have been made unavailable on the students desktop computers. In the Windows10 file manager, if a *python* file is double-clicked it will launch a terminal window to run the python program. When the program completes, this terminal window is closed. To prevent the programs closing before a student has been able to see what the program does, all these snippet programs end with the lines... |
| 18 | + |
| 19 | + input("Press Enter key to end the program.") |
| 20 | + sys.exit() |
| 21 | + |
| 22 | +Ian Stewart - 2016 © [](https://creativecommons.org/licenses/by/4.0/) |
| 23 | + |
| 24 | +## Template Programs |
| 25 | + |
| 26 | +**template_l2_01.py** - *Fundament_Components* |
| 27 | + |
| 28 | +Python program. Fundamental programming style components. |
| 29 | +Includes: Shebang, encoding, Commented text in help(), |
| 30 | +Importing modules, variables, constants, python2 check, |
| 31 | +main function, cli help(), `if __name__ == '__main__':`, |
| 32 | +cli sys.argv list, debug, call main() |
| 33 | + |
| 34 | +**template_l2_02.py** - *Time-Stamped_Logging* |
| 35 | + |
| 36 | +Python program using a procedural programming style. |
| 37 | +Add Logging of time-stamped data |
| 38 | + |
| 39 | +**template_l2_03.py** - *Check_Python_Min_Version* |
| 40 | + |
| 41 | +Python program using a procedural programming style. |
| 42 | +Add: Check python is above the minimum version. |
| 43 | + |
| 44 | +**template_l2_04.py** - *Functions* |
| 45 | + |
| 46 | +Python program using a procedural programming style. |
| 47 | +Add: 2 x functions. Includes data-type testing and +=1 |
| 48 | +Data can be passed as a command line option. |
| 49 | + |
| 50 | +**template_l2_05.py** - *Input_From_Console* |
| 51 | + |
| 52 | +Python program using a procedural programming style... |
| 53 | +Get integer or floating point value from console. |
| 54 | + |
| 55 | +**template_l2_06.py** - *Input_Integer_From_Console* |
| 56 | + |
| 57 | +Python program using a procedural programming style... |
| 58 | +Add: Integer only data from console. |
| 59 | + |
| 60 | +**template_l2_07.py** - *Circle_Calculator* |
| 61 | + |
| 62 | +When provided with the a value for the radius, the circle |
| 63 | +calculator program will determine the circles |
| 64 | +circumference and area. |
| 65 | + |
| 66 | +**template_l2_08.py** - *Sphere_Calculator* |
| 67 | + |
| 68 | +When provided with the a value for the radius, the sphere |
| 69 | +calculator program will determine the spheres |
| 70 | +surface area and volume. |
| 71 | + |
| 72 | +**template_l2_09.py** - *Cube_Calculator* |
| 73 | + |
| 74 | +When provided with the length of the edge of a cube |
| 75 | +calculator program will determine the cubes |
| 76 | +surface area, volume, and space diagonal. |
| 77 | + |
| 78 | +**template_l2_10.py** - *Circle_Plotter* |
| 79 | + |
| 80 | +When provided with a range of integer values for a radius |
| 81 | +the circle plotter program will determine the |
| 82 | +diameter, circumference and area. |
| 83 | +The output may be cut and pasted into a spreadsheet. |
| 84 | +If so, space delimiters must be merged. |
| 85 | + |
| 86 | +**template_l2_11.py** - *Sphere_Plotter* |
| 87 | + |
| 88 | +When provided with a range of integer values for a radius |
| 89 | +the sphere plotter program will determine the |
| 90 | +circumference, surface area and volume. |
| 91 | +The output may be cut and pasted into a spreadsheet. |
| 92 | +If so, space delimiters must be merged. |
| 93 | + |
| 94 | +**template_l2_12.py** - *Sphere_Volume_Plotter* |
| 95 | + |
| 96 | +When provided with a range of integer values for the |
| 97 | +cubic meter volume of a sphere, the sphere volume plotter |
| 98 | +program will determine the radius, diameter and |
| 99 | +circumference of the sphere in meters. |
| 100 | +The output may be cut and pasted into a spreadsheet. |
| 101 | +If so, space delimiters must be merged. |
| 102 | + |
| 103 | +**template_l2_13.py** - *Sphere_Litre_Plotter* |
| 104 | + |
| 105 | +When provided with a range of integer values for the |
| 106 | +litres a sphere will hold, the sphere litre plotter |
| 107 | +program will determine the radius, diameter and |
| 108 | +circumference of the sphere in centimeters. |
| 109 | +The output may be cut and pasted into a spreadsheet. |
| 110 | +If so, space delimiters must be merged. |
| 111 | + |
| 112 | +**template_l2_14.py** - *Circle_Plotter_CSV* |
| 113 | + |
| 114 | +When provided with a range of integer values for a radius |
| 115 | +the circle plotter program will determine the |
| 116 | +diameter, circumference and area. Like template_l2_10.py |
| 117 | +The data is output to a comma seperated values (.csv) file. |
| 118 | + |
| 119 | +**template_l2_15.py** - *Cube_Plotter_CSV* |
| 120 | + |
| 121 | +When provided with the length of the edge of a cube |
| 122 | +calculator program will determine the cubes |
| 123 | +surface area, volume, and space diagonal. |
| 124 | +Similar to template_l2_09.py |
| 125 | +The data is output to a comma seperated values (.csv) file |
| 126 | +The function update_data() is called from main(). |
| 127 | + |
| 128 | +**template_l2_16.py** - *Fibonacci_sequence* |
| 129 | + |
| 130 | +Generate the Fibonacci sequence of integers. |
| 131 | +The total for the sequence may be selected. |
| 132 | +As each Fibonacci number in the sequence is generated |
| 133 | +it is appended to a list. |
| 134 | +The items in the list are then output to the console. |
| 135 | +The sequence has been limited to 200 so they are easier |
| 136 | +to read on the console. |
| 137 | +Highlights there is no limit to python integer arithmetic |
| 138 | + |
| 139 | +**template_l2_17.py** - *Integer_Fun* |
| 140 | + |
| 141 | +Demonstrate the arithmetic performed on integers is |
| 142 | +accurate and has no limits. |
| 143 | +Starting with a funny number it is manipulated with |
| 144 | +multiplication (*), addition (+) and exponents (**) to |
| 145 | +create an integer that is comprised of only one's. |
| 146 | +Also highlights use of the modulo operator (%) and pythons |
| 147 | +integer division (//), which is floor division. |
| 148 | + |
| 149 | +**template_l2_18.py** - *Integer_Distance* |
| 150 | + |
| 151 | +Demonstrate the arithmetic performed on integers is |
| 152 | +accurate and has no limits. |
| 153 | +Introduce floating point calculation. |
| 154 | + |
| 155 | +**template_l2_19.py** - *Bike_Speed* |
| 156 | + |
| 157 | +Determine the speed of a bicycle. Parameters that may be |
| 158 | +adjusted are: |
| 159 | +1. Diameter of the rear wheel. |
| 160 | +2. Cadence. The revolutions per minute of the pedalling. |
| 161 | +3. Crankset teeth. Number of teeth on the front sprocket. |
| 162 | +A list provides the number of teeth on each sprocket of |
| 163 | +the casette on the rear wheel. |
| 164 | +Input values are Integers. Calculations are mostly floats. |
| 165 | + |
| 166 | +**template_l2_20.py** - *Floating_Point* |
| 167 | + |
| 168 | +Demonstrate floating-point arithmetic and |
| 169 | +highlight some of its issues and limitations. |
0 commit comments