class Person(object):
def __init__(self, name, gendar, birth,**params):
self.name = name
self.gendar = gendar
self.birth = birth
self.job = params['job']
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.job
def __init__(self, name, gendar, birth,**params):
self.name = name
self.gendar = gendar
self.birth = birth
self.job = params['job']
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.job
2017-10-27
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 p : p.name)
print L2[0].name
print L2[1].name
print L2[2].name
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 p : p.name)
print L2[0].name
print L2[1].name
print L2[2].name
2017-10-27
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
prode 是什么函数/?
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
prode 是什么函数/?
2017-10-27
本来就是学习,练习题要用的知识老师没讲,但是不妨碍自己思考啊,思路想出来,然后看答案或者别人是怎么写的,就知道用什么方法实现。也就掌握了
2017-10-27
sorted(L1, lambda p1, p2: cmp(p1.name, p2.name))
内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作;L1是可迭代的列表,lambda是匿名函数,p1和p2是匿名函数中传入的参数,cmp则是负责比较两个参数
内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作;L1是可迭代的列表,lambda是匿名函数,p1和p2是匿名函数中传入的参数,cmp则是负责比较两个参数
2017-10-27
class Person:
print 'this is a person example'
xiaoming = Person()
xiaohong = Person()
print xiaoming
print xiaohong
print xiaoming == xiaohong
print 'this is a person example'
xiaoming = Person()
xiaohong = Person()
print xiaoming
print xiaohong
print xiaoming == xiaohong
2017-10-27
try:
from simplejson import json
except ImportError:
import json
print json.dumps({'python':2.7})
from simplejson import json
except ImportError:
import json
print json.dumps({'python':2.7})
2017-10-27
def calc_prod(lst):
def chengji():
y=1
for x in lst:
y=x*y
return y
return chengji
f = calc_prod([1, 2, 3, 4])
print f()
def chengji():
y=1
for x in lst:
y=x*y
return y
return chengji
f = calc_prod([1, 2, 3, 4])
print f()
2017-10-26
def format_name(s):
return s[0].upper() + s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
capitalize() 首字母大写,其余全部小写
upper() 全转换成大写
lower() 全转换成小写
title() 标题首字大写,如"i love python".title() "I love python"
return s[0].upper() + s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
capitalize() 首字母大写,其余全部小写
upper() 全转换成大写
lower() 全转换成小写
title() 标题首字大写,如"i love python".title() "I love python"
2017-10-26
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)
没基础,完全不知道。
2017-10-26
import functools
sorted_ignore_case = functools.partial(sorted, key = str.lower)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
sorted_ignore_case = functools.partial(sorted, key = str.lower)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
2017-10-26
def performance(unit):
def factorial_decorator(f):
@functools.wraps(f)
def wrapper(*args, **kw):
print 'call %s...' % (f__name__)
return wrapper
return factorial_decorator
def factorial_decorator(f):
@functools.wraps(f)
def wrapper(*args, **kw):
print 'call %s...' % (f__name__)
return wrapper
return factorial_decorator
2017-10-26
def __cmp__(self, s):
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:
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:
2017-10-26