最新回答 / 扬暄晴玥
& 是位运算;and 是逻辑运算。a = 1#a的十进制是1,二进制是01b = 2#b的十进制是2,二进制是10a & b#对a和b的二进制进行位运算,即,01&10 = 00,转换成十进制之后,为0a and b#对a和b进行逻辑与运算,a = 1为真,b = 2为真,返回b,即 a and b = 2短路计算:a and b,a为False,返回a,a为True,返回ba or b,a为False,返回b,a为True,返回aPython解释器在做布尔运算时,只要能提前确定...
2018-11-24
class Fib(object):
def __call__(self, num) :
L = [0,1]
for x in range(2,num):
L.append(L[x-1] + L[x-2])
return L
f = Fib()
print f(10)
def __call__(self, num) :
L = [0,1]
for x in range(2,num):
L.append(L[x-1] + L[x-2])
return L
f = Fib()
print f(10)
2018-11-23
def calc_prod(lst):
def prod():
def product(x,y):
return x*y
return reduce(product,lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod():
def product(x,y):
return x*y
return reduce(product,lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-11-22
def read(self):
return r'["Tim", "Bob", "Alice"]'
return r'["Tim", "Bob", "Alice"]'
2018-11-22
class Person(object):
__count = 0
def __init__(self,name):
self.name = name
Person.__count += 1
@classmethod
def how_many(cls):
return cls.__count
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
__count = 0
def __init__(self,name):
self.name = name
Person.__count += 1
@classmethod
def how_many(cls):
return cls.__count
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
2018-11-22
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
s = u'am I an unicode 中文'
print isinstance(s, unicode)
from __future__ import unicode_literals
s = u'am I an unicode 中文'
print isinstance(s, unicode)
2018-11-22
最赞回答 / qq_慕妹7534246
reversed_cmp返回的三种结果 ,对应的就是倒序,顺序,和相同。sorted()这个函数的功能就是顺序排序,但是为了响应不同的需求,所以加了一个参数,reversed_cmp这个里面你也可以做成顺序的,但是这样就没实际的意义了。
2018-11-21
最赞回答 / 慕田峪6055735
factorial作为一个参数传入装饰器,在答案中import timedef performance(unit): def perf_decorator(f): def wrapper(*args, **kw): t1 = time.time() r = f(*args, **kw) #这里就调用了f,也就是原函数factorial t2 = time.time() t = (t2 - t1...
2018-11-21
import math
def is_sqr(x):
a = math.sqrt(x)
if a == int(a):
return x
print filter(is_sqr, range(1, 101))
大家看看我的有没有创新
def is_sqr(x):
a = math.sqrt(x)
if a == int(a):
return x
print filter(is_sqr, range(1, 101))
大家看看我的有没有创新
2018-11-20