5.变量的作用域
def fun(a,b): c=a+b print(c) #print(c)#因为a,c超出了其本身作用的范围 #print(a) name='杨老师' print(name) def fun2(): print(name) #调用函数 fun2() def fun3(): global age #函数内部定义的变量,局部变量,局部变量使用global声明,这个变量实际上就变成了全局变量 age=20 print(age) fun3() print(age)
6.递归函数
def fac(n): if n==1: return 1 else: return n*fac(n-1) print(fac(6))
第十一章 全民来找茬
0.本章总结
1.bug的由来及分类
try: a=int(int('请输入第一个整数')) b=int(input('请输入第二个整数')) result=a/b print('结果为:',result) except ZeroDivisionError: print('对不起,除数不允许为0') except ValueError: print('只能输入数字串') print('程序结束')
try: a=int(input('请输入第一个整数')) b=int(input('请输入第二个整数')) result=a/b print('结果为:',result) except BaseException as e: print('出错了',e) else: print('计算结果为:',result)
try: a=int(input('请输入第一个整数')) b=int(input('请输入第二个整数')) result=a/b print('结果为:',result) except BaseException as e: print('出错了',e) else: print('计算结果为:',result) finally: print('谢谢你的使用')
2.不同异常类型的处理方式
3.异常处理机制
import traceback try: print('-----------') print(1/0) except: traceback.print_exc()
4.pycharm的调式模式
第十二章 找对象不积极思想有问题
0.本章总结
1.两大编程思想
2.类和对象的创建
class Student: native_pace='安徽' def__init__(self,name,age): self.name=name self.age=age #实例方法 def eat(self): print('学生在吃饭...') #静态方法 @staticmethod def method(): print('我使用了staticmethod进行修饰,所以我是静态方法') #类方法 @classmethod def cm(cls): print('我是类方法,因为我使用了classmethod进行修饰') #在类之外定义的称为函数,在类之内定义的称为方法 def drink(): print('喝水')
class Student: native_place='安徽' def __init__(self,name,age): self.name=name self.age=age #实例方法 def eat(self): print('学生在吃饭...') #静态方法 @staticmethod def method(): print('我使用了staticmethod进行修饰,所以我是静态方法') #类方法 @classmethod def cm(cls): print('我是类方法,因为我使用了classmethod进行修饰') #在类之外定义的称为函数,在类之内定义的称为方法 def drink(): print('喝水') #创建一个student类的对象 stu1=Student('张三',20) print(id(stu1)) print(type(stu1)) print(stu1) print('---------------------') print(id(Student)) print(type(Student)) print(Student) #调用 stu1=Student('张三',20) stu1.eat() print(stu1.name) print(stu1.age) print('------------------') Student.eat(stu1)#33行与28行代码功能相同,都是调用Student中的eat方法 #类名.方法名(类的对象)-->实际上就是方法定义处的self
3.类对象与类属性
4.类方法与静态方法
class Student: native_place='安徽' def __init__(self,name,age): self.name=name self.age=age #实例方法 def eat(self): print('学生在吃饭...') stu1=Student('张三',20) stu2=Student('李四',30) print(id(stu1)) print(id(stu2)) print('----------为stu2动态绑定----------') stu1.gender='女' #print(stu1.name,stu1.age,stu1.gender) print(stu1.name,stu1.age,stu1.gender) print(stu2.name,stu2.age) print('---------------') stu1.eat() stu2.eat() def show(): print('定义在类之外的,称函数') stu1.show=show stu1.show()
第十三章 接着找对象
0.本章总结
1.封装
class Car: def __init__(self,brand): self.brand=brand def start(self): print('汽车已启动。。') car=Car('宝马x5') car.start() print(car.brand) class Student: def __init__(self,name,age): self.name=name self.__age=age def show(self): print(self.name,self.__age) stu=Student('张三',20) stu.show() #在类的外使用name和age print(stu.name) #print(stu.__age) print(dir(stu))#['_Student__age', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name', 'show'] print(stu._Student__age)
2.继承
class Person(object): #person集成object类 def __init__(self,name,age): self.name=name self.age=age def info(self): print(self.name,self.age) class Student(person): def __init__(self,name,age,stu_no): super().__init__(name,age) self.stu_no=stu_no class Teacher(Person): def __init__(self,name,age,teachofyear): super.__init__(name,age) self.teachofyear=teachofyear stu=Student('张三',20,'1001') teacher=Teacher('李四',34,10) print('--------多继承----------') class A(object): pass class B(object): pass class C(A,B): pass
3.方法重写(理解不了)
class Person(object): #person集成object类 def __init__(self,name,age): self.name=name self.age=age def info(self): print(self.name,self.age) class Student(person): def __init__(self,name,age,stu_no): super().__init__(name,age) self.stu_no=stu_no def info(self): super().info() print(self.stu_no) class Teacher(Person): def __init__(self,name,age,teachofyear): super.__init__(name,age) self.teachofyear=teachofyear def info(self): super().info() print('教龄',self.teachofyear) stu=Student('张三',20,'1001') teacher=Teacher('李四',34,10) print('-----------------') stu.info() teacher.info()
4.object类
class Student: def __init__(self,name,age): self.name=name self.age=age def __str__(self): return '我的名字是{0},今年{1}岁'.format(self.name,self.age) stu=Student('张三',20) print(dir(stu)) print(stu)#默认调用__str__()这样的方法 print(type(stu))
5.多态
6.特殊方法和特殊属性
特殊属性:
class A: pass class B: pass class C(A,B): def __init__(self,name): self.name=name class D(A): pass #创建C类的对象 x=C('Jack')#x是C类型的一个实例对象 print(x.__dict__) #实例对象的属性字典 print(C.__dict__) print('-------------------')# 输出对象所属的类 print(x.__class__)#C类的父类类型的元素 print(C.__bases__)#C类的父类类型的元素 print(C.__base__) print(C.__mro__)#类的层次结构 print(A.__subclasses__())#子类的列表
特殊方法;
a=20 b=100 c=a+b#两个整数类型的对象相加操作 d=a.__add__(b) print(c) print(d) class Student: def __init__(self,name): self.name=name def __add__(self,other): return self.name+other.name def __len__(self): return len(self.name) stu1=Student('张三') stu2=Student('李四') s=stu1+stu2 #实现了两个对象的加法运算(因为student类中,编写__add__()特殊的方法) print(s) s=stu1.__add__(stu2) print(s) print('-------------------------------') lst=[11,22,33,44] print(len(lst))#len是内容函数len print(lst.__len__()) print(len(stu1))
class Person(object): def __new__(cls, *args, **kwargs): print('__new__被调用执行了,cls的id值为{0}'.format(id(cls))) obj=super().__new__(cls) print('创建的对象的id为:{0}'.format(id(obj))) return obj def __init__(self,name,age): print('__init__被调用了',self的id值:{0}.format(id(self))) self.name=name self.age=age print('object这个累对象的id为:{0}'.format(id(object))) print('person这个类对象的id为:{0}'.format(id(person))) #创建person类的实例对象 p1=person('张三',20) print('p1这个person类的实例对象的id:{0}'.format(id(p1)))
class CPU: pass class Disk: pass class Computer: def __init__(self,cpu,disk): self.cpu=cpu self.disk=disk #1)变量的赋值 cpu1=CPU() cpu2=cpu1 print(cpu1,id(cpu1)) print(cpu2,id(cpu2))
class CPU: pass class Disk: pass class Computer: def __init__(self,cpu,disk): self.cpu=cpu self.disk=disk #1)变量的赋值 cpu1=CPU() cpu2=cpu1 print(cpu1,id(cpu1)) print(cpu2,id(cpu2)) #(2)类有浅拷贝 print('-----------------------') disk=Disk() #创建一个硬盘类的对象 computer=Computer(cpu1,disk)#创建一个计算机的对象 #浅拷贝 import copy cpmputer2=copy.copy(computer) print(computer,computer.cpu,computer.disk) print(computer2,computer2.cpu,computer2.disk)
深拷贝:
class CPU: pass class Disk: pass class Computer: def __init__(self,cpu,disk): self.cpu=cpu self.disk=disk #1)变量的赋值 cpu1=CPU() cpu2=cpu1 print(cpu1,id(cpu1)) print(cpu2,id(cpu2)) #(2)类有浅拷贝 print('-----------------------') disk=Disk() #创建一个硬盘类的对象 computer=Computer(cpu1,disk)#创建一个计算机的对象 #浅拷贝 import copy computer2=copy.copy(computer) print(computer,computer.cpu,computer.disk) print(computer2,computer2.cpu,computer2.disk) #深拷贝 computer3=copy.deepcopy(computer) print(computer,computer.cpu,computer.disk) print(computer3,computer3.cpu,computer3.disk)
第十四章 百宝箱
0.本章总结
1.什么叫模块
模块包含函数,包含类(类中包含类属性,实例方法,类方法,静态方法),包含语句
2.自定义模块
方法一:导入模块中所有
import math print(id(math)) print(type(math)) print(math) print(math.pi) print('--------------------') print(dir(math)) print(math.pow(2,3),type(math.pow(2,3))) print(math.ceil(9.001)) print(math.floor(9.999))
方法二:导入模块中指定的方法
from math import pi from math import pow print(pi) print(pow(2,3)) #print(math.pow(2,3))
自己定义的模块可以导入吗
先定义一个模块:
在新建一个程序文件.py,直接如下写会报错:
正确操作如下,先找到目录,在目录下,找到下面的步骤点好。
下面就可以正常使用了
3.以主程序的形式执行
单独运行calc2,会输出print(add(10,20)).如果不加if…==…,那么在其他程序中调用时,这就会也输出这个calc2中的print(add(10,20))。加上后,意思是只有calc2是主程序时候,才会执行print(add(10,20))。
4.python中的包
怎样去借python的包:
以上建立包和建立目录的区别,如下是怎么写包
写好包以后,在别的程序中导入包:
5.第三方模块的安装以及使用
windows+R 输入cmd
输入pip install schedule
第十五章 大宝藏
0.本章小结
1.编码格式介绍
#encoding=gbk ====>ANSI格式
2.文件的读写原理
3.文件的读写操作
file=open('a.txt','r') print(file.readlines()) file.close()
4.文件对象常用的方法
5.with语句(上下文管理器)
with open('a.txt','r') as file: print(file.read())
with open('logo.png','rb') as src_file: with open('copy2logo.png','wb') as target_file: target_file.write(src_file.read())
6.目录操作
是一个python自带的一个模块。