Python学习笔记
the best way to get started is to get started
注:摘抄于《O'Reilly - Learning Python 4th Edition》,持续更新中......
Python program structure
- Programs are composed of modules.
- Modules contain statements.
- Statements contain expressions
- Expressions create and process object
Why use Python Built-in Types?
-
Built-in objects make programs easy to write
If you’ve used lower-level languages such as C or C++, you know that much of your work centers on implementing objects—also known as data structures—to represent the components in your application’s domain. You need to lay out memory structures,manage memory allocation, implement search and access routines, and so on. Thesechores are about as tedious (and error-prone) as they sound, and they usually distract from your program’s real goals. Unlike C and C++, Python provide powerful built-in types such as list and dictionaries, thereforeyou don't need to build wheels by yourself
. -
Built-in objects are components of extensions
Use Built-in type to implement your own object. -
Built-in objects are often more efficient than custom data structures
Python’s built-in types employ already optimized data structure algorithms that are implemented in C for speed. -
Built-in objects are a standard part of the language
Python's Core Data Type
Object Type | Example literals/creation |
---|---|
Numbers | 1234, 3.1415, 3+4j, Decimal, Fraction |
Strings | 'spam', "guido's", b'ax01c' |
Lists | [1, [2, 'three'], 4] |
Dictionaries | {'food': 'spam', 'taste': 'yum'} |
Tuples | (1, 'spam', 4, 'U') |
Files | myfile = open('eggs', 'r') |
Sets | set('abc'), {'a', 'b', 'c'} |
Other core types | Booleans, types, None |
Program unit types | Functions, modules, classes |
Implementation-related types | Compiled code, stack tracebacks |
Note:
- There are no type declarations in Python, the syntax of the expressions you run determines the types of objects you create and use.
- Once you create an object, you bind its operation set for all time——you can perform only string operations on a string and list operations on a list
- Python is
dynamically typed
(it keeps track of types for you automatically instead of requiring declaration code), but it is alsostrongly typed
(you can perform on an object only operations that are valid for its type).
Numbers
Three type number type:
- Integers
- Float-point number
- exotic numeric types (complex numbers with imaginary parts,
fixed-precision decimals, rational fractions with numerator and denominator, and full-featured sets)
Useful module: math
, random
>>>import math
>>>import random
>>> math.pi
3.1415926535897931
>>> math.sqrt(85)
9.2195444572928871
>>> random.random()
0.59268735266273953
>>> random.choice([1, 2, 3, 4])
1
Strings
Strings are immutable
in Python, are sequences
and it's element can be accessed by index
sequence operations
indexing(索引)
S[index]
>>> S = 'Spam'
>>> len(S) # Length
4
>>> S[0] # The first item in S, indexing by zero-based position
'S'
>>> S[1] # The second item from the left
'p'
slicing(切片)
S[I:J]: means “give me everything in X from offset I up to but not including offset J.”In a slice,
the left bound defaults to zero, and the right bound defaults to the length of the sequence being sliced.
>>> S # A 4-character string
'Spam'
>>> S[1:3] # Slice of S from offsets 1 through 2 (not 3)
'pa'
concatenation(串联) and repetition(重复)
>>> S
Spam'
>>> S + 'xyz' # Concatenation
'Spamxyz'
>>> S # S is unchanged
'Spam'
>>> S * 8 # Repetition
'SpamSpamSpamSpamSpamSpamSpamSpam'
Note: These operation above are konwn as sequence operations
,that is, these operations will work on other sequences in Python as well, including lists and tuples.
Type-Specific Methods
- find(查找) and replace(替换)
>>> S.find('pa') # Find the offset of a substring
1
>>> S
'Spam'
>>> S.replace('pa', 'XYZ') # Replace occurrences of a substring with another
'SXYZm'
>>> S
'Spam'
#Despite the names of these string methods, we are not changing the original
#strings here, but creating new strings as the results—because strings are immutable
- others: split(),upper(),isalpha(),isdigit(),strip()...
>>> line = 'aaa,bbb,ccccc,dd'
>>> line.split(',') # Split on a delimiter into a list of substrings
['aaa', 'bbb', 'ccccc', 'dd']
>>> S = 'spam'
>>> S.upper() # Upper- and lowercase conversions
'SPAM'
>>> S.isalpha() # Content tests: isalpha, isdigit, etc.
True
>>> line = 'aaa,bbb,ccccc,dd
'
>>> line = line.rstrip() # Remove whitespace characters on the right side
>>> line
'aaa,bbb,ccccc,dd'
formatting
(格式化)
Available as both an expression (the original) and a string method call
>>> '%s, eggs, and %s' % ('spam', 'SPAM!') # Formatting expression (all)
'spam, eggs, and SPAM!'
>>> '{0}, eggs, and {1}'.format('spam', 'SPAM!') # Formatting method (2.6, 3.0)
'spam, eggs, and SPAM!'
Get help
dir(object)
call the built-in dir function, which returns a list of all the attributes available for a given object
>>> dir(S)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__',
'__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '_formatter_field_name_split', '_formatter_parser',
'capitalize', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find',
'format', 'index', 'isalnum','isalpha', 'isdecimal', 'isdigit', 'isidentifier',
'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join',
'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind',
'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines',
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
Note: In general, leading and trailing double underscores(__) is the naming pattern Python uses for implementation details. The names without the underscores in this list are the callable methods on string objects.带双下划线的方法相当于C++或者Java中的Private方法
help()
The dir function simply gives the methods’ names. To ask what they do, you can pass them to the help function:
>>> help(S.replace)
Help on built-in function replace:
replace(...)
S.replace (old, new[, count]) -> str
Return a copy of S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
Note help() is one of interface of PyDoc——a tool for extracting documentation from objects
Pattern Matching
This module re has analogous calls for searching, splitting, and replacement, but because we can use patterns to specify substrings:
>>> import re
>>> match = re.match('Hello[ ]*(.*)world', 'Hello Python world')
>>> match.group(1)
'Python '
>>> match = re.match('/(.*)/(.*)/(.*)', '/usr/home/lumberjack')
>>> match.groups()
('usr', 'home', 'lumberjack')
Lists
- Mutable
- Ordered
- Have no fixed type constraint
- Have no fixed size
Sequence operations
>>> L = [123, 'spam', 1.23] # A list of three different-type objects
>>> len(L) # Number of items in the list
3
>>> L[0] # Indexing by position
123
>>> L[:-1] # Slicing a list returns a new list
[123, 'spam']
>>> L + [4, 5, 6] # Concatenation makes a new list too
[123, 'spam', 1.23, 4, 5, 6]
Type-Specific Methods
- append
expands the list’s size and inserts an item at the end.
>>> L.append('NI') # Growing: add object at end of list
>>> L
[123, 'spam', 1.23, 'NI']
- pop
removes an item at a given offset, causing the list to shrink.
>>> L.pop(2) # Shrinking: delete an item in the middle
1.23
>>> L # "del L[2]" deletes from a list too
[123, 'spam', 'NI']
- insert
insert(index, object) -- insert object before index
>>> L.insert(1,456)
>>> L
[123, 456, 'spam', 'NI']
- remove
remove(value) -- remove first occurrence of value.
>>> L.remove('NI')
>>> L
[123, 456, 'spam']
- sort and reverse
>>> M = ['bb', 'aa', 'cc']
>>> M.sort()
>>> M
['aa', 'bb', 'cc']
>>> M.reverse()
>>> M
['cc', 'bb', 'aa']
Nesting
One nice feature of Python’s core data types is that they support arbitrary nesting, One immediate
application of this feature is to represent matrixes
, or “multidimensional arrays”
in Python
>>> M = [[1, 2, 3], # A 3 × 3 matrix, as nested lists
[4, 5, 6], # Code can span lines if bracketed
[7, 8, 9]]
>>> M
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> M[1] # Get row 2
[4, 5, 6]
>>> M[1][2] # Get row 2, then get item 3 within the row
6
Comprehension expression
>>> col2 = [row[1] for row in M] # Collect the items in column 2
>>> col2
[2, 5, 8]
>>> M # The matrix is unchanged
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
List comprehensions derive from set notation; they are a way to build a new list by
running an expression on each item in a sequence, one at a time, from left to right. List
comprehensions are coded in square brackets (to tip you off to the fact that they make
a list) and are composed of an expression and a looping construct that share a variable
name (row, here).More complex in practice:
>>> [row[1] + 1 for row in M] # Add 1 to each item in column 2
[3, 6, 9]
>>> [row[1] for row in M if row[1] % 2 == 0] # Filter out odd items
[2, 8]
>>> diag = [M[i][i] for i in [0, 1, 2]] # Collect a diagonal from matrix
>>> diag
[1, 5, 9]
>>> doubles = [c * 2 for c in 'spam'] # Repeat characters in a string
>>> doubles
['ss', 'pp', 'aa', 'mm']
Create Generator
, set
, Dictionary
>>> G = (sum(row) for row in M) # Create a generator of row sums
>>> next(G)
6
>>> next(G) # Run the iteration protocol
15
>>> [ord(x) for x in 'spaam'] # List of character ordinals
[115, 112, 97, 97, 109]
>>> {ord(x) for x in 'spaam'} # Sets remove duplicates
{112, 97, 115, 109}
>>> {x: ord(x) for x in 'spaam'} # Dictionary keys are unique
{'a': 97, 'p': 112, 's': 115, 'm': 109}
Dictionaries
A collection of other objects which stored by key instead of relative position. When written as literals, dictionaries are coded in curly braces and consist of a series of “key: value” pairs
- Mutable
- Mappings not sequences
Mapping Operations
- index
>>> D = {'food': 'Spam', 'quantity': 4, 'color': 'pink'}
>>> D['food'] # Fetch value of key 'food'
'Spam'
>>> D['quantity'] += 1 # Add 1 to 'quantity' value
>>> D
{'food': 'Spam', 'color': 'pink', 'quantity': 5}
>>> D = {}
>>> D['name'] = 'Bob' # Create keys by assignment
>>> D['job'] = 'dev'
>>> D['age'] = 40
>>> D
{'age': 40, 'job': 'dev', 'name': 'Bob'}
Nesting
>>> rec = {'name': {'first': 'Bob', 'last': 'Smith'},
'job': ['dev', 'mgr'],
'age': 40.5}
>>> rec['name'] # 'name' is a nested dictionary
{'last': 'Smith', 'first': 'Bob'}
>>> rec['name']['last'] # Index the nested dictionary
'Smith'
Note: As you can see, nesting allows us to build up complex information structures directly and easily. Building a similar structure in a low-level language like C would be tedious and require much more code: we would have to lay out and declare structures and arrays, fill out values, link everything together, and so on. In Python, this is all automatic—running the expression creates the entire nested object structure for us. In fact, this is one of the main benefits of scripting languages like Python.