Python Handbook

Installing Python

There is one, official distribution of Python available from http://www.python.org. In this course, I recommend its use because of its canonicity. You will find Python exists in many other interesting, distributed forms.

Below are detailed instructions for installing Python on your system.

You may install Python packaged up from other organizations, such as Anaconda, Jupyter, ActivePython, or Canopy. None of these will be covered in class, but I am happy to meet with you in person if you wish to use any of these distrbutions of Python. These distrbutions may include their own development environments (IDEs, see below.)

Helpful Python Resources

These links point to resources that I feel are beneficial towards your learning of Python:

Text Editors and IDEs

A nice page on your Python development environment.

Multi-Platform

  • GitHub’s Atom editor
  • JetBrains PyCharm
    • They have a free community version, and a Pro version which you can get a free 1 year license for as a student)
  • Vim
  • Emacs

Python Coding Style

In CSC 161 we will adopt a Python coding style to standardize ourselves in good coding practices.

CSC 161 Style

Starting with Lab #2, we will enforce a class coding style standard. What does this mean? Well, we’ll expect the Python code you are writing to adhere to very specific forms. This enforces consistiency and good future practices.

Coding Requirements:
  • Make use of the online PEP 8 style checker to see if your Python code conforms to good Python coding style.

  • A ``main`` function. While not strictly necessary, it helps you maintain the idea of breaking up your code into nice chunks. In class, all our full examples used this:

    def main():
        # Some code here
    
    main()
    

    We we also enforce the use of the name main, as well.

  • Line Length. Your source code should not contain lines that are longer than 80 characters long. If you have a line that is longer than 80 characters, break it up into multiple lines. Use the ‘’ character at the end of a line to tell the Python interpreter that the statment continues on the next line. Here is an example of how to break up a long boolean expression into three lines:

    if ( ((blah < 0 ) and (grr > 234)) \
            or ((foo == 3456) and (grr <= 4444)) \
            or ((blah > 10) and (grr == 3333))  \
        ):
        print("this crazy condition is true")
        print("and my code is really easy to read because")
        print("I didn't wrap lines!")
    else:
        print("this crazy condition is false")
    

    Here is an example of breaking up a long comment into multiple lines:

    x = foo(x);  # compute the value of the next prime number
        # that is larger than x  (foo is a really bad
        # choice for this function's name)
    

The Zen of Python

Open up the Python Shell, and at the >>> prompt type:

import this

Read and learn the Zen of Python!

Idiomatic Python

There is a Python development term, PEP, which means “Python Enhancement Proposals”. It is a series of documents that end up codfiying how we use Python. The official document on Idiomatic (or, “proper Python”) is PEP 8 - Style Guide for Python Code. This is a tremendously useful, if not dense read. An easier to read version of the PEP8 document can be found on pep8.org.

Installing graphics.py

Author John Zelle provides a Python module called graphics.py which is a wrapper around a more complex graphics library included with Python, TkInter.

There are a few ways to use this graphics library.

  1. The first option is to simply download graphics.py. Once downloaded, place this file graphics.py in the same folder as the Python code you might be working on.

    _images/graphics-py-example1.png
  2. It’s been noted that Mac users can download and then place graphics.py right into their own Documents folder. This option doesn’t work with Windows.

  3. I have added an option to permanently install graphics.py, using Python 3’s standard tool for install, pip3.

    See my tutorial: Installing graphics.py using pip

    This method is a standard practice on installing Python modules. It removes the need to include graphics.py in the same directory as your class code as described in option 1.

With either option, please quit and restart IDLE if it was open during the installation of graphics.py.

Using graphics.py

Once installed, you can start using graphics.py according to how I spoke about it in the lecture, and how it is described in the book. The simplest test to see if things are working is to open the Python interactive interpreter and at the >>> prompt, type:

import graphics
win = graphics.GraphWin()

Then, a small window should appear.

Note: Mac users, this window may appear behind your other windows, be sure to check.

Next, become a little more aquainted with Python, objected-oriented programming and graphics through the Graphics tutorial.

Using iPython

IPython provides a rich architecture for interactive computing with:

  • Powerful interactive shells (terminal and Qt-based).
  • A browser-based notebook with support for code, text, mathematical expressions, inline plots and other rich media.
  • Support for interactive data visualization and use of GUI toolkits.
  • Flexible, embeddable interpreters to load into your own projects.
  • Easy to use, high performance tools for parallel computing.

There are two ways you can use it, install it on your computer or use an IPython notebook server I setup.

  • To use my IPython Notebook server (the preferred, easy way):

    1. Visit: http://notebook.pas.rochester.edu/

    2. Login with your NetID.

    3. A page with two buttons, one red and green should appear.

      • Click on the green button labeled “My Server”.
    4. A directory listing appears.
      • You can click on the button labelled “New” in the upper right of that window to make a new Python 3 Notebook.
    5. You can now write code in Python!

  • To install it on your computer (requires some technical fortitude):

    Mileage may vary! If you have trouble, and can’t get it working, stop by my office and I’m happy to help.