Python Handbook

Installing Python

The Official Python Installation

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.

The Anaconda Python Installation

While you would be perfectly fine installing the above, official Python installation you may want to consider instead installation the Anaconda Python distribution.

It takes the “vanilla” Python install and adds in many very popular third-party packages. Many of these are numerical, data science, and visualization related. We won’t be using any of these in class, except maybe Jupyter Notebooks (which is optional to use, but included with Anaconda.)

Installation of this will not be covered in class, but I am happy to meet with you if you wish some assistance.

Helpful Python Resources

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

Text Editors and IDEs

Visit this nice page on tools you may enjoy as part of your Python software development environment.

Multi-Platform

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 the Writing Programs lab, we will enforce a class coding style standard. We’ll expect the Python code you are writing to adhere to very specific style. This enforces consistency, and readability.

Strict, but reasonable adherence to PEP 8 styling is required.

Coding Requirements:

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

    Note: As Emerson wrote: A Foolish Consistency is the Hobgoblin of Little Minds. In our case, understanding when to be inconsistent is more important than being perfectly consistent.

    For example: It maybe more more work to reduce slightly long lines than necessary, which may also affect the readability of your code!

    Maintain good style, but also think for yourself and make good decisions while doing it!

  • A main() function. I require a main() function for almost everything I have you write. While you may not know how the if conditions work, I will still require the below template. It will be properly explained when we reach the Decision Control topic.

    def main():
        print("Hello World!")
    
    if __name__ == '__main__':
        main()
    
  • Line Length. Your source code should not contain lines that are longer than 79 characters long. If you have a line that is longer than 79 characters, break it up into multiple lines.

    There are two basic techniques for this:

    1. Use the '\' (backslash) character.

      Add a backslash “" to the end of a line to tell the Python interpreter that the statement 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!")
      
    2. Use bracketing characters, e.g. '{}, (), []'.

      Certain constructs, like if statements, literal lists or dictionaries, can be easily broken into multiple lines because their contents are bracketed. For example, here’s how to break a long if statement. There is a pair of parentheses bracketing the entire boolean expression.

      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!").. inclusion-marker-styling-end
      

      In this example, here’s a literal list broken up into multiple lines. Notice, no backslashes are needed here. The square brackets contain all the content, letting you break the list any way you want.

      months = ["January", "February", "March", "April", "May",
                "June", "July", "August", "September", "October",
                "November", "December"]
      
    3. Breaking long strings cleanly using “implicit string concatenation”.

      One useful, but obscure trick is “implicit string concatenation”. For example, this will allow you to neatly break a long string because this is true:

      x = "This is part 1, of a " "two part string"
      

      Python, being a product of its historical heritage with the C programming language, allows you to put two strings next to each other, and they are interpreted as one single string (try it yourself). This lends to nice tricks like:

      x = ("This is part one, of a "
           "three part string "
           "which is very long!")
      print(x)
      

      Or, if you are calling a function immediately:

      print("This is part one, of a "
            "three part string "
            "which is very long!")
      

      Using the backslash (\) is fine for breaking a string, but it prevents you from indenting neatly as I have done above. If you had use the backslash, then the second, and third lines of the broken string in this case cannot be indented else the spacing you just added becomes part of the string itself. So the implicit concatenation method has some really great benefits.

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 codifying 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 acquainted with Python, objected-oriented programming and graphics through the Graphics tutorial.