*.py
文件就是一个模块;[Python提供的]
以及第三方模块以及自定义模块;sys
:主要是用户获取命令行参数的列表import sys
print(sys.argv)
for i in sys.argv:
print(i)
name = sys.argv[1]
age = sys.argv[2]
hoby = sys.argv[3]
print(name, age, hoby)
import sys
print(sys.argv)
for i in sys.argv:
print(i)
name = sys.argv[1]
age = sys.argv[2]
hoby = sys.argv[3]
print(name, age, hoby)
print(sys.path)
import module name [1,module name2, module name1]
import module name //模块名和文件名在同一个目录,否则就需要放在指定的目录里面;
//导入的一种方式
import hello
hello.sayhello() //模块名.方法[函数/变量]
//另一种引入的方式 引入模块的一种方法 从模块中导入某个指定的方法
from hello import tomorrow[,name1, name2]
tomorrow() //这里不需要加函数名
from ... import ...
这种导入方式是存在弊端的,如果在当前定义和模块里面的同名函数,就会导致模块里面的函数被覆盖from hello import tomorrow
def tomorrow():
print("tomorrow is a good day")
tomorrow()
import hello
def tomorrow():
print("tomorrow is a good day")
hello.tomorrow()
tomorrow()
//最后一种导入方式 用于将一个模块中所有的方法变量都进行导入
from hello import *
和第二种一样存在同样的弊端,不建议使用这种方式
关于__name__
属性:模块表示的是一个可执行的*.py
,如果不希望模块在被引入的过程中被执行,可以用__name__
属性,来使程序仅仅调用模块中的一部分;
每一个模块都存在一个__name__
属性,当其值为__main__
,表示执行模块本身
def man():
pass
if __name__ == "__main__":
man()
else:
def sayhello():
print("hello python")
def today():
print("today is new day")
def tomorrow():
print("hello")
如果模块当做可执行文件执行的时候,__name__
的值,就是__main__
,如果当做模块不希望执行的时候,__name__
的值是不确定的
输出指定年月的日历
import calendar
print(calendar.month(2018, 6))
June 2018
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
print(calendar.calendar(2018))
2018
January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7 1 2 3 4 1 2 3 4
8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 11
15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 18
22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 25
29 30 31 26 27 28 26 27 28 29 30 31
April May June
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 1 2 3 4 5 6 1 2 3
2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10
9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 17
16 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 24
23 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 30
30
July August September
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 1 2 3 4 5 1 2
2 3 4 5 6 7 8 6 7 8 9 10 11 12 3 4 5 6 7 8 9
9 10 11 12 13 14 15 13 14 15 16 17 18 19 10 11 12 13 14 15 16
16 17 18 19 20 21 22 20 21 22 23 24 25 26 17 18 19 20 21 22 23
23 24 25 26 27 28 29 27 28 29 30 31 24 25 26 27 28 29 30
30 31
October November December
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7 1 2 3 4 1 2
8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9
15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16
22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23
29 30 31 26 27 28 29 30 24 25 26 27 28 29 30
31
print(calendar.isleap(2000))
True
weekday
的第一天以及这个月的天数print(calendar.month(2018, 6))
print(calendar.monthrange(2018,6))
June 2018
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
(4, 30)
print(calendar.month(2018, 6))
print(calendar.monthcalendar(2018,6))
June 2018
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
[[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 0]]
import os
import collections
def formatFile(path):
recvPath = "/root/temp/mailfile"
with open(path,"r") as file:
while True:
lineinfo = file.readline()
#print(lineinfo)
if len(lineinfo) < 5:
break
mailStr = lineinfo.split("----")[0]
print(mailStr)
fileType = mailStr.split("@")[1].split(".")[0]
dirStr = os.path.join(recvPath, fileType)
if not os.path.exists(dirStr):
os.mkdir(dirStr)
filePath = os.path.join(dirStr,fileType + ".txt")
with open(filePath, "a") as filew:
filew.write(mailStr + "\n")
def mailprocess(path):
stack = []
stack.append(path)
while len(stack) != 0:
dirPath = stack.pop()
fileList = os.listdir(dirPath)
for fileName in fileList:
filePath = os.path.join(dirPath, fileName)
if os.path.isdir(filePath):
stack.append(filePath)
else:
formatFile(filePath)
path = "/root/temp/mail"
mailprocess(path)
pip
是否安装
pip -v
from PIL import Image
picturepath = "/root/PycharmProjects/test/123.jpg"
im = Image.open(picturepath)
print(im.format, im.size, im.mode)
im.thumbnail((640, 400))
im.save("new.jpg", "JPEG")
面向过程
自上而下执行,逐步求精
程序结构通常是若干个模块,各个模块之间的关系尽量简单,功能上相对独立;
每一个模块内部都是由顺序,选择和循环三种基本结构;
模块的具体使用方法是使用子程序;
程序流程在写程序时,就已经决定;
面向对象
将数据和对于数据操作的方法放在一起,作为一个相互依赖的整体,成为对象
将同类对象抽取出共性,形成类;
对于类中的大多数数据,只能够使用本类的方法进行处理;
类通过一个简单的外部接口与外界发生关系,对象和对象通过消息进行通信;
程序的流程在用户的使用过程中决定;
面向过程和面向对象
面向过程强调的是功能行为以及解决问题需要的步骤;
面向对象:将功能封装成对象,强调了具备了那些功能的对象,关注与解决问题需要那些对象;
面向对象是基于面向过程的;
面向对象的特点:
类和对象的关系:
类的定义
定义类其实就是在定义类中成员的成员变量和成员方法;
类的设计:类名,属性,行为;
object
:表示基类,超类,表示的是所有类的父类,一般没有合适的父类就使用object
;
class Person(object): #表示基类,超类,表示的是所有类的父类,一般没有合适的父类就使用`object`;
# 定义属性,本质上就是定义变量
name = ""
age = 0
height = 0
weight = 0 //这里定义的就是默认的值,如果创建对象不赋值,使用这里
# 定义方法(定义函数):方法的参数必须是以self当做第一个参数,self表示的含义就是类的实例
def run(self):
print("run")
def eat(self, food):
print("eat" + food)
格式 对象名 = 类名(参数列表) 没有参数时,()不能够省略
xixi = Person()
print(xixi)
<__main__.Person object at 0x7fdaf1613780> //返回值得到的是一个内存地址
xixi = Person()
print(xixi)
print(type(xixi))
print(id(xixi))
js = Person()
print(js)
print(type(js))
print(id(js))
格式: 对象名.属性名
赋值: 对象名.属性名 =
xixi.name = "hello"
xixi.age = 16
xixi.height = 180
print(xixi.name, xixi.age, xixi.height)
格式:对象名.方法名(参数列表)
xixi.eat("apple")
__init__()
:在使用类创建对象的时候,自动调用,如果不显示的写出构造函数,默认也是存在一个默认的空的构造函数;xixi.name = "hello"
xixi.age = 16
xixi.height = 180
print(xixi.name, xixi.age, xixi.height)
xixi.eat("apple")
class Person(object):
def run(self):
print("run")
def eat(self, food):
print("eat " + food)
def __init__(self, name, age, height, weight): //属性定义
self.name = name
self.age = age
self.height = height
self.weight = weight
print("__init__")
xixi = Person("hello", 19, 180, 66)
print(xixi.name, xixi.age, xixi.height, xixi.weight)
hello
19
180
66
__init__
self
的含义:
self
代表的是类的实例,而不是类;self
就代表哪个对象;self.__class__
:代表的是类名;class Person(object):
name = ""
age = 0
height = 0
weight = 0
def run(self):
print("run")
def eat(self, food):
print("eat " + food)
def say(self):
print("my name is %s,and i am %d years old" %(self.name, self.age))
print(self.__class__)
def __init__(self, name, age, height, weight):
self.name = name
self.age = age
self.height = height
self.weight = weight
print("__init__")
xixi = Person("hello", 19, 180, 66)
#print(xixi.name, xixi.age, xixi.height, xixi.weight)
xixi.say()
js = Person("js", 20, 150, 60)
js.say()
__init__
my name is hello,and i am 19 years old
__init__
my name is js,and i am 20 years old
self
不是关键字,可以换,但是不建议换;del
删除变量时,程序自动释放;析构函数: __del__()
lass Person(object):
def run(self):
print("run")
def eat(self, food):
print("eat " + food)
def say(self):
print("my name is %s,and i am %d years old" %(self.name, self.age))
print(self.__class__)
def __init__(self, name, age, height, weight):
self.name = name
self.age = age
self.height = height
self.weight = weight
print("__init__")
def __del__(self):
print("xigou funcation")
xixi = Person("hello", 19, 180, 66)
#print(xixi.name, xixi.age, xixi.height, xixi.weight)
xixi.say()
del xixi
js = Person("js", 20, 150, 60)
js.say()
del js
__str__()
;
print
打印对象时,会自动调用,是一个描述对象的方法;__repr__()
:
__str__()
,也会使用__repr__()
def __str__(self):
return "%s %d %d %d" % (self.name, self.age, self.height, self.weight)
hello 19 180 66
xigou funcation
__属性名
,在Python
中,如果在属性前加两个__
,属性就变为private
;__name__
的变量属于特殊变量,但是不是私有变量,外部是可以直接访问的;_name_
这样的外部变量也是可以进行访问的,按照约定的规则,是不能够访问这种类型的变量的; def __init__(self, name, age, height, weight, money):
self.name = name
self.age = age
self.height = height
self.weight = weight
self.__money = money
print("__init__")
print(xixi.__money)
Traceback (most recent call last):
File "/root/PycharmProjects/test/person.py", line 27, in
print(xixi.__money)
AttributeError: 'Person' object has no attribute '__money'
xixi.__money = 10
print(xixi.__money)
def run(self):
print("run")
print(self.__money)
def __init__(self, name, age, height, weight, money):
self.name = name
self.age = age
self.height = height
self.weight = weight
self.__money = money
print("__init__")
xixi.run()
def setMoney(self, money):
if money < 0 :
money = 0
self.__money = money
def getMoney(self):
return self.__money
xix._money
是因为Python
把__money
改为了_Person__money
,可以通过后面这个变量来进行访问xixi._Person__money = 30 //并且也直接修改了私有变量的值
xixi.run()
class bulletBox(object):
def __init__(self, count):
self.bulletCount = count
class gun(object):
def __init__(self, bulletBox):
self.bulletBox = bulletBox
def shoot(self):
if self.bulletBox.bulletCount == 0:
print("no bellit")
else:
self.bulletBox.bulletCount -= 1
print("left is %d" %(self.bulletBox.bulletCount))
class person(object):
def __init__(self, gun):
self.gun = gun
def fire(self):
self.gun.shoot()
def loadBullet(self, count):
self.gun.bulletBox.bulletCount += count
bulletBox = bulletBox(5)
gun = gun(bulletBox)
per = person(gun)
per.fire()
per.fire()
per.fire()
per.loadBullet(3)
per.fire()