Sei sulla pagina 1di 3

Python Tutorial Workshop Spring Qtr 2011!

Text: Python Tutorial (from website)

Ch 4 More Control Flow Tools

# if statements (and ʻelse ifʼ using elif)


*** this is where code indentation is very very important***
***do not confuse spaces with tabs!!!!!!!***

# following the example (in your python window), type”


>>> x = int(raw_input("Please enter an integer: "))

# then type an integer (this will be stored in variable x)


# then type
>>> if x < 0:
# once you hit return, you will find ʻ...ʼ meaning you are still writing to this if loop
# MAKE SURE YOU THEN PRESS TAB BEFORE YOU CONTINUE TYPING
... x = 0
... print 'Negative changed to zero'
... elif x == 0:
... print 'Zero'
... elif x == 1:
... print 'Single'
... else:
... print 'More'
...

# with you press return at this point (youʼll need one after the last ʻprintʼ statement and
then hit return again to tell python you are done typing the code), the if loop will run and
print out the answer (conditional upon the input integer you gave the program)

# for statements will iterate over a given variable (e.g. list, array) progressing from the
left of the variable to its end (low to high indices)
# # remember that some variable types are immutable
# # itʼs also recommended that you donʼt change the variable in the for loop....
rather you should MAKE A COPY of the variable so you then change the copy

# if your original code is this:


>>> # measure strings
... a = ['need','programming','cookbook']
>>> for x in a[:]:
... print x, len(x)
... if len(x) > 6: a.insert(0,x)

! 1
Python Tutorial Workshop Spring Qtr 2011!
Text: Python Tutorial (from website)

...
need 4
programming 11
cookbook 8

# then you can check at how you altered the list variable x:
>>> a
['cookbook', 'programming', 'need', 'programming', 'cookbook']

# you can then look at the index value for each element using the range() and len()
commands
>>> range(8)
[0, 1, 2, 3, 4, 5, 6, 7]

>>> range(0,12,2)
[0, 2, 4, 6, 8, 10]

# then use:
>>> a = ['biopython','may','beat','bioperl','or','maybe','not']
>>> for i in range(len(a)):
... print i, a[i]
...
0 biopython
1 may
2 beat
3 bioperl
4 or
5 maybe
6 not

# the enumerate() function is probably a better way to do this... weʼll cover this later in
the Looping Section

# you can exit out of a for or while loop using break


# you can skip to the next part of the loop using the continue statement

# Writing/defining functions
>>> def square(n):
... print n*n
...

>>> x = 2
>>> square(x)
4

! 2
Python Tutorial Workshop Spring Qtr 2011!
Text: Python Tutorial (from website)

# the def function is to define a function with the structure:


# def functionName(arguments)
# arguments are passed by value (as an object reference and not the actual value of the
object)
# you can set default values for arguments
# def functionName(outlier=ʼYesʼ, mean=0)
# # where ʻoutlierʼ is called a keyword and can be used to set arguments:
# def functionName(mean=0, outlier=ʼYesʼ)
# def functionName(ʻYesʼ, 0)

# use return to return a function result

! 3

Potrebbero piacerti anche