PythonLecture1_08

download PythonLecture1_08

of 30

Transcript of PythonLecture1_08

  • 8/12/2019 PythonLecture1_08

    1/30

    An Introduction to Python and ItsUse in Bioinformatics

    Dr. Nancy Warter-Perez

  • 8/12/2019 PythonLecture1_08

    2/30

    Introduction to Python 2

    Overview Overview of program/script development (BP Ch 1)

    Python Basics (BP Ch1)

    Python Types and Operators Numbers and Arithmetic operators (BP Ch1, App B)

    Strings (BP Ch3)

    Lists and Dictionaries (BP Ch2 and Ch4)

    Input & Output (BP Ch1)

    Programming Workshop #1

  • 8/12/2019 PythonLecture1_08

    3/30

    Introduction to Python 3

    Program DevelopmentProblem specification

    Algorithm design

    Testby hand

    Code in target language

    Testcode / debug

    Program/Script

    Problemsolving

    Implementation

  • 8/12/2019 PythonLecture1_08

    4/30

    Introduction to Python 4

    What is Python?A portable, interpretive, object-oriented

    programming language

    Elegant syntax

    Powerful high-level built-in data types

    Numbers, strings, lists, dictionaries

    Full set of string operations

  • 8/12/2019 PythonLecture1_08

    5/30

    Introduction to Python 5

    Why Python? Previously used C++

    Scripting languages useful for

    bioinformatics

    Perl was bioinformatics standard

    Python is more robust for largersoftware projects

  • 8/12/2019 PythonLecture1_08

    6/30

    Introduction to Python 6

    Useful Tutorials DNA from the Beginning

    http://www.dnaftb.org/dnaftb/

    Python Tutorial

    http://www.python.org/doc/current/tut/tut.html

    http://www.dnaftb.org/dnaftb/http://www.python.org/doc/current/tut/tut.htmlhttp://www.python.org/doc/current/tut/tut.htmlhttp://www.dnaftb.org/dnaftb/
  • 8/12/2019 PythonLecture1_08

    7/30Introduction to Python 7

    Python Development Open-

    Source Software Python interpreter - will run on windows, you need

    to download it in two parts:

    1. The actual interpreter and core of pythonhttp://www.python.org/download/releases/2.5/(were using python-2.3.3 in class).

    2. An integrated development environment for

    python called pythonwin, by Mark Hammondhttp://sourceforge.net/project/showfiles.php?group_id=78018

    http://www.python.org/download/releases/2.5/http://www.python.org/download/releases/2.5/http://sourceforge.net/project/showfiles.php?group_id=78018http://sourceforge.net/project/showfiles.php?group_id=78018http://sourceforge.net/project/showfiles.php?group_id=78018http://sourceforge.net/project/showfiles.php?group_id=78018http://www.python.org/download/releases/2.5/http://www.python.org/download/releases/2.5/
  • 8/12/2019 PythonLecture1_08

    8/30Introduction to Python 8

    Python Basics - Comments Python comments

    # line comment

    Header comments

    #Description of program

    #Written by:

    #Date created:

    #Last Modified:

  • 8/12/2019 PythonLecture1_08

    9/30Introduction to Python 9

    Python Basics - Variables Python variables are not declared. To assign a variable, just type: identifier=literal

    Identifiers Have the following restrictions:

    Must start with a letter or underscore (_)

    Case sensitive

    Must consist of only letters, numbers or underscore

    Must not be a reserved word

    Have the following conventions: All uppercase letters are used for constants

    Variable names are meaningfulthus, often multi-word (but nottoo long)

    Convention 1: alignment_sequence (align_seq)

    Convention 2: AlignmentSequence (AlignSeq)

    Python specific conventions (Avoid _X, __X__, __X, _)

  • 8/12/2019 PythonLecture1_08

    10/30Introduction to Python 10

    Numbers Numbers

    Normal Integersrepresent whole numbersEx: 3, -7, 123, 76

    Long Integersunlimited sizeEx: 9999999999999999999999L

    Floating-pointrepresent numbers with decimalplaces

    Ex: 1.2, 3.14159,3.14e-10 Octal and hexadecimal numbers

    Ex: O177, 0x9ff, Oxff

    Complex numbersEx: 3+4j, 3.0+4.0j, 3J

  • 8/12/2019 PythonLecture1_08

    11/30Introduction to Python 11

    Python Basicsarithmetic operations

    + add

    - subract

    * multiply/ divide

    % modulus/remainder

    y=5; z=3

    x = y + z

    x = yz

    x = y * zx = y / z

    x = y % z

    x = 8

    x = 2

    x = 15x = 1

    x = 2

    Operators Example

  • 8/12/2019 PythonLecture1_08

    12/30Introduction to Python 12

    Python Basicsarithmetic operations

    > shift right

    ** raise to power

    y=5; z=3

    x = y > 2

    x = y ** z

    x = 10

    x = 1

    x = 125

    Operators Example

  • 8/12/2019 PythonLecture1_08

    13/30Introduction to Python 13

    Python BasicsRelational and

    Logical Operators

    Relational operators

    == equal

    !=, not equal

    > greater than

    >= greater than or

    equal< less than

  • 8/12/2019 PythonLecture1_08

    14/30

    Introduction to Python 14

    Python BasicsRelational

    OperatorsAssume x = 1, y = 4, z = 14

    Expression Value Interpretation

    x < y + z 1 True

    y == 2 * x + 3 0 False

    z x 1 True

    x != y 1 True

  • 8/12/2019 PythonLecture1_08

    15/30

    Introduction to Python 15

    Python BasicsLogical

    OperatorsAssume x = 1, y = 4, z = 14

    Expression Value Interpretation

    x 1 0 False

    not (x

  • 8/12/2019 PythonLecture1_08

    16/30

    Introduction to Python 16

    Enclosed in single or double quotesEx: Hello! , Hello!, 3.5, a, a

    Sequence of characters:mystring=hello world!

    mystring[0] -> h mystring[1] -> e

    mystring[2] -> l mystring[-1] -> !

    Strings

    -1 is last,

    -2 next to last, etc

  • 8/12/2019 PythonLecture1_08

    17/30

    Introduction to Python 17

    String operationsmystring = Hello World!

    Expression Value Purpose

    len(mystring) 12 number of characters inmystring

    hello+world helloworld Concatenate strings

    %s world%hello hello world Format strings (like sprintf)

    world == hello

    world == world0 or False1 or True

    Test for equality

    a < b

    b < a

    1 or True

    0 or False

    Alphabetical ordering

  • 8/12/2019 PythonLecture1_08

    18/30

    Introduction to Python 18

    Strings (2) slicing:mystring = spoon!

    mystring[2:] -> oon!

    mystring[:3] -> spo#note last element is never included!mystring[1:3]-> po

    Many useful built-in functions

    mystring.upper() -> SPOON! mystring.replace(o, O) -> spOOn!

  • 8/12/2019 PythonLecture1_08

    19/30

    Introduction to Python 19

    Strings (3)% operator:

    sort of fill in the blanks operation:mystring=%s has %d marbles % (John,35)

    mystring -> John has 35 marbles

    %s replace with string

    %d,%i replace with integer %f replace with float

    Values to putin blanks

    blanks

  • 8/12/2019 PythonLecture1_08

    20/30

    Introduction to Python 20

    Listsmylist=[a,b,3.58,d,4,0]

    mylist[0]

    mylist[2]

    a

    3.58

    Indexing

    mylist[-1]

    mylist[-2]

    0

    4

    Negative indexing(counts from end)

    mylist[1:4] [b,3.58,d] Slicing (like strings)

    b in myliste notinmylist

    1 or True1 or True

    mylist.append(8) [a,b,3.58,d,4,0,8] Add to end of list

  • 8/12/2019 PythonLecture1_08

    21/30

    Introduction to Python 21

    Tuples Tuplessequence of values

    like lists, but cannot be changed after it

    is createdmytuple=(1,a,bc,3,87.2)

    mytuple[2] -> bc

    mytuple[1]=3

    Used when you want to pass severalvariables around at once

    Error!

  • 8/12/2019 PythonLecture1_08

    22/30

    Introduction to Python 22

    Dictionaries Dictionariesmap keys to values

    like lists, but indices can be of any type

    Also, keys are in no particular order

    Eg:mydict={b:3, a:4, 75:2.85}

    mydict[b] -> 3mydict[75] -> 2.85

    mydict[a] -> 4

  • 8/12/2019 PythonLecture1_08

    23/30

    Introduction to Python 23

    Dictionariesmydict={r:1,g:2,y:3.5,8.5:8,9:nine}

    mydict.keys() ['y', 8.5, 'r', 'g', 9] List of the keys

    mydict.values() [3.5, 8, 1, 2, 'nine'] List of the valuesmydict[y] 3.5 Value lookup

    mydict.has_key(r) True or 1 Check for keys

    mydict.update({a:75}) {8.5: 8, 'a': 75, 'r': 1, 'g':2, 'y': 3.5, 9: 'nine'}

    Add pairs todictionary

  • 8/12/2019 PythonLecture1_08

    24/30

    Introduction to Python 24

    Dictionariesother

    considerations Slicing not allowed

    Referencing invalid key is an error:

    >>> mydict={8.5: 8, 'a': 75, 'r': 1, 'g': 2, 'y':3.5, 9: 'nine'}

    >>> mydict["red"]

    Traceback (most recent call last):File "", line 1, in ?

    KeyError: 'redUse mydict.get(red) instead, it returns None if

    key is not found

  • 8/12/2019 PythonLecture1_08

    25/30

    Introduction to Python 25

    Input/Output Function raw_input() designed to read a line of input

    from the user 1 optional argument: string to prompt user If int or float desired, simply convert string:

    int(mystring)->convert to int (if possible)float(mystring)->convert to float (if possible)

    >>> mystr=raw_input("Enter a string:")

    Enter a string:Hello World!>>> mystr

    'Hello World!'

  • 8/12/2019 PythonLecture1_08

    26/30

    Introduction to Python 26

    Output Function print

    Prints eachargument, followedby space

    After all arguments,prints newline

    Put comma after last

    arg to preventnewline

    add strings to avoidspaces

    printa,b,c

    a b c

    print a,b,c,

    a b c

    print a+b+c

    abc

    Newline!

    NoNewline!

    Nospaces!

  • 8/12/2019 PythonLecture1_08

    27/30

    Introduction to Python 27

    Output Example>>> print"hello","world";print"hello","again"

    hello world

    hello again

    >>> print"hello","world",;print "hello","again"hello world hello again

    >>> print"hello %s world"% "cold and cruel"

    hello cold and cruel world

    >>> print"hello","cold"+ " "+ "and","cruel","world"

    hello cold and cruel world

  • 8/12/2019 PythonLecture1_08

    28/30

    Introduction to Python 28

    Creating a Python Program Enter your program in the editor

    Notice that the editor has a color coding

    Comments

    Key words

    Etc

    Also notice that it automatically indents

    Dont override!! this is how python tells when block

    statements end!

    If doesnt indent to proper location indicates bug

  • 8/12/2019 PythonLecture1_08

    29/30

    Introduction to Python 29

    Running your Program To build your program

    Under File->Run

    Select No Debugging in the drop-down window

    Fix any errors, then run again

  • 8/12/2019 PythonLecture1_08

    30/30

    Int od ction to P thon 30

    Workshop #6 Write a Python program to compute the

    hydrophobicity of an amino acidA m ino A c id H y dr op . V A LU E

    A 1.8

    C 2.5D -3 .5

    E -3 .5

    F 2 .8

    G -0 .4

    H -3 .2

    I 4 .5

    K -3 .9

    L 3 .8

    M 1.9

    N -3 .5

    P -1 .6

    Q -3 .5

    R -4 .5

    S -0 .8

    T -0 .7

    V 4.2

    W -0 .9

    Y -1 .3

    Program will promptthe user for anamino acid and willdisplay thehydrophobicity