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镜像变更

临时使用

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

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

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

如果升级pip慢

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

Python time

>>> import datetime
>>> print datetime.datetime.utcnow()
2012-12-15 10:14:51.898000

>>> print datetime.datetime.now()
2012-12-15 11:15:09.205000

>>> str(datetime.datetime.now())
'2012-12-15 11:15:24.984000'

>>> datetime.datetime.now().strftime("%A, %d. %B %Y %I:%M%p")
'Saturday, 15. December 2012 11:19AM'

>>> datetime.datetime.now().isoformat()
'2013-11-18T08:18:31.809000'

>>> ts = datetime.datetime.now()
>>> tf = datetime.datetime.now()
>>> te = tf - ts
>>> print ts
2015-04-21 12:02:19.209915
>>> print tf
2015-04-21 12:02:30.449895
>>> print te
0:00:11.239980

Python all 函数

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

reduce()函数

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

reduce(function, iterable[, initializer])

参数

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


map()函数

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

语法

map(function, iterable,...)

参数

  • function:函数
  • iterable:一个或多个序列
>>>def square(x) :            # 计算平方数
...     return x ** 2
... 
>>> map(square, [1,2,3,4,5])   # 计算列表各个元素的平方
[1, 4, 9, 16, 25]
>>> map(lambda x: x ** 2, [1, 2, 3, 4, 5])  # 使用 lambda 匿名函数
[1, 4, 9, 16, 25]

# 提供了两个列表,对相同位置的列表数据进行相加
>>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 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
x = next(mylist)
print(x)
x = next(mylist)
print(x)
x = next(mylist)
print(x)

'''
apple
banana
cherry
'''


mylist = iter(["apple", "banana", "cherry"])
x = next(mylist, "orange")
print(x)
x = next(mylist, "orange")
print(x)
x = next(mylist, "orange")
print(x)
x = next(mylist, "orange")
print(x)

'''
apple
banana
cherry
orange
'''