一:Python基础
import os;
########字符串##########
print("Hello,Python")
str = "this is a str"
str1 = 'this is a str1'
str2 = """this is a str2"""
print(str)
print(str1)
print(str, str1, str2)
print("-------------------------------------------------------------" * 2)
########列表List##########
list = ['runoob', 786, 2.23, 'john', 70.2]
tinylist = [123, 'john']
print(list, tinylist)
print(list[0])
print(list[1:])
print(list[1:3])
print(tinylist * 5)
print("-------------------------------------------------------------" * 2)
########元组##########
tuple = ('runoob', 786, 2.23, 'john', 70.2) # 只读列表, 不可二次赋值
list = ['runoob', 786, 2.23, 'john', 70.2]
print(list)
# tuple[2] = 1000 # 元组中是非法应用
list[2] = 1000 # 列表中是合法应用
print(list)
print("-------------------------------------------------------------" * 2)
########字典##########
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'jasun', 'code': 6734, 'dept': 'sales'} # 类似json格式
print(dict)
print(dict['one'])
print(dict[2])
print(tinydict.keys(), tinydict.values(), tinydict.get("name"))
print("-------------------------------------------------------------" * 2)
########条件语句##########
tag = False
name = "jasun"
if name == "ljx":
tag = True
print(tag, name)
else:
print(tag, name)
var = 100
if var == 99:
print("var is 99")
elif var != 100:
print("var is not 100")
else:
print("var is 100")
var1 = 5
if var1 > 5 and var1 < 2:
print("this is not exits ")
else:
print("var1 values is ", var1)
print("-------------------------------------------------------------" * 2)
########Python for 循环语句##########
for letter in 'jasun': # 第一个实例
if letter == "u":
break # 跳出当前循环
print(letter)
fruits = ['banana', 45, 3.21]
for fruit in fruits: # 第二个实例
print(fruit)
# 通过索引遍历
for index in range(len(fruits)):
print(fruits[index])
print("-------------------------------------------------------------" * 2)
########Python 函数##########
def printStr(st):
print(st)
printStr(False)
printStr("jasun")
printStr(5)
printStr(5.123)
# age默认参数 , *more不定长参数
def printMoreStr(name, age=21, *more):
print(name)
print(age)
for m in more:
print(m)
return
printMoreStr(name="jasun", age=1111)
printMoreStr(1717, 12, 12, 12, 12, )
# 匿名函数
sumLa = lambda la1, la2: la1 + la2
print(sumLa(3, 4))
print("-------------------------------------------------------------" * 2)
########文件I / O##########
# inp = input("请输入...\n")
# print(inp)
# 打开关闭文件
file = open("D:\ip.txt")
print(file.name)
print(file.readlines(4))
file.close()
# import os
os.rmdir("d:\os")
os.mkdir("d:\os")
# 异常处理
try:
fh = open("d:\ip.txt", "w")
fh.write("这是一个测试文件,用于测试异常!!")
except IOError:
print("IOError")
else:
print("文件写入成功")
fh.close()
finally:
print("finally语句块被执行")
print("-------------------------------------------------------------" * 2)
二:Python面向对象
########类的继承##########
class Parent:
parentAttr = 100
def __init__(self):
print("调用父类构造方法")
def parentMethod(self):
print("调用父类的method")
def setAttr(self, attr):
Parent.parentAttr = attr
def getAttr(self):
print("获取parentAttr :", Parent.parentAttr)
def printStr(self):
print("基类的printStr")
def testParentExtends(self):
print("测试python继承多个类testParentExtends")
class Child(Parent):
def __init__(self):
print("调用子类的的构造")
def childMethod(self):
print("调用子类的method")
def printStr(self):
print("方法被重写了, 子类的printStr")
def testChildExtends(self):
print("测试python继承多个类testChildExtends")
class Other(Child, Parent):
def __init__(self):
print("other 构造方法")
print("-------------------------------------------------------------" * 2)
if __name__ == '__main__':
# 调用父类的构造方法
parent = Parent()
child = Child()
parent.parentMethod()
# 子类可以调用父类的方法
child.parentMethod()
child.setAttr(999)
parent.getAttr()
child.getAttr()
# 方法重写
child.printStr()
# 测试python继承多了类
other = Other()
other.testParentExtends()
other.testChildExtends()
3:Python Gui
from tkinter import *
root = Tk()
root.title("Python")
root.geometry("400x300")
def click(event):
Message(root, text="点你咋的").pack()
text = Text(width=30, height=3)
btn = Button(root, text="你点我下试试")
btn.bind("
", click)
text.pack()
btn.pack()
root.mainloop()