python tutorial
play

Python Tutorial Benjamin L. Zaitlen Biocomplexity Institute, - PowerPoint PPT Presentation

Python Tutorial Benjamin L. Zaitlen Biocomplexity Institute, Indiana University February 26, 2008 Outline Introduction 1 Interactive Shell 2 Strings and Numbers 3 Storing Data 4 Control Structures 5 Functions 6 Classes 7 CompuCell


  1. Python Tutorial Benjamin L. Zaitlen Biocomplexity Institute, Indiana University February 26, 2008

  2. Outline Introduction 1 Interactive Shell 2 Strings and Numbers 3 Storing Data 4 Control Structures 5 Functions 6 Classes 7 CompuCell Classes 8 Python Tutorial B. Zaitlen February 26, 2008 2 / 21

  3. Introduction Introduction Created Guido van Rossum in 1991 Human-readable Fast creation and debugging See http://www.python.org/doc and Python Essential Reference by David Beazley Python Tutorial B. Zaitlen February 26, 2008 3 / 21

  4. Introduction Introduction Created Guido van Rossum in 1991 Human-readable Fast creation and debugging See http://www.python.org/doc and Python Essential Reference by David Beazley Python Tutorial B. Zaitlen February 26, 2008 3 / 21

  5. Introduction Introduction Created Guido van Rossum in 1991 Human-readable Fast creation and debugging See http://www.python.org/doc and Python Essential Reference by David Beazley Python Tutorial B. Zaitlen February 26, 2008 3 / 21

  6. Introduction Introduction Created Guido van Rossum in 1991 Human-readable Fast creation and debugging See http://www.python.org/doc and Python Essential Reference by David Beazley Python Tutorial B. Zaitlen February 26, 2008 3 / 21

  7. Introduction Comments and Quirks # A comment Python is space senstive. Lines after a : must be indented. Python Tutorial B. Zaitlen February 26, 2008 4 / 21

  8. Introduction Comments and Quirks # A comment Python is space senstive. Lines after a : must be indented. Python Tutorial B. Zaitlen February 26, 2008 4 / 21

  9. Introduction Comments and Quirks # A comment Python is space senstive. Lines after a : must be indented. Indentation Example for i in (1,2,3,4): print i, 1 2 3 4 Python Tutorial B. Zaitlen February 26, 2008 4 / 21

  10. Interactive Shell The Shell Invoke the shell from the command line using the keyword python Useful for basic math, testing ideas Do not build programs in interpreter Python Tutorial B. Zaitlen February 26, 2008 5 / 21

  11. Interactive Shell The Shell Invoke the shell from the command line using the keyword python Useful for basic math, testing ideas Do not build programs in interpreter Python Tutorial B. Zaitlen February 26, 2008 5 / 21

  12. Interactive Shell The Shell Invoke the shell from the command line using the keyword python Useful for basic math, testing ideas Do not build programs in interpreter Python Tutorial B. Zaitlen February 26, 2008 5 / 21

  13. Interactive Shell The Shell Invoke the shell from the command line using the keyword python Useful for basic math, testing ideas Do not build programs in interpreter Interpreter >>>print "Hello, World!" Hello, World! >>>var = 9+2 >>>var*11 121 Python Tutorial B. Zaitlen February 26, 2008 5 / 21

  14. Strings and Numbers Strings A simple string "hello world" Concatenation: "hello"+" world" → "hello world" Repetition: "hello"*3 → "hellohellohello" Indexing: "world"[3] → "l" Note: python lists are zero-offset Searching: "o" in "hello" → True Python Tutorial B. Zaitlen February 26, 2008 6 / 21

  15. Strings and Numbers Strings A simple string "hello world" Concatenation: "hello"+" world" → "hello world" Repetition: "hello"*3 → "hellohellohello" Indexing: "world"[3] → "l" Note: python lists are zero-offset Searching: "o" in "hello" → True Python Tutorial B. Zaitlen February 26, 2008 6 / 21

  16. Strings and Numbers Strings A simple string "hello world" Concatenation: "hello"+" world" → "hello world" Repetition: "hello"*3 → "hellohellohello" Indexing: "world"[3] → "l" Note: python lists are zero-offset Searching: "o" in "hello" → True Python Tutorial B. Zaitlen February 26, 2008 6 / 21

  17. Strings and Numbers Strings A simple string "hello world" Concatenation: "hello"+" world" → "hello world" Repetition: "hello"*3 → "hellohellohello" Indexing: "world"[3] → "l" Note: python lists are zero-offset Searching: "o" in "hello" → True Python Tutorial B. Zaitlen February 26, 2008 6 / 21

  18. Strings and Numbers Strings A simple string "hello world" Concatenation: "hello"+" world" → "hello world" Repetition: "hello"*3 → "hellohellohello" Indexing: "world"[3] → "l" Note: python lists are zero-offset Searching: "o" in "hello" → True Python Tutorial B. Zaitlen February 26, 2008 6 / 21

  19. Strings and Numbers Strings A simple string "hello world" Concatenation: "hello"+" world" → "hello world" Repetition: "hello"*3 → "hellohellohello" Indexing: "world"[3] → "l" Note: python lists are zero-offset Searching: "o" in "hello" → True Python Tutorial B. Zaitlen February 26, 2008 6 / 21

  20. Strings and Numbers Numbers Basic math notation: 1.4, 2+2, 2**10,1e10 Note: Integer division floors values: 2/3 → 0, 2./3 → .6667 Math functions require import of math import math math.sqrt(4) → 2.0 from math import * sqrt(4) → 2.0 Python Tutorial B. Zaitlen February 26, 2008 7 / 21

  21. Strings and Numbers Numbers Basic math notation: 1.4, 2+2, 2**10,1e10 Note: Integer division floors values: 2/3 → 0, 2./3 → .6667 Math functions require import of math import math math.sqrt(4) → 2.0 from math import * sqrt(4) → 2.0 Python Tutorial B. Zaitlen February 26, 2008 7 / 21

  22. Strings and Numbers Numbers Basic math notation: 1.4, 2+2, 2**10,1e10 Note: Integer division floors values: 2/3 → 0, 2./3 → .6667 Math functions require import of math import math math.sqrt(4) → 2.0 from math import * sqrt(4) → 2.0 Python Tutorial B. Zaitlen February 26, 2008 7 / 21

  23. Strings and Numbers Numbers Basic math notation: 1.4, 2+2, 2**10,1e10 Note: Integer division floors values: 2/3 → 0, 2./3 → .6667 Math functions require import of math import math math.sqrt(4) → 2.0 from math import * sqrt(4) → 2.0 Python Tutorial B. Zaitlen February 26, 2008 7 / 21

  24. Strings and Numbers Numbers Basic math notation: 1.4, 2+2, 2**10,1e10 Note: Integer division floors values: 2/3 → 0, 2./3 → .6667 Math functions require import of math import math math.sqrt(4) → 2.0 from math import * sqrt(4) → 2.0 Python Tutorial B. Zaitlen February 26, 2008 7 / 21

  25. Strings and Numbers Numbers Basic math notation: 1.4, 2+2, 2**10,1e10 Note: Integer division floors values: 2/3 → 0, 2./3 → .6667 Math functions require import of math import math math.sqrt(4) → 2.0 from math import * sqrt(4) → 2.0 Python Tutorial B. Zaitlen February 26, 2008 7 / 21

  26. Strings and Numbers Numbers Basic math notation: 1.4, 2+2, 2**10,1e10 Note: Integer division floors values: 2/3 → 0, 2./3 → .6667 Math functions require import of math import math math.sqrt(4) → 2.0 from math import * sqrt(4) → 2.0 Python Tutorial B. Zaitlen February 26, 2008 7 / 21

  27. Storing Data Variables and List Dynamically-Typed Variables x = 5 x = 3.14 x = ’text’ x = ’3.14’ Dynamically-Typed Lists numbers = [0,1,2,3,4,5] words = [’compucell’,’workshop’] combo = [12,23,[’text’,’knot’]]+words Python Tutorial B. Zaitlen February 26, 2008 8 / 21

  28. Storing Data List Operations words.append(’almond’) → [’compucell’,’workshop’,’almond’] words.insert(1,’water’) → [’compucell’,’water’, ’workshop’,’almond’] words.reverse() → [’almond’,’workshop’,’water’,’compucell’] words.remove(’water’) → [’almond’,’workshop’,’compucell’] Python Tutorial B. Zaitlen February 26, 2008 9 / 21

  29. Storing Data Dictionaries aka Hash Tables, Associative Arrays, Lookup Tables dictionary = {’indefatigable’:’untiring’, ’intrepid’:’fearlessness’,’dissemble’:’simulate’} constants = {’pi’:3.1415, ’e’:2.7182, ’phi’:1.6180} com_dict = {1:[1,2,3],2:[1,0,3],3:[0,4,5]} Python Tutorial B. Zaitlen February 26, 2008 10 / 21

  30. Storing Data Dictionaries Operations com_dict.keys() → [1,2,3] com_dict.values() → [[1, 2, 3], [1, 0, 3], [0, 4, 5]] Accessing Members com_dict[3] → [0,4,5] constants[’phi’] → 1.6180 Python Tutorial B. Zaitlen February 26, 2008 11 / 21

  31. Control Structures If, While, and For The If... The While... if condition: while condition: statements statements elif condition: statements The For... else condition: statements for var in sequence: statements Note: Beware of Python’s requirement for indentation Python Tutorial B. Zaitlen February 26, 2008 12 / 21

  32. Control Structures Control Structure Examples The If... The While... if (x > 0): while (1): print "Positive" print "Always true" elif (x < 0): print "Negative" else: Print "Zero" The For... for i in range(5): print i Python Tutorial B. Zaitlen February 26, 2008 13 / 21

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend