`
friendsys
  • 浏览: 338817 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

Learning.Python.3rd.Edition 笔记[9:14]

阅读更多
第三部分
Statements and Syntax
声明与语法


Chapter 10
Introducing Python Statements 介绍Python声明

Python程序的结构
1. Programs are composed of modules.  程序由模块组成
2. Modules contain statements. 模块包含声明
3. Statements contain expressions. 声明包含表达式
4. Expressions create and process objects. 表达式创建和传递对象

Python的语法由声明和表达式组成

在Python中 if等语句中 () 是可选的,而且必须加上:,在下一行中使用四个空格缩进表示一个块
每行结束时, ;号是可选的

Python提倡使用缩进来编写代码,利于提高可读性
WYSIWYG 所见即所得

如果要在一行内,执行多个代码,需要使用;分隔,不过不推荐这个风格

在使用{}和(),[]是,代码可以跨多行使用
mlist = [111,
    222,
    333]
X = (A + B +
+ D)

if (A == 1 and
B == 2 and
C == 3):

执行计算时: 使用\换行
X = A + B + \
C + D

单行语句  if x > y: print x

str的isdigit()方法,用于判断字符串是否可以转换成int

本章只是快速的介绍了Python声明语法上的一些内容..比较简单

凑字数问题省略...


Chapter 11

Assignment, Expressions, and print
赋值,表达式,和输出

Python中常见的赋值方式
Operation Interpretation
spam = 'Spam' Basic form
spam, ham = 'yum', 'YUM' Tuple assignment (positional)
[spam, ham] = ['yum', 'YUM'] List assignment (positional)
a, b, c, d = 'spam' Sequence assignment, generalized
spam = ham = 'lunch' Multiple-target assignment
spams += 42 Augmented assignment (equivalent to spams = spams + 42)


可以方便的使用
a, b, c, d = 'spam' 对序列进行一一取值,不过需要保证元素数量正确

可以使用另外的方式进行避免
a, b, c = string[0], string[1], string[2:] //在表达式的右边进行修改切片
a, b, c = list(string[:2]) + [string[2:]]
(a, b), c = string[:2], string[2:]
a, b = string[:2]

其他的方式
((a, b), c) = ('SP', 'AM')

一种特殊的循环方式
>>> L = [1, 2, 3, 4]
>>> while L:
... front, L = L[0], L[1:]
... print front, L
...
1 [2, 3, 4]
2 [3, 4]
3 [4]
4 []

和Java类似,对同一个引用的对象修改值后,将会修改其他的引用..类似参数传递与引用传递,同样对于
简单的str和int的修改并不会修改掉其他对象.

列表list中添加元素时, 单元素使用append()比+号更快,多元素时,同样适用extend()比+更快
如果使用+=号进行添加,Python会自动调用extend代替+号操作

注意区分对引用对象修改造成的影响,比如在使用list+= 和list = list +[],效果并不相同

命名规范,使用_或字母开头,注意区分大小写,注意不要使用关键字作为变量名

使用_开头的变量名,将不会被import * 语句导入
使用__x__格式命名的,一般是Python 解释器所使用的内置对象
使用__x,而不是用__结尾的变量名,将被设置成私有变量
一种特殊的_格式...具体用途未知

在本书的规范中,模块名使用小写字母开头,class使用大写字母开头

常见的表达式语句 Expression Statements
Operation Interpretation
spam(eggs, ham) Function calls
spam.ham(eggs) Method calls
spam Printing variables in the interactive interpreter
spam < ham and ham != eggs Compound expressions
spam < ham < eggs Range tests

注意区分方法调用后的返回结果,避免出现返回空值,并拿去赋值而导致的错误
>>> L = L.append(4) # But append returns None, not L
>>> print L # So we lose our list!
None

print常见操作

Operation Interpretation
print spam, ham Print objects to sys.stdout; add a space between the items and a

linefeed at the end
print spam, ham, Same, but don’t add linefeed at end of text
print >> myfile, spam, ham Send text to myfile.write, not to sys.stdout.write

其中需要注意最后一种中将字符串输出到文件中的方法

print只是简单的调用了sys.stdout.write()方法,可以通过重定向流的方法,将内容输出到其他地方

通过修改sys.stdout,将内容输出到文件中
import sys
sys.stdout = open('log.txt', 'a') # Redirects prints to file
...
print x, y, x # Shows up in log.txt

使用修改sys.stdout的缺点在于需要每次进行切换,可以使用>>进行代替

log = open('log.txt', 'a')
print >> log, x, y, z
log.close( ) //注意输出结束后需要进行关闭

需要注意log必须是一个文件,而不是一个string

也可以将print转向sys.stderr 用于输出错误信息
print >> sys.stderr, 'Bad!' * 8

其实关键在于类对象内置否存在write(self,string) 方法,可以通过自定义该类达到重定向的方法

传统方法
class FileFaker:
def write(self, string):
# Do something with the string
import sys
sys.stdout = FileFaker( )
print someObjects

>>方法
myobj = FileFaker( ) # Redirect to an object for one print
print >> myobj, someObjects # Does not reset sys.stdout

在Python 3中,将print转换成了一个内置方法,可以通过传递文件参数,来达到重定向效果

凑字数问题

Chapter Quiz
1. Name three ways that you can assign three variables to the same value.

2. Why might you need to care when assigning three variables to a mutable object?

3. What’s wrong with saying L = L.sort( )?

4. How might you use the print statement to send text to an external file?

Quiz Answers

1. You can use multiple-target assignments (A = B = C = 0), sequence assignment (A,B, C = 0, 0, 0), or multiple assignment statements on separate lines (A = 0, B = 0,C = 0). With the latter technique, as introduced in Chapter 10, you can also string the three separate statements together on the same line by separating them with semicolons (A = 0; B = 0; C = 0).

2. If you assign them this way: A = B = C = []
all three names reference the same object, so changing it in-place from one (e.g.,
A.append(99)) will affect the others. This is true only for in-place changes to
mutable objects like lists and dictionaries; for immutable objects, such as numbers and strings, this issue is irrelevant.

3. The list sort method is like append in that is an in-place change to the subject list—it returns None, not the list it changes. The assignment back to L sets L to None, not the sorted list. As we’ll see later in this part of the book, a newer builtin function, sorted, sorts any sequence, and returns a new list with the sorting result; because it is not an in-place change, its result can be meaningfully assigned to a name.

4. You can assign sys.stdout to a manually opened file before the print, or use the extended print >> file statement form to print to a file for a single print statement.You can also redirect all of a program’s printed text to a file with special syntax in the system shell, but this is outside Python’s scope.


Chapter 12
If Tests

常见的格式
if <test1>:
    <statements1>
elif <test2>:
    <statements2>
else:
    <statements3>

与Java中区别在于 条件中() 不是必须, 需要使用:符号 ,{}被空格分块代替, else if 被elif代替

另外一种灵活的用法
>>> choice = 'ham'
>>> print {'spam': 1.25,
   'ham': 1.99,
   'eggs': 0.99,
   'bacon': 1.10}[choice]
使用字典代替硬编码的if..

为了防止key是无效的,也可以使用dict.get('spam', 'Bad choice'),通过默认值来防止错误
同样dict中可以包含function对象

Python语法中,可以通过(),[],\来达到多行的效果.也可以使用;在一行编写多个语句,以及编写特殊
的单行语句块:if 1: print 'hello'
不过上述特殊的用法,并不利于提高可读性,所以不建议使用

执行判断时,常用的语句 and or not, 与Java中的 && || !类似
还需要注意一些Python的false定义规则

or的使用

print 2 or 3, 3 or 2
(2,3) //返回左边的元素

>>> [ ] or 3
3  //返回true的元素
>>> [ ] or { }
{ } //返回右边元素..未知

大概意思是: 如果左边为true,那将返回左边,如果右边为true,将返回右边,如果两边都为true,也将返回左边,如果左边和右

边都为false,将返回右边

只会找到true后返回,如果没有将返回最后一个元素


and的使用
>>> 2 and 3, 3 and 2
(3, 2) # Else,
>>> [ ] and { }
[ ]
>>> 3 and [ ]
[ ]

遇到false,麻烦返回当前元素,否则返回最后一个元素

在Python中,and 和 or 将不会简单的返回一个bool类型,而根据比较的具体类型进行返回

三元表达式

普通的版本
if X:
    A = Y
else:
    A = Z

特殊的版本1:
A = Y if X else Z

A = ((X and Y) or Z)

使用bool值,从两个中选择一个值
A = [Z, Y][bool(X)]

原理通过bool得到1和0值,转换为索引使用


凑字数问题
Chapter Quiz
1. How might you code a multiway branch in Python?

2. How can you code an if/else statement as an expression in Python?

3. How can you make a single statement span many lines?

4. What do the words True and False mean?

Quiz Answers
1. An if statement with multiple elif clauses is often the most straightforward way to code multiway branching, though not necessarily the most concise. Dictionary indexing can often achieve the same result, especially if the dictionary contains callable functions coded with def statements or lambda expressions.

2. In Python 2.5, the expression form Y if Xelse Z returns Y if X is true, or Z otherwise; it’s the same as a four-line if statement. The and/or combination ((Xand Y) or Z) can work the same way, but it’s more obscure, and requires that the Y part be true.

3. Wrap up the statement in an open syntactic pair ((), [], or {}), and it can span as many lines as you like; the statement ends when Python sees the closing, right half of the pair.

4. True and False are just custom versions of the integers 1 and 0, respectively.
They always stand for Boolean true and false values in Python


Chapter 13
while and for Loops
while和for循环(Python应该不包含do..while循环)

while的格式如下
while <test>: # Loop test
    <statements1> # Loop body
else: # Optional else
    <statements2> # Run if didn't exit loop with break

循环中还包括一些常见的关键字 break,continue pass和else
break与continue的功能与Java的类似
pass只是代表一个无意义的语句
else放置在循环后,用于在非break而结束的循环后执行


格式如下
while <test1>:
    <statements1>
    if <test2>: break # Exit loop now, skip else
    if <test3>: continue # Go to top of loop now, to test1
else:
    <statements2> # Run if we didn't hit a 'break'


for <target> in <object>: # Assign object items to target
    <statements>
    if <test>: break # Exit loop now, skip else
    if <test>: continue # Go to top of loop now
else:
    <statements> # If we didn't hit a 'break'

常用的循环文件的方法

for line in open('test.txt').readlines( ):
    print line

for line in open('test.txt').xreadlines( ):
    print line

for line in open('test.txt'):
    print line

手动调用文件读取时,使用readline比next()方法更安全一些
不过对for循环并不会有影响

不建议使用readlines()配合循环,因为会同时加载所有文件内容到内存中

自定义迭代器
在自定义class中重写 _ _iter_ _ or _ _getitem_ _方法

range() 可以代替loop生成指定区间的数字List
range(5, -5, -1)  5开始到 -5之前,递减,步数为-1

for 循环结束后,临时生成的对象也会保存下来
for x in L:
    x += 1
print x  //x将会被包括保留

上述例子中, 对list的循环,对x的修改并不会更新list,可以使用下面的方法进行修改

for i in range(len(L)): # Add one to each item in L
... L[i] += 1

也可以使用
c=range(1,10)
print [x+1 for x in c]

内置zip与map方法的使用

zip用于同时循环多个sequences序列(list string tuple)

for (x, y) in zip(L1, L2):
    print x, y, '--', x+y

其中元素循环的次数,与数量最少的序列相关

如果需要完全循环多个序列,可以使用另外一种内置方法 map
map(None, S1, S2)
[('a', 'x'), ('b', 'y'), ('c', 'z'), (None, '1'), (None, '2'), (None,'3')]

None为缺失时默认的值, S1,S2为不同长度的两个sequence
S1 = 'abc'
S2 = 'xyz123'

使用zip,将两个list合并成一个dict字典
keys = ['spam', 'eggs', 'toast']
vals = [1, 3, 5]
D3 = dict(zip(keys, vals))
print D3
{'toast': 5, 'eggs': 3, 'spam': 1}

使用内置函数enumerate 在循环序列时,同时获取索引与值
S = 'spam'
for (offset, item) in enumerate(S):
    print item, 'appears at offset', offset

也可以简单的使用enumerate(sequence)来创建一个枚举
E = enumerate(S)
E.next( )
(1, 'p')


List推导式
L = [x + 10 for x in L]
区别与传统的循环修改
for i in range(len(L)):
    L[i] += 10

list推导式的运行速度比普通的loop循环更快一些(大概两倍)

在文件读取中使用list推导式的例子
lines = [line.rstrip( ) for line in open('script1.py')] //rstrip用于过滤掉\n

更复杂的推导式例子
lines = [line.rstrip( ) for line in open('script1.py') if line[0] == 'p']
等同于
res = []
    for line in open('script1.py'):
        if line[0] == 'p':
           res.append(line.rstrip( ))

其他例子
[x + y for x in 'abc' for y in 'lmn']

凑字问题省略


Chapter 14

Python中的文档
Form Role
# comments In-file documentation
The dir function Lists of attributes available in objects
Docstrings: _ _doc_ _ In-file documentation attached to objects
PyDoc: The help function Interactive help for objects
PyDoc: HTML reports Module documentation in a browser
Standard manual set Official language and library descriptions
Web resources Online tutorials, examples, and so on
Published books Commercially available reference texts


# Comments 最常用的注释,相比较docstring适合用于多行的注释,comments更适合小型的单行注释

The dir Function dir方法
可以带不同的参数,来查看不同类型所拥有的对象

dir(str) == dir('') # Same result as prior example
dir(list) == dir([])


Docstrings: _ _doc_ _
可以通过该属性,获取当前模块到docstring内容,也可以获取到类所拥有的docstring
print docstrings._ _doc_ _
print docstrings.square._ _doc_ _

help方法
help(sys.getrefcount) 可以获取指定模块或者类的docstring

还有其他的方法,通过专门的GUI和HTML查看docstring信息











分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics