# -*- coding:utf-8 -*-
'''
Created on 2012-8-19
@author: corleone
'''
import random
def test_all_1():
'''
@summary: test all
'''
def print_x(x):
print x
return x
print u"get all member first , then calc" , all([print_x(i) == 0 for i in range(10)])
print u"get one member and calc , then get next " , all((print_x(i) == 0 for i in range(10)))
def test_any_1():
'''
@summary: like all
'''
def test_bin_1():
print bin(3)
def test_int_1():
'''
int([x[, base]])
@attention: base in [2,36]
'''
print 2, int(u"0b11", 2)
print 8, int(u"11", 8)
print 16, int(u"11", 16)
print 5, int(u"11", 5)
print 36, int(u"11", 36)
# print 37, int(u"11", 37)
def test_chr_1():
print chr(97), type(chr(97))
def test_unichr_1():
print unichr(97), type(unichr(97))
def test_ord_1():
print ord("a")
def test_hex_1():
print hex(16)
def test_oct_1():
print oct(17)
def test_map_1():
print map(lambda x:x * 2, range(4))
print map(lambda x, y:unicode(x) + unicode(y), range(4), range(3, 6))
print map(lambda x, y, z:u"+".join(map(unicode, [x, y, z])), range(4), range(3, 7), range(7, 9))
def test_max_1():
items = range(10)
random.shuffle(items)
print items
print max(items)
print max(*items)
def test_iter_1():
'''
@summary: iter(o[, sentinel])
@attention:
Return an iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, o must be a collection object which supports the iteration protocol (the __iter__() method), or it must support the sequence protocol (the __getitem__() method with integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised. If the second argument, sentinel, is given, then o must be a callable object. The iterator created in this case will call o with no arguments for each call to its next() method; if the value returned is equal to sentinel, StopIteration will be raised, otherwise the value will be returned.
One useful application of the second form of iter() is to read lines of a file until a certain line is reached. The following example reads a file until "STOP" is reached:
with open("mydata.txt") as fp:
for line in iter(fp.readline, "STOP"):
process_line(line)
New in version 2.2.
@param sentinel: 哨兵
'''
for i in iter(range(10)):
print i
with open(u"built_in.py") as f:
for i in iter(f.readline, u""" with open(u"built_in.py") as f:\n"""):
print i
def test_next_1():
'''
@summary:
Retrieve the next item from the iterator by calling its next() method.
If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.
'''
items = iter(range(10))
while 1:
print next(items)
# while 1:
# print next(items, 100) # loop will not stop
def test_pow_1():
'''
@summary: pow(x, y[, z])
Return x to the power y; if z is present, return x to the power y, modulo z (computed more efficiently than pow(x, y) % z).
The two-argument form pow(x, y) is equivalent to using the power operator: x**y.
此方法是快速指数取模的优化实现,可以演算,已推算过,超管用
'''
print pow(2,5,3)
if __name__ == '__main__':
test_all_1()
test_bin_1()
test_int_1()
test_chr_1()
test_ord_1()
test_unichr_1()
test_hex_1()
test_oct_1()
test_map_1()
test_max_1()
test_iter_1()
test_next_1()
test_pow_1()
看python doc 的 built-in 部分 。 选了些比较实用的记下来。
分享到:
相关推荐
这篇文章主要介绍了Python易忽视知识点,实例分析了Python...Help on built-in function input in module __builtin__: input(...) input([prompt]) -> value Equivalent to eval(raw_input(prompt)). >>> he
最后,可以使用 Python 的 built-in 帮助函数来获取 Python-freegames 的帮助信息: ``` >>> import freegames >>> help(freegames) ``` 这将显示 Python-freegames 的帮助信息,包括 CLI 的使用方法和游戏的列表。 ...
PEP 553, Built-in breakpoint();PEP 557, Data Classes;PEP 560, Core support for typing module and generic types;PEP 562, Customization of access to module attributes;PEP 563, Postponed evaluation ...
本文档主要介绍了Python 3.7.4rc2版本中的内置函数。内置函数指的是Python解释器中预先定义好的函数,它们总是可以被使用,无需额外导入或定义。以下是Python内置函数的详细介绍。 ### 数值相关函数 - **abs(x)**...
The final chapter covers some additional built-in and add-in Python modules and functions that are important for data processing and analysis tasks, as well as some additional data structures that ...
The library contains built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules ...
AsynchronousProgramming异步编程Audio音频Authentication认证Build Tools构建工具Built-in Classes Enhancement内置类增强功能Caching缓存ChatOps ToolsChatOps 工具CMSCode Analysis代码分析Command-line ...
遍历pd.Series的index和value的方法如下,python built-in list的enumerate方法不管用 for i, v in s.items(): print('index: ', i, 'value: ', v) #index: a value: 1 #index: b value: 2 #index: c value: 3 #...
是的,使用 dir(__builtins__),显示的内容除了内置函数(Built-in Function),还包含了一些内置常量(Built-in Constants)和内置异常(Built-in Exception)等内容。 4、Tab 键除了用于缩进,你还发现它在 IDLE ...
一篇以php代码为例,基于抽象语法树、数据流分析、控制流分析、污点分析,发现代码中可能存在的漏洞的论文,按照论文的原理,不仅适用于php代码扫描,同样适用python、java等语言;若加以软件工程技术,可以开发出...
是的,使用 dir(__builtins__),显示的内容除了内置函数(Built-in Function),还包含了一些内置常量(Built-in Constants)和内置异常(Built-in Exception)等内容。 4、Tab 键除了用于缩进,你还发现它在 IDLE ...
The library also contains built-in functions and exceptions — objects that can be used by all Python code without the need of an import statement. Some of these are defined by the core language, but ...
Get a comprehensive, in-depth introduction to the core Python language with this hands-on book. Based on author Mark Lutz’s popular training course, this updated fifth edition will help you quickly ...
Well Supported in Third-Party Tools ...................................................................12 Good Selection of Tools Available ...............................................................