参考教材:
视频不是我做的,放在这里供有需要的人查看
1.1基本介绍
数据分为内建原子类型和内建集合数据类型,前者包括数值类(int,float)与bool类,后者包括列表,字符串,元组,集合,字典,其中列表、元组以及字符串都是有序类,字典和集合属于无序类;
1.2变量存储的是指向数据的引用,而不是数据本身,这点很重要!!!!!!
1.3列表是零个或多个指向python数据对象的引用的有序集合,列表是异构的,即列表内的数据对象不需要全是同一个类;
1.4注意字符串中的split()方法,str1.split(char)指在char位置将str1切割成列表。
str1 = 'pypypython'
list1 = str1.split('y')
print(list1)
['p', 'p', 'p', 'thon']
值得注意的是:如果没有提供分隔符,python将会自动寻找空白分隔符,如空格,制表符等
1.5字典的索引是由散列决定的,散列在以后的文章中还会介绍
1.6关于列表,字典以及集合的基本操作,请读者自行寻找资料;
以下为类的介绍:
有关类的定义与基本操作,请参考:头歌实践教学平台-武汉理工大学-模块与包https://www.educoder.net/tasks/iolf3ytkhw/jupyter
1.7基于Fraction介绍类的定义
目的:自定义一个类来实现有理数的描述,即把0.2改写为1/5;
1.7.1构造方法
class Fraction: #注意冒号不要省略
def __init__(self,top,bottom):
self.num = top
self.den = bottom
注意:①分数需要分母与分子两部分数据,所以这里在构造时要传入两个参数;
②self 参数是指向对象本身的一个特殊参数,在创建实例时可以确保让计算机识别到底是哪个实例对象即将调用class中的方法,可以说,每个实例对象都有自己的“防伪码”,即self参数。感兴趣的读者可以不妨自己创建一个新类并创建不同的实例对象并将self参数打印出来验证我的话;
③类名命名时首字母要求大写:Fraction;
1.7.2利用构造方法创建类实例
class Fraction: #注意冒号不要省略
def __init__(self,top,bottom):
self.num = top
self.den = bottom
f1 = Fraction(3,5)#创建实例对象的办法
print(f1)
结果:<__main__.Fraction object at 0x00000190F4521A10>
注意:①可见返回了一个地址:变量存储的是指向数据的引用,而不是数据本身
②两种解决办法:
法一:重写__str__方法(因为标准类存在str方法,将对象转换为字符串,但很明显这里标准类自带的str方法并没有正常工作,需要重写覆盖)
class Fraction: #注意冒号不要省略
def __init__(self,top,bottom):
self.num = top
self.den = bottom
def __str__(self):
return str(self.num) + '/' + str(self.den)
f1 = Fraction(3,5)#创建实例对象的办法
print(f1)
结果:3/5
法二:定义一个show方法
class Fraction: #注意冒号不要省略
def __init__(self,top,bottom):
self.num = top
self.den = bottom
def __str__(self):
return str(self.num) + '/' + str(self.den)
def show(self):
print(self.num,'/',self.den)
f1 = Fraction(3,5)#创建实例对象的办法
print(f1.show())
结果:3/5
None
注意:3/5是f1.show()产生的结果,但__str__会自动执行,故返回No
诸如此类请参考:Python dunder/magic 方法https://blog.csdn.net/qq_28087491/article/details/128313886
1.7.3重写__add__以实现分数相加
class Fraction: # 注意冒号不要省略
def __init__(self, top, bottom):
self.num = top
self.den = bottom
def __str__(self):
return str(self.num) + '/' + str(self.den)
def show(self):
print(self.num, '/', self.den)
f1 = Fraction(3, 5) # 创建实例对象的办法
f2 = Fraction(1,5)
print(f1+f2)
结果:
Traceback (most recent call last):
File "C:\Users\Lenovo\Desktop\python\自学教材\数据结构与算法\test001.py", line 16, in <module>
print(f1+f2)
~~^~~
TypeError: unsupported operand type(s) for +: 'Fraction' and 'Fraction'
注意:可见标准类中的方法并不能直接实现此功能,需要我们重写__add__方法进行覆盖,改进如下:
class Fraction: # 注意冒号不要省略
def __init__(self, top, bottom):
self.num = top
self.den = bottom
def __str__(self):
return str(self.num) + '/' + str(self.den)
def show(self):
print(self.num, '/', self.den)
def __add__(self, other):
newnum = self.num * other.den + self.den * other.num
newden = self.den * other.den
return Fraction(newnum,newden)
f1 = Fraction(3, 5) # 创建实例对象的办法
f2 = Fraction(1,5)
print(f1+f2)
结果:20/25
1.7.4定义静态方法化简分数
静态方法,类方法:
在1.7.3中改进的结果是20/25,显然没有化简,定义静态方法进行化简
class Fraction:
@staticmethod
def gcd(m, n): # 分数类的构造
while m % n != 0:#欧几里得算法
oldm = m
oldn = n
m = oldn
n = oldm % oldn
return n
def __init__(self, top, bottom):
self.num = top
self.den = bottom
def show(self):
print(self.num, "/", self.den)
def __str__(self):
return (str(self.num)+'/'+str(self.den))
def __add__(self, other):
newnum = self.num * other.den + self.den * other.num
newden = self.den * other.den
common = Fraction.gcd(newnum,newden)
return Fraction(newnum//common,newden//common)#约去最大公因数
f1 = Fraction(3,5)
f2 = Fraction(1,5)
f3 = f1 + f2
print(f3)#print时自动调用__str__
结果:
注意:①为什么要定义一个静态方法呢?因为gcd显然是一个局部的函数,只在类内部调用而不在全局调用,故定义为静态方法。实际上放在外面作为一个函数也可以哦
②原理:
(图片来自书中)
1.7.5重写__eq__方法实现比较分数相等操作
class Fraction:
@staticmethod
def gcd(m, n): # 分数类的构造
while m % n != 0:
oldm = m
oldn = n
m = oldn
n = oldm % oldn
return n
def __init__(self, top, bottom):
self.num = top
self.den = bottom
def show(self):
print(self.num, "/", self.den)
def __str__(self):
return (str(self.num)+'/'+str(self.den))
def __add__(self, other):
newnum = self.num * other.den + self.den * other.num
newden = self.den * other.den
common = Fraction.gcd(newnum,newden)
return Fraction(newnum//common,newden//common)
def __eq__(self, other):
firstnum = self.num * other.den#十字相除法
secondnum = self.den * other.num
return firstnum == secondnum
f1 = Fraction(3,5)
f2 = Fraction(1,5)
print(f1==f2)
结果:False
1.7.6写方法,实现获取分母/分子的功能
class Fraction:
@staticmethod
def gcd(m, n): # 分数类的构造
while m % n != 0:
oldm = m
oldn = n
m = oldn
n = oldm % oldn
return n
def __init__(self, top, bottom):
self.num = top
self.den = bottom
def show(self):
print(self.num, "/", self.den)
def __str__(self):
return (str(self.num)+'/'+str(self.den))
def __add__(self, other):
newnum = self.num * other.den + self.den * other.num
newden = self.den * other.den
common = Fraction.gcd(newnum,newden)
return Fraction(newnum//common,newden//common)
def __eq__(self, other):
firstnum = self.num * other.den
secondnum = self.den * other.num
return firstnum == secondnum
def getden(self):
print(self.den)
def getnum(self):
print(self.num)
f1 = Fraction(3,5)
f2 = Fraction(1,5)
f1.getden()
结果:5
1.7.7
class Fraction:
@staticmethod
def gcd(m, n): # 分数类的构造
while m % n != 0:
oldm = m
oldn = n
m = oldn
n = oldm % oldn
return n
def __init__(self, top, bottom):
common = Fraction.gcd(top,bottom)
self.num = top//common
self.den = bottom//common
def show(self):
print(self.num, "/", self.den)
def __str__(self):
return (str(self.num)+'/'+str(self.den))
def __add__(self, other):
newnum = self.num * other.den + self.den * other.num
newden = self.den * other.den
return Fraction(newnum,newden)
def __eq__(self, other):
firstnum = self.num * other.den#十字相乘法
secondnum = self.den * other.num
return firstnum == secondnum
f1 = Fraction(6,10)
f2 = Fraction(1,5)
print(Fraction(6,10))
1.7.8
Python中的__sub__
, __mul__
, 和 __truediv__
是特殊方法,也被称为魔术方法(Magic Methods),它们允许你在创建自定义对象时,通过操作符直接对这些对象进行加、减、乘和除等运算,而无需显式地编写对应的函数。
-
__sub__(self, other)
:这是用于实现“-”操作符的方法,即两个对象相减。当你看到a - b
这样的表达式时,实际上是在调用a.__sub__(b)
。 -
__mul__(self, other)
:这个方法实现了“*”操作符,用于两个对象相乘,如a * b
就会触发a.__mul__(b)
。 -
__truediv__(self, other)
:这是Python 3中引入的,用于处理除法操作。“/”在大多数情况下会调用此方法,返回两个对象相除的结果。注意,“//”地板除会引发不同的方法。
这些魔法方法通常在类中被覆盖,以便给自定义的对象赋予特定的行为。例如,如果你有一个表示复数的类,你可以覆盖这些方法来处理复数之间的算术运算。
class Fraction:
@staticmethod
def gcd(m, n): # 分数类的构造
while m % n != 0:
oldm = m
oldn = n
m = oldn
n = oldm % oldn
return n
def __init__(self, top, bottom):
common = Fraction.gcd(top,bottom)
self.num = top//common
self.den = bottom//common
def show(self):
print(self.num, "/", self.den)
def __str__(self):
return (str(self.num)+'/'+str(self.den))
def __add__(self, other):
newnum = self.num * other.den + self.den * other.num
newden = self.den * other.den
return Fraction(newnum,newden)
def __eq__(self, other):
firstnum = self.num * other.den#十字相乘法
secondnum = self.den * other.num
return firstnum == secondnum
def __sub__(self, other):
newnum = self.num * other.den - self.den * other.num
newden = self.den * other.den
return Fraction(newnum, newden)
def __mul__(self, other):
newnum = self.num * other.num
newden = self.den * other.den
return Fraction(newnum, newden)
def __truediv__(self, other):
newnum = self.num * other.den
newden = self.den * other.num
return Fraction(newnum, newden)
f1 = Fraction(1,2)
f2 = Fraction(1,5)
print(f1-f2)
print(f1*f2)
print(f1/f2)
结果:
3/10
1/10
5/2
1.7.9
Python 中的 __gt__
、__ge__
、__lt__
、__le__
和 __ne__
是特殊方法(也称为魔术方法或 dunder 方法),它们用于实现 Python 对象之间的比较操作符。这些方法允许自定义类的行为,当与 <
, >
, <=
, >=
, !=
运算符结合时。
__gt__(self, other)
:当实例self
比other
大时返回True
,即self > other
。例如,如果你定义了一个自定义排序类,这个方法将用于比较两个对象的大小。__ge__(self, other)
:返回True
当且仅当self >= other
,即self
至少与other
相等。__lt__(self, other)
:如果self
小于other
,返回True
。__le__(self, other)
:返回True
当且仅当self <= other
。__ne__(self, other)
或者__eq__(self, other)
(因为 Python 的默认==
等于操作对应的是__eq__
):返回True
如果self
不等于other
,反之则返回False
。
使用这些方法,你可以扩展内置的操作符行为,并让自定义类的行为与其他标准数据类型相一致。例如,如果你创建了一个自定义日期类,可以定义这些方法以便支持日期间的比较。
class Fraction:
@staticmethod
def gcd(m, n): # 分数类的构造
while m % n != 0:
oldm = m
oldn = n
m = oldn
n = oldm % oldn
return n
def __init__(self, top, bottom):#构造函数
common = Fraction.gcd(top,bottom)
self.num = top//common
self.den = bottom//common
def show(self):
print(self.num, "/", self.den)
def __str__(self):#字符化输出
return (str(self.num)+'/'+str(self.den))
def __add__(self, other):#加法
newnum = self.num * other.den + self.den * other.num
newden = self.den * other.den
return Fraction(newnum,newden)
def __sub__(self, other):#减法
newnum = self.num * other.den - self.den * other.num
newden = self.den * other.den
return Fraction(newnum, newden)
def __mul__(self, other):#乘法
newnum = self.num * other.num
newden = self.den * other.den
return Fraction(newnum, newden)
def __truediv__(self, other):#除法
newnum = self.num * other.den
newden = self.den * other.num
return Fraction(newnum, newden)
def __gt__(self, other):#>
firstnum = self.num * other.den # 十字相乘法
secondnum = self.den * other.num
return firstnum > secondnum
def __ge__(self, other):#>=
firstnum = self.num * other.den # 十字相乘法
secondnum = self.den * other.num
return firstnum >= secondnum
def __lt__(self, other):#<
firstnum = self.num * other.den # 十字相乘法
secondnum = self.den * other.num
return firstnum < secondnum
def __le__(self, other):#<=
firstnum = self.num * other.den # 十字相乘法
secondnum = self.den * other.num
return firstnum <= secondnum
def __eq__(self, other): # 判断相等与否
firstnum = self.num * other.den # 十字相乘法
secondnum = self.den * other.num
return firstnum == secondnum
f1 = Fraction(1,2)
f2 = Fraction(1,5)
print(f1>f2)
print(f1<f2)
print(f1>=f2)
print(f1<=f2)
结果:
True
False
True
False
1.7.10
class Fraction:
@staticmethod
def gcd(m, n): # 分数类的构造
while m % n != 0:
oldm = m
oldn = n
m = oldn
n = oldm % oldn
return n
def __init__(self, top, bottom):#构造函数
if type(top) == int and type(bottom) == int:
common = Fraction.gcd(top,bottom)
self.num = top//common
self.den = bottom//common
else:
raise TypeError
def show(self):
print(self.num, "/", self.den)
def __str__(self):#字符化输出
return (str(self.num)+'/'+str(self.den))
def __add__(self, other):#加法
newnum = self.num * other.den + self.den * other.num
newden = self.den * other.den
return Fraction(newnum,newden)
def __sub__(self, other):#减法
newnum = self.num * other.den - self.den * other.num
newden = self.den * other.den
return Fraction(newnum, newden)
def __mul__(self, other):#乘法
newnum = self.num * other.num
newden = self.den * other.den
return Fraction(newnum, newden)
def __truediv__(self, other):#除法
newnum = self.num * other.den
newden = self.den * other.num
return Fraction(newnum, newden)
def __gt__(self, other):#>
firstnum = self.num * other.den # 十字相乘法
secondnum = self.den * other.num
return firstnum > secondnum
def __ge__(self, other):#>=
firstnum = self.num * other.den # 十字相乘法
secondnum = self.den * other.num
return firstnum >= secondnum
def __lt__(self, other):#<
firstnum = self.num * other.den # 十字相乘法
secondnum = self.den * other.num
return firstnum < secondnum
def __le__(self, other):#<=
firstnum = self.num * other.den # 十字相乘法
secondnum = self.den * other.num
return firstnum <= secondnum
def __eq__(self, other): # 判断相等与否
firstnum = self.num * other.den # 十字相乘法
secondnum = self.den * other.num
return firstnum == secondnum
f1 = Fraction(1.0,2)
f2 = Fraction(1,5)
结果:
TypeError
1.7.11
class Fraction:
@staticmethod
def gcd(m, n): # 分数类的构造
while m % n != 0:
oldm = m
oldn = n
m = oldn
n = oldm % oldn
return n
def __init__(self, top, bottom):#构造函数
if not isinstance(top,int) or not isinstance(bottom,int):
raise TypeError
if bottom == 0:
raise ZeroDivisionError
if top * bottom <0:
top = -abs(top)
bottom = abs(bottom)
common = Fraction.gcd(top,bottom)
self.num = top//common
self.den = bottom//common
def show(self):
print(self.num, "/", self.den)
def __str__(self):#字符化输出
return (str(self.num)+'/'+str(self.den))
def __add__(self, other):#加法
newnum = self.num * other.den + self.den * other.num
newden = self.den * other.den
return Fraction(newnum,newden)
def __sub__(self, other):#减法
newnum = self.num * other.den - self.den * other.num
newden = self.den * other.den
return Fraction(newnum, newden)
def __mul__(self, other):#乘法
newnum = self.num * other.num
newden = self.den * other.den
return Fraction(newnum, newden)
def __truediv__(self, other):#除法
newnum = self.num * other.den
newden = self.den * other.num
return Fraction(newnum, newden)
def __gt__(self, other):#>
firstnum = self.num * other.den # 十字相乘法
secondnum = self.den * other.num
return firstnum > secondnum
def __ge__(self, other):#>=
firstnum = self.num * other.den # 十字相乘法
secondnum = self.den * other.num
return firstnum >= secondnum
def __lt__(self, other):#<
firstnum = self.num * other.den # 十字相乘法
secondnum = self.den * other.num
return firstnum < secondnum
def __le__(self, other):#<=
firstnum = self.num * other.den # 十字相乘法
secondnum = self.den * other.num
return firstnum <= secondnum
def __eq__(self, other): # 判断相等与否
firstnum = self.num * other.den # 十字相乘法
secondnum = self.den * other.num
return firstnum == secondnum
f1 = Fraction(1,-5)
f2 = Fraction(1,5)
print(f1)
结果:
-1/5
1.8基于数字逻辑电路介绍类的继承
先悄悄挖个坑,等我下学期学完数字电路之后补上。