User Tools

Site Tools


Sidebar

~~NOTOC~~ * [[:start|Wiki home]] * [[http://mkonar.org/dogus|Main site]] * [[:come101:]] * [[:come101-lab:]] * [[:csharp:]] * [[:python:]] * [[:rad:]] * [[:vcs2008:]] * [[:misc:]]

python:about_python_ii

====== About Python II ====== ===== Python is imperative ===== A language is **imperative** if it uses //sequence// as the default control mechanism with //flow control// (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. <code python> print "Hello. Watch as I compute a value:" print "(2012 - 13) / 3 is", (2012 - 13) / 3 print "Evil!!!" </code> This is //sequence//. ==== Variables and assignment ==== We also saw in an earlier exercise that Python uses variables and assignment. <code python> b = 1.5708 * 2.0 foo = "Hello world" print b print foo </code> ==== Flow control ==== We will now see that Python has a number of flow-control statements for selection and repetition. === Selection === == if == <code python> 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?' </code> == if-else == <code python> 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.' </code> == nested if-else == <code python> 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?' </code> 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: <code python> 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?' </code> 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 == <code python> i = 0 while i < 5: print i i = i + 1 </code> == Iteration == <code python> 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" </code> ===== 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. <code python> # 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" </code> ===== 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. <code python> def count_to(n): i = 1 while i <= n: print i i = i + 1 type(count_to) </code> 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.

python/about_python_ii.txt · Last modified: 2013-05-31 07:54 (external edit)