Содержание
- 2. Overview History Installing & Running Python Names & Assignment Sequences types: Lists, Tuples, and Strings Mutability
- 3. Brief History of Python Invented in the Netherlands, early 90s by Guido van Rossum Named after
- 4. Python’s Benevolent Dictator For Life “Python is an experiment in how much freedom program-mers need. Too
- 5. http://docs.python.org/
- 6. The Python tutorial is good!
- 7. Running Python
- 8. The Python Interpreter Typical Python implementations offer both an interpreter and compiler Interactive interface to Python
- 9. Installing Python is pre-installed on most Unix systems, including Linux and MAC OS X The pre-installed
- 10. IDLE Development Environment IDLE is an Integrated DeveLopment Environ-ment for Python, typically used on Windows Multi-window
- 11. Editing Python in Emacs Emacs python-mode has good support for editing Python, enabled enabled by default
- 12. Running Interactively on UNIX On Unix… % python >>> 3+3 6 Python prompts with ‘>>>’. To
- 13. Running Programs on UNIX Call python program via the python interpreter % python fact.py Make a
- 14. Example ‘script’: fact.py #! /usr/bin/python def fact(x): """Returns the factorial of its argument, assumed to be
- 15. Python Scripts When you call a python program from the command line the interpreter evaluates each
- 16. Example of a Script #! /usr/bin/python """ reads text from standard input and outputs any email
- 17. results python> python email0.py bill@msft.com gates@microsoft.com steve@apple.com bill@msft.com python>
- 18. Getting a unique, sorted list import re from sys import stdin pat = re.compile(r'[-\w][-.\w]*@[-\w][-\w.]+[a-zA-Z]{2,4}’) # found
- 19. results python> python email2.py bill@msft.com gates@microsoft.com steve@apple.com python>
- 20. Simple functions: ex.py """factorial done recursively and iteratively""" def fact1(n): ans = 1 for i in
- 21. Simple functions: ex.py 671> python Python 2.5.2 … >>> import ex >>> ex.fact1(6) 1296 >>> ex.fact2(200)
- 22. The Basics
- 23. A Code Sample (in IDLE) x = 34 - 23 # A comment. y = “Hello”
- 24. Enough to Understand the Code Indentation matters to code meaning Block structure indicated by indentation First
- 25. Basic Datatypes Integers (default for numbers) z = 5 / 2 # Answer 2, integer division
- 26. Whitespace Whitespace is meaningful in Python: especially indentation and placement of newlines Use a newline to
- 27. Comments Start comments with #, rest of line is ignored Can include a “documentation string” as
- 28. Assignment Binding a variable in Python means setting a name to hold a reference to some
- 29. Naming Rules Names are case sensitive and cannot start with a number. They can contain letters,
- 30. Naming conventions The Python community has these recommend-ed naming conventions joined_lower for functions, methods and, attributes
- 31. Assignment You can assign to multiple names at the same time >>> x, y = 2,
- 32. Accessing Non-Existent Name Accessing a name before it’s been properly created (by placing it on the
- 33. Sequence types: Tuples, Lists, and Strings
- 34. Sequence Types Tuple: (‘john’, 32, [CMSC]) A simple immutable ordered sequence of items Items can be
- 35. Similar Syntax All three sequence types (tuples, strings, and lists) share much of the same syntax
- 36. Sequence Types 1 Define tuples using parentheses and commas >>> tu = (23, ‘abc’, 4.56, (2,3),
- 37. Sequence Types 2 Access individual members of a tuple, list, or string using square bracket “array”
- 38. Positive and negative indices >>> t = (23, ‘abc’, 4.56, (2,3), ‘def’) Positive index: count from
- 39. Slicing: return copy of a subset >>> t = (23, ‘abc’, 4.56, (2,3), ‘def’) Return a
- 40. Slicing: return copy of a =subset >>> t = (23, ‘abc’, 4.56, (2,3), ‘def’) Omit first
- 41. Copying the Whole Sequence [ : ] makes a copy of an entire sequence >>> t[:]
- 42. The ‘in’ Operator Boolean test whether a value is inside a container: >>> t = [1,
- 43. The + Operator The + operator produces a new tuple, list, or string whose value is
- 44. The * Operator The * operator produces a new tuple, list, or string that “repeats” the
- 45. Mutability: Tuples vs. Lists
- 46. Lists are mutable >>> li = [‘abc’, 23, 4.34, 23] >>> li[1] = 45 >>> li
- 47. Tuples are immutable >>> t = (23, ‘abc’, 4.56, (2,3), ‘def’) >>> t[2] = 3.14 Traceback
- 48. Operations on Lists Only >>> li = [1, 11, 3, 4, 5] >>> li.append(‘a’) # Note
- 49. The extend method vs + + creates a fresh list with a new memory ref extend
- 50. Operations on Lists Only Lists have many methods, including index, count, remove, reverse, sort >>> li
- 51. Operations on Lists Only >>> li = [5, 2, 6, 8] >>> li.reverse() # reverse the
- 52. Tuple details The comma is the tuple creation operator, not parens >>> 1, (1,) Python shows
- 54. Скачать презентацию