目录
96-101 Bug
lst=[{'rating':[9.7,206287],'id':'129052','type':['剧情','犯罪'],'title':'肖申克的救赎','actors':['蒂姆.罗宾斯','摩根.弗里曼']}]
name=input('请输入你要查询的演员')
for item in lst:#遍历列表:{} item是一个又一个的字典
act_lst = item['actors']
for actor in act_lst:
if name in actor:
print(name,'出演了',item['title'])
# print(act_lst)
# for movie in item: #遍历字典,得到movie是一个字典中的key
# print(movie)
# actors=movie['actors']
# if name in actors:
# print(name+'出演了'+movie)
try:
a = int(input('请输入第一个整数'))
b = int(input('请输入第二个整数'))
result = a / b
print('结果为:', result)
except ZeroDivisionError:
print('除数不能为0!')
except ValueError:
print('请输入数字串!')
except BaseException as e:
print(e)
print('程序结束')
try:
a = int(input('请输入第一个整数'))
b = int(input('请输入第二个整数'))
result = a / b
except BaseException as e:
print('出错了,错误为:',e)
else:
print('结果为:', result)
print('程序结束')
# 请输入第一个整数a
# 出错了,错误为: invalid literal for int() with base 10: 'a'
# 程序结束
# 请输入第一个整数10
# 请输入第二个整数0
# 出错了,错误为: division by zero
# 程序结束
try:
a = int(input('请输入第一个整数'))
b = int(input('请输入第二个整数'))
result = a / b
except BaseException as e:
print('出错了,错误为:',e)
else:
print('结果为:', result)
finally:
print('谢谢您的使用')
print('程序结束')
# 请输入第一个整数10
# 请输入第二个整数3
# 结果为: 3.3333333333333335
# 谢谢您的使用
# 程序结束
102 常见异类类型
103 traceback模块
import traceback
try:
print('1.____________')
num=10/0
except:
traceback.print_exc()
104 pycharm调试
105 编程思想
106 类与对象
python中一切皆对象
107 类的创建
类的名称由一个或多个单词组成,每个单词的首字母大写,其余小写
类是对象,开辟内存空间
108 对象的创建
(这节课讲的很差,可能听不懂,需要继续往后听)
class Student:# Student称为类的名称
native_pace='吉林'#直接写在类里面的变量,称为类属性
def __init__(self,name,age):#name,age是实例属性
self.name=name
self.age=age
#实例方法
def eat(self):
print('学生在吃饭')
#静态方法,使用@staticmethod
@staticmethod
def method():
print('静态方法中不允许写self')
#类方法,@classmethod
@classmethod
def cm(cls):
print('类方法中传cls')
#在类之外定义的称为函数,在类之内定义的称为方法
stu1=Student('张三',20)
stu1.eat()
stu1.cm()
stu1.method()
print(stu1.name,stu1.age)
# 学生在吃饭
# 类方法中传cls
# 静态方法中不允许写self
# 张三 20
Student.eat(stu1)
# 学生在吃饭
- 对象名.方法名()
- 类名.方法名(类的对象)
109 类属性、类方法、静态方法
print(Student.native_pace)#吉林
stu1=Student('张三',20)
stu2=Student('李四',30)
print(stu1.native_pace)
print(stu2.native_pace)
# 吉林
# 吉林
Student.native_pace='天津'
print(stu1.native_pace)
print(stu2.native_pace)
# 天津
# 天津
Student.cm()
Student.method()
# 类方法中传cls
# 静态方法中不允许写self
110 动态绑定属性和方法
class Student:
def __init__(self,name,age):
self.name=name
self.age=age
def eat(self):
print(self.name+'在吃饭')
stu1=Student('张三',30)
stu2=Student('李四',40)
stu2.gender='男'
print(stu1.name,stu1.age)
print(stu2.name,stu2.age,stu2.gender)
# 张三 30
# 李四 40 男
def show():
print('定义在类之外,函数')
stu1.show=show
stu1.show()
(小声吐槽:这一章讲得。。。。。。懂也可能听不懂了)
110 面向对象的三大特征-封装
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(stu.__age)AttributeError: 'Student' object has no attribute 'name'
print(dir(stu))
print(stu._Student__age)#在类的外部强制访问
#20
111 面向对象的三大特征-继承
class Person(object):
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print('姓名:{0},年龄:{1}'.format(self.name,self.age))
#定义子类
class Student(Person):
def __init__(self,name,age,score):
super().__init__(name,age)
self.score=score
#测试
stu=Student('Jack',20,'1001')
stu.info()
# 姓名:Jack,年龄:20
112 方法重写
class 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
stu = Student('张三', 20, '10010')
stu.info()
# 张三 20
# 10010
113 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))
# ['__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__']
print(stu)
# <__main__.Student object at 0x0000016E05C38400>
# 我的名字是张三,今年20岁了 #默认调用__str__()这样的方法,重写了
114 面向对象的三大特征-多态
Python是一门动态语言
class Animal(object):
def eat(self):
print('动物吃')
class Dog(Animal):
def eat(self):
print('狗吃')
class Cat(Animal):
def eat(self):
print('猫吃')
class Person(object):
def eat(self):
print('人吃')
def fun(animal):
animal.eat()
fun(Dog())
fun(Cat())
fun(Person())
# 狗吃
# 猫吃
# 人吃
115 116 特殊方法和特殊属性
print(dir(object))
# ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__',
# '__getattribute__', '__gt__', '__hash__','__init__', '__init_subclass__', '__le__',
# '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
# '__sizeof__', '__str__', '__subclasshook__']
class A:
pass
class B:
pass
class C(A,B):
def __init__(self,name):
self.name=name
x=C('Jack')
print(x.__dict__) #{'name': 'Jack'}
print(C.__dict__)#{'__module__': '__main__', '__init__': <function C.__init__ at 0x000001C1EF107700>, '__doc__': None}
print(x.__class__)#对象所属类<class '__main__.C'>
print(C.__bases__)#父类元组(<class '__main__.A'>, <class '__main__.B'>)
print(C.__base__)#基类元组<class '__main__.A'>
print(C.__mro__)#类的层次结构(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)
print(B.__subclasses__())#子类[<class '__main__.C'>]
a=20
b=100
c=a+b
d=a.__add__(b)
print(c)#120
print(d)#120
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('李四')
#print(stu1+stu2)TypeError: unsupported operand type(s) for +: 'Student' and 'Student'
print(stu1+stu2)#张三李四
#实现了两个对象的加法运算,在类中编写了add特殊的方法
print(len(stu1))#TypeError: object of type 'Student' has no len()
#2 在类中编写了len特殊的方法,return len(self.name)
117 _new_ / _init_
class Person(object):
def __new__(cls, *args, **kwargs): #创建对象
print('__new__被调用执行了,cls的id值为{0}'.format(id(cls))) #4864
obj=super().__new__(cls)
print('创建的对象的id为:{0}'.format(id(obj))) #7264
return obj
def __init__(self,name,age): #对创建的对象初始化
self.name=name
self.age=age
print(' _init__被调用了,self的id为:{0}'.format(id(self))) #7264
print('object id:{0}'.format(id(object))) #4048
print('Person id:{0}'.format(id(Person))) #4864
p1=Person('三',20)
print('p1 id:{0}'.format(id(p1))) #7264
# object id:140709592514048
# Person id:2701662344864
# __new__被调用执行了,cls的id值为2701662344864
# 创建的对象的id为:2701663637264
# _init__被调用了,self的id为:2701663637264
# p1 id:2701663637264
118 类的赋值与浅拷贝
119深拷贝
#类对象的赋值操作,形成两个变量,实际上还是指向同一个对象
cpu1=CPU()
cpu2=cpu1
print(cpu1)
print(cpu2)
# <__main__.CPU object at 0x0000021A25626FD0>
# <__main__.CPU object at 0x0000021A25626FD0>
disk=Disk()
computer=Computer(cpu1,disk)
import copy
computer2=copy.copy(computer) #浅拷贝,对象包含的子对象内容不拷贝
computer3=copy.deepcopy(computer) #深拷贝,递归拷贝对象中包含的子对象
print(computer,computer.cpu,computer.disk)
print(computer2,computer2.cpu,computer2.disk)
print(computer3,computer3.cpu,computer3.disk)
# <__main__.Computer object at 0x000002281D258F70> <__main__.CPU object at 0x000002281D258FD0> <__main__.Disk object at 0x000002281D258FA0>
# <__main__.Computer object at 0x000002281D258820> <__main__.CPU object at 0x000002281D258FD0> <__main__.Disk object at 0x000002281D258FA0>
# <__main__.Computer object at 0x000002281D258730> <__main__.CPU object at 0x000002281D258430> <__main__.Disk object at 0x000002281D258460>
120 模块化编程的好处
121 模块的导入
import math
print(id(math))
print(type(math)) #<class 'module'>
print(math) #<module 'math' (built-in)>
print(math.pi) #3.141592653589793
print('==================')
print(dir(math))
print(math.pow(2,3),type(math.pow(2,3))) #8.0 <class 'float'>
print(math.ceil(9.324)) #10
print(math.floor(32.435)) #32
122 以主程序方式运行
main+回车 快速输入:if __name__ == '__main__':
#只有点击运行本身模块时才运行if __name__ == '__main__':下面的代码,import时不会执行
123 python中的包
使用import方式进行导入,只能跟包名或模块名
使用from...import方式进行导入,可以导入包、模块、函数、变量
import pageage1
import calc
from pageage1 import modulea
from pageage1.modulea import a
124 常用的内容模块
125 第三方模块的安装与使用
126 编码格式的介绍
127 文件读写的原理
类似队列先进先出
128 常用的文件打开模式
129 文件对象的常用方法
130 with语句
with open('logo.png','rb') as src_file:
with open('copy2logo.png','wb') as target_file:
target_file.write(src_file.read())
131 os模块的常用函数
132 os.path模块的常用方法