KS2: Python Programming Unit - Primary Computing

10m ago
4.93 MB
33 Pages

For free Primary Computing resources please visit KS2: PythonProgramming UnitJon [email protected]

For free Primary Computing resources please visit IntroductionThis document sets out a scheme of work aimed to introduceupper Key Stage 2 pupils to the Python programming language.The scheme intends to familiarise pupils with the Pythonprogramming environment and syntax, and equip pupils with theskills and knowledge to write simple programs.It is anticipated that pupils will have had prior experience of codingusing a visual based programming language, such as Scratch orKodu, and that this is likely to be the first time they will code usinga scripting language. i.e. writing lines of code as opposed todragging blocks to build algorithms and programs. The examplebelow illustrates the difference between a visual programminglanguage and a scripting language.An if/else condition block in Scratch and the equivalent coding in PythonPedagogyThere are four lessons in this scheme of work followed by a finalproject. Lessons broadly follow a model in which skills andknowledge are taught using worked/modelled examples beforepupils tackle a ‘Coding Challenge’ requiring application of suchskills and knowledge. Whilst suggested ‘Coding Challenges’ havebeen presented here, I encourage those using this resource toalso generate coding challenges for pupils (or encourage pupils togenerate their own challenges), which may link into areas of pupils’topic work making the programming more relevant to pupils’ widerlearning. Similarly, whilst two suggestions have been made for thefinal project, it is anticipated that teachers using this resourcesmay choose to adapt these for their pupils or encourage pupils togenerate their own ideas for the final project programs they wish tocode.

For free Primary Computing resources please visit Whilst answers to ‘Coding Challenges’ are presented, it should benoted that there will often be different ways to program asuccessful solution, and pupils should be encouraged toexperiment and explore their own methods as opposed to beingfunnelled towards a predefined solution, since it is the journey ofexperimentation, trial and error that will facilitate learning.It is hoped class and school organisation will be such that pupilsare given the opportunity to tackle some challenges independentlyand others cooperatively, helping to develop pupils’ collaborativeskills as well as independent perseverance and resilience inproblem solving.Proposed Computing National Curriculum coverageThis scheme aims to cover the following objectives from theproposed Key Stage 2 National Curriculum for Computing. SpecificNC objectives appear at the beginning of each lesson. design and write programs that accomplish specific goals; solve problemsby decomposing them into smaller parts use sequence, selection, and repetition* in programs; work with variablesand various forms of** input and output; generate appropriate inputs andpredicted outputs to test programs use logical reasoning to explain how a simple algorithm works and todetect and correct errors in algorithms and programs* Note this scheme does not cover repetition (loops)** This scheme only covers one form of input/output (that of the program userentering data via a keyboard)Lesson OverviewLesson1.IntroducingPythonLesson objectives- Navigate Idle (create, save, runprograms)Understandandusemathematical operation and visualprogramminglanguage, syntax,Idle2. Variables andcomments-Declare a variablevariables, declare,comment-Write comments within Pythoncode-Use mathematical operations andprint statement with variables

For free Primary Computing resources please visit 3. User inputs- Use raw input() statement- Use input() statement- Print sentencesInput4. Selection andinequalities-Use conditional statements if, elseif (elif) and else-Use comparison operatorsconditionalstatement,comparisonoperator5. Final projectReinforcement and application ofskills and knowledge coveredaboveVocabularyA glossary of terms used throughout this SoW is included at theend of this document. Any term appearing in bold in lesson plansappears in the glossary and should be introduced to pupils usingthe definition provided.It is suggested that starters including matching words withdefinitions or code are used to help develop pupils’ knowledge ofthe technical terminology of coding and the syntax of Python.Code representation and line numberingWithin this document Python code to be written appears in adifferent font as shown below. Also note that lines of code arenumbered for ease of reference within the text however you donot include these numbers when coding. Code that would be onelong line in Python but which spans several lines when presentedhere does not start with a new number to indicate this. Finally,please note that Python reads the indentation of code so layout isimportant (i.e. in the example below ‘print’ is deliberately indented).1.num1 input(“Pleaseenteranumber”)2.num2 input(“Pleaseenterasecondnumber”)3.ifnum1 l resourcesAt the end of this document there are pupil resources to print toaccompany each lesson with the model code and CodingChallenges.

For free Primary Computing resources please visit Lesson 1: Introducing PythonL.Os:1. Navigate Idle (create, save, run programs)2. Understand and use mathematical operation and ‘print’statementN.C: usesequenceinprograms;Vocabulary: Python, scripting language, visual programminglanguage, syntax, IdleIntroducing Python and mathematical functions: Explain thatpupils will be using a language called Python to write programs.Recap that pupils will have had prior experience of writingprograms using Scratch, which is a visual programminglanguage as we built up programs by visually draggingprogramming blocks.Explain that Python is a scriptinglanguage, so instead of dragging blocks we have to write codeand we have to learn the language of Python’s code (called thesyntax) - just like we may learn languages such as French ofUrdu.Ask pupils to open Idle (which is on both Windows and Mac).Explain this is a program we use to write our Python codes.Introduce the following code to pupils:Mathematical Python code */Demonstrate that we can use this code to communicate in Pythonto work out numeracy calculations for us. For example, try writingthe following into Idle and press return after each. Give time forpupils to try using Python to complete calculations.300 400987-65312*930/6

