python语法学习(下)

简介: python语法学习

5.变量的作用域

1670683947887.jpg


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.递归函数

1670683972108.jpg

1670683979449.jpg

def fac(n):
    if n==1:
        return 1
    else:
        return n*fac(n-1)
print(fac(6))


第十一章 全民来找茬


0.本章总结

1670684006038.jpg


1.bug的由来及分类

1670684013140.jpg

1670684020826.jpg

1670684028467.jpg

1670684033862.jpg

try:
    a=int(int('请输入第一个整数'))
    b=int(input('请输入第二个整数'))
    result=a/b
    print('结果为:',result)
except ZeroDivisionError:
    print('对不起,除数不允许为0')
except ValueError:
    print('只能输入数字串')
print('程序结束')

1670684055127.jpg

try:
    a=int(input('请输入第一个整数'))
    b=int(input('请输入第二个整数'))
    result=a/b
    print('结果为:',result)
except BaseException as e:
    print('出错了',e)
else:
    print('计算结果为:',result)

1670684086981.jpg

try:
    a=int(input('请输入第一个整数'))
    b=int(input('请输入第二个整数'))
    result=a/b
    print('结果为:',result)
except BaseException as e:
    print('出错了',e)
else:
    print('计算结果为:',result)
finally:
    print('谢谢你的使用')


2.不同异常类型的处理方式

1670684105918.jpg


3.异常处理机制

1670684125394.jpg

import traceback
try:
    print('-----------')
    print(1/0)
except:
        traceback.print_exc()


4.pycharm的调式模式

1670684142907.jpg

1670684149145.jpg


第十二章 找对象不积极思想有问题


0.本章总结

1670684169385.jpg


1.两大编程思想

1670684176369.jpg


2.类和对象的创建

1670684182803.jpg

1670684191261.jpg

1670684198518.jpg

1670684206433.jpg

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('喝水')

1670684227699.jpg

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.类对象与类属性

1670684257184.jpg


4.类方法与静态方法

1670684264529.jpg

1670684270717.jpg

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.本章总结

1670684299850.jpg


1.封装

1670684309816.jpg

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.继承

1670684331178.jpg

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.方法重写(理解不了)

1670684350199.jpg

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类

1670684375172.jpg

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))


对象中__str__描述


5.多态

1670684403367.jpg

1670684412792.jpg


6.特殊方法和特殊属性

1670684420717.jpg

特殊属性:

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)))

1670684476125.jpg

1670684482520.jpg

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))

1670684499283.jpg

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)


深拷贝:

1670684518142.jpg

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.本章总结

1670684555203.jpg


1.什么叫模块

1670684563749.jpg

模块包含函数,包含类(类中包含类属性,实例方法,类方法,静态方法),包含语句

1670684571813.jpg


2.自定义模块

1670684583864.jpg

方法一:导入模块中所有

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))


自己定义的模块可以导入吗

先定义一个模块:

1670684607186.jpg

在新建一个程序文件.py,直接如下写会报错:

1670684615739.jpg

正确操作如下,先找到目录,在目录下,找到下面的步骤点好。

1670684625336.jpg

1670684633580.jpg

下面就可以正常使用了

1670684640483.jpg

3.以主程序的形式执行

1670684671522.jpg

1670684681378.jpg

单独运行calc2,会输出print(add(10,20)).如果不加if…==…,那么在其他程序中调用时,这就会也输出这个calc2中的print(add(10,20))。加上后,意思是只有calc2是主程序时候,才会执行print(add(10,20))。

1670684692352.jpg


4.python中的包

1670684699515.jpg

1670684706674.jpg


怎样去借python的包:

1670684715889.jpg

1670684722651.jpg

1670684730168.jpg

1670684737167.jpg

1670684744168.jpg

以上建立包和建立目录的区别,如下是怎么写包

1670684756444.jpg

1670684764474.jpg


写好包以后,在别的程序中导入包:


1670684772382.jpg

1670684780688.jpg

1670684787670.jpg

5.第三方模块的安装以及使用

1670684802987.jpg

windows+R 输入cmd

输入pip install schedule

1670684809936.jpg

1670684816907.jpg


第十五章 大宝藏


0.本章小结

1670684838048.jpg

1.编码格式介绍

1670684848800.jpg

#encoding=gbk ====>ANSI格式


2.文件的读写原理

1670684856065.jpg


3.文件的读写操作


1670684863022.jpg

file=open('a.txt','r')
print(file.readlines())
file.close()

1670684878661.jpg


4.文件对象常用的方法

1670684891566.jpg


5.with语句(上下文管理器)

1670684903169.jpg

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.目录操作

1670684929485.jpg

是一个python自带的一个模块。

1670684938055.jpg

1670684945463.jpg

1670684952407.jpg

相关文章
|
15天前
|
Python
python函数的参数学习
学习Python函数参数涉及五个方面:1) 位置参数按顺序传递,如`func(1, 2, 3)`;2) 关键字参数通过名称传值,如`func(a=1, b=2, c=3)`;3) 默认参数设定默认值,如`func(a, b, c=0)`;4) 可变参数用*和**接收任意数量的位置和关键字参数,如`func(1, 2, 3, a=4, b=5, c=6)`;5) 参数组合结合不同类型的参数,如`func(1, 2, 3, a=4, b=5, c=6)`。
16 1
|
11天前
|
Python
python学习3-选择结构、bool值、pass语句
python学习3-选择结构、bool值、pass语句
|
2天前
|
运维 Shell Python
Shell和Python学习教程总结
Shell和Python学习教程总结
|
2天前
|
Python
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
|
2天前
|
开发框架 前端开发 数据库
Python从入门到精通:3.3.2 深入学习Python库和框架:Web开发框架的探索与实践
Python从入门到精通:3.3.2 深入学习Python库和框架:Web开发框架的探索与实践
|
2天前
|
数据采集 数据可视化 数据处理
Python从入门到精通的文章3.3.1 深入学习Python库和框架:数据处理与可视化的利器
Python从入门到精通的文章3.3.1 深入学习Python库和框架:数据处理与可视化的利器
|
2天前
|
存储 网络协议 关系型数据库
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
|
9天前
|
机器学习/深度学习 算法 Python
使用Python实现集成学习算法:Bagging与Boosting
使用Python实现集成学习算法:Bagging与Boosting
19 0
|
10天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
51 0
|
10天前
|
Python
02-python的基础语法-01python字面量/注释/数据类型/数据类型转换
02-python的基础语法-01python字面量/注释/数据类型/数据类型转换