Содержание
- 2. Advanced Programming Spring 2002 Introduction Most recent popular (scripting/extension) language although origin ~1991 heritage: teaching language
- 3. Advanced Programming Spring 2002 Python philosophy Coherence not hard to read, write and maintain power scope
- 4. Advanced Programming Spring 2002 Python features Lutz, Programming Python
- 5. Advanced Programming Spring 2002 Python features Lutz, Programming Python
- 6. Advanced Programming Spring 2002 Python elements from C++, Modula-3 (modules), ABC, Icon (slicing) same family as
- 7. Advanced Programming Spring 2002 Uses of Python shell tools system admin tools, command line programs extension-language
- 8. Advanced Programming Spring 2002 What not to use Python (and kin) for most scripting languages share
- 9. Advanced Programming Spring 2002 Using python /usr/local/bin/python #! /usr/bin/env python interactive use Python 1.6 (#1, Sep
- 10. Advanced Programming Spring 2002 Python structure modules: Python source files or C extensions import, top-level via
- 11. Advanced Programming Spring 2002 First example #!/usr/local/bin/python # import systems module import sys marker = '::::::'
- 12. Advanced Programming Spring 2002 Basic operations Assignment: size = 40 a = b = c =
- 13. Advanced Programming Spring 2002 String operations concatenate with + or neighbors word = 'Help' + x
- 14. Advanced Programming Spring 2002 Lists lists can be heterogeneous a = ['spam', 'eggs', 100, 1234, 2*2]
- 15. Advanced Programming Spring 2002 Basic programming a,b = 0, 1 # non-zero = true while b
- 16. Advanced Programming Spring 2002 Control flow: if x = int(raw_input("Please enter #:")) if x x =
- 17. Advanced Programming Spring 2002 Control flow: for a = ['cat', 'window', 'defenestrate'] for x in a:
- 18. Advanced Programming Spring 2002 Loops: break, continue, else break and continue like C else after loop
- 19. Advanced Programming Spring 2002 Do nothing pass does nothing syntactic filler while 1: pass
- 20. Advanced Programming Spring 2002 Defining functions def fib(n): """Print a Fibonacci series up to n.""" a,
- 21. Advanced Programming Spring 2002 Functions: default argument values def ask_ok(prompt, retries=4, complaint='Yes or no, please!'): while
- 22. Advanced Programming Spring 2002 Keyword arguments last arguments can be given as keywords def parrot(voltage, state='a
- 23. Advanced Programming Spring 2002 Lambda forms anonymous functions may not work in older versions def make_incrementor(n):
- 24. Advanced Programming Spring 2002 List methods append(x) extend(L) append all items in list (like Tcl lappend)
- 25. Advanced Programming Spring 2002 List methods count(x) how many times x appears in list sort() sort
- 26. Advanced Programming Spring 2002 Functional programming tools filter(function, sequence) def f(x): return x%2 != 0 and
- 27. Advanced Programming Spring 2002 List comprehensions (2.0) Create lists without map(), filter(), lambda = expression followed
- 28. Advanced Programming Spring 2002 List comprehensions cross products: >>> vec1 = [2,4,6] >>> vec2 = [4,3,-9]
- 29. Advanced Programming Spring 2002 List comprehensions can also use if: >>> [3*x for x in vec
- 30. Advanced Programming Spring 2002 del – removing list items remove by index, not value remove slices
- 31. Advanced Programming Spring 2002 Tuples and sequences lists, strings, tuples: examples of sequence type tuple =
- 32. Advanced Programming Spring 2002 Tuples Tuples may be nested >>> u = t, (1,2) >>> u
- 33. Advanced Programming Spring 2002 Tuples Empty tuples: () >>> empty = () >>> len(empty) 0 one
- 34. Advanced Programming Spring 2002 Tuples sequence unpacking ? distribute elements across variables >>> t = 123,
- 35. Advanced Programming Spring 2002 Dictionaries like Tcl or awk associative arrays indexed by keys keys are
- 36. Advanced Programming Spring 2002 Dictionaries no particular order delete elements with del >>> del tel['foo'] keys()
- 37. Advanced Programming Spring 2002 Conditions can check for sequence membership with is and is not: >>>
- 38. Advanced Programming Spring 2002 Conditions Can assign comparison to variable: >>> s1,s2,s3='', 'foo', 'bar' >>> non_null
- 39. Advanced Programming Spring 2002 Comparing sequences unlike C, can compare sequences (lists, tuples, ...) lexicographical comparison:
- 40. Advanced Programming Spring 2002 Comparing sequences (1,2,3) [1,2,3] 'ABC' (1,2,3) == (1.0,2.0,3.0) (1,2)
- 41. Advanced Programming Spring 2002 Modules collection of functions and variables, typically in scripts definitions can be
- 42. Advanced Programming Spring 2002 Modules import module: import fibo Use modules via "name space": >>> fibo.fib(1000)
- 43. Advanced Programming Spring 2002 Modules function definition + executable statements executed only when module is imported
- 44. Advanced Programming Spring 2002 Module search path current directory list of directories specified in PYTHONPATH environment
- 45. Advanced Programming Spring 2002 Compiled Python files include byte-compiled version of module if there exists fibo.pyc
- 46. Advanced Programming Spring 2002 Standard modules system-dependent list always sys module >>> import sys >>> sys.p1
- 47. Advanced Programming Spring 2002 Module listing use dir() for each module >>> dir(fibo) ['___name___', 'fib', 'fib2']
- 48. Advanced Programming Spring 2002 Classes mixture of C++ and Modula-3 multiple base classes derived class can
- 49. Advanced Programming Spring 2002 Classes classes (and data types) are objects built-in types cannot be used
- 50. Advanced Programming Spring 2002 Class definitions Class ClassName: ... must be executed can be executed conditionally
- 51. Advanced Programming Spring 2002 Namespaces mapping from name to object: built-in names (abs()) global names in
- 52. Advanced Programming Spring 2002 Namespaces scope = textual region of Python program where a namespace is
- 53. Advanced Programming Spring 2002 Class objects obj.name references (plus module!): class MyClass: "A simple example class"
- 54. Advanced Programming Spring 2002 Class objects class instantiation: >>> x = MyClass() >>> x.f() 'hello world'
- 55. Advanced Programming Spring 2002 Instance objects attribute references data attributes (C++/Java data members) created dynamically x.counter
- 56. Advanced Programming Spring 2002 Method objects Called immediately: x.f() can be referenced: xf = x.f while
- 57. Advanced Programming Spring 2002 Notes on classes Data attributes override method attributes with the same name
- 58. Advanced Programming Spring 2002 Another example bag.py class Bag: def __init__(self): self.data = [] def add(self,
- 59. Advanced Programming Spring 2002 Another example, cont'd. invoke: >>> from bag import * >>> l =
- 60. Advanced Programming Spring 2002 Inheritance class DerivedClassName(BaseClassName) ... search class attribute, descending chain of base classes
- 61. Advanced Programming Spring 2002 Multiple inheritance class DerivedClass(Base1,Base2,Base3): depth-first, left-to-right problem: class derived from two classes
- 62. Advanced Programming Spring 2002 Private variables No real support, but textual replacement (name mangling) __var is
- 63. Advanced Programming Spring 2002 ~ C structs Empty class definition: class Employee: pass john = Employee()
- 64. Advanced Programming Spring 2002 Exceptions syntax (parsing) errors while 1 print 'Hello World' File " ",
- 65. Advanced Programming Spring 2002 Handling exceptions while 1: try: x = int(raw_input("Please enter a number: "))
- 66. Advanced Programming Spring 2002 Handling exceptions try.py import sys for arg in sys.argv[1:]: try: f =
- 68. Скачать презентацию