For free Primary Computing resources please visit Hello World program: We are now going to move on to teachingpupils how to write a program to display text within Python. Whenrun, the text displayed will read ‘Hello World’, as traditionally this isthe first program anyone learns to write in a new programminglanguage!To do this (and from here onwards when programming in Python)we are not going to write our code in the ‘outer’ Idle window wehave just been using but rather from within Idle ask pupils to selectFile New Window to create a window which we can code intoand save. Show in the screenshots below. From this newwindow, pupils should click File Save and name the filehelloworld.py. Explain it is important to include the .py fileextension to indicate this is a Python file. Ask pupils to save the fileto an appropriate location.3 screenshots showing outer window of Idle (Python shell) then opening anew window to write programs into.Similarly to introducing the code for mathematical operationsabove, introduce pupils to the statement ‘print””’. Explain thatwhen using the statement ‘print””’ programs will display the textwith the quotation marks. So to create a ‘Hello World’ program askpupils to write the following code into the window. Note there is nocapital P for the statement print. This is important as Python is acase-sensitive language.1. print “Hello world!”Once pupils have written this line of code, explain that to run theirprogram they must first save the changes they have made to theirfile. Once they have done this, they must select ‘Run’ then ‘RunModule’.

For free Primary Computing resources please visit The window they have been coding in will close and their programwill run in the ‘outer’ Python window we used earlier. If they havetyped the code correctly their program will display the words ‘HelloWorld!’. They’ve now written their first Python program!Coding Challenge: Can pupils write a program which displaysmore lengthily text on different lines? For example:Hello, how are you today?I hope you are enjoying learning Python.What shall we code next?Challenge solution: Pupils need to use the ‘print’ statement oneach new line to display text over several lines. e.g. Remembernot to include the numbers1.print “Hello, how are you today?”2.print “I hope you are enjoying learning Python”3.print “What shall we code next?”

For free Primary Computing resources please visit Lesson 2: Variables and commentsL.Os:1. Declare a variable2. Write comments within Python code3. Use mathematical operations and print statement withvariablesN.C: design and write programs that accomplish specific goals; use sequence inprograms; work with variables and output; generate appropriate inputs andpredicted outputs to test programs; use logical reasoning to explain how a simplealgorithm works and to detect and correct errors in algorithms and programs.Vocabulary: variables, declare, commentIntroducing variables: Explain to pupils that variables may bethought of as boxes within our program where we can place data(numbers or text). Explain that we can then use the contents of theboxes within our program and that the values assigned to ourvariable (the contents of our boxes) may change as our programruns.We need to name ourvariable e.g. variable1A variable may bethough of as a boxwhere we place data foruse in our programse.g.‘Hello world’‘4.78’Explain that when we create a variable and assign data (get a boxand put something in it) it is called declaring a variable.Demonstrate that to declare a variable we use the following code:1.variable1 8Explain that in this example above we have created a variablecalled ‘variable1’ and assigned the value 8 to it. Demonstrate thatwe can create more than one variable and that we can assign textas well as numbers to variables by writing the following code:

For free Primary Computing resources please visit 1.name sarah2.laps 8Can pupils names the 2 variables you have created using the codeabove and their values? Explain here that naming variables withnames that relate to the data they hold (e.g. name & laps), asopposed to using generic terms such as variable1, makessubsequently writing code using these variables easier.Ask pupils to now create a new Python file: i.e. open Idle, selectFile New window then Save as, and call the file variables.py andsave to an appropriate location.Ask pupils to create a variable called ‘children’ with a value of 30and a variable called ‘sweets’ with a value of 5. Explain that we willbe using these variables to write a program that calculates the totalnumber of sweets required to give a defined number of sweets to adefined number of children.Solution code:1.children 302.sweets 5Comments in Python: Explain that a comment is a line of codethat isn’t part of the program but is there to explain to theprogrammer what parts of the code are doing. We write commentsin regular clear English (or whatever language we as programmersmay speak) as opposed to the language of Python. Explain thatcomments are important as when we write longer pieces of codewe may forget the function of different parts, or we may work oncode collaboratively with others and therefore we need to explainwhat we are doing.However, explain that the trouble with writing in regular Englishwithin our program is that our computer thinks we are still writing inPython and may try and interpret commands from what we havesaid. As such, we need to indicate when we are writing a commentand we do that by using ‘#’. Explain that the computer will ignoreany line starting with # as it knows this is a comment to theprogrammer and not part of the code. Demonstrate addingcomments to the variable we just created to add explanation aboutwhat we are coding and ask the children to do the same i.e.

For free Primary Computing resources please visit 1.children 302.#The number of children in the class3.sweets 54.#The number of sweets each child will getUsing mathematical operations and print statement withvariables: Explain we are now going to continue coding to write aprogram that uses the variables we have just declared as well asthe mathematical operations and print statement we covered in theprevious lesson.Recap that the purpose of this program was to work out the totalnumber of sweets required based on the two variables we havedeclared: the number of children and the number of sweets theyget each.To write the program to calculate this, add the code beneath ourvariables and comments that appears on line 4&5 below and ishighlighted for clarity – and ask pupils to add the same to theirprogram.1.children 302.#The number of children in the classsweets 53.#The number of sweets each child will get4. total children*sweets5. print totalTake a moment to discuss with pupils lines 4&5 of code above.Can children spot a new variable being declared? What is thename of this new variable? Can children spot the mathematicaloperator? What mathematical operation is it? (Note the scope hereto reinforce numeracy problem solving objectives on choice ofoperation) Can pupils see the print statement we used in lesson 1?Note – we don’t need to use quotation marks when requesting toprint a variable.What do pupils anticipate the output