JE Python 2020
Total Page:16
File Type:pdf, Size:1020Kb
Python Programming A Manual for Teachers of Leaving Certificate Computer Science Please cite as: PDST, Leaving Certificate Computer Science, Python Workshop, Dublin, 2020 Python Programming: A Manual for Teachers of Leaving Certificate Computer Science Page ii Table of Contents Section 1 – Getting Started 1 Hello World ……………………………………………………………………… 2 Language Syntax ……………………………………………………………..... 3 Basic Python Syntax …………………………………………………………… 5 Escape Sequences …………………………………………………………….. 8 Flow of Control ………………………………………………………………….. 10 Programming Exercises ……………………………………………………….. 12 BREAKOUT ACTIVITIES …………………………………………………… 13 Section 2 – Data, Variables, Assignments and Expressions 23 Introduction …………………………………………………………………….. 24 Variable Syntax ………………………………………………………………… 26 Datatypes and Operations ……………………………………………………. 29 Program Tracing ……………………………………………………………….. 31 Input-Process-Output ………………………………………………………….. 34 More Built in Functions ………………………………………………………… 40 The Remainder Operator (%) …………………………………………………. 41 Programming Exercises ……………………………………………………….. 43 Running Totals ………………………………………………………………….. 45 Introducing Random Numbers ………………………………………………… 47 Additional Notes ………………………………………………………………… 49 BREAKOUT ACTIVITIES ……………………………………………………… 51 Section 3 – Strings 63 Introduction ……………………………………………………………………… 64 String Indexing ………………………………………………………………….. 66 String Slicing ……………………………………………………………………. 70 String Addition and Multiplication …………………………………………….. 71 String Formatting ………………………………………………………………. 74 Built-in String Commands …………………………………………………….. 76 Coding Systems ……………………………………………………………….. 77 Python Programming: A Manual for Teachers of Leaving Certificate Computer Science Page iii Programming Exercises ……………………………………………………….. 79 String Methods ………………………………………………………………….. 80 Additional Notes (Sequences) ………………………………………………… 83 BREAKOUT ACTIVITIES ……………………………………………………… 85 Section 4 – Lists 95 Introduction ……………………………………………………………………… 96 Creating Lists …………………………………………………………………… 97 Common List Operations ………………………………………………………. 99 List Indexing ……………………………………………………………………. 101 List Slicing ……………………………………………………………………….. 105 List Methods …………………………………………………………………….. 109 Two More String Methods (split and splitlines) …………………….. 111 BREAKOUT ACTIVITIES ……………………………………………………… 113 Section 5 – Programming Logic 120 Introduction ……………………………………………………………………… 121 Hangman! ……………………………………………………………………….. 122 Boolean Expressions …………………………………………………………… 124 The Guessing Game …………………………………………………………… 128 Selection (if, else, elif) ……………………………………………… 129 BREAKOUT ACTIVITIES (selection) ……………………………………….. 138 Iteration (for, while) ………………………………………………………... 141 BREAKOUT ACTIVITIES (iteration) 160 Section 6 – Modular Programming Using Functions 168 Introduction ……………………………………………………………………… 169 Basic Function Syntax …………………………………………………………. 177 Function Parameters and Arguments ………………………………………… 182 Function Return Values ……………...………………………………………… 187 Examples and Exercises …………...………………………………………….. 190 Boolean Functions ……………...………………………………………………. 194 Python Programming: A Manual for Teachers of Leaving Certificate Computer Science Page iv Using Functions to Validate Data ……………………………………………... 199 Programming Exercises 6.1 ….……...………………………………………... 201 Recursion ……………...………………………………………………………… 205 Programming Exercises 6.2 ………………...…………………………………. 209 Programming Exercises 6.3 ………………...…………………………………. 210 BREAKOUT ACTIVITY 6.1 (ATM System) ………………………………….. 214 BREAKOUT ACTIVITY 6.2 (Summing Numbers) …………………………... 224 BREAKOUT ACTIVITY 6.3 (Turtle Graphics) ……………………………….. 228 BREAKOUT ACTIVITY 6.4 (Check Digits) …………………………………... 234 Section 7 – Dictionaries 242 Introduction ……………………………………………………………………… 243 Dictionary Definitions – some examples ……………………………………... 244 Creating Dictionaries – syntax ………………………………………………… 247 Indexing Dictionaries …………………………………………………………… 249 Adding, Changing and Deleting Dictionary Elements ………………………. 255 Programming Exercises 7.1 .………...………………………………………... 263 Iterating over dictionaries ……………………………………………………… 268 Dictionaries and Lists …………………………………………………………... 271 BREAKOUT ACTIVITY 7.1 (Frequency Counters) …………………………. 273 Appendices 280 Appendix A: Python Keywords ………………………………………………... 281 Appendix B: Python Built-in Functions ……………………………………….. 281 Appendix C: Python Assignment Operators …………………………………. 282 Appendix D: Python Arithmetic Operators …………………………………… 282 Appendix E: Python Relational Operators …………………………………… 282 Appendix F: Truth Tables for not, and, and or ………………………………. 283 Appendix G: Sample Solutions to Selected Problems ……………………… 284 Python Programming: A Manual for Teachers of Leaving Certificate Computer Science Page v Manual Overview The purpose of this manual is to provide Leaving Certificate Computer Science (LCCS) teachers with an enhanced knowledge of the Python programming language thereby enabling them to independently improve their own programming skills. Although the manual will serve as support material for teachers who attend the Python Skills Workshops which are a key component in our two-year CPD programme for LCCS teachers, it is envisaged that its real value will only come into play in the weeks and months after the workshops have been delivered. Beyond these workshops, the manual may be used as a basic language reference for Python, but more importantly, as a teaching resource that might be used to promote in teachers, a constructivist pedagogic orientation towards the planning and teaching of Python in the LCCS classroom. The manual itself is divided into seven sections as follows: - sections 1 and 2 cover basic, beginner programming concepts such as program execution, flow of control, simple datatypes, variables, assignments and expressions - sections 3 and 4 cover the two datatypes - strings and lists – and associated operations - section 5 covers programming logic – specifically, Boolean expressions, selection and iteration - sections 6 and 7 cover more advanced Python programming topics such as functions and dictionaries Of course there is much more to Python than the material covered in this manual. Among the more notable topics that are not explicitly addressed are, object-oriented programming and classes, list comprehensions and exceptions. That said, every effort has been made to ensure that the content contained here is adequate in order to mediate LCCS in the classroom. Throughout the manual there are lots of examples and related exercises. Readers will find it helpful if they read (and try) the examples before attempting the exercises. The source code from most of the examples are available to download from the PDST GitHub repository (https://github.com/pdst-lccs/lccs-python) and links to sample solutions for many of the programming exercises as are available at the end of the manual. Python Programming: A Manual for Teachers of Leaving Certificate Computer Science Page vi A high-level overview of the content of this manual is presented below. Section 1 – Getting Started The aim of this section is to get readers up and running. By the end of this section participants should have a basic understanding of program execution, sequential processing, strings, escape sequences and the importance of language syntax. Participants will have the opportunity to write and modify simple programs. The section concludes with a practical lab/breakout session which introduces turtle graphics and games programming using the pygame library. Section 2 – Data, Variables, Assignments and Expressions This section will provide participants with a broad overview of the use of variables, assignments and expressions. Participants will have the opportunity to learn how to initialise simple variables as well as use an assignment statement to change its value. The importance of data and datatype is made clear. Arithmetic expressions and the input command are introduced. Participants learn how to apply these concepts through program tracing, testing, the use of the remainder (modulus) operator, running totals and random numbers. The section concludes with a practical lab/breakout session which as well as building on some of the ‘projects’ started in Section 1, introduces some basic file i/o operations. Section 3 – Strings In this section we will cover strings – basic sequence operations such as concatenation, multiplication, indexing and slicing will be explained. Example programs will extend thinking on coding systems and ciphers, and draw on the use the use of built-in functions - ord and chr. String specific methods and formatting will also be explained. Participants will be given a hands-on tour of the official online Python reference at https://docs.python.org/3/. The section concludes with a practical lab/breakout session where participants will be given an opportunity to write programs to generate web pages and analyse text from live RSS feeds. Section 4 –Lists The aim of this section is to extend participants knowledge of sequences through the concept of lists. Motivation is provided through a discussion on the many real-world applications of lists. List construction, indexing and slicing are explored in greater detail. The section describes the most common list specific methods and how to use them. Examples of how to use split and splitlines to generate lists are provided. The breakout activities Python Programming: A Manual for Teachers of Leaving Certificate Computer Science Page vii at the end of this section includes the use of lists to construct random sentences, further statistical analysis of data read