【学习笔记】python

1.  len( s ) 


返回对象(字符、列表、元祖等)的长度或项目个数。

1 >>>str = "runoob"
2 >>> len(str)             # 字符串长度
3 6
4 >>> l = [1,2,3,4,5]
5 >>> len(l)               # 列表元素个数
6 5

 2.  from __future__ import xxx 


首先要说明的是, from __future__ import print_function  一般需要放在脚本文件第一行。因为这个  statement  改变的是语言的基础功能,编译解释器需要从一开始就知道这一点。

以  from __future__ import print_function  为例,导入之后, print  就不再作为  special keyword  使用而是作为一个函数  print ( arg )  来使用了,小括号需要加上。

1 from __future__ import print_function
2 import sys
3 import os
4 import time
5 
6 for x in range(0,10):
7     print(x, sep=' ', end='')  # No need for sep here, but okay :)
8     time.sleep(1)

附  python 说明文档摘录,可以出现在 __future__  之前的只有the module docstring, comments, blank lines, and other future statements

A future statement is recognized and treated specially at compile time: Changes to the semantics of core constructs are often implemented by generating different code. It may even be the case that a new feature introduces new incompatible syntax (such as a new reserved word), in which case the compiler may need to parse the module differently. Such decisions cannot be pushed off until runtime.

再例如下面的代码,如果没有__future__ 的声明,两个 print 输出都是1。本质差异在于,如果没有 import division /  会映射到 __div__() 方法,如果有 import 则会映射到 __truediv__() 方法(注意,两种情况下, // 都映射到 __floordiv__()

Future statements are special -- they change how your Python module is parsed, which is why they must be at the top of the file. They give new -- or different -- meaning to words or symbols in your file. 

3.  xrange() 


xrange() 函数用法与 range() 完全相同,所不同的是 xrange() 生成的不是一个数组,而是一个生成器。使用方法如下:

1 xrange(stop)
2 xrange(start, stop[, step])

参数说明: start 计数从 start 开始。默认为0。如 xrange(5) 等价于 xrange(0, 5); stop 计数到 stop 结束,但不包括 stop。如xrange(0, 5) 是 [0, 1, 2, 3, 4] 没有 5; step 步长,默认为1。如xrange(0, 5) 等价于 xrange(0, 5, 1)。

 range() 方法返回的是一个 list 对象,它需要开辟专门的空间保存序列中所有的元素。

 xrange() 方法返回的是 xrange 对象,它是一个序列对象,但并不保存序列中的元素。

根据 python 官方文档的定义,一个序列对象不必要保存所有的元素。

如果只对序列进行读操作, xrange() 方法效率较高;但如果要改变序列的元素,或者需要对序列增删元素,那么只能通过 range() 方法生成一个list对象。

4.  lambda 表达式


相对于def定义的函数而言, lambda 表达式则简单很多,因为其主体是一个表达式而非代码块,并允许在代码内嵌入一个函数定义,不过一般只能封装有限的逻辑。

如下面的例子所示, lambda 表达式定义了一个匿名函数,用于筛选100以内3的倍数,并生成一个列表。

list (filter(lambda x:True if x % 3 == 0 else False, range(100)))

当然lambda表达式也可以嵌套在函数体内,使用的时候可以用一个变量来接收,如下:

1 def make_repeat(n):
2     return lambda s : s * n
3 
4 double = make_repeat(2)    # double变量此处是一个函数
5 print (double(8))    # 使用double向lambda表达式里的s传一个参数,并得到表达式的结果

 5.  with 关键字


如果你有两个想过的操作,你想要的这个操作成对执行,中间插入一段其他的代码,使用 with 就是一种很方便的方法。最经典的例子就是打开一个文件,对文件进行操作然后在关闭文件。

1 with open('output.txt', 'w') as f:
2     f.write('Hi there!')

上述的 with 语句会在嵌入的程序运行完后自动关闭文件。使用 with 语句的好处就是无论嵌套模块如何退出,都可以保证文件被关闭。

如果嵌套模块结束之前有异常发生,系统将在异常被外部异常处理程序捕获之前关闭该文件。

如果嵌套模块中包含有 return 语句、 continue 语句或者 break 语句, with 语句一样可以在跳转之前自动关闭文件。

6.  chr()函数


用于将一个范围在 range(256)内的整数(也即是 0~255)转换成对应的 ASCII码字符。

1 >>> print chr(0x30)    # 十六进制
2 0
3 >>> print chr(1)    # 十进制数,ASCII码1表示start of heading,什么也不输出
4 
5 >>> chr(1)
6 'x01'

与此相对应的有一个函数 ord(),则是返回单个 ASCII字符对应的 ASCII值或 Unicode值。

1 >>> ord('d')
2 100
3 >>> ord('A')
4 65

reference


[1] PYTHON中XRANGE和RANGE

[2] python lambda表达式

未完待续……

原文地址:https://www.cnblogs.com/phillee/p/10617276.html