Python int 最大值和最小值

  1. # Python 2.7
  2. import sys
  3. max = sys.maxint
  4. min = -sys.maxint - 1
  5. # Python 3.7
  6. import sys
  7. max = sys.maxsize

Python2 和 Python3的区别

Python2 中 None可以和整数比较,但Python3中不可以,会报错

  1. # python2
  2. >>> None > 1
  3. False
  4. >>> None < 1
  5. True
  6. # python3
  7. >>> None > 1
  8. Traceback (most recent call last):
  9. File "<stdin>", line 1, in <module>
  10. TypeError: '>' not supported between instances of 'NoneType' and 'int'
  11. >>> None < 1
  12. Traceback (most recent call last):
  13. File "<stdin>", line 1, in <module>
  14. TypeError: '<' not supported between instances of 'NoneType' and 'int'

Pip镜像变更

临时使用

  1. pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
  2. #example: install tensorflow use tsinghua mirrors
  3. pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow

设为默认:
升级pip到最新的版本(>=10.0.0)后进行配置

  1. pip install pip -U
  2. pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

如果升级pip慢

  1. pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U

Python time

  1. >>> import datetime
  2. >>> print datetime.datetime.utcnow()
  3. 2012-12-15 10:14:51.898000
  4. >>> print datetime.datetime.now()
  5. 2012-12-15 11:15:09.205000
  6. >>> str(datetime.datetime.now())
  7. '2012-12-15 11:15:24.984000'
  8. >>> datetime.datetime.now().strftime("%A, %d. %B %Y %I:%M%p")
  9. 'Saturday, 15. December 2012 11:19AM'
  10. >>> datetime.datetime.now().isoformat()
  11. '2013-11-18T08:18:31.809000'
  12. >>> ts = datetime.datetime.now()
  13. >>> tf = datetime.datetime.now()
  14. >>> te = tf - ts
  15. >>> print ts
  16. 2015-04-21 12:02:19.209915
  17. >>> print tf
  18. 2015-04-21 12:02:30.449895
  19. >>> print te
  20. 0:00:11.239980

Python all 函数

all() 函数用于判断给定的可迭代参数 iterable 中的所有元素是否都为 TRUE,如果是返回 True,否则返回 False。
元素除了是 0、空、None、False 外都算 True。

reduce()函数

reduce()函数会对参数序列中元素进行累积。
函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给 reduce 中的函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,最后得到一个结果。
语法

  1. reduce(function, iterable[, initializer])

参数

  • function: 函数,有两个参数
  • iterable: 可迭代对象
  • initializer: 可选,初始参数


map()函数

map()会根据提供的函数对指定序列做映射。
第一个参数function以参数序列中的每一个元素调用function函数,返回包含每次function函数返回值的新列表。

语法

  1. map(function, iterable,...)

参数

  • function:函数
  • iterable:一个或多个序列
  1. >>>def square(x) : # 计算平方数
  2. ... return x ** 2
  3. ...
  4. >>> map(square, [1,2,3,4,5]) # 计算列表各个元素的平方
  5. [1, 4, 9, 16, 25]
  6. >>> map(lambda x: x ** 2, [1, 2, 3, 4, 5]) # 使用 lambda 匿名函数
  7. [1, 4, 9, 16, 25]
  8. # 提供了两个列表,对相同位置的列表数据进行相加
  9. >>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
  10. [3, 7, 11, 15, 19]

.pyc file in Python

.pyc files are created by the Python interpreter when a .py file is imported. They contain the “compiled bytecode” of the imported module/program so that the “translation” from source code to bytecode (which only needs to be done once) can be skipped on subsequent imports if the .pyc is newer than the corresponding .py file, thus speeding startup a little. But it’s still interpreted. Once the .pyc file is generated, there is no need of .py file, unless you edit it.

next()函数

next()函数用来返回迭代器中的下一个item
语法:next(iterable, default)
参数:

参数 描述
iterable Required. An iterable object
default Optional. An default value to return if the iterable has reached to its end
  1. x = next(mylist)
  2. print(x)
  3. x = next(mylist)
  4. print(x)
  5. x = next(mylist)
  6. print(x)
  7. '''
  8. apple
  9. banana
  10. cherry
  11. '''
  12. mylist = iter(["apple", "banana", "cherry"])
  13. x = next(mylist, "orange")
  14. print(x)
  15. x = next(mylist, "orange")
  16. print(x)
  17. x = next(mylist, "orange")
  18. print(x)
  19. x = next(mylist, "orange")
  20. print(x)
  21. '''
  22. apple
  23. banana
  24. cherry
  25. orange
  26. '''

python // 与 /的含义

在python2.2中引用 from _ future_ import division
“ / “ 就表示 浮点数除法,返回浮点结果;” // “表示整数除法
在python3以后,无需引用任何库
“/ “表示浮点数除法,返回浮点结果;”//“ 表示整数除法

heapq

heapq.heapify(x)

Transform list x into a heap, in-place, in linear time
将list转换成为堆

heapq.heappop(heap)

弹出并返回堆中的最小项,维持堆属性。如果堆是空,则抛出异常IndexError,如果希望在不弹出的情况下访问最小值,可以使用heap[0]