Learning to Program with Python
Total Page:16
File Type:pdf, Size:1020Kb
Learning to Program with Python An introduction to the Python programming language for those who haven’t programmed before Version 2020-01 Learning to program with Python 2 Licence This manual is © 2020, Steven Wingett & Simon Andrews. This manual is distributed under the creative commons Attribution-Non-Commercial-Share Alike 2.0 licence. This means that you are free: to copy, distribute, display, and perform the work to make derivative works Under the following conditions: Attribution. You must give the original author credit. Non-Commercial. You may not use this work for commercial purposes. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a licence identical to this one. Please note that: For any reuse or distribution, you must make clear to others the licence terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author's moral rights. Full details of this licence can be found at http://creativecommons.org/licenses/by-nc-sa/2.0/uk/legalcode Learning to program with Python 3 Table of Contents Licence ................................................................................................................................................... 2 Table of Contents .................................................................................................................................. 3 Introduction ........................................................................................................................................... 6 Why learn Python? .............................................................................................................................. 6 What this course covers ...................................................................................................................... 6 Python 2 or Python 3? ......................................................................................................................... 7 A note about learning a programming language ................................................................................. 7 Chapter 1: getting started with Python ............................................................................................... 8 How to install Python ........................................................................................................................... 8 On Linux/Unix/MacOSX etc. ............................................................................................................ 8 On Windows ..................................................................................................................................... 8 What do I use to write Python code? ................................................................................................... 9 Thonny ............................................................................................................................................... 10 Hello World! and getting started with Python .................................................................................... 11 Chapter 2: data types and expressions ............................................................................................ 12 Integers (int) ...................................................................................................................................... 12 Floats ................................................................................................................................................. 13 Booleans (bool) ................................................................................................................................. 14 Strings (str) ........................................................................................................................................ 16 Operations on strings ..................................................................................................................... 17 Compound expressions ..................................................................................................................... 20 Chapter 3: names, functions and methods ...................................................................................... 21 Writing scripts using Thonny ............................................................................................................. 21 Names ............................................................................................................................................... 22 F-strings ............................................................................................................................................. 24 Functions ........................................................................................................................................... 25 Built-in functions ............................................................................................................................. 25 Functions and data types ............................................................................................................... 27 Creating Functions ......................................................................................................................... 28 Methods ............................................................................................................................................. 33 Chapter 4 – Collections ...................................................................................................................... 34 Sets.................................................................................................................................................... 34 Sequences ......................................................................................................................................... 36 Ranges ........................................................................................................................................... 36 Tuples ............................................................................................................................................ 37 Lists ................................................................................................................................................... 38 Mappings ........................................................................................................................................... 40 Dictionaries .................................................................................................................................... 40 Streams ............................................................................................................................................. 42 Learning to program with Python 4 Files ................................................................................................................................................ 42 Generators ..................................................................................................................................... 43 Copying Collections ........................................................................................................................... 43 Further points on manipulating collections with functions and methods ........................................... 45 The key parameter ......................................................................................................................... 45 Key expressions ............................................................................................................................. 45 Anonymous functions (the lambda function).................................................................................. 46 Chapter 5 – Conditionals, loops and iterations ............................................................................... 47 Conditionals ....................................................................................................................................... 47 Simple, one-alternative and multi-test conditionals ....................................................................... 47 Loops ................................................................................................................................................. 48 While Loops ................................................................................................................................... 48 Processing file input using a loop .................................................................................................. 50 Iterations ............................................................................................................................................ 50 Simple Iteration .............................................................................................................................. 50 Iterating over a file ......................................................................................................................... 51 Iterating over a dictionary............................................................................................................... 51 Enumerating iterations ................................................................................................................... 51 Iterating over ranges .....................................................................................................................