====== About Python II ====== ===== Python is imperative ===== A language is **imperative** if it uses //sequence// as the default control mechanism with //control flow// (notably //selection// and //repetition//), if it uses //variables// with //assignment//, and if it employs //statements//. Python supports all of these. ==== Sequence and statements ==== We saw in an earlier exercise that Python will execute statements in a script one-by-one, one after the other. print "Hello. Watch as I compute a value:" print "(2012 - 13) / 3 is", (2012 - 13) / 3 print "Evil!!!" This is //sequence//. ==== Variables and assignment ==== We also saw in an earlier exercise that Python uses variables and assignment. b = 1.5708 * 2.0 foo = "Hello world" print b print foo ==== Control flow==== We will now see that Python has a number of control flow statements for selection and repetition. === Selection === == if == x = 100 if x > 0: print 'x is a positive number.' print 'That means it is greater than zero.' if x == 666: print 'Ooooh ... so evil!' print 'Are you listening to Black Sabbath or something?' == if-else == x = 100 if x > 0: print 'x is a positive number.' print 'That means it is greater than zero.' else: print 'x is not a positive number.' print 'That means it is not greater than zero.' == nested if-else == x = 100 if x > 0: print 'x is a positive number.' print 'That means it is greater than zero.' elif x < 0: print 'x is a negative number.' print 'That means it is less than zero.' else: print 'x is zero.' print 'Does that need elaboration?' Note that Python does **not** use semicolons at the end of statements. A statement is terminated with a new line. Also note the indentation used above. **Indentation has syntactic value in Python.** It is used to indicate blocks. In other words, these two code fragments below are //not// the same: if x == 666: print 'Ooooh ... so evil!' print 'Are you listening to Black Sabbath or something?' if x == 666: print 'Ooooh ... so evil!' print 'Are you listening to Black Sabbath or something?' It is traditional to use four spaces for each level of indentation. Some people use tabs. Pick one or the other but //do not use both//. === Repetition === == Simple repetition == i = 0 while i < 5: print i i = i + 1 == Iteration == for i in range(5): print i num_list = ['one', 'two', 'three', '4', 'five', '6', '7even', 'ate'] for num in num_list: print "I", num, "a skunk" ===== Abstracting processes with functions ===== When you create a meaningful and modular process, you typically want to abstract it out of your code as a function. # A function that prints from 1 to n (inclusive). def count_to(n): i = 1 while i <= n: print i i = i + 1 # A function that returns true if a is divisible by b, false otherwise. def is_divisible(a, b): if a % b == 0: return True else: return False x = 99 y = 11 count_to(y) if is_divisible(x, y): print "Foo" else: print "Bar" ===== Python is functional ===== **Functional languages** rely on the //function call// as the default control mechanism. They also typically replace iteration/repetition with //recursion// and statements with //evaluation// (of functions). Functional languages blur the distinction between data and instruction ("functions are first class objects"). After you have understood functional programming better, you can return to Python to discover how it offers everything you need to program functionally.((Or you can just read [[http://gnosis.cx/publish/programming/charming_python_13.html|Charming Python #13]])) But as a start, try the following exercise. ==== Exercise: Function as a type ==== Execute the following in an interactive session and observe the results. def count_to(n): i = 1 while i <= n: print i i = i + 1 type(count_to) Notice that the function ''count_to'' is typed just like anything else in Python. It's type is '''function'''. Copyright © 2011 Mithat Konar. All rights reserved.