def calc_prod(lst):
def cj():
num = 1
for i in lst:
num = num *i
return num
return cj
f = calc_prod([1, 2, 3, 4])
print f()
def cj():
num = 1
for i in lst:
num = num *i
return num
return cj
f = calc_prod([1, 2, 3, 4])
print f()
2019-10-22
print map(str.capitalize, ['adam', 'LISA', 'barT'])
2019-10-19
def format_name(s):
ss=''
if s[0]>'Z':
ss=ss+chr(ord(s[0])-32)
else:
ss=ss+s[0]
for i in range(1,len(s)):
if s[i]<'a':
ss=ss+chr(ord(s[i])+32)
else:
ss=ss+s[i]
return ss
print map(format_name, ['adam', 'LISA', 'barT'])
ss=''
if s[0]>'Z':
ss=ss+chr(ord(s[0])-32)
else:
ss=ss+s[0]
for i in range(1,len(s)):
if s[i]<'a':
ss=ss+chr(ord(s[i])+32)
else:
ss=ss+s[i]
return ss
print map(format_name, ['adam', 'LISA', 'barT'])
2019-10-19
def __init__(self, num):
self.l=[]
f=0
g=1
x=1
while x<=num:
self.l.append(f)
g=g+f
f=g-f
num-=1
def __len__(self):
return len(self.l)
def __str__(self):
return str(self.l)
self.l=[]
f=0
g=1
x=1
while x<=num:
self.l.append(f)
g=g+f
f=g-f
num-=1
def __len__(self):
return len(self.l)
def __str__(self):
return str(self.l)
2019-10-17
if self.score<s.score:
return 1
elif self.score>s.score:
return -1
else:
if self.name<s.name:
return -1
elif self.name>s.name:
return 1
else:
return 0
return 1
elif self.score>s.score:
return -1
else:
if self.name<s.name:
return -1
elif self.name>s.name:
return 1
else:
return 0
2019-10-17
def __init__(self, name, gender, course):
super(Teacher,self).__init__(name,gender)
self.course=course
super(Teacher,self).__init__(name,gender)
self.course=course
2019-10-15
@classmethod
def how_many(cls):
return cls.__count
def __init__(self,name):
self.name=name
Person.__count+=1
def how_many(cls):
return cls.__count
def __init__(self,name):
self.name=name
Person.__count+=1
2019-10-15
sorted(iterable, cmp=None, key=None, reverse=False)
iterable -可迭代對象。
cmp -比較的函數,這個具有兩個參數,參數的值都是從可迭代對象中取出,大于則返回1,小于則返回-1,等于則返回0。
key -- 主要是用來進行比較的元素,只有一個參數,具體的函數的參數就是取自于可迭代對象中,指定可迭代對象中的一個元素來進行排序。
reverse - 排序規則,reverse = True 降序 , reverse = False 升序(默認)。
其實定義的reversed_cmp就是cmp內容,按大于返回-1小于返回1,等于返回0。
iterable -可迭代對象。
cmp -比較的函數,這個具有兩個參數,參數的值都是從可迭代對象中取出,大于則返回1,小于則返回-1,等于則返回0。
key -- 主要是用來進行比較的元素,只有一個參數,具體的函數的參數就是取自于可迭代對象中,指定可迭代對象中的一個元素來進行排序。
reverse - 排序規則,reverse = True 降序 , reverse = False 升序(默認)。
其實定義的reversed_cmp就是cmp內容,按大于返回-1小于返回1,等于返回0。
2019-10-08
def format_name(s):
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
2019-10-08
# Python 3中,Python3.x取消了 cmp參數,需使用functools中的cmp_to_key,即在開頭加上
from functools import cmp_to_key
def ......
print(sorted(['bob', 'about', 'Zoo', 'Credit'], key=cmp_to_key(cmp_ignore_case)))
結果:['about', 'bob', 'Credit', 'Zoo']
from functools import cmp_to_key
def ......
print(sorted(['bob', 'about', 'Zoo', 'Credit'], key=cmp_to_key(cmp_ignore_case)))
結果:['about', 'bob', 'Credit', 'Zoo']
2019-10-07