CS 696 Lecture 3: Introduction to Grid Computing - Control Statements and Functions, Study notes of Computer Science

The third lecture notes for the cs 696 - introduction to ci & grid computing course at san diego state university. It covers the topics of control statements (if, while, for) and functions in python programming language.

Typology: Study notes

Pre 2010

Uploaded on 03/28/2010

koofers-user-sva
koofers-user-sva 🇺🇸

10 documents

1 / 31

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 696 - Introduction to Grid Computing:
Lecture #3
Mary Thomas
San Diego State
Thursday, 25-Jan-07
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download CS 696 Lecture 3: Introduction to Grid Computing - Control Statements and Functions and more Study notes Computer Science in PDF only on Docsity!

CS 696 - Introduction to Grid Computing:

Lecture

Mary Thomas

San Diego State

Thursday, 25-Jan-

Basics

 Mailing List: please sign up

 http://scilists.sdsu.edu/mailman/listinfo.cgi/cs696-grid

 References:

 Python Tutorial & Docs, Guido van Rossum,

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

 Learning Python, Lutz & Ascher, O'Reilly, 1999

 Web Programming in Python, by Thiruvathukal, et

al.

 Out of print, but we will get electronic copy soon.

Control Statements

if while for Basic Structure

HeaderLine:

block

Block are indicated by indentation Space Tab Indentation always indicates a block Following does not compile print "Good Start" print "This is a compile error"

Importing modules (quick lesson)

#Fibonacci numbers module

#return Fiboacci series up to n

def fib(n):

a, b = 0, 1

while b < n :

print b,

a, b = b, a+b

def fib2(n):

result = []

Print ‘fib2’

a, b = 0, 1

while b < n :

result.append(b)

a, b = b, a+b

return result

>>> import fibo

>>> fibo.fin(20)

>>> fibo.fib2(50)

[1, 1, 2, 3, 5, 8, 13, 21, 34]

# edit changes fibo.py

>>>reload(fibo)

>>> fibo.fib2(50)

fib

[1, 1, 2, 3, 5, 8, 13, 21, 34]

[gidget:% chmod 755 fibo.py

[gidget:% ls fibo.py

-rwxr-xr-x … fibo.py

while

while : else: #optional else is run if didn't exit from loop with a break Example

>>> x='cat'

>>> while x:

... print x

... x=x[1:]

... else:

... print 'else'

... print 'the end'

cat

at

t

else

the end

break, continue, pass

break Jump out of closest enclosing loop continue Jump to top of the closest enclosing loop pass Does nothing, empty statement

while x<5: ... x=x+ ... print x ...

1 2 3 4 5

while x<5: ... x=x+ ... if x == 3: ... break ... print x ... x= while x<5: ... x=x+ ... if x == 3: ... break ... print x ... 1 2

Range

range(5) [0, 1, 2, 3, 4] range(5, 10) [5, 6, 7, 8, 9] range(5, 10, 3) [5, 8] for k in range(10): print k, #prints 1 2 3 4 5 6 7 8 9 for k in range(1, 20, 2): print k #prints odd number < than 20 list = ['cat', 'rat', 'bat']

for k in range(len(list)): print k, list[k], range(upto) range(start, upto) range(start, upto, increment)

A list with successive integers

Functions

def fibonacci(n): result = [] a, b = 0, 1 while b < n: result.append(b) a,b = b,a+b return result def doesNothing(): pass print fibonacci(20) print doesNothing() Output [1, 1, 2, 3, 5, 8, 13] None Can define default arg values def f(a=5): or use key-value pairs Def f( action=‘go’ )

Scope of Names

What does this print? x = 10 def whichValue(x): print x whichValue(5)

Local Variables to a Function

Each call to a function creates a new local scope Arguments to the function are local Assigned names are local, unless declared global x = 10 def printGlobal(): print x printGlobal() # prints 10 x = 5 printGlobal() # prints 5 x = 10 def printLocal(): x = 5 #Makes a local x print x

>>> printGlobal()

>>> printLocal()

>>> printGlobal()

Global Declaration Example

x = 10 def globalDeclaration(): global x x = 5 globalDeclaration() print x # prints 5 Seems like something to avoid

Recursive Functions

def factorial(x): if x == 1: return 1 else: return x * factorial(x - 1) print factorial(4)

Default Parameter Values

def defaultValues(x, y=10): return x + y defaultValues(2,3) #returns 5 defaultValues(2) #returns 12 ouch = 1 def tricky(x = ouch ): print x tricky() ouch = 2 tricky() Output 1 1

Positional Parameter Passing

def concat(x, y, z): return x + y + z concat('a', 'b', 'c') #'abc’ concat('a', z='b', y='c') #'acb’ concat('a', y='c',z='b') #'acb’ concat(y='a', x='c',z='b') #'cab'