Python course Day 1

download Python course Day 1

If you can't read please download the document

  • date post

    19-May-2015
  • Category

    Technology

  • view

    1.117
  • download

    5

Embed Size (px)

description

These slides are for the first day of a python course. Note, some slides are specific for the computers that the course was run on.

Transcript of Python course Day 1

2. Format of course3 hours session1 part of homework review1 part lecture1 part assisted programmingBook: Python for Bioinformatics, Sebastian BassiNote: no Python3 here. 3. What is programming?Programming: ordered set of instructionsProgramming can be compared to a:Cooking recipeIkea furniture instructionsLab protocolLanguage: instruction setProgramming: combining instructions to solve problem 4. How to make a programNeed a programming languageProgramming language is the set of available instructionsSeveral types of languages several types of instruction setsTwo main types:Interpreted languagesCompiled languages 5. Interpreted languagesNo compilation neededProgram interpreted on-the-flyPrograms often called scriptsExample of interpreted languages:General purpose: perl, pythonSpecial purpose: RPossible disadvantage: can be slower than compiled programs. 6. Interactive batch modePython can be used in the shell, interactivelyUseful for testing etcExit from python: Ctrl-DMost common: save code in text file, run in shellCalled batch mode 7. Python data typesNumbers: integers and floatsStringsListsTuplesDictionariesSets 8. Data type featuresSequence datatypes:Sequential orderStrings, lists and tuplesImmutable datatypes:Cannot be changedNumbers, strings, tuples 9. Python operators 10. Python as a calculator[[email protected]]/projects/temporary/cees-python-course/Karin% python Python 2.6.2 (r262:71600, Sep 1 2009, 10:39:29) [GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 2+2 4 >>> 4-2 2 >>> 5*23 115 >>> 12/6 2 >>> 11/6 1 >>> 11.0/6 1.8333333333333333 >>> 2**8 256 >>> 5%3 2 >>> 7%3 1 >>> 11. Exercise in classLog in to freebee.titan.uio.noGo to/projects/temporary/cees-python-course/Create a directory under your nameDo module load pythonType in python, then press enterYou are now in the python interactive shellRepeat what was done on the last slide 12. StringsUse , orto delineateRemember: same type on each end can be used to create block text>>> This is a piece ... of block text This is a piecenof block text >>>Newline: nTab: t 13. String operations>>> "TTAAGAGGA".replace("T", "U") UUAAGAGGA >>> "TTAAGAGGA".count("G") 3 >>> "TTAAGAGGA".find("AG") 3 >>> "TTAAGAGGA".find("AGX") -1 >>> "TTAAGAGGA".index("AG") 3 >>> "TTAAGAGGA".index("AGX") Traceback (most recent call last):File "", line 1, in Note the error message! ValueError: substring not foundThis is pythons way of telling >>> "TTAAGAGGA".split("A") [TT, , G, GG, ]you something went wrong. >>> "TTA,AGA,GGA".split(",") [TTA, AGA, GGA] >>> "TTA AGA GGA".split() [TTA, AGA, GGA] >>>Repeat onfreebee 14. VariablesA variable is something with a value that may changeNaming variables:Letters, numbers and _CasE sEnsitiveNumbers may not be firstSome words are reservedConvention: small letters, underscore to separate words 15. Reserved words 16. Using variables>>> t1 = "TTAAGAGGA" >>> t2 = "GGGG" >>> t1 + t2 TTAAGAGGAGGGG >>> t1.replace("T", "U") UUAAGAGGA >>> t1 TTAAGAGGA >>> t3 = t1.replace("T", "U") >>> t3 UUAAGAGGA >>>We are using the variable instead of the string itselfCan do the same thing to another string 17. Dynamic, strong typingNoneedtospecifytypeTypeisinterpretedaswego alongpythonobjectswedo somethingwrong>>> t4 = 123 >>> t1 + t4 Traceback (most recent call last): File "", line 1, in TypeError: cannot concatenate str and int objects >>> 18. Printing variablesInteractive mode,type name of variable, press enterprint variable_nameBatch mode:Only print variable_name 19. ListsOrdered collection of elementslist1 = [elem, elem, elem]Can hold elements of any type, including another listCan be sorted (in place) using .sort() >>> list1 = ["a", "c", b] >>> list2 = ["X", "Y", list1] >>> list2 [X, Y, [a, c, b]] >>> list1.sort() >>> list1 [a, b, c] >>> 20. Adding to listCreate empty list: list1 = []Add to list: list1.append(elem) Adds element to the endAdd at position: list1.insert(position, elem) Adds element at positionExtend list: list1.extend(elem) Extends list with elements 21. List adding example>>> list1 = "A,B,C,D,E".split(",") >>> list1 [A, B, C, D, E] >>> list1.append(F) Basic adding to list >>> list1 [A, B, C, D, E, F] >>> list1.extend(G) >>> list1 [A, B, C, D, E, F, G] >>> list1.insert(3,G) >>> list1 [A, B, C, G, D, E, F, G] >>> list1.extend([1,2]) Note difference between >>> list1 append and extend! [A, B, C, G, D, E, F, G, 1, 2] >>> list1.append([1,2]) >>> list1 [A, B, C, G, D, E, F, G, 1, 2, [1, 2]] >>> 22. List removallist1.remove(elem) remove specified elementlist1.pop(index) return elem in index, default is lastdel list1[index] not recommended 23. TupleBasically an immutable listCannot add or remove to tupleList: order can be changed, tuple: lockedInitiation: (elem, elem)One element: (elem,) #note commaCan contain all data types 24. Sequence methodsIndexingIndex starts at zeroNegative indices go from right edgeSlicingCan access portions of sequence using indicesIn operator test for membershipConcatenation add two together with +Len, min, maxCreate list from sequence using list() 25. Indices>>> text = "ABCDEFG" >>> text[2] C >>> text[-2] F 0 1 2 3 4 5 6 >>> text[2:4] A B C D E F G CD >>> text[2:-2]-7 -6 -5 -4 -3 -2 -1 CDE >>> text[:4] ABCD >>> text[4:] EFG >>>Note:forslicing,itis [fromandincluding:tobut excluding] 26. in operatorTest if element is in sequenceWorks with lists, sequences, tuples>>> X = [1,4,8,2,9] >>> X = "ABCDEF" >>> X>>> X [1, 4, 8, 2, 9]ABCDEF >>> 5 in X >>> "Y" in X FalseFalse >>> 8 in X >>> True >>> "BC" in X >>> True>>> 27. ConcatenationConcatenation: + signCan only concatenate same types>>> a = (1,2)>>> b = (3,4)>>> a+b(1, 2, 3, 4)>>> c = [5,6]>>> a + cTraceback (most recent call last):File "", line 1, in TypeError: can only concatenate tuple (not "list") to tuple>>> 28. Len, min, maxLen: length of sequence objectMin: minimumMax: maximum>>> txt1 = "ABCDEF">>> len(txt1)6>>> max(txt1)F>>> min(txt1)A>>> 29. ExerciseCreate the string GATTAGATFind two ways to print the character in the4th positionCount the number of Ts in itFind the indices needed to print AGAAdd the sequence TTG to the string, countTsReplace the Ts with Us 30. ExerciseCreate list [1,4,8,2,10]Find the maximum numberUse sort to find the two lowest numbersFind out if the number 9 is in the listAdd the number 9 to the list, repeat testUse the same numbers to create a tuple, and repeat the above operations 31. Batch mode programWrite programOpen text fileWrite the following:print Hello worldSave file as Hello_world.pyRun program% module load python% python Hello_world.py 32. Different ways of running% python filename.pyAs you did on previous slide% filename.pyMake executable:chmod gu+x filename.pyAdd shebang to script#!/usr/bin/env pythonenv: run with first python in your path 33. Module load pythonSeveral versions of python availableWithout:[[email protected]]~% which python/usr/bin/pythonWith:[[email protected]]~% which python/site/VERSIONS/python-2.6.2/bin/pythonBiopython compatible with 2.6 34. ExerciseGather what you have done earlier into a text fileRemember print!Name the file datatypes.pyRun python datatypes.pyCongratulations: your first script! 35. Getting helpIn python:help(what you want to know more about)Example: help(list)wiki.python.org/moin/BeginnersGuide/NonPr ogrammersLook at the book!Send me an email 36. HomeworkORF.py Define stringATTAATGAGATTACAGAGCTAAGAC Replace all Ts with Us Find position of start codon AUG Find position of stop codon UAA Print sequence from (including) start codon to stop codon (excluding) 37. HomeworkGCcontent.pyDefine DNA string AGCAGATCAGCGACalculate the frequency of Gs and Cs in the stringCalculate the frequency of the dinucleotide GCin the input stringPrint the results to screen 38. Homework resultsWill be examined by your fellow studentBringPaper copy of codePrintout of results of running code