class student:
name=None#初始值置空
sex=None
age=None
def way(self):
print(f"大家好!我是{self.name}")
def say(self,mesy):
print(f"大家好!我是{self.name},{mesy}")
std=student()
std.name="lihua"
std.age=12
std.sex="nan"
print(std.age)
print(std.name)
print(std.sex)
print(f"大家好!我是{std.name},以后请多多关照")
std.way()
std.say("以后互相帮助!")
scd=student()
scd.name="xiaoming"
scd.say("感谢")
for n in range(1,3):
class students:
def __init__(self,name,age):#类的构造方法
self.name=name
self.age=age
print("你已完成students类对象的创建!")
def sway(self,sey):
print(f"大家好!我是{self.name},{sey}")
std1=students( input("请输入姓名"), input("请输入年龄"))
print(std1.name)
print(std1.age)
std1.sway("以后请多多关照!")
class students:
def __init__(self,name,age):
self.name=name
self.age=age
def __str__(self):
return f"我的名字是{self.name},很高兴认识你!"
def __lt__(self, other):#lt小于的判断,le小于等于或者大于等于的判断,eq是否等于
return self.age<other.age
stu1=students("lihua",12)
stu2=students("xiaoming",14)
#print(stu1.__init__(name="lihua",age=12))
#print(str(stu1))
print(stu1<stu2)
class phone:
__mul=None
def __medeol(self):
if self.__mul>1:
print(f"成功!{self.__mul}")
else:
print("失败!")
stu1=phone()
stu1.__mul=2#私有成员赋值,不报错但无效
#单继承
class phone:
idea="123"
def way(self):
print("4G")
class phone2022:
id=111
def two(self):
print("华为")
class phone2023(phone):
face_id=None
idea="234"
def new_way(self):
print(f"5G{phone.idea}")
def way(self):
print("5g")
#复写
stu1=phone2023()
#stu1.idea=11
stu1.face_id=12
print(stu1.idea)
stu1.new_way()
#多继承
class my_new(phone,phone2022):
pass
#多继承 父类方法同名 左边优先
#多态
class bridge():
def hot(self):
pass
def cool(self):
pass
def sway(self):
pass
class meide(bridge):
def hot(self):
print("美的制热")
def cool(self):
print("美的制冷")
def sway(self):
print("美的左右摆风")
class geli(bridge):
def hot(self):
print("格力制热")
def cool(self):
print("格力制冷")
def sway(self):
print("格力左右摆风")
MEIDE=meide()
GELI=geli()
MEIDE.hot()
GELI.hot()
#闭包
def outer(num1):
def inner(num2):
nonlocal num1
num1+=num2
print(num1)
return inner
fn=outer(10)
fn(10)
#正则表达式
#match(从第一个开始搜索,第一个不是就结束),search(搜索到一个之后停止),findaly(全部搜索)
import re
s='python;n37483274'
result=re.match(r'python',s)
print(result)
闭包需要满足的三个条件是什么?按照上面三个条件创建一个用于计算平方根的闭包函数
如果在一个内部函数中对外部函数作用域(非全局作用域)的变量进行引用,那么内部函数就成为闭包。闭包需要满足以下三个条件:
(1)存在于嵌套关系的函数中
(2)嵌套的内部函数引用了外部函数的变量
(3)嵌套的外部函数会将内部函数名作为返回值返回
def outer(a):
def inner(b):
return b**a
return inner
c=outer(1/2)
b=float(input("请输入一个数字"))
print(c(b))
输入一个数,使用reduce计算这个数的阶乘
from functools import reduce
x=int(input("请输入x的值"))
def f(a,b):
return a*b
c=reduce(f,range(1,x+1))
实现乘法的重载,将两个列表对应得元素相乘
class list:
list1=None
def __mul__(self, other):
a = len(self.list1)
for b in range(a):
print(self.list1[b]*other[b])
c=list()
c.list1=[1,2,3]
c.__mul__([4,5,6])
完成鱼、鸟、水鸟的多重继承创建
class a():
def fish (self):
print("鱼")
class b():
def bird (self):
print("鸟")
class c(a,b):
def brid_fly (self):
print("飞鸟")
d=c()
d.brid_fly()
d.bird()
d.fish()
创建一个实例。按照实例.属性的方式分别打印出类 属性和实例属性,要求打印的类属性为9,实例属性为5,按照类.属性的方法打印出所有属性,要求其中打印的类属性值为6
class students:
age1=None
def __init__(self,age2):
self.age2=age2
std=students("5")
std.age1="9"
print(std.age1)
print(std.age2)
创建一个类,要求内部包含一个用于求和的类方法,一个用于打印求和结果的静态方法
class a:
def __init__(self,list1,list2):
self.list1=list1
self.list2=list2
@staticmethod
def add():
print(std.list2+std.list1)
std=a(2,3)
std.add()
打印杨辉三角
n = int(input())
list1 = []
for n in range(n):
row = [1]
list1.append(row)
if n==0:
for num in row:
print(num,end=" ")
print()
for m in range(n):
row.append(list1[n - 1][m - 1] + list1[n - 1][m])
row.append(1)
for num in row:
print(num,end=" ")
print()
任务:编写一个能实现双色球选号的小程序。要求:双色球选号由7个数字组成,其中有6个红球,其号码的取值范围为[1,33],一个蓝球的取值范围为[1,16],要求6个红球从小到大排列,蓝球在最后输出。其输出格式为09 12 16 20 30 33 | 03。(注意,如双色球号码为3,则必须输出03)
from random import randint, sample
import random
red = random.sample(range(1,34),6)#从10-20之间随机抽十个数,randint 产生的随机数区间是包含左右极限的
#c = sample()#从抽出来的十个数中再抽取五个数randint(1,33) for _ in range(6)
print(red)
j=5
while j>0:
i=0
while i<j:
if red[i]>red[i+1]:
c=red[i]
red[i]=red[i+1]
red[i+1]=c
i+=1
j-=1
print(red)
for new_red in red:
if new_red<10:
print('{:0>2}'.format(new_red),end=" ")
else:
print(new_red,end=" ")
print('|',end=" ")
blue=[randint(1,16) for _ in range(1)]
for new_blue in blue :
if new_blue<10:
print('{:0>2}'.format(new_blue))
else:
print(new_blue)
Python实现井字棋
现在有这样一个叫scores.txt的文件,里面有赫敏、哈利、罗恩、马尔福四个人的几次魔法作业的成绩。
但是呢,因为有些魔法作业有一定难度,教授不强制同学们必须上交,所以大家上交作业的次数并不一致。
罗恩 23 35 44
哈利 60 77 68 88 90
赫敏 97 99 89 91 95 90
马尔福 100 85 90
希望你来统计这四个学生的魔法作业的总得分,然后再写入一个txt文件。注意,这个练习的全程只能用Python。
图像增强采用pillow中的imageEnhance处理(图片自选)