Tuesday, July 23, 2013

Python 101, part 1

I was asked to provide some more in depth information about the Rosalind Python Village problems.  Rosalind moves pretty quickly though the process.  If one knows any computer languages Python will come pretty quickly but if you are new to computer programming all languages may be a bit hard.  I'm assuming anyone who want to start programming is willing to do the first step, that is load the software on your computer.  Here's the steps:
  1. go to the Python Download page.
  2. choose the correct version(s).  V3.3.2 is the current version as of this writing but some Python code won't run on V3.3.2 so one might need to get V2.7.5.  I have both version installed and use V2.7.5 for Rosalind and V3.3.2 for Computational Molecular Evolution.  I haven't run across the need for V2.7.5 yet but that's the version Rosalind's ini1 asks you to load.  I'm going to keep it simple but suggest you install both versions.
    1. Download and Install V2.7.5.  Let it use its default file folder.  For Windows that's C:\Python27.
    2. Download and Install V3.3.2.  Let it use its default file folder.  For Windows that's C:\Python33.
On windows both versions have their own folders in the start menu.  You can run either version from the start menu and all is well.  If you are like me you don't want to do that because the startup folder for both are their home directories.  The .py extension is associated with V3.3.2 if you load both.  Here's a simple trick to get the right version set up for a particular project.
  1. Create a folder for each project.  I have a folder for Rosalind under Documents.
  2. If the project will be using V2.7.5:
    1. Find the "IDLE (Python GUI)" Icon in the start menu under "Python 2.7".
    2. Right click on it and select "Copy".
    3. Open the projects folder.
    4. Right Click on the folder's background and select "Paste".
    5. Right Click on the newly pasted short cut and select "Properties".
    6. Click on the "Shortcut" tab.
    7. Change the "Start in: to the fully qualified path of the directory you just created.
    8. Click on the "OK" button.
When you want to work on a V2.7.5 project use the "IDLE (Python GUI)" Icon in the folder to start Python then load modules using the "Open" item under the "File" menu.  When you want to work on a V3.3.2 project right click on the module and select "Edit with IDLE".

I hope that gets you set up.

The first time I tried INI1 I didn't do the right thing.  I don't know why.  It said to type
  import this
in the command line and see what happens.  I did that but for some reason I didn't get "Then, click the 'Download dataset' button below and copy the Zen of Python into the space provided."  What a mess. 

As it turns out this.py is a module in C:\Python33\Lib for V3.3.2 or in C:\Pythod27\Lib for V2.7.5.  You can open these modules in the editor and see what they look like The only difference is the format of the print statement.  The small difference in the print statement is one of the few differences between the two versions beginners will notice right away.  There are other differences.

When you want to run a module from the shell you type "import " followed by the name of the module then press enter.  I haven't tried putting the same module name in my working directory and in the library folder so I don't know what would happen.

About String constants. 

String constants can be quoted by either single quote(') or double quote(").
While writing this I learned a little bit about Python string constants.
If you haven't played around with string constants try some of these:

>>> ''  ''
''
>>> '   ''    '
'       '
>>> "  "
'  '
>>> " ' "
" ' "
>>> ' \' '
" ' "
>>>

Then try some of your own.  Have fun.  Work Problem INI1 if you haven't done so already.

2 comments: