您的位置:首页 > 编程语言 > Python开发

python部落之python刷题宝之基础知识

2016-05-19 19:05 513 查看
1
any([True,False,True]), all([True,False,True])


any(iterable)->bool 迭代器对象中只要有一个为True,返回结果为True。

all(iterable)->bool 迭代器对象中都为True时,返回结果才为True。

Answer: True, False

2

isinstance(u'我‘,basestring)
isinstance(object, classinfo)等同于java中的的 instanceOf.

isinstance(object, class-or-type-or-tuple) -> bool
Return whether an object is an instance of a class or of a subclass thereof.  With a type as second argument, return whether that is the object's type.  The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for  isinstance(x, A) or isinstance(x, B) or ... (etc.).
而basestring是str和unicode的超类(父类),也是抽象类,因此不能被调用和实例化,但可以被用来判断一个对象是否为str或者unicode的实例,isinstance(obj,
basestring)等价于isinstance(obj, (str, unicode));

Answer: True

3

len(bin(5))
bin()函数用法 bin(number)-> string. 返回整型(integer)或长整型(long)的二进制表示,返回类型为字符串。

a = bin(5) print a 得到 a = '0b101'. '0b'表示二进制表示,‘101’为正数5的二进制表示。因为返回类型为string,故len()后得到为5.

Answer:5

4
表达式 callable(1) 的值为?
callable() 检查对象是否可用。Return True if the object argument appears callable, False if not. If this returns
true, it is still possible that a call fails, but if it is false, calling object will never succeed. Note that classes are callable (calling a class returns a new instance); class instances are callable if they have a __call__() method.

5 cmp(2,3)

比较。 cmp(x,y)->integer. 如果x<y,返回-1,如果x==y,返回0,如果x>y,返回1.

6 complex(2,3)==complex('2+3j')

True。 complex()可以接受string对象。

7 dir(arg) 返回对应arg如class的变量和方法。

8 填空,如下表达式等价于(a//b, a%b)。 _(a,b)

Answer: divmod divmod(x,y)-> (quotient,remainder) 返回元组。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: