# ---------------------------------------------------------------------- # def my_function1(name, address, age): # print('姓名:%s, 地址:%s, 年龄:%s' % (name, address, age)) # # # 正确: # my_function1('北京',50,'田老师') # # # 错误: # my_function1(50, '北京', '田老师') # # # 指定参数名,顺序可以与定义时不一致 # my_function1(age=50, address='北京', name='田老师') # 默认值 # 我们已知的情况: # def my_function2(name, age): # print('姓名:%s, 年龄:%s' % (name, age)) # # # my_function2('田老师') # 一定场景下,带默认值 # def my_function2(name, age=18): # print('姓名:%s, 年龄:%s' % (name, age)) # # # my_function2('张老师') # my_function2('田老师', 46) # 有默认值时的参数顺序 # 正确: # def enroll(name, city='Beijing'): # # print('name:', name) # print('city:', city) # print('-------------') # def enroll(name, city='Beijing'): # # print('name:', name) # print('city:', city) # print('-------------') # # # enroll('ShangHai') # 以下的代码的输出什么? 你将如何修改 extendList 的定义所带来的隐患问题 # def extendList(val, list=[]): # list.append(val) # return list # def extendList(val, list=None): # if list == None: # list = [] # list.append(val) # return list # # list1 = extendList(10) # list1 = [10] # list2 = extendList(123, []) # list2 = [123] # list3 = extendList('a') # list3 = ['a'] # # print("list1 = %s" % list1) # print("list2 = %s" % list2) # print("list3 = %s" % list3) # 隐患和修改方法: # 函数定义时,如果参数有默认值,则在定义时就会开辟一块内存空间来存储该默认值 # 调用函数时,如果未给有默认值的参数传值,就会使用该内存空间的数据 # 多次调用时,该空间就会被重复使用,如果某一次调用时改变了该空间的数据,其他次调用也会收到影响 # 可变数据类型,内存空间内容可以被改变,所以尽可能不用可变数据类型 # 如果一定要用,默认值不要用空对象,应该用None # 可变参数 * # def my_function3(name, age, gender, *other): # print('name:', name) # print('age:', age) # print('gender:', gender) # print('other:', other) # # my_function3('田老师', 56, '男', '北京','慧测','汤立路220号','哈哈哈') # 可变参数 ** # def my_function4(name, age, gender, **other): # print('name:', name) # print('age:', age) # print('gender:', gender) # print('other:', other) # # # my_function4('田老师',56, '男', city='北京', corp='慧测', address='汤立路220号') # 参数顺序一般是非可选参数在前,可选参数在后 # 1 * # def enroll(name, *other): # # print('name:', name) # print('other:', other) # # enroll('zhanglaoshi', 18, '北京') # 这样也可以,但不建议 # def enroll(*other, name): # # print('name:', name) # print('other:', other) # # enroll( 'Beijing', 18, name='张老师') # 2 ** # def enroll(name, **other): # print('name:', name) # print('other:', other) # # # 调用时传参 # enroll(address='Beijing', age=18, name='张老师') # return # def my_function4(): # print(1) # return # def calc(x,y): # a = x+y # b = x-y # c = x*y # # return a,b,c # # print(calc(3,4)) # # def is_longer_than_5(seq): # if len(seq)>5: # return True # else: # return False # # print(is_longer_than_5('hello')) # def avg(list): # sum = 0 # for ele in list: # sum += ele # length = len(list) # return sum/length # # print(avg([1,2,5,7,8])) # # my_function4() # 3. (选做)编写一个生成get请求地址的函数,上游传递过来的参数有url=“”,domain=“”,data={}, # 请根据以上参数返回一个get请求的完整链接,其中data和url有可能不传 # # http://127.0.0.1/access/log?a=1&b=2 # domain url data {'a':1,'b':2} # def gen_get_address(domain, url=None, data=None): # address = 'http://' + domain # if url: # address += '/' + url # if data: # params_list = [] # for k, v in data.items(): # param = k + '=' + v # params_list.append(param) # params = '&'.join(params_list) # address += '?' + params # return address # # # print(gen_get_address(domain='127.0.0.1', # url='huice/huice', # data={'usr': 'tiantian', 'passwd': 'bugaosuni'})) # 递归 # 5! = 5* 4! # 4! = 4* 3! # # n! = n * (n-1)! # def fact(n): # if n == 1: # return 1 # return n * fact(n-1) # # print(fact(100)) # def factorial(n): # if n > 1: # return n * factorial(n - 1) # elif n == 0 or n == 1: # return 1 # # print(factorial(100)) # a = 'hello' # for i in map(ord, a): # print(i) # # def my_func(x): # return x + 'kitty' # m = map(lambda x: x + 'kitty', ['hello', 'haha', 'hei', 'hehe', 'helloworld']) # print(list(m)) # # # def is_longer_than_3(seq): # if len(seq) > 3: # return True # else: # return False # # # result = filter(is_longer_than_3, ['hello', 'haha', 'hei', 'hehe', 'helloworld']) # print(list(result)) # filter # def ispositive(x): # if x>0: # return True # else: # return False # # result = filter(ispositive, [1,-2,3,-1,4]) # print(list(result)) # # a = ['adam', 'LISA', 'barT'] # # def my_capitalize(string): # return string.capitalize() # # print(list(map(my_capitalize, a))) class HuiceStudent: '慧测学生类' school = 'Huice' __xuefei = 980 def __init__(self, xingming, nianling=18): self.name = xingming self.age = nianling def __study(self): '' print(self.name +'交完'+ str(self.xuefei) + '在学习!') def eat(self, something): print(self.name + '在吃' + something) @classmethod def go_to_school(cls): print('欢迎来' + cls.school) @staticmethod def my_print(): print('haha'+ HuiceStudent.school) def __len__(self): return len(self.name) def __str__(self): return '%d岁的学生%s' % (self.age, self.name) @classmethod def get_xuefei(cls): # if authority = 'boss': return cls.__xuefei # else: # return None @classmethod def set_xuefei(cls, new_xuefei): if new_xuefei > 1000 or new_xuefei < 900: print('学费不合理') else: cls.__xuefei = new_xuefei print('设置成功') # 学校 # 学费 类变量 # # 姓名 # 身高 实例变量 成员变量 # # 上学 类方法 # # 跑 # 跳 # 吃 # 学习 成员方法 # # # # pass # a = HuiceStudent('张三') # b = HuiceStudent('李四') # print(a.name) # print(a.xuefei) # a.xuefei = 930 # print('李四的学费是', b.xuefei) # b.study() # b.eat('肉') # b.go_to_school() # # # print(HuiceStudent.xuefei) # # HuiceStudent.go_to_school() # print(HuiceStudent._HuiceStudent__xuefei) # print(HuiceStudent.__doc__) # c = HuiceStudent('王五') # print(c.name) # c.eat('肉肉') # print(len(c)) # print(c) # # # print(HuiceStudent.get_xuefei()) # # HuiceStudent.set_xuefei(990) # print(HuiceStudent.get_xuefei()) # # class Stack: # def __init__(self, max_size): # __lst = [] # self.max_size = max_size # # def pop(self): # pass # # def push(self, element): # pass # # def get # class Auto: # def __init__(self, pinpai, yanse): # self.brand = pinpai # self.speed = 0 # self.color = yanse # # def start(self): # print self.brand + '启动了' # # def speedup(self): # self.speed += 10 # # def stop(self): # if self.speed > 30: # self.speed -= 30 # else: # self.speed = 0 # # # if __name__ == '__main__': # # mycar = Auto('BMW', '蓝色') # mycar.start() # mycar.speedup() # mycar.speedup() # mycar.speedup() # mycar.speedup() # mycar.speedup() # print mycar.speed # mycar.stop() # print mycar.speed # mycar.stop() # print mycar.speed # class Animal: # def __init__(self, name): # self.name = name # # def eat(self): # print(self.name + '在吃东西') # # def run(self): # print(self.name + 'is running!') # # # # ani = Animal('动物') # # ani.eat() # # ani.run() # # class Dog(Animal): # # def bark(self): # print(self.name + '在狂吠') # # def eat(self): # print(self.name + '在吃骨头') # # class Cat(Animal): # # def eat(self): # print(self.name + '在吃<・)))><<') # # class Tiger(Animal): # # def eat(self): # print(self.name + '在吃肉') # # class Elephant(Animal): # # def eat(self): # print(self.name + '在吃树叶') # # class Person: # def __init__(self, name): # self.name = name # # def feed(self, a): # a.eat() # def feed_cat(self, cat): # cat.eat() # # def feed_tiger(self, tiger): # tiger.eat() # t = Person('tiantian') # d = Dog('旺旺') # c = Cat('花花') # tg = Tiger('大王') # e = Elephant('大白') # # t.feed(d) # t.feed(c) # t.feed(tg) # t.feed(e) # # def bark(self): # print('汪汪') # # def eat(self): # print(self.name, '在吃骨头') # # class Cat(Animal): # # def eat(self): # print(self.name, '在吃鱼') # # class Tiger(Animal): # # def eat(self): # print(self.name, '在吃肉') # # class Elephant(Animal): # # def eat(self): # print(self.name, '在吃树叶') # # class Person: # def __init__(self,name): # self.name = name # # # def feed_dog(self, dog): # # dog.eat() # # # # def feed_cat(self, cat): # # cat.eat() # # # # def feed_tiger(self, tiger): # # tiger.eat() # # def feed(self, anim): # anim.eat() # # # tls = Person('tianlaoshi') # # # mydog = Dog('旺旺') # mycat = Cat('花花') # mytiger = Tiger('大王') # myele = Elephant('小白') # # # tls.feed_cat(mycat) # # tls.feed_dog(mydog) # # tls.feed_tiger(mytiger) # # # tls.feed(mydog) # tls.feed(mycat) # tls.feed(mytiger) # tls.feed(myele) # # #多态 #异常 # import sys # import traceback # ImportError:导入模块错误 # import A # IndexError:索引超出范围 # list1 = [1,2,3] # print(list1[3]) # KeyError:字典中不存在的键 # dict1 = {'name':'ivy','age':20,'gender':'female'} # print(dict1['height']) # NameError:访问没有定义的变量 # print(a) # IndentationError:缩进错误 # if 1==1: # print 'aaa' # SyntaxError:语法错误 # list2 = [1,2,3,4 # TypeError:不同类型间的无效操作 # print(1+'1') # ZeroDivisionError:除数为0 # print(a/b) # try: # print(5/0) # # print('hehe') # except: # print('haha') # raise BaseException('除数不能为0') # # finally: # # print('xixi') # # print('haha') # import traceback # #捕获 抓 # def sum(a,b): # try: # return a/b # except (ZeroDivisionError, TypeError) as e: # traceback.print_exc() # print e # return 0 # # print sum(5, 'a') # print 'haha' # 无法预知的调用错误 # def sum(a, b): # print a+b # # print sum(0, 1)+2 # print long(a) # raise ZeroDivisionError('除数不能为0') # def test(): # try: # print 5/0 # print 1 # except (TypeError, ZeroDivisionError): # print 'error' # return 0 # finally: # print '0000' # # print test() # a = [1,'a','c',10,23] # # counter = 0 # for ele in a: # try: # int(ele) # except: # counter += 1 # print counter # try: # print 'try...' # r = 10 / 0 # print 'result:', r # except ZeroDivisionError, e: # print 'except:', e # finally: # print 'finally...' # print 'END' # # try: # print 'try...' # r = 10 / int('a') # print 'result:', r # except ValueError, e: # print 'ValueError:', e # except ZeroDivisionError, e: # print 'ZeroDivisionError:', e # finally: # print 'finally...' # print 'END' import day3 from Day4 import file1 # from Day4.file1 import HuiceStudent # HuiceStudent() import time print(time.strftime('%Y%m%d%H%M%S', time.localtime())) import random for i in range(1, 11): a = random.randint(1,100) print(a)