﻿ COMP 200 Resources

# COMP 200 Resources

## How-To's

On this page, you can find quick, helpful tips on how to do a variety of common tasks for the class.

## Start Python Using IDLE

The most straightforward way to start Python is to use IDLE, Python’s built-in environment and editor. To start IDLE under

• Windows: Under the Start Menu, select Enthought/IDLE.
• Mac OS X: Under Applications, select Enthought/IDLE.
• Linux: If you have IDLE on your path, you can cd to the directory where you want to write your code, and enter idle & .

## Connect to Your Network Storage

Rice IT provides online storage that is linked to your NetID.   This storage is regularly backed up, so it is an ideal place to keep your work.   It is also the same location that you connect to when you use your NetD to log into any campus computer laboratory computer.    For your laptop or desktop machine, you will want to "map" your network storage as a virtual drive on your machine.   This is very easy to accomplish:

## Transpose a List of Lists

List of lists, or a list of tuples (or a tuple of lists or tuple of tuples) are 2-dimensional data structures that can be thought of a collections of rows of data.   When all the rows are the same length, such data structures are conceptually similar to 2-dimensional matrices.    But in a matrix, one can access the data by columns as well, not just by rows.

Sometimes, it is easier to construct a 2-dimensional data matrix-like data structure by rows than it is by columns but one may wish to further process the data by columns.   For instance, it is often easy to create a list of (x, y) tuples of data but then be faced with the dilemma that the plotting functions want to see separate lists of x data and y data.  In terms of matrices, what you want is the transpose of the original data matrix.

Luckily, Python provides a built-in function called "zip" that can easily transpose a list of lists or tuples.   The only catch is that the input to zip must be the individual rows (lists or tuples) of data, not a list or tuple of rows.   However, the unary asterisks operator, "*", in Python can convert a list or tuple of data into multiple input parameters for a function.

Example:

Suppose your data is in this form, a list of tuples

myData = [(1, 2, 3, 4), ('a', 'b', 'c', 'd'), (5, 6, 7, 8)]

but what you wanted was

myData_Transpose = [(1, 'a', 5), (2, 'b', 6), (3, 'c', 7), (4, 'd', 8)]

We can achieve this transposition by using the zip function in conjunction with the unary astericks operator:

myData_Transpose = zip(*myData)

The unary asterisks operator (*) here causes the single input parameter, the list myData, to expand input multiple input parameters corresponding to the individual elements of myData, which are the individual tuples.

Also, it should be noted that the output of zip is always a list of tuples, independent of whether it input was a list of lists, list of tuples, tuple of lists or tuple of tuples.  The only requirement is that all the rows have the same length, i.e. the data is in the form of a NxM matrix.    The number of rows does NOT have to equal the number of columns.

References:

## Comment/Uncomment a Block of Code

Unfortunately, the Python language does not include a syntax to comment out an entire block of code the way that many languages such as C-based languages such as Java provide.    One is thus forced to comment out lines one by one by putting the '#' symbol at the front of each line.

Luckily the editors used in the class have the ability to quickly comment out or uncomment a block of code in a few keystrokes:

In Notepad++:

1. Highligh the block of code you wish to either comment out or uncomment.
2. From the main menu, select Edit/Comment/UnCommand/Toggle Block Comment to go back and forth between commented and uncommented.
• One could also use the shortcut key, Ctrl-Q, to perform this operation.

In TextWrangler   (see the manual, p. 97)

1. After selecting all the text you want to change, go to the top menu bar and choose Text --> Un/Comment Selection
2. TextWrangler chooses to add or remove a leading "#" character based on what your first selected line is

## Zip Up a Folder into a Single Compressed File

If you need to transmit or turn in an entire folder of files, for instance, your code plus data files, you can easily do so by "zipping" the folder up into a single, compressed ".zip" file.

Windows:    In Windows Explorer, right-click the the desired folder and select "Send to/Compresseed (zipped) folder".   By default, the name of the zip file witll be "folderName.zip" where folderName is the name of the folder you wish to zip up.

Mac:  Hold the Ctrl key and  click the the desired folder and select "'Compress".   By default, the name of the zip file witll be "folderName.zip" where folderName is the name of the folder you wish to zip up.   Compress is also available on the File menu.

Linux:  At a command prompt, type "zip targetName.zip  folderName"   (no quotes) where targetName is the name of the zip file you wish to create--be sure to add the ".zip" extension so that other systems will proper recognize the file-- and where folderName is the name of the folder you wish to zip up.   On some Linux systems, the zip program is not installed by default and must be installed first.

.