PYTHON PROGRAMMING
Site owner: Samy Zafrany
"A programming language that doesn't change the way you think is not worth learning."
-- Alan Perlis, Epigrams on Programming
Quick Overviews
-
An Incremental Introduction to Python
Ralph Noack, University of Alabama at Birmingham -
Python Quick Tutorial
Sven H. Chilton, University of California - Berkeley - David M. Beazley - Advanced Python Programming
-
Python Tutorials
tutorialpoint.com -
Python's Class Development Toolkit
Great video introduction to class development in Python
Advanced Tutorials
-
Code Like a Pythonista: Idiomatic Python
By David Goodger, licensed under a Creative Commons Attribution/Share-Alike (BY-SA) license -
Official Python Tutorial (www.python.org)
By Guido Van Rossum - MIT (Massachusettes Institute of Technology) Introduction to Computer Science based on Python
- Python Alternative Execution: Passcall Advanced Technologies
Free Books
-
Think Python: How to Think Like a Computer Scientist
Version 2.0.5, December 2012
Allen Downey, Green Tea Press -
A byte of Python
Free book by: Swaroop C H, 19 Aug 2013 -
Dive Into Python
Mark Pilgrim, Apress -
Fast Lane to Python: A quick, sensible route to the joys of Python coding
Norm Matloff, University of California, Davis -
The Little Book of Semaphores
Allen Downey, Green Tea Press
Links
- Python Programming Language - Official Website
- The Python Wiki
- ANACONDA - Enterprise-ready Python distribution for large-scale data processing, predictive analytics, and scientific computing.
- ENTHOUGHT - Scientific Computing Solutions
- 4542 Python Program Examples to learn from and use
- The Python Module of the Week - Many Cool examples for Pythons Standard Library Modules
-
Bjarne Stroustrup: The 5 Programming Languages You Need to Know
Bjarne Stroustrup is the inventor of C++ -
Scripting: Higher Level Programming for the 21st Century
John Ousterhout, IEEE Magazine, 1998 - Why did Google choose Python?
- 6 Lessons From Dropbox - One Million Files Saved Every 15 Minutes
- How Dropbox Did It and How Python Helped at PyCon 2011
- Guido Van Rossum: Python Creator Scripts Inside Google
- Strong versus Weak Typing A Conversation with Guido van Rossum
- Guido van Rossum on the History of Python (YouTube Video)
-
NumPy
A fundamental package for scientific computing with Python. -
SciPy
An open-source software for mathematics, science, and engineering. The SciPy library depends on NumPy, which provides convenient and fast N-dimensional array manipulation. NumPy and SciPy are easy to use, but powerful enough to be depended upon by some of the world's leading scientists and engineers. -
David Beazly Youtube Channel
Dave Beazly is a frequent conference presenter and tutorial instructor. Find videos and slides above. - Python Programming Tutorial
- Monty Python's Flying Circus: Parrot Sketch
SOFTWARE
- We will be using Python version 2.7.5. Beware that Python versions 3.x.x are not backward compatible and should not be used for our courses!
-
There are two installation methods:
[1] DESKTOP: on a local disk of the computer
[2] MOBILE: on a flash drive (disconkey)
If you choose to use both, you will also have to install every package twice (once for your local pc, and second for your disconkey) - The executables below are for Microsoft Windows 32bit os (so they will also work for Windows 64bit).
ANACONDA - Python Bundle for Desktop (Installer)
This is a large bundle that containse the core Python 2.7.5 system and a large collection of modules and packages. To be installed on a local disk of your desktop (or laptop).- Recommended installation path: "C:\Anaconda" !
- Make sure to check that Anacondat will be your default Python installation !
- Before you start, make sure to uninstall old Python versions from your pc! This includes removing all Python keys from your Windows Registry (use the regedit command to search and delete all Python keys)
Portable Python (for disconkey)
- This is a full self contained Python environment that can be installed on a flash drive (disconkey) and carried everywhere.
- Can be used almost on any windows computer, anywhere, by simply inserting your disconkey to the computer, and a mouse click.
- Contains a full Python with libraries. Additional libraries can be later installed.
- Suppose it is installed on drive H:\PortablePython_2.7.5.1
-
Then you can run the main programs from the drive:
- PyScripter - H:\PortablePython_2.7.5.1\PyScripter-Portable.exe
- IPython - H:\PortablePython_2.7.5.1\IPython-Portable.exe
IDE's - Integrated Development Environments for Python
There are more than 40 Python integrated development environments, some of them are commercial and require usage license. We list here only 7 popular IDE's that we think are most suitable for our course. But most of the time we will use the PyScripter IDE and IPython console, but students are encouraged to also try PyDev or Microsoft PTVS extension (requires Visual Studio license).-
PyScripter
- A simple free Python IDE (Integrated Development Environment) for Windows only.
- It is included in the Portable Python package, but is not included in ANACONDA. So to use it on your local PC, Click on the PyScripter title to install it on your hard drive.
- PyScripter setup assumes that ANACONDA (or basic Python 2.7.5) is already installed on the local disk.
- Make sure to install PyScripter in directory C:\PyScripter (and not in "C:\Program Files")
-
IPython
- This is the most recommended Python console which is very useful for testing and experimenting.
-
IPython video tutorial at PyCon 2013:
IPython in-depth: high-productivity interactive and parallel python.
This is a must see video to understand how to use the IPython console (which we will be using a lot in class and laboratory!) - IPython is included in ANACONDA and also in Portable-Python and can be invoked from the command line by the command: ipython.
-
PyCharm
One of the most popular IDE's, now has a free community version: https://www.jetbrains.com/pycharm/download/ .
Highly recommended!
JetBrains PyCharm — Python IDE with complete set of tools for productive development with Python programming language. In addition, the IDE provides high-class capabilities for professional Web development with Django framework. -
PyDev a Professional Python IDE for Eclipse
PyDev is a Python IDE for Eclipse, which may be used in Python, Jython and IronPython development. It is considered to be one of the best Python IDE today, but it requires a not simple installation process. You must install, Java JDK, Eclipse (Java standard IDE), and then add Python components on top of it. If you are serious about Python Programming then visit the Install - PyDev Home Page and try installing it on your PC. It is recommended to- instal JAVA under "C:\Java"
- install JDK (Java Development Kit) and JRE (Java runtime environment libraries) under "C:\Java\jdk*" and "C:\Java\jre*"
- install Eclipse under "C:\eclipse".
- install the Python extensions as described in PyDev.org
-
Microsoft Python Tools for Visual Studio (PTVS)
This is a professional and commercial Microsoft IDE extension to its successful Visual Studio IDE. Note that you must have a valid Visual Studio License in order to be able to install it. The other limitation is that it is not available for Linux/Mac/Android platforms. -
Wingware's Python IDE - a Professional Integrated Development Environment designed specifically
for the Python programming language
This is a professional commercial IDE, but special low prices are offered for students. It is considered as one of the best ones in the market. Please visit their web site - they seem to offer free licenses for classroom usage for students. (make sure to download the professional 4.1 version)
"Serious Python developers should take a serious look at Wing IDE" -- Doctor Dobb's Journal - For more information and comparisons between Python IDE's click here.
EDITORS
In many circumstances it is preferable to write Python scripts on a simple text editor that supports Python syntax highlighting and name completions. Here are two popular choices:-
Notepad++
Notepad++ is a free (as in "free speech" and also as in "free beer") source code editor and Notepad replacement that supports several languages. Running in the MS Windows environment, its use is governed by GPL License (which means it is free to use) You must however configure it to run with Python.
To configure Notepad++ for Python use the following link:
How to configure Notepad++ for Python? -
Notepad++ Portable
This is a portable version of the Notepad++ editore that you can carry on your disconkey. You can configure it exactly as Notepad++ as described above -
Komodo Edit
To get code auto-completion you need to configure Komodo by going to:Edit -> Preferences -> Languages -> Python
Make sure to define the "Default Python Interpreter" as "C:\anaconda\python.exe". You can add more import directories.
To run Python program, goto the "Tools" -> "Run" menu button and type the following text in the "Run" textbox:%(python) "%F"
It takes some time to build the auto-completion database, but this is only a one time operation. -
VIM as a Python IDE - Martin Brochhaus
This is for the brave and adventurous. It requires a lot of investment and patience in the beginning, but in the long run this investment pays off. Here is a link to a video which was published on Jun 11, 2012: "Vim as a Python IDE - Martin Brochhaus", PyCon Asia Pacific 2012 Conference, Singapore.
A Byte of VIM: great book on the VIM editor
PyCmd.exe
PyCmd is a smart command prompt extension for Windows' cmd.exe; its purpose is to emulate a few power features of UNIX shells (decent Tab-completion, persistent history, etc.) It is best combined with Console2: http://www.hanselman.com/blog/Console2ABetterWindowsCommandPrompt.aspxPython Modules
Following is a list of Python modules that we may need for this and other college courses. Most of them are already included in ANACONDA or Portable-Python, so you do not have to install them (just know about their existence). However, those packages that are not included in ANACONDA or Portable-Python, must be installed twice: once for the portable Python installation, and second time for the desktop Python installation. If you plan to work on the Desktop Python only, then you can ignore the portable option.-
psutil - Process Management Utilities
Platform independent operating system utilities for Python (Windows 32bit). This is a self-extracting package for the desktop Python version. For the Portable Python use the zipped version psutil-0.6.1.zip, and then you have to install it manually as described in installing non-self-extracting modules. -
pywin32 - Microsoft Windows Python API package
A set of extension modules that provides access to many of the Windows API functions. This package is specifically targeted for Microsoft Windows operating systems only. This package is already installed by default in the "Portable Python", so you only have to install it to your desktop Python. -
WMI - Windows Management Instrumentation
Windows Management Instrumentation (WMI) is Microsoft's implementation of Web-Based Enterprise Management (WBEM), an industry initiative to provide a Common Information Model (CIM) for pretty much any information about a computer system. The Python WMI module is a lightweight wrapper on top of the pywin32 extensions, and hides some of the messy plumbing needed to get Python to talk to the WMI API. It is pure Python and should work with any version of Python from 2.1 onwards (list comprehensions) and any recent version of pywin32. More info to be found in: https://pypi.python.org/pypi/WMI .
For the Portable Python installation use the zip file from: https://pypi.python.org/pypi/WMI, and then you have to install it manually as described in installing non-self-extracting modules. -
Requests - HTTP for Humans
The 'requests' module allow you to send HTTP/1.1 requests: retrieve html files, images, zip files from the web with a very easy and simple programming interface.
Documentation is available at: http://docs.python-requests.org/en/latest .
A quote from this page: "Her Majesty's Government, Amazon, Google, Twilio, Mozilla, Heroku, PayPal, NPR, Obama for America, Transifex, Native Instruments, The Washington Post, Twitter, SoundCloud, Kippt, Readability, and Federal US Institutions use Requests internally. It has been downloaded over 2,000,000 times from PyPI."
Click here to see how to install this module -
Beautiful soup 4.1.3 - an advanced HTML/XML parser
This module is needed for the Client/Server and parallel programming course and may not be relevant for the Operating Systems course.
Click here to see how to install this module
Usage in Python scripts:- import bs4
- from bs4 import BeautifulSoup
-
numpy:
http://numpy.org.
NumPy is the fundamental package for scientific computing with Python. This module is for windows. By defaults it is installed in ANACONDA and in Portable Python distributions. -
matplotlib
matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib can be used in python scripts, the python and ipython shell (ala MATLAB®* or Mathematica®†), web application servers, and six graphical user interface toolkits. More info and examples at: http://matplotlib.org. -
The Basics Module
Last update: Aug 28, 2013
A set of basic utilities that we will be using for college projects and laboratory exercises. These are supposed to be primitive utilities that perform basic common tasks (such as read_file(str), write_file(str), list_intersection(), etc). This is an evolving project, and newere utilities are added occasionally as time goes by. You may need to update your copy from time to time according to the update date above. Students are encouraged to suggest new utilities that they think should be there. Use the email address above to send me new suggestions.
Installation: simply copy this file to "C:\Python27\Lib" and/or to your Portable Python library : "X:\Portable Python 2.7.5.1\App\Lib". Another option is to copy it to your private library (which you added to sys.path). -
py2exe - convert python scripts into standalone windows programs
For Python 2.7 (the version we use), it can also be downloaded from:
http://sourceforge.net/projects/py2exe/files/py2exe/0.6.9 -
Zwidget package
A GUI framework for building GUI forms and application easily and quickly. Work in progress.
Installing New Modules - HOW?
If you have installed the Anaconda bundle then you probably have all modules and packages that you need and most likely that you will not have to install anything else. However, as a fast evolving language, new Python modules are released and updated frequently. So you may need to install or update a module once in a while.Most modules for Windows are self-extracting msi or exe files that automatically install to the default library directories. There are however some modules that are not of this type and need to be installed manually (usually from the DOS console).
-
The most recommended method is to use the pip command from the DOS console.
If needed you can access this command by its full path from:
- ANACONDA: C:\Anaconda\Scripts\pip.exe
- Portable-Python: App\lib\site-packages\pip.exe
-
pip install pywin32
Installs the pywin32 module to the default library -
pip install --upgrade pywin32
Upgrades the pywin32 module -
pip install -t F:\Portable-Python\App\lib\site-packages psutil
Install the psutil module into the Portable-Python library -
pip list
List the currently installed modules -
pip help
Print help on all available pip commands
-
c:\Anaconda\Scripts\easy_install.bat
This is similar to pip, but should be avoided unless pip fails to do the job. -
If
pip and easy_install methods do not work for you,
then you must download the module, unzip it, and run its setup.py program manually.
- Download your new_module and extract it to a temporary directory: "C:\TEMP\new_module"
- Open a cmd.exe console
-
Go inside that folder: "CD C:\TEMP\new_module"
You should find there a python setup program: "setup.py" -
To install to the standard Python, run the command:
"setup.py install" -
To install to your Portable Python flash drive, use a command like:
"x:\Portable Python 2.7.5.1\App\python.exe" setup.py install --prefix="x:\Portable Python 2.7.5.1\App"
Make sure to replace "x:" with the volume name of your flash drive.
COURSE PROJECTS
- The only way to learn and understand a new programming language is by writing many programs in it! There is no other way!!! It is therefore your duty and reponsibility to solve all the course projects by yourself. It is OK to get hints and cooperate with other students, but it is your reponsibility to acquire a sufficient practice, code discipline, and programming expertise which will enable you to cope with final projects and examinations (as most home work is not checked and graded, the final exam will be partly based on the homework sets so that your work on homework will be reflected in your final grade)
- Before starting each project please read the following Python Coding Style Guides and please make sure to stick to these rules in every line of code that you turn in!
- Every project contains a detailed and precise list of directives on how to do it and how to check it in. Please make sure to read all these direction carefully and act accordingly.
- Make sure you follow exactly the required names to functions, classes, and methods! Any deviation from the suggested name (even with upper or lower case letter difference) can result in your work not being graded, since in many cases your code will be graded by an automatic checker that imports your code and tests it. It is therefore very important that you follow each and every direction in the project text.
-
For this course it is mandatory to understand and follow the following two Style Guide:
- James Madison University Python Coding Conventions (by Nathan Sprague). This is a shorter version of the PEP8 style guide
- Google Python Style Guide This is a long version of the PEP8 style guide
- Important Note: viewing Python files (*.py) is best done with Google Chrome browser! In other browsers they might not be recognized as normal text files and thus cannot be viewed (you'll need to download them to disk and then open them with a text editor)
Project 1
Introduction to the Python programming languageSolutions
It is the student responsibility to do anything he can to solve these problems before reading the solutions. Just by reading and understanding the solutions is not enough for gaining programming expertise.- Solution to Exercise 1
- Solution to Exercise 3
- Solution to Exercise 4
- Solution 2 to Exercise 4 - Advanced approach to solve part (d)
Project 2
- Solution of problem 1
-
Solution of problem 2
Solution b of problem 2 - Solution of problem 3
- Partial solution of problem 4 [Not required to do!]
Project 3
-
The Point class
A simple class for two dimensional integer point -
The Line class
A simple class for two dimensional integer line -
The Rectangle class
A simple class for representing and manipulating rectangle objects -
The Graphics functions
A simple library for drawing two dimensional geometrical objects (Tkinter Canvas) -
The XCanvas Class
This is a super class of the standard Tkinter.Canvas class. It defines a window on which we can draw shapes and text with colors. It has more than 270 methods! And each method may have many options! But you don't have to know more than a few methods and options in order to draw simple shapes like point, line, and rectangle. For the moment, all you need to do is download this file to your workspace. -
The Textfile class
Object orient approach to text files operations
Project 3 Solutions
To make it simple, downloa all solutions file (with graphics files) from: PROJ3_solutions.zipEXTRA PROBLEMS
Some more challenging algorithmic problems that are great for practice towards final examinations and major course projects. You are encouraged to try as many as possible of them in order to learn how to program - this is the only way to gain programming experience and expertise!!! The solutions of these are currently not available, but we may provide solutions to some of them at the end of the semester.
Kernighan's Law: Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
The Zen of Python, by Tim Peters
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!