Programming with Python
Total Page:16
File Type:pdf, Size:1020Kb
COURSE MATERIAL Programming with Python The Open University of Sri Lanka Department of Electrical and Computer Engineering Copyright This course has been developed as part of the collaborative advanced ICT course development project of the Commonwealth of Learning (COL). COL is an intergovernmental organisation created by Commonwealth Heads of Government to promote the development and sharing of open learning and distance education knowledge, resources and technologies. The Open University of Sri Lanka (OUSL) is the premier Open and Distance learning institution in the country where students can pursue their studies through Open and Distance Learning (ODL) methodologies. Degrees awarded by OUSL are treated as equivalent to the degrees awarded by other national universities in Sri Lanka by the University Grants Commission of Sri Lanka. The Python Software Foundation is owner of Python. Copyright © 2001-2019 Python Software Foundation; All Rights Reserved. Reference: https://docs.python.org/ Kivy is an open source Python library for development of applications. Reference: https://kivy.org/ This Programming with Python is a teaching learning resource developed by the Open University of Sri Lanka using various sources duly acknowledged. © Open University of Sri Lanka and Commonwealth of Learning, 2018. Except where otherwise noted, The Programming with Python course material is made available under Creative Commons AttributionShareAlike 4.0 International (CC BY-SA 4.0) License: https://creativecommons.org/licenses/by-sa/4.0/legalcode. For the avoidance of doubt, by applying this license the Commonwealth of Learning does not waive any privileges or immunities from claims that it may be entitled to assert, nor does the Commonwealth of Learning submit itself to the jurisdiction, courts, legal processes or laws of any jurisdiction. The ideas and opinions expressed in this publication are those of the author/s; they are not necessarily those of Commonwealth of Learning and do not commit the organisation. Commonwealth of Learning The Open University of Sri Lanka 4710 Kingsway, Suite 2500, Burnaby P. O. Box 10250, V5H 4M2, Nawala, British Columbia, Nugegoda, Canada Sri Lanka Phone: +1 604 775 8200 Phone: +94 112881481 Fax: +1 604 775 8210 Fax: +94112821285 Email: [email protected] Email: [email protected] Website: www.col.org Website: www.ou.ac.lk 4 Copyright Acknowledgements The Open University of Sri Lanka (OUSL), Department of Electrical and Computer Engineering (ECE) wishes to thank those below for their contribution to this course material and accompanying videos and screencasts: Authors: WASN Perera (Lecturer, Dept. of ECE, OUSL) Units 1 and 2 AP Madurapperuma (Senior Lecturer, Dept. of ECE, OUSL) Unit 3 PTR Dabare (Lecturer, Dept. of Mechanical Eng, OUSL) Unit 4 HUW Ratnayake (Senior Lecturer, Dept. of ECE, OUSL) Unit 5 BK Werapitiya (Lecturer, Dept. of ECE, OUSL) Units 6 and 7 S Rajasingham (Lecturer, Dept. of ECE, OUSL) Units 8,9 and 10 NT De Silva (Project Assistant, Dept. of ECE, OUSL) Unit 11 GSN Meedin (Lecturer, Dept. of ECE, OUSL) Units 12 and 13 MHMND Herath (Lecturer, Dept. of ECE, OUSL) Units 14 and 15 Content Editor DN Koggalahewa (Senior Lecturer, Sri Lanka Institue of IT) Language Editor: KARD Gunaratne (Lecturer, Dept. of ECE, OUSL) Reviewer: KARD Gunaratne (Lecturer, Dept. of ECE, OUSL) Video presenters: S Rajasingham (Lecturer, Dept. of ECE, OUSL) M Abeykoon (Software Engineer, Duo Software) SN Muhandiram (Associate Software Engineer, VizuaMatix ) Screencasters: MHMND Herath (Lecturer, Dept. of ECE, OUSL) M Abeykoon (Software Engineer, Duo Software) Programming with Python Contents About this Course Material 9 How this Course Material is structured ............................................................................ 9 Course overview 11 Welcome to Programming with Python ......................................................................... 11 Programming with Python—is this course for you? ...................................................... 11 Video 1: Introduction to the course ‘Programming with Python’ ................................. 11 Course objectives ............................................................................................................ 11 Course outcomes ............................................................................................................ 12 Timeframe ...................................................................................................................... 12 Study skills ..................................................................................................................... 13 Need help? ...................................................................................................................... 14 Assignments ................................................................................................................... 14 Assessments .................................................................................................................... 14 Getting around this Course Material 15 Margin icons ................................................................................................................... 15 Unit 1 17 Introduction to Basic Programming Concepts with Python ........................................... 17 Introduction ........................................................................................................... 17 1.1 Need for programming languages ................................................................... 18 1.2 Programming languages .................................................................................. 18 1.3 Examples of Programming languages ............................................................ 19 Activity ........................................................................................................................... 21 1.4 What is programming? .................................................................................... 21 1.5 Getting to know Python .................................................................................. 27 Activity ........................................................................................................................... 27 1.6 Features of Python Programming ............................................................................. 28 1.7 Download, installation and run the first program with Python ....................... 28 Video 2: How to download and install Python .............................................................. 28 Activity ........................................................................................................................... 32 1.8 Python for Mobile App Development ............................................................. 32 Unit summary ................................................................................................................. 32 References and Further Reading..................................................................................... 33 Atribution ....................................................................................................................... 33 Unit 2 35 Variables, Expressions and Statements .......................................................................... 35 Introduction ........................................................................................................... 35 2.1 Data types and their values ............................................................................. 35 2.2 Variables in Python ......................................................................................... 36 4 Contents 2.3 Differenciating variable names and keywords ................................................ 37 2.4 Operators, Operands and Expressions ............................................................ 38 2.5 Interactive mode and script mode ................................................................... 39 Video 3: Data Types in Python ...................................................................................... 40 Activity ........................................................................................................................... 40 2.6 Order of operations ......................................................................................... 40 2.7 Comments in Programs ................................................................................... 42 Activity ........................................................................................................................... 43 Activity ........................................................................................................................... 44 Unit summary ................................................................................................................. 44 References and Further Reading..................................................................................... 44 Unit 3 45 Control Structures, Data structures-and Linked lists, queues ......................................... 45 Introduction ........................................................................................................... 45 3.1 Selection Structure .......................................................................................... 46 3.2 Iteration structures .......................................................................................... 47 Video 4: Control Structures in Python .........................................................................