introduction to python for biologists
play

Introduction to Python for Biologists skova 1 Jean-Fred Fontaine 1 , - PowerPoint PPT Presentation

Introduction to Python for Biologists skova 1 Jean-Fred Fontaine 1 , 2 K aterina Ta 1 Faculty of Biology, Johannes Gutenberg-Universit at Mainz, Mainz, Germany 2 Genomics and Computational Biology, Kernel Press, Mainz, Germany


  1. Introduction to Python for Biologists skova 1 Jean-Fred Fontaine 1 , 2 K aterina Taˇ 1 Faculty of Biology, Johannes Gutenberg-Universit¨ at Mainz, Mainz, Germany 2 Genomics and Computational Biology, Kernel Press, Mainz, Germany https://cbdm.uni-mainz.de/mb17 March 21, 2017

  2. Introduction to Python for Biologists – Table of Contents Convert and copy Introduction Loops Running code – Exercise – Literals and variables Functions Numeric types Branching Strings – Exercise – – Exercise– Regular Expressions Lists, tuples and ranges – Exercise – Sets and dictionaries Annexes 2 March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine

  3. Introduction to Python for Biologists – Introduction Convert and copy Introduction Loops Running code – Exercise – Literals and variables Functions Numeric types Branching Strings – Exercise – – Exercise– Regular Expressions Lists, tuples and ranges – Exercise – Sets and dictionaries Annexes 3 March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine

  4. Introduction to Python for Biologists – Introduction What is Python? � Python is a general-purpose programming language � created by Guido van Rossum (1991) � high-level (abstraction from the details of the computer) � interpreted (needs an interpreter software) � Python design philosophy � code readability � syntax brevity � Python is widely used for Biology � rich built-in features � powerful scientific extensions � plotting capabilities 4 March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine

  5. Introduction to Python for Biologists – Introduction Structured programming I � Instructions are executed sequentially, one per line � Conditional statements allow selective execution of code blocks � Loops allow repeated execution of code blocks � Functions allow on-demand execution of code blocks 5 March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine

  6. Introduction to Python for Biologists – Introduction Structured programming II 1 i n s t r u c t i o n 1 # 1 st i n s t r u c t i o n ( hashtag # s t a r t s comments ) # blank l i n e 2 3 repeat 20 times # 2nd i n s t r u c t i o n ( loop s t a r t s a block ) i n s t r u c t i o n a # block defined by indentation ( spaces or tabs ) 4 i n s t r u c t i o n b # 2nd i n s t r u c t i o n in block 5 # blank l i n e 6 i f n > 10 # 3rd i n s t r u c t i o n ( Conditional statement ) 7 i n s t r u c t i o n a # 1 st i n s t r u c t i o n in block 8 i n s t r u c t i o n b # 2nd i n s t r u c t i o n in block 9 # blank l i n e 10 # blank l i n e 11 12 # backslashs j o i n l i n e s 13 i n s t r u c t i o n 3 \ # 3rd i n s t r u c t i o n , part 1 14 i n s t r u c t i o n 3 # 3rd i n s t r u c t i o n , part 2 # blank l i n e 15 16 # Expressions in ( ) , {} , or [ ] can span m u lt ip le l i n e s 17 i n s t r u c t i o n 4 (1 , 2 , 3 # 4th i n s t r u c t i o n , part 1 4 , 5 , 6) # 4th i n s t r u c t i o n , part 2 18 6 March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine

  7. Introduction to Python for Biologists – Introduction Namespace � Variables are names associated with data � e.g. a=2 assigns value 2 to variable a � Functions are names associated to specific code blocks � built-in functions are available (see list on slide 100) � e.g. print(a) will display ’2’ on the screen � The user namespace is the set of names available to the user � users can define new names of variables and functions in their namespace � imported modules can add names of variables and functions in the user namespace 7 March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine

  8. Introduction to Python for Biologists – Introduction Object-oriented programming � Data is organized in classes and objects � a class is a template defining what objects can store and do � an object is an instance of a class � objects have attributes to store data and methods to do actions � object namespaces are different from user namespace � Example class ”Human” is defined as: � has a name (an attribute ”name”) � has an age (an attribute ”age”) � can introduce itself (a method ”who”) � example with 1 existing Human object P1: 1 P1 .name = ” Mary ” # assigns value to a t t r i b u t e name 2 P1 . age = 26 # assigns value to a t t r i b u t e age 3 P1 . who ( ) # displays ”My name i s Mary I am 26!” 4 who ( ) # error ! not in the user namespace 8 March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine

  9. Introduction to Python for Biologists – Introduction Modules � Modules can add functionalities to Python � e.g. classes and functions � Example of available modules: � NumPy for scientific computing � Matplotlib for plotting � BioPython for Biology � Modules have to be imported into the code 1 # import datetime module in i t s own namespace 2 import datetime 3 datetime . date . today ( ) # 2017 − 03 − 16 4 today ( ) # error ! 5 6 # import functions log2 and log10 from module math 7 # in current namespace 8 from math import log2 , log10 9 log10 (1) # equal 0 9 March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine

  10. Introduction to Python for Biologists – Running code Convert and copy Introduction Loops Running code – Exercise – Literals and variables Functions Numeric types Branching Strings – Exercise – – Exercise– Regular Expressions Lists, tuples and ranges – Exercise – Sets and dictionaries Annexes 10 March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine

  11. Introduction to Python for Biologists – Running code Running code I � From a terminal by using the interactive Python shell 1 $ python3 # opens Python s h e l l 2 a=2 # assigns 2 to a 3 b=3 # assigns 3 to b 4 e x i t ( ) # closes Python s h e l l � From a terminal by running a script file � e.g. let say myscript.py is a script file (simple text file) � and it contains: print(”hello world!”) 1 $ python3 myscript . py # runs python3 and the s c r i p t 2 hello world ! # r e s u l t of the s c r i p t on the terminal 11 March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine

  12. Introduction to Python for Biologists – Running code Running code II � From Jupyter Notebook � web-based graphical interface � manage cells of code or text � see execution results on the same notebook � save/open notebooks 12 March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine

  13. Introduction to Python for Biologists – Running code Documentation and messages I Documentation and help: � https://docs.python.org/3 � use the built-in help() function � e.g. help(print) to display help for function print() � see help menu or Google it Examples of error messages 1 # Forgetting quotes p r i n t ( Hello world ) 2 3 # F i l e ” < stdin > ”, l i n e 2 4 # p r i n t ( Hello world ) 5 # ˆ 6 # SyntaxError : i n v a l i d syntax 13 March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine

  14. Introduction to Python for Biologists – Running code Documentation and messages II 1 # Spelling mistakes 2 prin ( ” Hello world ” ) 3 # Traceback ( most recent c a l l l a s t ) : 4 # F i l e ” < stdin > ”, l i n e 2 , in < module > 5 # NameError : name ’ prin ’ i s not defined 1 # Wrong l i n e break within a s t r i n g p r i n t ( ” Hello 2 3 World ” ) 4 # F i l e ” < stdin > ”, l i n e 2 5 # p r i n t ( ” Hello 6 # ˆ 7 # SyntaxError : EOL while scanning s t r i n g l i t e r a l 14 March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine

  15. Introduction to Python for Biologists – Literals and variables Convert and copy Introduction Loops Running code – Exercise – Literals and variables Functions Numeric types Branching Strings – Exercise – – Exercise– Regular Expressions Lists, tuples and ranges – Exercise – Sets and dictionaries Annexes 15 March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine

  16. Introduction to Python for Biologists – Literals and variables Numeric and strings literals I 1 # Numeric l i t e r a l s 2 12 3 − 123 4 1.6E3 # means 1600 5 6 # Strings l i t e r a l s ’A s t r i n g ’ # A s t r i n g 7 ’A ” s t r i n g ” ’ # A ” s t r i n g ” 8 9 ”A ’ s t r i n g ’ ” # A ’ s t r i n g ’ ’ ’ ’ Three single quotes ’ ’ ’ # Three single quotes 10 ” ” ” Three double quotes ” ” ” # Three double quotes 11 ’A \ ’ s t r i n g \ ’ ’ # A ’ s t r i n g ’ ( backslash escape sequence ) 12 13 r ’A \ ’ s t r i n g \ ’ ’ # A \ ’ s t r i n g \ ’ ( raw s t r i n g ) Python stores literals in objects of corresponding classes (class int for integers, float for floatting point, and str for strings) 16 March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine

Recommend


More recommend