冒泡排序:
# 冒泡排序
list = [1, 9, 7, 13, 11, 99, 33, 22]
print(len(list))
def listSort(list):
# 外层循环 控制次数,
for i in range(0, len(list) -1):
# 内层循环,控制两两比较
for j in range(i + 1, len(list)):
if list[i] > list[j]: # 这里是升序,降序换成 "<"
temp = 0
temp = list[i]
list[i] = list[j]
list[j] = temp
return list
print(listSort(list))
输出结果: [1, 7, 9, 11, 13, 22, 33, 99]
def maxNum(list):
maxNumber = list[0]
for item in list:
if item > maxNumber:
maxNumber = item
return maxNumber
print("-----------------最大值", maxNum(list))
输出结果: 这个数组最大的值为: 1
def maxNum(list):
maxNum = list[0]
minNum = list[0]
for item in list:
if item > maxNum:
maxNum = item
if item < minNum:
minNum = item
#这里用字典来接受最大值、最小值
return {"minNum":minNum, "maxNum":maxNum}
print("----------数组里的最大值和最小值分别:", maxNum(list))
控制台打印结果:
----------数组里的最大值和最小值分别: {'minNum': 1, 'maxNum': 99}
# 99乘法表
def form():
for i in range(1, 10):
for j in range(1, i+1):
print("%d*%d=%2d\t"%(i, j, i*j), end = "")
print("")
form()
qearlStr = "QearlYuQearlYuYuuu"
def foundStr(qearlStr):
dic = {}
for item in qearlStr:
if item in dic.keys(): #字符是否存在这个字典keys里
dic[item] += 1 # 有则,键值+1
else:
dic[item] = 1 # 无则,插入这个字符到字典里,并设置键值为1
return dic
print(foundStr(qearlStr))
查看控制台结果,一目了然:
{'a': 2, 'u': 5, 'l': 2, 'r': 2, 'Y': 3, 'e': 2, 'Q': 2}