5.23
set() #创建集合,集合用{}表示
>>> A = set("123124sdfsdf")
>>> A
{'d', '1', 's', 'f', '2', '4', '3'}
try:
while True:
print(A.pop(), end="")
except:
pass
p123y
>>> A
set()
ls = ["p","p", "y", "y", 123]
s =set(ls) #利用了集合不重复的特点
{"p", "y", 123}
lt = list(s) #将集合转换为列表
["p", "y", 123]
>>> ls = ["python", 123, ".io"]
>>> ls[::-1]
[".io", 123, "python"]
>>> ls = ["cat", “dog”, "tiger", 1024]
>>> lt = tuple(ls)
>>> lt
('cat', 'dog', 'tiger', '1024')
def getNum():
nums = []
iNumStr = input("输入数字(回车退出):")
while iNumStr != "":
nums.append(eval(iNumStr))
iNumStr = input("输入数字(回车退出):")
return nums
def mean(numbers):
s = 0.0
for num in numbers:
s= s + num
return s / len(numbers)
def dev(numbers, mean):
sdev = 0.0
for num in numbers:
sdev = sdev + (num - mean)**2
return pow(sdev/(len(numbers)-1),0.5)
def median(numbers):
sorted(numbers)
size = len(numbers)
if size % 2 == 0:
med = (numbers[size//2-1] + numbers[size//2])/2
else:
med = numbers[size//2]
return med
n = getNum()
m = mean(n)
print("平均值{:.2f},方差{:.2f},中位数{:.2f}".format(m, dev(n, m),median(n)))