函数
1. 定义一个方法 func,该func可以引入任意多的整型参数,结果返回其中最大与最小的值.
答: 方法一:
def m_num(*args):
for i in args:
if isinstance(i, int):
pass
else:
return "输入的参数必须为整型"
a = sorted(args)
max_num = a[-1]
min_num = a[0]
return "the max num is %s, the min num is %s" % (max_num, min_num)
print(m_num(1,2,3,4,435,55456,76,67,345,7))
方法二:
def m_num(*args):
for i in args:
if not isinstance(i, int):
return "输入的参数必须为整型"
return 'the max num is {}, the min num is {}'.format(max(args), min(args))
print(m_num(1,2,3,4,435,55456,76,67,345,7))
2. 定义一个方法func,该func可以引入任意多的字符串参数,结果返回(长度)最长的字符串。
答: 方法一
def l_str(*args):
for i in args:
if not isinstance(i, str):
return "输入的参数必须为字符串"
a = sorted(args, key=lambda k: len(k))
return "the longest str is: " + a[-1]
print(l_str('sad', 'asd', 'sadff', 'f', 'ffsff', 'sfdreyheg'))
方法二:
def l_str(*args):
str_list = []
for i in args:
if not isinstance(i, str):
return "输入的参数必须为字符串"
str_list.append(len(i))
return "the longest str is: " + args[str_list.index(max(str_list))]
print(l_str('sad', 'asd', 'sadff', 'f', 'ffsff', 'sfdreyheg'))
3. 定义一个方法get_doc(module),module参数为该脚本中导入或定义的模块对象,该函数返回module的帮助文档。
答:
def get_doc(module):
print(module.__doc__)
get_doc(max)
4. 定义一个方法get_text(f),f参数为任意一个文件的磁盘路径,该函数返回f文件的内容。
答:
import os
def get_text(f):
if os.path.exists(f):
with open(f, 'r') as g:
return g.read()
return "没有这个文件"
print(get_text('E:/Python/python进阶篇/进阶篇02-函数第一节/习题.txt'))
5. 定义一个方法get_dir(folder),folder参数为任意一个文件夹,该函数返回folder文件夹的文件列表。提示(可以了解python的glob模块)
答: 方法一:
import glob
def get_dir(folder):
for i in glob.glob(folder + '\*'):
print(i)
get_dir('E:\Python\python进阶篇\进阶篇02-函数第一节')
方法二:
import glob
a = []
def get_dir(folder):
a = glob.glob(folder + "\*")
return [a[i] for i in range(len(a))]
print(get_dir('E:\Python\python进阶篇\进阶篇02-函数第一节'))
5. 定义一个方法get_num(num),num参数是列表类型,判断列表里面的元素为数字类型。其他类型则报错,并且返回一个偶数列表:(注:列表里面的元素为偶数)
答:
def get_num(num):
l = []
if not isinstance(num, list):
return "参数不是列表类型"
for i in num:
if not isinstance(i, int):
return str(i) + "不是数字类型, 是: " + str(type(i))
if i%2 == 0:
l.append(i)
return l
print(get_num([1,2,3,4,5,6,7,8,9,12,24,45]))
6. 定义一个方法get_page(url),url参数是需要获取网页内容的网址,返回网页的内容。提示(可以了解python的urllib模块)
答:
import urllib.request
def get_page(url):
res = urllib.request.Request(url)
data = urllib.request.urlopen(res).read()
print(data.decode('utf-8'))
get_page('http://blog.csdn.net/mr_tank_/article/details/14102159')
7. 定义一个方法 func,该func引入任意多的列表参数,返回所有列表中最大的那个元素。
答: 方法一:
def max_list_num(*args):
ls = []
for l in args:
if not isinstance(l, list):
return "里面有至少一个非列表"
ls.append(max(l))
return max(ls)
print(max_list_num([1,2],[33],[3,4,5,6],[7,7,8,9,8,9,8,3],[67,88]))
方法二:
def func(*lists):
get_list = []
for i in lists:
if not isinstance(i, list):
return "里面有至少一个非列表"
get_list.append(i)
return max(max(get_list))
print(func([1,2],[33],[3,4,5,6],[7,7,8,9,8,9,8,3],[67,88]))
8. 定义一个方法get_dir(f),f参数为任意一个磁盘路径,该函数返回路径下的所有文件夹组成的列表,如果没有文件夹则返回"Not dir"。
答:
import os
import glob
def get_dir(file):
if os.path.exists(file):
return glob.glob(file + '/*')
else:
return 'No dir'
print(get_dir('E:/'))
9. 定义一个方法get_fundoc(func),func参数为任意一个函数对象,返回该函数对象的描述文档,如果该函数没有描述文档,则返回"not found"
答:
def get_fundoc(func):
# 使用calllable判断是否能被调用
if not callable(func):
return "func 参数不是函数对象"
elif str(type(func)) == "<type 'classobj'>" or str(type(func)) == "<type 'module'>":
return "func 参数不是函数对象"
else:
if func.__doc__ == None:
return 'func 参数没有文档'
else:
return func.__doc__
print(get_fundoc(sorted))
10. 定义一个方法get_cjsum(),求1-100范围内的所有整数的平方和。返回结果为整数类型。
答: 方法一:
def get_cjsum():
count = 0
for i in range(1, 101):
count += i*i
return count
print(get_cjsum())
方法二:
def get_cjsum():
i = 0
count = 0
while i <= 100:
count += i*i
i +=1
return count
print(get_cjsum())
11. 定义一个方法list_info(list), 参数list为列表对象,怎么保证在函数里对列表list进行一些相关的操作,不会影响到原来列表的元素值,比如:
a = [1,2,3]
def list_info(list):
'''要对list进行相关操作,不能直接只写一句return[1,2,5],这样就没意义了'''
print list_info(a):返回结果:[1,2,5]
print a 输出结果:[1,2,3]
写出函数体内的操作代码。
答:
def list_info(l):
a = l[:] #a = l.copy()也可以, a = l 不可以
# print(id(a), id(l))
a[-1] = 5
print(a)
l = [1,2,3]
list_info(l)
print(l)
12. 定义一个方法get_funcname(func),func参数为任意一个函数对象,需要判断函数是否可以调用,如果可以调用则返回该函数名(类型为str),否则返回 “fun is not function"。
答:
def get_funcname(func):
if callable(func):
if 'func_name' in dir(func):
print(func.func_name)
elif str(type(func)) == "<type 'classobj'>" or str(type(func)) == "<type 'module'>":
print('this not a function')
else:
print('the func is builtin_function_or_method!')
else:
print('fun is not function')
get_funcname(max)
13. 定义一个func(name),该函数效果如下。
assert func("lilei") = "Lilei"
assert func("hanmeimei") = "Hanmeimei"
assert func("Hanmeimei") = "Hanmeimei"
答:
def func(name):
return name.capitalize()
print(func("lilei"))
14. 定义一个func(name,callback=None),效果如下。
assert func("lilei") == "Lilei"
assert func("LILEI",callback=string.lower) == "lilei"
assert func("lilei",callback=string.upper) == "LILEI"
答:
def func(name,callback=None):
if isinstance(name, str):
if callback == None:
return name.capitalize()
elif callback == 'string.lower':
return name.lower()
elif callback == 'string.upper':
return name.upper()
else:
return "name must be string"
print(func("LILEI", callback='string.lower'))
print(func("lilei",callback='string.upper'))
15. 定义一个func(*kargs),效果如下。
l = func(1,2,3,4,5)
for i in l:
print i,
输出 1 2 3 4 5
l = func(5,3,4,5,6)
for i in l:
print i
输出 5 3 4 5 6
答: 方法一:
def func(*kargs):
return kargs
l = func(5,3,4,5,6)
for i in l:
print(i)
方法二:
def func(*kargs):
b = []
for i in kargs:
b.append(i)
return b
l = func(5,3,4,5,6)
for i in l:
print(i)
方法三: yield方法
def func(*kargs):
for i in kargs:
yield i
l = func(1,2,3,4,5)
for i in l:
print(i)
16. 定义一个func(*kargs),该函数效果如下。
assert func(222,1111,'xixi','hahahah') == "xixi"
assert func(7,'name','dasere') == 'name'
assert func(1,2,3,4) == None
答:
def func(*kargs):
c = []
for i in kargs:
if isinstance(i, str):
c.append(i)
if len(c) == 0:
return None
return c[0]
assert func(222,1111,'xixi','hahahah') == "xixi"
print(func(7,'name','dasere'))
assert func(1,2,3,4) == None
17. 定义一个func(name=None,**kargs),该函数效果如下。
assert func(“lilei”) == "lilei"
assert func("lilei",years=4) == "lilei,years:4"
assert func("lilei",years=10,body_weight=20) == "lilei,years:4,body_weight:20"
答:
def func(name=None, **kargs):
l = []
for k, v in kargs.items():
l.extend([str(k) + ':' + str(v)])
info = ','.join(l)
return '%s,%s' % (name, info)
print(func("lilei",years=10,body_weight=20))
面向对象
1. 定义一个学生类。有下面的类属性:
姓名
年龄
成绩(语文,数学,英语)[每课成绩的类型为整数]
类方法:
获取学生的姓名:get_name() 返回类型:str
获取学生的年龄:get_age() 返回类型:int
返回3门科目中最高的分数。get_course() 返回类型:int
答:
class Student():
def __init__(self, name, age, score):
self.name = name
self.age = age
self.score = score
def get_name(self):
print(self.name)
def get_age(self):
print(self.age)
def get_course(self):
print(max(self.score))
zm = Student('zhangming', 20, [69, 88, 100])
zm.get_name()
zm.get_age()
zm.get_course()
2. 定义一个字典类:dictclass。完成下面的功能:
dict = dictclass({你需要操作的字典对象})
删除某个key
del_dict(key)
判断某个键是否在字典里,如果在返回键对应的值,不存在则返回"not found"
get_dict(key)
返回键组成的列表:返回类型;(list)
get_key()
合并字典,并且返回合并后字典的values组成的列表。返回类型:(list)
update_dict({要合并的字典})
答:
class Dictclass():
def __init__(self, dict1):
self.dict = dict1
def get_dict(self, key):
if self.dict.get(key):
return self.dict[key]
def del_dict(self, key):
if self.dict.get(key):
self.dict.pop(key)
else:
return "Not Found"
def get_key(self):
return self.dict.keys()
def update_dict(self, dict2):
self.dict = dict(self.dict, **dict2)
return self.dict.values()
A = Dictclass({'a': 1, 'b': 2})
print(A.get_dict('c'))
print(A.del_dict('c'))
print(A.get_key())
print(A.update_dict({'c': 3, 'd': 4}))
3. 定义一个列表的操作类:Listinfo
包括的方法:
列表元素添加: add_key(keyname) [keyname:字符串或者整数类型]
列表元素取值:get_key(num) [num:整数类型]
列表合并:update_list(list) [list:列表类型]
删除并且返回最后一个元素:del_key()
list_info = Listinfo([44,222,111,333,454,'sss','333'])
答:
class Listinfo():
def __init__(self, ls):
self.list = ls
def add_key(self, keyname):
if isinstance(keyname, str) or isinstance(keyname, int):
self.list.append(keyname)
else:
return "must be str or int"
return self.list
def get_key(self, num):
if isinstance(num, int):
if num >0 and num < len(self.list):
return self.list[num]
else:
return "num 超出列表长度"
else:
return "num 必须是 int 类型"
def update_list(self, l):
if isinstance(l, list):
self.list.extend(l)
return self.list
else:
return "l 必须是列表类型"
def del_key(self):
return self.list.pop()
list_info = Listinfo([44, 222, 111, 333, 454, 'sss', '333'])
print(list_info.add_key('1111'))
print(list_info.get_key(4))
print(list_info.update_list(['1', '2', '3']))
print(list_info.del_key())
3. 定义一个集合的操作类:Setinfo, 包括的方法:
集合元素添加: add_setinfo(keyname) [keyname:字符串或者整数类型]
集合的交集:get_intersection(unioninfo) [unioninfo :集合类型]
集合的并集: get_union(unioninfo)[unioninfo :集合类型]
集合的差集:del_difference(unioninfo) [unioninfo :集合类型]
set_info = Setinfo(你要操作的集合)
答:
class Setinfo():
def __init__(self, s):
self.set = s
def add_setinfo(self, keyname):
if isinstance(keyname, str) or isinstance(keyname, int):
self.set.add(keyname)
return self.set
else:
return "must be str or int"
def get_intersection(self, unioninfo):
if isinstance(unioninfo, set):
return self.set & unioninfo
else:
return 'Added element must be a set!'
def get_union(self, unioninfo):
if isinstance(unioninfo, set):
return self.set | unioninfo
else:
return 'Added element must be a set!'
def del_difference(self, unioninfo):
if isinstance(unioninfo, set):
return self.set - unioninfo
else:
return 'Added element must be a set!'
A = set([1, 2, 3, 4, 5, 2])
B = set([5, 6, 3])
set_info = Setinfo(A)
print(set_info.add_setinfo('f'))
print(set_info.get_intersection(B))
print(set_info.get_union(B))
print(set_info.del_difference(B))
模块
1. 用time模块获取当前的时间戳.
答:
import time
print(time.strftime("%Y%m%d %H:%M:%S"))
2. 用datetime获取当前的日期,例如:2013-03-29
答:
from datetime import date
print(date.today())
3. 用datetime返回一个月前的日期:比如今天是2013-3-29 一个月前的话:2013-02-27
答:
from datetime import date
date0 = date(2017, 9, 5) - date(2017, 8, 5)
print(date.today() - date0)
4. 用os模块的方法完成ping www.baidu.com 操作。
** 答:**
# 方法一:
import os
os.system('ping www.baidu.com')
# 方法二:
import subprocess
subprocess.call('ping www.baidu.com', shell=False)
5. 定义一个函数kouzhang(dirpwd),用os模块的相关方法,返回一个列表,列表包括:dirpwd路径下所有文件不重复的扩展名,如果有2个".py"的扩展名,则返回一个".py"。
答:
import os
def kouzhang(dirpwd):
c = []
if not os.path.isdir(dirpwd):
return "Please input a dir"
for i in os.listdir(dirpwd):
if os.path.isfile(i):
c.append(os.path.splitext(i)[1])
else:
kouzhang(i)
return set(c)
print(kouzhang(os.getcwd()))
6. 定义一个函数xulie(dirname,info) 参数:dirname:路径名,info:需要序列化的数据,功能:将info数据序列化存储到dirname路径下随机的文件里。
答:
import os
import pickle
import random
def xulie(dirname, info):
if not os.path.exists(dirname):
return "Not Found"
a = pickle.dumps(info)
dirlist = os.listdir(dirname)
filelist = list(filter(lambda k: os.path.splitext(k)[1] == '.txt', dirlist))
print(filelist)
ran = random.randint(0, len(filelist)-1)
f = open(filelist[ran], 'wb')
f.write(a)
f.close()
xulie(os.getcwd(), [3, 2, 1])
异常处理
1. 定义一个函数func(filename) filename:文件的路径,函数功能:打开文件,并且返回文件内容,最后关闭,用异常来处理可能发生的错误。
答:
def open_file(filename):
try:
f = open(filename)
return f.read()
except Exception as err:
return err
else:
f.close()
print(open_file('test.py'))
2. 定义一个函数func(urllist) urllist:为URL的列表,例如:['http://xx.com','http://www.xx.com','http://www.xxx.com'...]
函数功能:要求依次打开url,打印url对应的内容,如果有的url打不开,则把url记录到日志文件里,并且跳过继续访问下个url。
答:
import urllib.request
def get_url(url_list):
for url in url_list:
try:
print(url)
date = urllib.request.urlopen(url)
print(date.read())
except Exception as er:
f = open('error.txt', "a")
f.write(url + '\r\n' + str(er) + '\r\n')
print('error url: ', url)
url_list = ['http://www.xxx.com','www.qq.com']
get_url(url_list)
3. 定好一个函数func(listinfo) listinfo:为列表,listinfo=[133,88,33,22,44,11,44,55,33,22,11,11,444,66,555] 返回一个列表包含小于100的偶数,并且用assert来断言返回结果和类型。
答:
def func(listinfo):
try:
result = filter(lambda k: k<100 and k%2==0, listinfo)
except Exception as err:
return err
else:
return list(result)
listinfo = [133, 88, 33, 22, 44, 11, 44, 55, 33, 22, 11, 11, 444, 66, 555]
assert type(func(listinfo)) == list
assert func(listinfo) == [88, 22, 44, 44, 22, 66]
4. 自己定义一个异常类,继承Exception类, 捕获下面的过程:判断raw_input()输入的字符串长度是否小于5,如果小于5,比如输入长度为3则输出:" The input is of length 3,expecting at least 5',大于5输出"print success'
答:
class My_error(Exception):
def __init__(self, stri):
self.len = len(stri)
def input_error(self):
if self.len < 5:
return 'The input is of length %s,expecting at least 5' % self.len
else:
return 'print success'
try:
raise My_error('sss')
except My_error as e:
print(e.input_error())
5. 编写with操作类Fileinfo(),定义enter和exit方法。完成功能:
在enter方法里打开Fileinfo(filename),并且返回filename对应的内容。如果文件不存在等情况,需要捕获异常。
在enter方法里记录文件打开的当前日期和文件名。并且把记录的信息保持为log.txt。内容格式:"2014-4-5 xxx.txt"
答:
import time
import logging
class Fileinfo():
new_time = time.time()
def __init__(self, filename):
self.filename = filename
def __enter__(self):
try:
f = open(self.filename, 'r')
content = f.read()
except Exception as er:
print(str(er))
else:
f.close()
return content
def __exit__(self, type, value, trackback):
with open('log.txt', 'a+') as f:
f.write('%s%s\n' % (Fileinfo.new_time, self.filename))
with Fileinfo('error.txt') as fn:
#__enter__中的返回值给予给fn
print(fn)
线程与协程
1. 已知列表 info = [1,2,3,4,55,233]
生成6个线程对象,每次线程输出一个值,最后输出:"the end"。
答:
import time
import threading
def test(p):
time.sleep(1)
print(str(p) + '\n')
info = [1, 2, 3, 4, 55, 233]
l = []
for i in range(0, len(info)):
th = threading.Thread(target=test, args=[info[i]])
th.start()
l.append(th)
for i in l:
i.join()
print('the end!')
2. 已知列表 urlinfo = ['http://www.sohu.com','http://www.163.com','http://www.sina.com'] 用多线程的方式分别打开列表里的URL,并且输出对应的网页标题和内容。
答:
import threading
import urllib.request
import re
def get_url(url):
res = urllib.request.urlopen(url).read()
data = res.decode('utf-8')
re_title = '<title>(.*)</title>'
title = re.findall(re_title, data)
print(title)
print(data)
urlinfo = ['http://www.sohu.com','http://www.baidu.com','http://www.sina.com']
l =[]
for i in range(0, len(urlinfo)):
th = threading.Thread(target=get_url, args=[urlinfo[i]])
th.start()
l.append(th)
for i in l:
i.join()
3. 已知列表 urlinfo = ['http://www.sohu.com','http://www.163.com','http://www.sina.com'] 用多线程的方式分别打开列表里的URL,输出网页的http状态码。
答:
import threading
import urllib.request
def get_url(url):
print(url)
code = urllib.request.urlopen(url).code
print(code)
urlinfo = ['http://www.sohu.com','http://www.163.com','http://www.sina.com']
l = []
for i in range(0, len(urlinfo)):
th = threading.Thread(target=get_url, args=[urlinfo[i]])
th.start()
l.append(th)
for i in l:
i.join()
4. 有10个刷卡机,代表建立10个线程,每个刷卡机每次扣除用户一块钱进入总账中,每个刷卡机每天一共被刷100次。账户原有500块。所以当天最后的总账应该为1500, 用多线程的方式来解决
答:
import threading
mlock = threading.Lock()
num = 500 # 账户原有500元
def a():
global num
for i in range(0, 100): # 每个刷卡机每天一共被刷100次
mlock.acquire() # 加锁
num += 1 # 每个刷卡机每次扣除用户1块钱
mlock.release() # 释放
print(num)
l = []
for i in range(0,10):
d = threading.Thread(target=a)
d.start()
l.append(d)
for i in l:
i.join()
print("当天最后总账为: %s 元" % num)
5. 定义一个生成器函数,函数里只能用yield,要求输出结果:
step 1
step 2 x=haha
step 3 y=haha
提示步骤:建立生成器对象,并且用对象的next()和send()方法来输出结果。send()方法传入的参数是"haha"
答:
def test():
x = yield 'step 1'
y = yield 'step2 x=%s' % x
z = yield 'step3 y=%s' % y
t = test()
print(t.__next__())
# print(t.__next__()) # output: step2 x=None
# print(t.__next__()) # output: step3 y=None
print(t.send('haha')) # output: step2 x=haha
print(t.send('haha')) # output: step3 y=haha
6. 用生成器yield实现斐波拉切数列。
答:
# 方法一:(普通方法)
def febir(num):
x, y = 1, 1
f = [x]
while y < num:
f.append(y)
x, y = y, x+y
return f
print(febir(100))
# 方法二:(yield 方法)
def febir(num):
x, y = 1, 1
while x < num:
yield x
x, y = y, x+y
for i in febir(100):
print(i)
# 方法三:(递归)
def febir(num):
if num == 0:
res = num
elif num == 1:
res = 1
else:
res = febir(num-1) + febir(num-2)
return res
def print_febir(num):
i = 0
while i < num:
print(febir(i))
i += 1
print_febir(13)
# 方法四:(尾递归)
def febir(n):
def fib_iter(n, x, y):
if n ==0:
return x
else:
return fib_iter(n-1, y, x+y)
return fib_iter(n, 0, 1)
def print_febir(n):
i = 0
while i < n:
print(febir(i))
i += 1
print_febir(13)
7. 生成一个素数列表:
(1)只能被1和自己整除
(2)1不是素数
答:
方法一:
def is_p(n):
if n == 1:
return False
elif n == 2:
return True
else:
for i in range(2, n):
if n % i == 0:
return False
return True
l = [i for i in range(1, 101) if is_p(i)]
print(l)
方法二:
def is_p(n):
l = []
if n < 2:
pass
else:
for i in range(2, n):
j = 2
k = 0
while j<i:
if i%j == 0:
j += 1
k += 1
else:
j += 1
if k == 0:
l.append(i)
return l
print(is_p(100))