一、类与对象和构造函数
class Point:
def __init__(self,x,y):
self.x = x
self.y = y
def move(self):
print("move")
def draw(self):
print("draw")
# 创建一个对象
point1 = Point(10,20)
print(point1.x) #10
二、继承
class Mammal:
def walk(self):
print("walk")
class Dog(Mammal):
def bark(self):
print("bark")
class Cat(Mammal):
pass
dog = Dog()
dog.bark()
cat = Cat()
cat.walk()
三、模块
import 模块名
from 模块名 import 函数名
四、包
1.随机包random
import random
#随机打印一个人的名字
members = [\'John\',\'Mary\',\'Bob\',\'Mosh\']
leader = random.choice(members)
print(leader)
#打印三个10-30之间的正整数
for i in range(3):
print(random.randint(10,30))
#掷骰子
class Dice:
def roll(self):
first = random.randint(1,6)
second = random.randint(1,6)
return (first,second)
dice = Dice()
print(dice.roll())
2.路径 Path
from pathlib import Path
#造对象
path = Path("app")
#打印是否有app这个文件夹
print(path.exists())
#创建app文件夹
print(path.mkdir())
#遍历索引xls格式的文件
path1 = Path()
for file in path1.glob(\'*.xls\'):
print(file)
五、包的下载和安装
#安装处理表格的 openpyxl包
pip install openpyxl