def calc_prod(lst):
def calc():
s=1
for x in list:
s=s*x
return s
return calc
f = calc_prod([1, 2, 3, 4])
print f()
def calc():
s=1
for x in list:
s=s*x
return s
return calc
f = calc_prod([1, 2, 3, 4])
print f()
2018-03-23
import math
def is_sqr(x):
return math.sqrt(x).is_integer()
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x).is_integer()
print filter(is_sqr, range(1, 101))
2018-03-23
import math
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt
2018-03-23
def count():
fs = []
for i in range(1, 4):
fs.append(lambda m=i : m**2)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
fs.append(lambda m=i : m**2)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2018-03-23
class Fib(object):
def __call__(self,num):
a, b = 0, 1
for i in range(num):
print(b)
a, b = b, a + b
f = Fib()
f(10)
def __call__(self,num):
a, b = 0, 1
for i in range(num):
print(b)
a, b = b, a + b
f = Fib()
f(10)
2018-03-23
三种方法:
def format_name(s):
return s[:1].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
def format_name(s):
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
def format_name(s):
return s[:1].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
def format_name(s):
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-03-23
class Person(object):
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1,key=lambda x:x.name)
print L2[0].name
print L2[1].name
print L2[2].name
God,你离成神已经不远了!下一节吧。
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1,key=lambda x:x.name)
print L2[0].name
print L2[1].name
print L2[2].name
God,你离成神已经不远了!下一节吧。
2018-03-22
# -*- coding: UTF-8 -*-
def __init__(self, name, score):
self.name = name
self._score = score
def get_grade(self):
if self._score>=90:
return "A"
elif self._score>=60 and self._score <90:
return "B"
else:
return "C"
def __init__(self, name, score):
self.name = name
self._score = score
def get_grade(self):
if self._score>=90:
return "A"
elif self._score>=60 and self._score <90:
return "B"
else:
return "C"
2018-03-22
import math
def is_sqr(x):
return math.sqrt(x) == int(math.sqrt(x))
a=list(filter(is_sqr, range(1, 101)))
print (a)
def is_sqr(x):
return math.sqrt(x) == int(math.sqrt(x))
a=list(filter(is_sqr, range(1, 101)))
print (a)
2018-03-22
class Person(object):
__count = 0
@classmethod
def how_many(cls):
return cls.__count
def __init__(self, name):
self.name = name
Person.__count += 1
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
__count = 0
@classmethod
def how_many(cls):
return cls.__count
def __init__(self, name):
self.name = name
Person.__count += 1
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
2018-03-21
def format_name(s):
return s[:].title()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[:].title()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-03-21
默认升序:L2 = sorted(L1,key=lambda s:s.name)
设置降序:L2 = sorted(L1,key=lambda s:s.name,reverse=True)
设置降序:L2 = sorted(L1,key=lambda s:s.name,reverse=True)
2018-03-21
@cnnlion
sorted([36,5,12,9,21],reversed_cmp)的执行过程:
5 < 36
[5, 36, 12, 9, 21]
12 > 5
[5, 12, 36, 9, 21]
12 < 36
[5, 12, 36, 9, 21]
12 > 5
[5, 12, 36, 9, 21]
9 < 12
[5, 9, 12, 36, 21]
9 > 5
[5, 9, 12, 36, 21]
21 > 12
[5, 9, 12, 21, 36]
21 < 36
[5, 9, 12, 36, 21]
#和排好序的列表中位数比较
sorted([36,5,12,9,21],reversed_cmp)的执行过程:
5 < 36
[5, 36, 12, 9, 21]
12 > 5
[5, 12, 36, 9, 21]
12 < 36
[5, 12, 36, 9, 21]
12 > 5
[5, 12, 36, 9, 21]
9 < 12
[5, 9, 12, 36, 21]
9 > 5
[5, 9, 12, 36, 21]
21 > 12
[5, 9, 12, 21, 36]
21 < 36
[5, 9, 12, 36, 21]
#和排好序的列表中位数比较
2018-03-21
def count():
fs = []
def freeze(fs,val):
fs.append(val)
for i in range(1, 4):
freeze(fs,i*i)
return fs
fs = []
def freeze(fs,val):
fs.append(val)
for i in range(1, 4):
freeze(fs,i*i)
return fs
2018-03-20