个人昵称:lxw-pro
个人主页:欢迎关注 我的主页
个人感悟: “失败乃成功之母”,这是不变的道理,在失败中总结,在失败中成长,才能成为IT界的一代宗师。
Python 类中包含一个特殊的变量self,它表示当前对象自身,可以访问类的成员
init()方法的作用是初始化对象
静态方法的装饰器为static method
在类中,实例方法可以访问类属性
在类中,类方法可以访问类成员
对象描述的是现实的个体,它是类的实例
# 简单类创建练习
class Maleguests:
sex = "M"
marriage = "False"
def hello(self):
print("各位大佬好")
zhangsan = Maleguests()
print(zhangsan.sex)
print(zhangsan.marriage)
zhangsan.hello()
# 简单介绍
class Maleguests:
def __init__(self, name, age, city):
print("各位大佬好")
self.name = name
self.age = age
self.city = city
def js(self):
print(f"我叫{self.name}")
print(f"我今年{self.age}岁")
print(f"我来自{self.city}")
lxw = Maleguests('Lxw', 21, '贵州')
lxw.js()
pro = Maleguests('Pro', 1, '贵州')
pro.js()
# 学生的多重身份
class Stu():
stu_no = '001'
def study(self):
print("使用python编程")
class Chi():
sta = "孩子"
def care(self):
print("照顾父母")
class Son(Stu, Chi):
pass
lxw = Son()
print(lxw.stu_no)
print(lxw.sta)
lxw.study()
lxw.care()
# 中西兼备的厨师
class master():
sta = "厨师"
def cake(self):
print("制作手抓饼")
def cook(self):
print("制作传统煎饼果子")
class app(master):
def cook(self):
print("制作中西方口味融合的煎饼果子")
print("师傅")
shifu = master()
print(shifu.sta)
shifu.cake()
shifu.cook()
print("徒弟")
tudi = app()
print(tudi.sta)
tudi.cake()
tudi.cook()
————————————————————————————————————————————
问题描述
创建父类Pet
,在父类中,使用构造方法定义name
、age
、varietie
,color
四个实例属性,定义两个方法eat
和bark
,重写__str__()
方法,使得打印对象时将对象的属性全部打印输出。
要求
创建子类Dog
类和子类Cat
类,在两个子类中要求对方法eat
和方法bark
进行重写,在子类Cat
中,重写构造方法,增加实例属性sex
,重写__str__()
方法,要求打印输出时在父类的基础上增加sex
属性。创建宠物店类PetShop,创建实例属性shopName
和petList
,属性petList
的值为宠物列表,定义方法showPets
,要求该方法打印宠物店名称和宠物列表。
通过Dog
类、Cat
类和PetShop
类创建对象,然后对三个类中的方法进行调用,查看运行结果。
class Pet:
def __init__(self, name, age, variety, color):
self.name = name
self.age = age
self.variety = variety
self.color = color
def cat(self):
print("宠物要吃东西")
def bark(self):
print("宠物会叫唤")
def __str__(self):
return f"名字: {self.name}, 年龄: {self.age}, 品种: {self.variety}, 颜色:{self.color}"
class Dog(Pet):
def eat(self):
print(self.name+"吃骨头")
def bark(self):
print(self.name+"汪汪叫")
def guardHose(self):
print(self.name+"看家护院")
class Cat(Pet):
def __init__(self, name, age, variety, color, sex):
super(Cat, self).__init__(name, age, variety, color)
self.sex = sex
def eat(self):
print(self.name+"喜欢吃鱼")
def bark(self):
print(self.name+"会喵喵叫")
def catchMice(self):
print(self.name+"会抓老鼠")
def __str__(self):
str = super(Cat, self).__str__()
str += ',性别:{0}'.format(self.sex)
return str
class PetShop:
def __init__(self, store_name, *pet_list):
self.store_name = store_name
self.pet_list = pet_list
def showPets(self):
if len(self.pet_list) == 0:
print(self.store_name+"暂无宠物")
return
print(f"{self.store_name}有{len(self.pet_list)}个宠物,它们分别是:")
for pet in self.pet_list:
print(pet)
dog1 = Dog('旺财', 3, '金毛', '黄色')
dog1.cat()
dog1.bark()
dog1.guardHose()
print("-------------------------")
cat1 = Cat('嘟嘟', 2, '咖啡', '灰色', '男')
cat1.eat()
cat1.bark()
cat1.catchMice()
print("---------------------------")
dog2 = Dog('黑贝', 3, '牧羊犬', '黑色')
cat2 = Cat('糖果', 3, '布偶', '白色', '女')
petshop = PetShop('乐派宠物店', dog1, dog2, cat1, cat2)
petshop.showPets()
class A:
name = "AA"
def Aprint(self):
print("A类的方法")
class B(A):
def Aprint(self, X):
print("B类的方法:" + X)
class C(B):
def Aprint(self,name,age):
print("My name is ", name, "I am ", age, "years old!")
c1 = C()
c1.Aprint("lxw_pro", 21) # 调用父类的方法
————————————————————————————————————————————
问题描述
设计一个宠物口粮花费计算系统(按月计算),其中包括父类Pet,子类Dog、Cat、Pig,提供宠物信息,计算出一个月内该宠物需要多少口粮。其中每只狗狗一天能够吃掉狗粮1KG,狗粮的价格为4元/KG;每只小猫每天能够吃掉0.25KG,猫粮价格为5元/KG,每只宠物猪每天能够吃掉1.5KG猪粮,猪粮的价格为3元/KG。
import abc
class Pet(metaclass=abc.ABCMeta):
def __init__(self, name, age):
self.name = name
self.age = age
@abc.abstractmethod
def mealPay(self):
pass
class Dog(Pet):
def mealPay(self, name):
print(f"我是一只宠物狗,我的名字叫{name}")
money = 1*4*30
print(f"我每个月的生活费需要{money}元")
class Cat(Pet):
def mealPay(self, name):
print(f"我是一只宠物猫,我的名字叫{name}")
money = 0.25*5*30
print(f"我每个月的生活费需要{money}元")
class Pig(Pet):
def mealPay(self, name):
print(f"我是一只宠物猪猪,我的名字叫{name}")
money = 1.5*3*30
print(f"我每个月的生活费需要{money}元")
while True:
print("宠物月花费计算系统:\n1, 狗狗\n2, 猫猫\n3, 猪猪")
choice = input("请输入您的选择:(输入0可退出程序)")
if choice == '0':
print("已退出程序!")
break
if choice == '1':
d1 = Dog('大壮', 3)
d1.mealPay(d1.name)
elif choice == '2':
c1 = Cat('圆圆', 2)
c1.mealPay(c1.name)
elif choice == '3':
p1 = Pig('嘟嘟', 2)
p1.mealPay(p1.name)
else:
print("您的输入有误,请重新输入:")
————————————————————————————————————————————
# -*- coding = utf-8 -*-
# @Time : 2022/7/25 14:10
# @Author : lxw_pro
# @File : pandas-7 练习.py
# @Software : PyCharm
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_excel('text5.xlsx')
print(df)
Unnamed: 0 Unnamed: 0.1 project ... test_time date time
0 0 00:00:00 Python ... 2022-06-20 18:30:20 2022-06-20 18:30:20
1 1 1 Java ... 2022-06-18 19:40:20 2022-06-18 19:40:20
2 2 2 C ... 2022-06-08 13:33:20 2022-06-08 13:33:20
3 3 3 MySQL ... 2021-12-23 11:26:20 2021-12-23 11:26:20
4 4 4 Linux ... 2021-12-20 18:20:20 2021-12-20 18:20:20
5 5 5 Math ... 2022-07-20 16:30:20 2022-07-20 16:30:20
6 6 6 English ... 2022-06-23 15:30:20 2022-06-23 15:30:20
7 7 7 Python ... 2022-07-19 09:30:20 2022-07-19 09:30:20
[8 rows x 7 columns]
zws = np.median(df['popularity'])
print(f"popularity列的中位数为:{zws}")
popularity列的中位数为:143.0
df.popularity.plot(kind='hist')
plt.show()
df.popularity.plot(kind='kde', xlim=(0, 150))
plt.show()
法一
:
del df['time']
print("删除最后一列time后的表为:\n", df)
删除最后一列time后的表为:
Unnamed: 0 Unnamed: 0.1 project popularity test_time date
0 0 00:00:00 Python 95 2022-06-20 18:30:20 2022-06-20
1 1 1 Java 92 2022-06-18 19:40:20 2022-06-18
2 2 2 C 145 2022-06-08 13:33:20 2022-06-08
3 3 3 MySQL 141 2021-12-23 11:26:20 2021-12-23
4 4 4 Linux 84 2021-12-20 18:20:20 2021-12-20
5 5 5 Math 148 2022-07-20 16:30:20 2022-07-20
6 6 6 English 146 2022-06-23 15:30:20 2022-06-23
7 7 7 Python 149 2022-07-19 09:30:20 2022-07-19
法二
:
df.drop(columns=['date'], inplace=True)
print("删除列date后的表为:\n", df)
删除列date后的表为:
Unnamed: 0 Unnamed: 0.1 project popularity test_time
0 0 00:00:00 Python 95 2022-06-20 18:30:20
1 1 1 Java 92 2022-06-18 19:40:20
2 2 2 C 145 2022-06-08 13:33:20
3 3 3 MySQL 141 2021-12-23 11:26:20
4 4 4 Linux 84 2021-12-20 18:20:20
5 5 5 Math 148 2022-07-20 16:30:20
6 6 6 English 146 2022-06-23 15:30:20
7 7 7 Python 149 2022-07-19 09:30:20
df['test'] = df['project']+['test-time']
print("将df第一列与第三列合并为新的一列后的表为:\n", df)
将df第一列与第三列合并为新的一列后的表为:
Unnamed: 0 Unnamed: 0.1 ... test_time test
0 0 00:00:00 ... 2022-06-20 18:30:20 Pythontest-time
1 1 1 ... 2022-06-18 19:40:20 Javatest-time
2 2 2 ... 2022-06-08 13:33:20 Ctest-time
3 3 3 ... 2021-12-23 11:26:20 MySQLtest-time
4 4 4 ... 2021-12-20 18:20:20 Linuxtest-time
5 5 5 ... 2022-07-20 16:30:20 Mathtest-time
6 6 6 ... 2022-06-23 15:30:20 Englishtest-time
7 7 7 ... 2022-07-19 09:30:20 Pythontest-time
[8 rows x 6 columns]
df['text'] = df['project']+df['popularity'].map(str)
print("将project列于popularity列合并为新的一列后的表为:\n", df)
将project列于popularity列合并为新的一列后的表为:
Unnamed: 0 Unnamed: 0.1 ... test text
0 0 00:00:00 ... Pythontest-time Python95
1 1 1 ... Javatest-time Java92
2 2 2 ... Ctest-time C145
3 3 3 ... MySQLtest-time MySQL141
4 4 4 ... Linuxtest-time Linux84
5 5 5 ... Mathtest-time Math148
6 6 6 ... Englishtest-time English146
7 7 7 ... Pythontest-time Python149
[8 rows x 7 columns]
print(df[['popularity']].apply(lambda x: x.max() - x.min()))
popularity 65
dtype: int64
print(pd.concat([df[:1], df[-2:-1]]))
Unnamed: 0 Unnamed: 0.1 ... test text
0 0 00:00:00 ... Pythontest-time Python95
6 6 6 ... Englishtest-time English146
[2 rows x 7 columns]
df = df.append(df.loc[2])
print("将第2行数据添加至末尾的表为:\n", df)
将第2行数据添加至末尾的表为:
Unnamed: 0 Unnamed: 0.1 ... test text
0 0 00:00:00 ... Pythontest-time Python95
1 1 1 ... Javatest-time Java92
2 2 2 ... Ctest-time C145
3 3 3 ... MySQLtest-time MySQL141
4 4 4 ... Linuxtest-time Linux84
5 5 5 ... Mathtest-time Math148
6 6 6 ... Englishtest-time English146
7 7 7 ... Pythontest-time Python149
2 2 2 ... Ctest-time C145
[9 rows x 7 columns]
lsj = df.dtypes
print("查看每列的数据类型为:\n", lsj)
查看每列的数据类型为:
Unnamed: 0 int64
Unnamed: 0.1 object
project object
popularity int64
test_time datetime64[ns]
test object
text object
dtype: object
人生不能后悔,只能遗憾,因为遗憾只是在感叹错过,后悔却是否定了自己曾经的选择!!
点赞,你的认可是我创作的
动力
!
收藏,你的青睐是我努力的方向
!
评论,你的意见是我进步的财富
!
关注,你的喜欢是我长久的坚持
!
欢迎关注微信公众号【程序人生6】,一起探讨学习哦!!!