Python基础(二)
一、类与对象和构造函数
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
相关推荐
huavhuahua 2020-11-20
xiaoseyihe 2020-11-16
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
meylovezn 2020-11-20
逍遥友 2020-11-20