python基础练习

#模拟ATM取钱系统,取款机里一共有10000元, # 有五个人分别去取钱,每次取钱的金额在100元到500元之间, # 要求输出他们每次取钱的金额,以及ATM机里的余额, # 当余额小于取款的金额时,退出系统。

import random
import os
import threading
import time
from threading import Thread ,Lock
lock=Lock()
meon = 5000
def a(i):
    while True:
        global meon
        time.sleep(1)
        lock.acquire()
        mon =random.randint(100,500)
        if mon <=meon:
            meon -= mon
            print("%s取了%d,剩余%d"%(i,mon,meon))
        else:
            print("余额不足")
            os._exit(0)
        lock.release()
if __name__ == '__main__':
    list=["张三","李四","王五","赵六","葛七"]
    for i in list:
        t = threading.Thread(target=a,args=(i,) )
        t.start()
from functools import reduce
def fun(i):
    return reduce(lambda x,y:x+y,i)
b =fun(eval(input("输入列表:") ) )
print(b)

冒泡排序:

def li_stor(aliter):
    n = len(aliter)
    for i in range(n-1):
        for j in range(n-1-i):
            if aliter[j] > aliter[j+1]:
                aliter[j],aliter[j+1] = aliter[j+1],aliter[j]
if __name__ == '__main__':
    a_lit = [1,56,8,90,34,-12,45,67,87,98,100]
    li_stor(a_lit)
    print(a_lit)

选择排序:

def select_sort(alist):
    n = len(alist)
    for i in range(n-1):
        min_index = i
        for j in range(i+1,n):
            if alist[min_index] > alist[j]:
                min_index = j
        if min_index !=i:
            alist[i],alist[min_index] = alist[min_index],alist[i]
if __name__ == '__main__':
    a_list = [-2,3,45,78,90,23,35,5,7,8,80]
    select_sort(a_list)
    print(a_list)

二分查找(折半查找):

def select_sort(alist,stim):
    first = 0
    last = len(alist)-1
    while first <= last:
        mid = (first + last)//2
        if stim == alist[mid]:
            return True
        else:
            if stim > alist[mid]:
                first = mid + 1
            else:
                last = mid -1
    return False
if __name__ == '__main__':
    a_list = [1,2,3,45,89,1111,2222,33333,444444,55555555]
    print(select_sort(a_list,3))

随机练习

# # 青蛙跳台阶
# def fun(n):
#     if n == 0:
#         return 0
#     elif n == 1:
#         return 1
#     else:
#         return fun(n-1) + fun(n-2)
#
# for i in range(100):
#     print(fun(i))
# # 等边三角形
# for i in range(1, 6):
#     for j in range(1, 6 - i):
#         print(" ", end="")
#     print("* " * i)
# # 倒等边三角形
# num = 5
# for i in range(0, num):
#     print(' ' * i, end='')
#     print('* ' * (num - i))
#
# # 正三角
# for i in range(6):
#     print("*" * i)

# # 正三角
# i = 1
# while i <= 5:
#     j = 1
#     while j <= i:
#         print("*",end="")
#         j += 1
#     print()
#     i += 1
#
# # 倒三角
# i = 5
# while i >= 1:
#     j = 1
#     while j <= i:
#         print("*", end="")
#         j += 1
#     print()
#     i -= 1
#
#
# # 九九乘法表
# i = 1
# while i <= 9:
#      j = 1
#      while j <= i:
#          print("%d*%d=%-2d" %(i , j ,i*j),end="  ")
#          j += 1
#      print()
#      i += 1
#
# # 跳出循环
# for i in range(0,10):
#     print('----')
#     if i == 6:
#         continue
#     else:
#         if i == 8:
#             break
#         print(i)


# i = 1
# sum = 0
# while i <= 100:
#     sum += i
#     i += 1
# print("1--100之间所有整数的和为:%d"%sum)
#
# i = 1
# sum = 0
# while i <=100:
#     if i % 2 == 0:
#         sum += i
#     i += 1
#     print('1-100之间的偶数合为:%d'%sum)
#
# i = 1
# sum = 0
# while i <=100:
#     if i % 2 == 1:
#         sum += i
#     i += 1
#     print('1-100之间奇数的合为:%d'%sum)

# # 第一题
# def fun1(set1):
#     a = []
#     b = []
#     for i in set1:
#         if i < 66:
#             a.append(i)
#         else:
#             b.append(i)
#     dic = {'k1':a,'k2':b}
#
#     print(dic)
# list1 = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
# fun1(list1)

# # 第二题
# li = ['手机','电脑','鼠标垫','游艇']
# goods = {}
# a = str(input('请输入商品:'))
# li.append(a)
# print(li)
# for i in li :
#     goodsid = str(li.index(i)+1)
#     print('{0}{1}'.format(goodsid,i))
#     goods[goodsid] = i
# b = int(input('请输入:'))
# c = li[b-1]
# print(c)
# 第三题

# # 第四题
# l1 = [11,22,33]
# l2 = [22,33,44]
#
#
# c=[i for i in l1 if i in l2]
# d=[y for y in (l1+l2) if y not in c]
# print(c)
# print(d)
# l1 = set(l1)
# print(l1)
# l2 = set(l2)
# print(l2)
#
# print(l1.intersection(l2))
# print(l1.difference(l2))
# print(l2.difference(l1))
#
# # 第五题
# s = 'alex'
# s1 = s.split(' ')
# dic = list(s1)
# print(dic)
#
# s = 'alex'
# s1 = s.split(' ')
# dic = tuple(s1)
# print(dic)
#
# li = ["alex","seven"]
# dic = tuple(li)
# print(dic)
#
# tu = ("Alex","seven")
# dic = list(tu)
# print(dic)


# 闭包函数

"""
def outer():
    a = 100

    def inner():
        b = a + 1
        return b

    return inner


print(outer()())

 

上一篇:python语言名片管理系统,进行增删改查


下一篇:Python数据结构(四):Python各排序算法简易理解