在使用{}和(),[]是,代码可以跨多行使用
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 []
常见的表达式语句 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
传统方法
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 :
elif :
else:
与Java中区别在于 条件中() 不是必须, 需要使用:符号 ,{}被空格分块代替, else if 被elif代替
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 : # Loop test
# Loop body
else: # Optional else
# Run if didn't exit loop with break
利用javascript读取表单数据,可以利用以下三种方法获取:
1、通过表单ID属性:var a = document.getElementByIdx_x_x("id");
2、通过表单名称属性:var b = document.getElementsByName("name");
3、直接通过表单名字获取:var c = form.content.
什么是Spring Data Mongo
Spring Data MongoDB项目对访问MongoDB的Java客户端API进行了封装,这种封装类似于Spring封装Hibernate和JDBC而提供的HibernateTemplate和JDBCTemplate,主要能力包括
1. 封装客户端跟MongoDB的链接管理
2. 文档-对象映射,通过注解:@Document(collectio
The insertion algorithm for 2-3 trees just described is not difficult to understand; now, we will see that it is also not difficult to implement. We will consider a simple representation known