Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types....

28
Python Programming Dr Diarmuid Ó Briain

Transcript of Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types....

Page 1: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Python Programming

Dr Diarmuid Ó Briain

Page 2: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Learning Outcomes

● Knowledge – Demonstrate an understanding of programming concepts and their application in the Python language.

● Kind – Demonstrate knowledge of how to apply programming concepts to solve problems of data retrieval, processing, and visualisation.

● Skills – Demonstrate ability to write, build and troubleshoot Python programs that include network connections, databases, RESTful API, access to filesystems and to understand and build and troubleshoot basic programs using the object oriented paradigm.

● Context – To apply knowledge and skills learnt to practice through a capstone project that simulates real-world programming challenges.

● Competence – Be able to develop reusable Python code blocks and programs that solve an overall problem through the use of appropriate data structures and algorithms.

Page 3: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Course Overview

● History.● Installing & Running Python.● Names & Assignment.● Sequences types.● Iteration.● Functions.● Generators.● Regular Expressions.● Arrays.

● Data-frames.● Files & Databases.● Handling Errors.● Networking Python.● OOP.● Decorators.● GUI.● Flask and Jinja2.● Capstone Project.

Page 4: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Brief History of Python

● Invented in the Netherlands, early 90s by Guido van Rossum.

● Named after Monty Python.● Open sourced from the beginning.● Considered a scripting language, but is much

more.● Scalable, object oriented and functional from

the beginning.● Used by Google from the beginning.● Increasingly popular.

Page 5: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Python’s Benevolent Dictator For Life

“Python is an experiment in how much freedom programmers need. Too much freedom and nobody can read another's code; too little and expressiveness is endangered.” - Guido van Rossum

Page 6: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

http://docs.python.org/

Page 7: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

The Python tutorial is good!

Page 8: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Cloud TestbedExercise 1

Page 9: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Linode testbed

sshsftpscprdpvnc

● ssh – shell● sftp/scp – file transfer● rdp – graphical window

● Follow the instructions in module 1 to build your testbed.

Page 10: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Python3 cloud testbed

Page 11: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

RunningPython

Page 12: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

The Python Interpreters, python2, python3

● Typical Python implementations offer both an interpreter and compiler

● Interactive interface to Python with a read-eval-print loop

$ python2Python 2.7.16 (default, Apr 6 2019, 01:42:57) [GCC 8.3.0] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> def square(x):... return x * x... >>> [square(x) for x in [1,2,3,4]][1, 4, 9, 16]>>>

$ python3Python 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.3.0] on linuxType "help", "copyright", "credits" or "license" for more information.>>> def square(x):... return x * x... >>> [square(x) for x in [1,2,3,4]][1, 4, 9, 16]>>>

Page 13: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Installing

● Python is pre-installed on most GNU/Linux, UNIX and MAC OS X.

● The pre-installed version may not be the most recent– Python 2.7.17 (7 Nov 2019)– Python 3.7.5 (20 Nov 2019)

● Download from http://python.org/download/● Python comes with a large library of standard modules● There are several options for an IDE

– IDLE – works well with Windows– Most GNU/Linux editors, gedit, emacs, etc...– Eclipse with Pydev (http://pydev.sourceforge.net/)

Page 14: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

IDLE Development Environment

● IDLE Integrated DeveLopment Environment (IDLE) for Python, typically used on Windows (Can be used on GNU/Linux also).– idle, idle3

● Multi-window text editor with syntax highlighting, auto-completion, smart indent and other.

● Python shell with syntax highlighting.● Integrated debugger with stepping, persistent

breakpoints, and call stack visibility.

Page 15: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

IDLE Development Environment

● IDLE

● IDLE3

Page 16: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Editing Python in gedit

$ chmod +x hello_world.py $ ./hello_world.py Hello world

Page 17: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Running Interactively on GNU/Linux and UNIX

● On GNU/Linux and UNIX

$ python3

>>> 3 + 3

6

● Python prompts with ‘>>>’. ● To exit Python (not Idle):

– In GNU/Linux or UNIX, type CONTROL-D– In Windows, type CONTROL-Z + <Enter>– or >>> exit()

Page 18: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Running Programs on GNU/Linux and UNIX

● Call python program via the python interpreter

$ python my_program.py

● Make a python file directly executable by – Adding the appropriate path to your python

interpreter as the first line of your file (shebang line)

#!/usr/bin/env python3

#!/usr/bin/python3– Making the file executable

$ chmod a+x my_program.py– Invoking file from Unix command line

$ ./my_program.py

Page 19: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Example ‘script’: factorial.py

$ chmod a+x factorial.py $ ./factorial.py

N fact(N)---------0 11 12 23 64 245 1206 7207 50408 403209 362880

Page 20: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Python Scripts

● When you call a python program from the command line the interpreter evaluates each expression in the file.

● Familiar mechanisms are used to provide command line arguments and/or redirect input and output.

● Python also has mechanisms to allow a python program to act both as a script and as a module to be imported and used by another python program.

Page 21: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Example of a Script

$ ./RE-script.py < email.txt [email protected]@[email protected]@[email protected]

$ cat email.txt Electrical [[email protected]] on behalf of Rose Assumiwe [[email protected]][Reply] [Reply All] [Forward]ActionsTo: [email protected] Cc: Electrical Engineering Dept. Staff List [[email protected]] Attachments: (4)Download all attachmentsFinal Year Project Present~1.pdf (148 KB)[Open as Web Page]; FinalProjectPresentations~1.docx (16 KB)[Open as Web Page]; Final Project Report Templ~1.pdf (31 KB)[Open as Web Page]; ATT00001.txt (254 B) 05 May 2016 16:51Dear Students,

As we prepare for the 2015/2016 Final Project Presentations, we need an indication from the main project supervisor on your readiness for presentation. In addition, the Proposal Record Form needs to indicatethe correct status of your project.

Attached kindly find a clearance form that you need to get signed by your main supervisor indicating your readiness (or not) to present during the final project presentations which are scheduled for 26 – 27 May 2016 as follows:Thursday 26 May 2016: Computer Engineering (and optional EE/TE) project presentationsFriday 27 May 2016: Electrical Engineering and Telecommunication Engineering project presentations EE/TE students that are available/ready to present on Thursday should indicate so on their clearance forms. Such allocations will be made on a case-by-case basis.

A note to flag the requirement in your Clearance Process to include an abstract of your work. You will need to provide the abstract both within the Clearance Form and with the Proposal Record Form too. The Abstract field is the last entry among the fields you need to complete to ensure your proposal record is up-to-date.

The completed clearance forms may be submitted electronically to [email protected] or dropped off at my office, Room 3013.

Clearance forms are due by Wednesday 18 May 2016, in order to enable us circulate the presentation timetable in good time. For your information, kindly also find attached some guidelines as you prepare for the project presentations as well as guidelines for your project reports.

Happy to answer queries or provide clarification as may be required.

Regards,

Rose

-- Eng. Dr. Rose AssumiweSenior Lecturer, Department of Electrical and Computer Engineering /Researcher, netLabs!UG (formerly Community Wireless Resource Centre)President, Uganda Institution of Professional Engineers (UIPE)Room 3013 (New CEDAT Building)School of EngineeringCollege of Engineering, Design, Art and Technology (CEDAT)Makerere UniversityP.O. Box 7062Kampala, UgandaTel: +256-41-5551055/5553144Fax: +256-41-5553166

Page 22: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Getting a unique, sorted list

$ ./unique_sorted_list.py < email.txt [email protected]@[email protected]@[email protected]

Page 23: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Simple functions

● exmod.py called by execute-exmod.py● exmod.py imported by execute-exmod.py

– import exmod

● Python automatically compiles a script to compiled byte code, before running it. ● When a module is imported for the first time a .pyc file containing the compiled code will

usually be created in the same directory as the .py file. ● Note the exmod.cpython-34.pyc file created in the __pycache__ directory.

Page 24: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Where do modules go ?

● Modules should be placed on the python path

>>> import sys>>> sys.path['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/home/ubuntu/.local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/dist-packages', '/usr/lib/python3/dist-packages']

Page 25: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Getting help on functions in a module

● Getting help on functions in modules>>> import exmod>>> help(exmod)Help on module exmod:

NAME exmod - factorial done recursively and iteratively

FUNCTIONS fact1(n) First factorial function fact2(n) Second factorial function

FILE /usr/lib/python3/dist-packages/exmod.py

Page 26: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Running 5-execute-exmod.py

$ .execute-exmod.py 1296

788657867364790503552363213932185062295135977687173263294742533244359449963403342920304284011984623904177212138919638830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853277524242407573903240321257405579568660226031904170324062351700858796178922222789623703897374720000000000000000000000000000000000000000000000000

<function fact1 at 0x7fca6dc048c8>

Page 27: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Exercise 2

Page 28: Python Programming€¦ · Installing & Running Python. Names & Assignment. Sequences types. Iteration. Functions. Generators. Regular Expressions. Arrays. Data-frames. Files & Databases.

Thank you