|
|
|
|
@ -0,0 +1,428 @@
|
|
|
|
|
import os
|
|
|
|
|
import random
|
|
|
|
|
import re
|
|
|
|
|
import string
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sumNumber(n):
|
|
|
|
|
sum = 0
|
|
|
|
|
for i in range(1, n+1):
|
|
|
|
|
sum += i
|
|
|
|
|
return sum
|
|
|
|
|
def listDuplicate(list):
|
|
|
|
|
list_duplicate = []
|
|
|
|
|
for i in list:
|
|
|
|
|
if i not in list_duplicate:
|
|
|
|
|
list_duplicate.append(i)
|
|
|
|
|
return list_duplicate
|
|
|
|
|
def listDuplicate2(list):
|
|
|
|
|
list1 = []
|
|
|
|
|
for i in set(list):
|
|
|
|
|
list1.append(i)
|
|
|
|
|
return list1
|
|
|
|
|
def listDuplicate3(list):
|
|
|
|
|
return [i for i in set(list)]
|
|
|
|
|
|
|
|
|
|
def fileLine(file):
|
|
|
|
|
with open(file, 'r') as f:
|
|
|
|
|
for line in f:
|
|
|
|
|
print(line)
|
|
|
|
|
def fileLine2(file):
|
|
|
|
|
with open(file, 'r') as f:
|
|
|
|
|
for line in f:
|
|
|
|
|
print(line.strip())
|
|
|
|
|
def fileLine3(file):
|
|
|
|
|
with open(file, 'r') as f:
|
|
|
|
|
for line in f:
|
|
|
|
|
print(line.strip().split())
|
|
|
|
|
def fileLine4(file):
|
|
|
|
|
with open(file, 'r') as f:
|
|
|
|
|
for line in f:
|
|
|
|
|
print(line.strip().split(','))
|
|
|
|
|
def fileLine5(file):
|
|
|
|
|
with open(file, 'r') as f:
|
|
|
|
|
for line in f:
|
|
|
|
|
print(line.strip().split(',')[0])
|
|
|
|
|
def fileLine6(file):
|
|
|
|
|
with open(file, 'r') as f:
|
|
|
|
|
for line in f:
|
|
|
|
|
print(line.strip().split(',')[1])
|
|
|
|
|
def stringUnicode():
|
|
|
|
|
s1 = "去除首尾空格"
|
|
|
|
|
s2 = u"去除首尾空格" #无前缀 & u前缀 字符串默认创建即以Unicode编码存储,可以存储中文。
|
|
|
|
|
#s3 = b"去除首尾空格" SyntaxError: bytes can only contain ASCII literal characters
|
|
|
|
|
s4 = b"hello" #b前缀 字符串存储为Ascll码,无法存储中文。
|
|
|
|
|
s5 = r"hello" #r前缀就相当于三引号,主要解决的是 转义字符,特殊字符 的问题,其中所有字符均视为普通字符。
|
|
|
|
|
print(s1)
|
|
|
|
|
print(s2)
|
|
|
|
|
print(s4)
|
|
|
|
|
print(s5)
|
|
|
|
|
def randomFunc():
|
|
|
|
|
a = random.random() #随机生成0-1之间的小数
|
|
|
|
|
b = random.randint(1, 10) #随机生成1-10之间的整数
|
|
|
|
|
c = random.randrange(1, 10, 2) #随机生成1-10之间的整数,步长为2
|
|
|
|
|
d = random.choice(['a', 'b', 'c']) #随机生成['a', 'b', 'c']中的一个
|
|
|
|
|
e = random.sample(['a', 'b', 'c'], 2) #随机生成['a', 'b', 'c']中的2个
|
|
|
|
|
f = random.choices(['a', 'b', 'c'], k=2) #随机生成['a', 'b', 'c']中的2个
|
|
|
|
|
g = random.SystemRandom().random() #随机生成系统安全的随机数
|
|
|
|
|
print(a)
|
|
|
|
|
print(b)
|
|
|
|
|
print(c)
|
|
|
|
|
print(d)
|
|
|
|
|
print(e)
|
|
|
|
|
print(f)
|
|
|
|
|
print(g)
|
|
|
|
|
def stringReverse():
|
|
|
|
|
s1 = "hello world"
|
|
|
|
|
s2 = s1[::-1] #[::-1]表示从后向前
|
|
|
|
|
print(s2)
|
|
|
|
|
s3l = list(s1)
|
|
|
|
|
lens1 = len(s1)
|
|
|
|
|
for i in range(lens1):
|
|
|
|
|
s3l[i] = s1[lens1-i-1]
|
|
|
|
|
print("".join(s3l)) #join()方法用于将序列中的元素以指定的字符连接生成一个新的字符串。
|
|
|
|
|
s4l = list(s1)
|
|
|
|
|
print("".join(s4l[::-1]))
|
|
|
|
|
s5l = list(s1)
|
|
|
|
|
s5l.reverse()
|
|
|
|
|
print("".join(s5l))
|
|
|
|
|
def listTupleDict():
|
|
|
|
|
list1 = [1, 2, 3, 4, 5]
|
|
|
|
|
tuple1 = (1, 2, 3, 4, 5)
|
|
|
|
|
set1 = {1, 2, 3, 4, 5}
|
|
|
|
|
dict1 = {'a': 1, 'b': 2, 'c': 3}
|
|
|
|
|
print(list1)
|
|
|
|
|
print(tuple1)
|
|
|
|
|
print(dict1)
|
|
|
|
|
print(set1)
|
|
|
|
|
def isNone():
|
|
|
|
|
a = None
|
|
|
|
|
if a is None:
|
|
|
|
|
print("a is None")
|
|
|
|
|
else:
|
|
|
|
|
print("a is not None")
|
|
|
|
|
def listReverse():
|
|
|
|
|
list1 = [1, 2, 3, 4, 5]
|
|
|
|
|
list1.reverse()
|
|
|
|
|
print(list1)
|
|
|
|
|
s1 = "hello world"
|
|
|
|
|
def negativeIndex():
|
|
|
|
|
list1 = [1, 2, 3, 4, 5]
|
|
|
|
|
print(list1[-1])
|
|
|
|
|
print(list1[-2])
|
|
|
|
|
print(list1[0])
|
|
|
|
|
print(list1[-0])
|
|
|
|
|
print(list1[::-1])
|
|
|
|
|
print(list1[::-1])
|
|
|
|
|
def chaosList():
|
|
|
|
|
list1 = [1, 2, 3, 4, 5]
|
|
|
|
|
random.shuffle(list1)
|
|
|
|
|
print(list1)
|
|
|
|
|
list1.sort()
|
|
|
|
|
print(list1)
|
|
|
|
|
list1.sort(reverse=True)
|
|
|
|
|
print(list1)
|
|
|
|
|
list1.reverse()
|
|
|
|
|
print(list1)
|
|
|
|
|
list1.reverse()
|
|
|
|
|
print(list1)
|
|
|
|
|
def joinSplit():
|
|
|
|
|
s1 = "hello world"
|
|
|
|
|
list1 = s1.split() #分割字符串,返回列表
|
|
|
|
|
print(list1)
|
|
|
|
|
s2 = " ".join(list1) #将列表中的元素连接成字符串
|
|
|
|
|
print(s2)
|
|
|
|
|
def stringStrip():
|
|
|
|
|
s1 = " hello world "
|
|
|
|
|
print(s1.strip()) #去除首尾空格
|
|
|
|
|
print(s1.lstrip()) #去除首部空格
|
|
|
|
|
print(s1.rstrip()) #去除尾部空格
|
|
|
|
|
s1.replace(" ", "") #替换字符串中的字符
|
|
|
|
|
print(s1)
|
|
|
|
|
def passUsage():
|
|
|
|
|
a = 1
|
|
|
|
|
if a == 1:
|
|
|
|
|
pass #在编写代码时只写框架思路,具体实现还未编写就可以用 pass 进行占位,使程序不报错,不会进行任何操作。
|
|
|
|
|
def isIn():
|
|
|
|
|
list1 = [1, 2, 3, 4, 5]
|
|
|
|
|
print(1 in list1)
|
|
|
|
|
print(6 in list1)
|
|
|
|
|
print(6 not in list1)
|
|
|
|
|
list2 = [1, 2, 3, 4, 5]
|
|
|
|
|
print(list1 == list2)
|
|
|
|
|
print(list1 is list2)
|
|
|
|
|
def tupleList():
|
|
|
|
|
tuple1 = (1, 2, 3, 4, 5)
|
|
|
|
|
list1 = list(tuple1)
|
|
|
|
|
print(list1)
|
|
|
|
|
tuple2 = tuple(list1)
|
|
|
|
|
print(tuple2)
|
|
|
|
|
list1.append(6)
|
|
|
|
|
del(list1[0])
|
|
|
|
|
print(list1)
|
|
|
|
|
del(list1[0:1])
|
|
|
|
|
print(list1)
|
|
|
|
|
list1.remove(5)
|
|
|
|
|
print(list1)
|
|
|
|
|
def getInfo():
|
|
|
|
|
x,y = 1,"hello"
|
|
|
|
|
return x,y
|
|
|
|
|
def searchMatch():
|
|
|
|
|
s1 = "hello world"
|
|
|
|
|
a = re.match(r"hello", s1)
|
|
|
|
|
a.group()
|
|
|
|
|
def fileCreate():
|
|
|
|
|
f = open("test.txt", "w")
|
|
|
|
|
f.write("hello world")
|
|
|
|
|
f.close()
|
|
|
|
|
def fileDelete():
|
|
|
|
|
os.remove("test.txt")
|
|
|
|
|
def zenOfPython():
|
|
|
|
|
print("Beautiful is better than ugly.")
|
|
|
|
|
print("Explicit is better than implicit.")
|
|
|
|
|
print("Simple is better than complex.")
|
|
|
|
|
print("Complex is better than complicated.")
|
|
|
|
|
print("Flat is better than nested.")
|
|
|
|
|
print("Sparse is better than dense.")
|
|
|
|
|
print("Readability counts.")
|
|
|
|
|
print("Special cases aren't special enough to break the rules.")
|
|
|
|
|
print("Although practicality beats purity.")
|
|
|
|
|
print("Errors should never pass silently.")
|
|
|
|
|
print("Unless explicitly silenced.")
|
|
|
|
|
print("In the face of ambiguity, refuse the temptation to guess.")
|
|
|
|
|
print("There should be one-- and preferably only one --obvious way to do it.")
|
|
|
|
|
print("Although that way may not be obvious at first unless you're Dutch.")
|
|
|
|
|
print("Now is better than never.")
|
|
|
|
|
print("Although never is often better than *right* now.")
|
|
|
|
|
print("If the implementation is hard to explain, it's a bad idea.")
|
|
|
|
|
print("If the implementation is easy to explain, it may be a good idea.")
|
|
|
|
|
print("Namespaces are one honking great idea -- let's do more of those!")
|
|
|
|
|
def stringSplicing():
|
|
|
|
|
s1 = "hello world"
|
|
|
|
|
s2 = " hahha"
|
|
|
|
|
print(s1 + s2)
|
|
|
|
|
s3 =s1,s2
|
|
|
|
|
print(s3)
|
|
|
|
|
print(s1.join(s2))
|
|
|
|
|
def pickleUsage():
|
|
|
|
|
import pickle
|
|
|
|
|
d1 = dict(name="Bob", age=20, score=88)
|
|
|
|
|
print(d1)
|
|
|
|
|
print(type(d1))
|
|
|
|
|
b1 = pickle.dumps(d1) #将 Python 中的对象序列化成二进制对象,并返回;
|
|
|
|
|
print(b1)
|
|
|
|
|
print(type(b1))
|
|
|
|
|
d2 = pickle.loads(b1) #读取给定的二进制对象数据,并将其转换为 Python 对象;
|
|
|
|
|
print(d2)
|
|
|
|
|
print(type(d2))
|
|
|
|
|
pickle.dump(d1, open("test.txt", "wb")) #将 Python 中的对象序列化成二进制对象,并写入文件;
|
|
|
|
|
d3 = pickle.load(open("test.txt", "rb")) #读取指定的序列化数据文件,并返回对象。
|
|
|
|
|
print(d3)
|
|
|
|
|
print(type(d3))
|
|
|
|
|
def dirTypeInstance():
|
|
|
|
|
# dir()
|
|
|
|
|
# 函数是
|
|
|
|
|
# Python
|
|
|
|
|
# 自省机制中最著名的部分了。它返回传递给它的任何对象的属性名称经过排序的列表。如果不指定对象,则
|
|
|
|
|
# dir()
|
|
|
|
|
# 返回当前作用域中的名称。
|
|
|
|
|
# type()
|
|
|
|
|
# 函数有助于我们确定对象是字符串还是整数,或是其它类型的对象。
|
|
|
|
|
# 对象拥有属性,并且
|
|
|
|
|
# dir()
|
|
|
|
|
# 函数会返回这些属性的列表。但是,有时我们只想测试一个或多个属性是否存在。如果对象具有我们正在考虑的属性,那么通常希望只检索该属性。这个任务可以由
|
|
|
|
|
# hasattr()
|
|
|
|
|
# 和
|
|
|
|
|
# getattr()
|
|
|
|
|
# 函数来完成。
|
|
|
|
|
# isinstance()
|
|
|
|
|
# 函数测试对象,以确定它是否是某个特定类型或定制类的实例。
|
|
|
|
|
s1 = "hello world"
|
|
|
|
|
print(dir())
|
|
|
|
|
print(dir(s1))
|
|
|
|
|
dir() #dir() 函数是 Python 自省机制中最著名的部分了。它返回传递给它的任何对象的属性名称经过排序的列表。如果不指定对象,则 dir() 返回当前作用域中的名称。
|
|
|
|
|
print(type(s1))
|
|
|
|
|
type(s1) #type() 函数返回对象的类型。
|
|
|
|
|
isinstance(s1, str) #isinstance() 函数用于判断一个对象是否是一个已知的类型。
|
|
|
|
|
print(isinstance(s1, str))
|
|
|
|
|
def dictUsage():
|
|
|
|
|
dict1 = {"name":"Bob", "age":20, "score":88}
|
|
|
|
|
print(dict1)
|
|
|
|
|
print(type(dict1))
|
|
|
|
|
print(dict1["name"])
|
|
|
|
|
print(dict1.get("name"))
|
|
|
|
|
print(dict1.get("name", "default"))
|
|
|
|
|
print(dict1.get("name1", "default"))
|
|
|
|
|
dict1.setdefault("name1", "default")
|
|
|
|
|
print(dict1)
|
|
|
|
|
dict1["name"] = "Jack"
|
|
|
|
|
print(dict1)
|
|
|
|
|
del(dict1["name"])
|
|
|
|
|
print(dict1)
|
|
|
|
|
#dict(中文叫字典)是另一种可变容器模型,且可存储任意类型对象。
|
|
|
|
|
# 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号{}中
|
|
|
|
|
# 字典的特性
|
|
|
|
|
#
|
|
|
|
|
# 查找速度快
|
|
|
|
|
# 无论dict有10个元素还是10万个元素,查找速度都一样。而list的查找速度随着元素增加而逐渐下降。
|
|
|
|
|
# 不过dict的查找速度快不是没有代价的,dict的缺点是占用内存大,还会浪费很多内容,list正好相反,占用内存小,但是查找速度慢。
|
|
|
|
|
# 字典值可以没有限制地取任何python对象,既可以是标准的对象,也可以是用户定义的,但键不行。
|
|
|
|
|
# 不允许同一个键出现两次。
|
|
|
|
|
# 键必须不可变,所以可以用数字,字符串或元组充当,所以用列表就不行。
|
|
|
|
|
# dict的第二个特点就是存储的key-value序对是没有顺序的!这和list不一样。
|
|
|
|
|
def multiThreads():
|
|
|
|
|
import threading
|
|
|
|
|
import time
|
|
|
|
|
def run(n):
|
|
|
|
|
print("task", n)
|
|
|
|
|
time.sleep(2)
|
|
|
|
|
print("task done", n)
|
|
|
|
|
def run1(n):
|
|
|
|
|
print("task1", n)
|
|
|
|
|
time.sleep(2)
|
|
|
|
|
print("task1 done", n)
|
|
|
|
|
|
|
|
|
|
for i in range(500000):
|
|
|
|
|
for p in range(16):
|
|
|
|
|
tt = threading.Thread(target=run, args=(i,))
|
|
|
|
|
tt1 = threading.Thread(target=run1, args=(i,))
|
|
|
|
|
tt.start()
|
|
|
|
|
tt1.start()
|
|
|
|
|
print("main thread")
|
|
|
|
|
time.sleep(10)
|
|
|
|
|
print("main thread done")
|
|
|
|
|
|
|
|
|
|
def triElement():
|
|
|
|
|
x = 1
|
|
|
|
|
y =2
|
|
|
|
|
#z = x>y?x:y
|
|
|
|
|
z = x if x>y else y
|
|
|
|
|
print(z)
|
|
|
|
|
def printBase():
|
|
|
|
|
print("hello world")
|
|
|
|
|
print("hello world")
|
|
|
|
|
sys.stdout.write("hello world\n")
|
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
def rangeXrange():
|
|
|
|
|
list1 = [1,2,3,4,5,6,7,8,9,10]
|
|
|
|
|
for i in range(len(list1)):
|
|
|
|
|
print(list1[i])
|
|
|
|
|
for i in list1:
|
|
|
|
|
print(i)
|
|
|
|
|
def rangex():
|
|
|
|
|
list(xrange(0,6,2))
|
|
|
|
|
def writeBigFile():
|
|
|
|
|
with open("big.txt", "w") as f:
|
|
|
|
|
for i in range(1000000000):
|
|
|
|
|
f.write("hello world\n")
|
|
|
|
|
def getFileInfo():
|
|
|
|
|
import os
|
|
|
|
|
print(os.name)
|
|
|
|
|
print(os.uname())
|
|
|
|
|
print(os.environ)
|
|
|
|
|
print(os.environ.get("path"))
|
|
|
|
|
print(os.path.abspath("."))
|
|
|
|
|
print(os.path.join("/tmp", "test"))
|
|
|
|
|
print(os.path.split("/tmp/test.txt"))
|
|
|
|
|
print(os.path.splitext("/tmp/test.txt"))
|
|
|
|
|
print(os.path.exists("/tmp/test.txt"))
|
|
|
|
|
print(os.path.isfile("/tmp/test.txt"))
|
|
|
|
|
print(os.path.isdir("/tmp/test.txt"))
|
|
|
|
|
print(os.path.getsize("/tmp/test.txt"))
|
|
|
|
|
print(os.path.getatime("/tmp/test.txt"))
|
|
|
|
|
print(os.path.getmtime("/tmp/test.txt"))
|
|
|
|
|
print(os.path.getctime("/tmp/test.txt"))
|
|
|
|
|
print(os.path.getcwd())
|
|
|
|
|
print(os.listdir("/tmp"))
|
|
|
|
|
print(os.path.exists("/tmp/test.txt"))
|
|
|
|
|
print(os.path.isfile("/tmp/test.txt"))
|
|
|
|
|
print(os.path.isdir("/tmp/test.txt"))
|
|
|
|
|
print(os.path.getsize("/tmp/test.txt"))
|
|
|
|
|
print(os.path.getatime("/tmp/test.txt"))
|
|
|
|
|
print(os.path.getmtime("/tmp/test.txt"))
|
|
|
|
|
print(os.path.getctime("/tmp/test.txt"))
|
|
|
|
|
print(os.path.getcwd())
|
|
|
|
|
print(os.listdir("/tmp"))
|
|
|
|
|
print(os.path.exists("/tmp/test.txt"))
|
|
|
|
|
print(os.path.isfile("/tmp/test.txt"))
|
|
|
|
|
print(os.path.isdir("/tmp/test.txt"))
|
|
|
|
|
print(os.path.getsize("/tmp/test.txt"))
|
|
|
|
|
def getFileSize():
|
|
|
|
|
import os
|
|
|
|
|
print(os.path.getsize("big.txt"))
|
|
|
|
|
def readBigFile():
|
|
|
|
|
with open("big.txt", "r") as f:
|
|
|
|
|
for line in f:
|
|
|
|
|
print(line)
|
|
|
|
|
def readBigFileByLine():
|
|
|
|
|
with open("big.txt", "r") as f:
|
|
|
|
|
while True:
|
|
|
|
|
line = f.readline(100)
|
|
|
|
|
if not line:
|
|
|
|
|
break
|
|
|
|
|
print(line)
|
|
|
|
|
def tryExceptFinally():
|
|
|
|
|
try:
|
|
|
|
|
print("try")
|
|
|
|
|
raise Exception("error")
|
|
|
|
|
return 2
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print("except")
|
|
|
|
|
raise e
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
print("finally")
|
|
|
|
|
return 3
|
|
|
|
|
def sayHello():
|
|
|
|
|
"""
|
|
|
|
|
这是docstring
|
|
|
|
|
"""
|
|
|
|
|
#print(sayHello.__doc__)
|
|
|
|
|
print("hello world")
|
|
|
|
|
def stringType():
|
|
|
|
|
s1 = "hello world"
|
|
|
|
|
s2 = "444444444444444444444"
|
|
|
|
|
print(s1.isalnum())
|
|
|
|
|
print(s1.isalpha())
|
|
|
|
|
print(s2.isdigit())
|
|
|
|
|
str.isalnum()
|
|
|
|
|
# 所有字符都是数字或者字母
|
|
|
|
|
# str.isalpha()
|
|
|
|
|
# 所有字符都是字母
|
|
|
|
|
# str.isdigit()
|
|
|
|
|
# 所有字符都是数字
|
|
|
|
|
# str.isspace()
|
|
|
|
|
# 所有字符都是空白字符、t、n、r
|
|
|
|
|
def concatenation():
|
|
|
|
|
list1 = [1,2,3,4,5,6,7,8,9,10]
|
|
|
|
|
list2 = [11,12,13,14,15,16,17,18,"dsdsdsdsd",20]
|
|
|
|
|
list3 = list1 + list2
|
|
|
|
|
print(list3)
|
|
|
|
|
def getBoth():
|
|
|
|
|
list1 = [1,2,3,4,5,6,7,8,9,10]
|
|
|
|
|
list2 = [11,2,13,14,15,16,17,18,"dsdsdsdsd",20]
|
|
|
|
|
for i in list1:
|
|
|
|
|
if i in list2:
|
|
|
|
|
print(i)
|
|
|
|
|
def sliceUsage():
|
|
|
|
|
list1 = [1,2,3,4,5,6,7,8,9,10]
|
|
|
|
|
print(list1[0:5])
|
|
|
|
|
#Go 中的 slice 就像是 Python 中的 list,但是 Python 中的 list 支持非常多的操作,有很丰富的内置函数去操作,但是 Go 中的 slice 只有简单 append 函数和切片功能,好吧,我们自己实现这些功能。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
sliceUsage()
|
|
|
|
|
|
|
|
|
|
#fileLine4('t0726.py')
|
|
|
|
|
#list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 9]
|
|
|
|
|
#print(listDuplicate(list1))
|
|
|
|
|
#print(listDuplicate3(list1))
|
|
|
|
|
#print(sumNumber(100))
|