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

python tutorial
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Python Tutorial

Benjamin L. Zaitlen

Biocomplexity Institute, Indiana University

February 26, 2008

slide-2
SLIDE 2

Outline

1

Introduction

2

Interactive Shell

3

Strings and Numbers

4

Storing Data

5

Control Structures

6

Functions

7

Classes

8

CompuCell Classes

Python Tutorial

  • B. Zaitlen

February 26, 2008 2 / 21

slide-3
SLIDE 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

slide-4
SLIDE 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

slide-5
SLIDE 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

slide-6
SLIDE 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

slide-7
SLIDE 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

slide-8
SLIDE 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

slide-9
SLIDE 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

slide-10
SLIDE 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

slide-11
SLIDE 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

slide-12
SLIDE 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

slide-13
SLIDE 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

slide-14
SLIDE 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

slide-15
SLIDE 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

slide-16
SLIDE 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

slide-17
SLIDE 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

slide-18
SLIDE 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

slide-19
SLIDE 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

slide-20
SLIDE 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

slide-21
SLIDE 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

slide-22
SLIDE 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

slide-23
SLIDE 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

slide-24
SLIDE 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

slide-25
SLIDE 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

slide-26
SLIDE 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

slide-27
SLIDE 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

slide-28
SLIDE 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

slide-29
SLIDE 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

slide-30
SLIDE 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

slide-31
SLIDE 31

Control Structures

If, While, and For

The If... if condition: statements elif condition: statements else condition: statements Note:Beware of Python’s requirement for indentation The While... while condition: statements The For... for var in sequence: statements

Python Tutorial

  • B. Zaitlen

February 26, 2008 12 / 21

slide-32
SLIDE 32

Control Structures

Control Structure Examples

The If... if (x > 0): print "Positive" elif (x < 0): print "Negative" else: Print "Zero" The While... while (1): print "Always true" The For... for i in range(5): print i

Python Tutorial

  • B. Zaitlen

February 26, 2008 13 / 21

slide-33
SLIDE 33

Functions

Defining Functions

No Arguments def Hello_World(): print ’Hello World’ def return_5(): return 5 With Arguments You can pass a single variable: def one_var(x): print ’You passed the variable: %s’ %(x) You can also pass a list: def list_func(list): for i in list: print i, Note:Python passes all agruments by reference. This means changing the value in the function will change the value when the program exits the function

Python Tutorial

  • B. Zaitlen

February 26, 2008 14 / 21

slide-34
SLIDE 34

Functions

Calling Functions

No Arguments >>>Hello_World() Hello World >>>return_5() 5 With Arguments Passing a single variable: >>>x = ’my variable’ >>>one_var(x) my variable Passing a list: list = [0,1,2,3,4] >>>list_func(list): 0 1 2 3 4 In General def name(arg1,arg2,...): statements return expression

Python Tutorial

  • B. Zaitlen

February 26, 2008 15 / 21

slide-35
SLIDE 35

Classes

The Container for Everything

What is a Class ? Classes contain stuff. What kind of stuff ? Anything! What are the good for ? Almost everything What can they store ? Classes can store everything: variables, lists, dictionaries, functions,

  • ther lists, etc

Python Tutorial

  • B. Zaitlen

February 26, 2008 16 / 21

slide-36
SLIDE 36

Classes

Typical Python Class

Defining class Complex: var_every = 0 #class variable def __init__(self,var = 0): self.instance = var #instance variable def class_print(self): print "My Value is: %s" %(self.instance)

Python Tutorial

  • B. Zaitlen

February 26, 2008 17 / 21

slide-37
SLIDE 37

Classes

Classes Continued...

General Class class name(baseclass1, baseclass2,...): statements def name(self, arg1, arg2,...): more statements Example: The Complex Variables One object contains both the real and imaginary parts of a complex

  • variable. Can be achieved with a list, dictionary, or class. Class seems

to make the most sense...

Python Tutorial

  • B. Zaitlen

February 26, 2008 18 / 21

slide-38
SLIDE 38

Classes

The Complex Class

Defining class Complex: def __init__(self,real,imag): self.r = real self.i = imag def cprint(self): if(self.i < 0): print "%.3f%.3f*i" %(self.r, self.i) else: print "%.3f+%.3f*i" %(self.r, self.i) self is like this and must be passed to member functions

Python Tutorial

  • B. Zaitlen

February 26, 2008 19 / 21

slide-39
SLIDE 39

Classes

Complex Class Continued...

Initializing Let’s initalize 1-i

Python Tutorial

  • B. Zaitlen

February 26, 2008 20 / 21

slide-40
SLIDE 40

Classes

Complex Class Continued...

Initializing Let’s initalize 1-i Complex(1,-1)

Python Tutorial

  • B. Zaitlen

February 26, 2008 20 / 21

slide-41
SLIDE 41

Classes

Complex Class Continued...

Initializing Let’s initalize 1-i Complex(1,-1) We need to store the class. Simple: comp = Complex(1,-1). Now we have access to the variables r and i and the member function cprint()

Python Tutorial

  • B. Zaitlen

February 26, 2008 20 / 21

slide-42
SLIDE 42

Classes

Complex Class Continued...

Initializing Let’s initalize 1-i Complex(1,-1) We need to store the class. Simple: comp = Complex(1,-1). Now we have access to the variables r and i and the member function cprint() Accessing Members Use the . operator to access variables comp.r comp.i

Python Tutorial

  • B. Zaitlen

February 26, 2008 20 / 21

slide-43
SLIDE 43

Classes

Complex Class Continued...

Initializing Let’s initalize 1-i Complex(1,-1) We need to store the class. Simple: comp = Complex(1,-1). Now we have access to the variables r and i and the member function cprint() Accessing Members Use the . operator to access variables comp.r comp.i Use the . operator to access functions comp.cprint()

Python Tutorial

  • B. Zaitlen

February 26, 2008 20 / 21

slide-44
SLIDE 44

CompuCell Classes

The Cell

What are the attributes of a cell ? Type

Python Tutorial

  • B. Zaitlen

February 26, 2008 21 / 21

slide-45
SLIDE 45

CompuCell Classes

The Cell

What are the attributes of a cell ? Type Volume

Python Tutorial

  • B. Zaitlen

February 26, 2008 21 / 21

slide-46
SLIDE 46

CompuCell Classes

The Cell

What are the attributes of a cell ? Type Volume Center of Mass These are a few of the variables inside the cell class built for

  • CompuCell. We can interface with this class through Python

Python Tutorial

  • B. Zaitlen

February 26, 2008 21 / 21