# 11、将字符串类似:"k:1|k3:2|k2:9|...|kn:m", 处理成 key:value 或json格式, 比如{"k": "1", "k3": "2"} # str1 = 'k:1|k3:2|k2:9' # dict1 = {} # str2 = str1.split('|') # for i in str2: # str3 = i.split(':') # dict1.setdefault(str3[0],str3[1]) # print(str3) # print(dict1) # def func(): # str1 = 'k:1|k3:2|k2:9' # dict1 = {} # str2 = str1.split('|') # for i in str2: # str3 = i.split(':') # dict1.setdefault(str3[0], str3[1]) # # return dict1 # print(func()) # 打印结果为:{'k': '1', 'k3': '2', 'k2': '9'} # 12、分析以下数字的规律, 1 1 2 3 5 8 13 21 34用Python语言编程实现输出,斐波拉契数 # 1+1=2 1+2=3 2+3=5 3+5=8 # def func(n): # list1 = [] # for i in range(n): # if i == 0 or i == 1: # list1.append(1) # else: # list1.append(list1[i-2]+list1[i-1]) # # print(i) # print(list1) # func(9) # 打印结果为:[1, 1, 2, 3, 5, 8, 13, 21, 34] # 13、先定义一个字典来存放用户名和密码:dic = {'admin':'123456','dcs':'654321'} # 要求如下 # 1、从字典中获取用户完成登录,登陆时判断用户是否存在,存在直接登录 # 2、如果输入的用户判断不存在字典,调用注册方法,完成该用户的注册,注册成功后写入字典,定义登陆函数 # dict1 = {'admin':'123456','dcs':'654321'} # def register(): # newusername = input('请输入您需要注册的账号:') # if dict1.__contains__(newusername): # print('您注册的账号已经存在,请重新输入!!!') # register() # 函数本身调用自己,叫递归 # else: # newpassword = input('请输入您的密码:') # newpassword1 = input('请再次输入您的密码:') # if newpassword == newpassword1: # print('注册成功!!!') # dict1.setdefault(newusername,newpassword) # print(dict1) # else: # print('您两次输入的密码不一致,请重新输入!!!') # register() # def login(): # username = input('请输入您的账号:') # if not dict1.__contains__(username): # print('您输入的账号不存在,请重新输入!!!') # register() # # else: # password = input('请输入您的密码:') # if password == dict1[username]: # print('登录成功') # else: # print('您输入的密码有误,请重新输入!!!') # login() # # if __name__ == '__main__': # login() #14、用字符串aabbcdbaaabc,用你熟悉的语言实现去除ab子串 # def func(): # str1 = 'aabbcdbaaabc' # str2 = str1.replace('ab','') # print(str2) # func() # def func(): # str1 = 'aabbcdbaaabc' # for i in range(len(str1)): # str1 = str1.replace('ab','') # print(str1) # func() # 打印结果为:cdbaac # 15、水仙花数:一个三位数,其按位立方之和等于该数本身,该数称为水仙花数。求出100-1000之间的水仙花 153 # (其实水仙花数就是自幂数中的一种,自幂数:一个n位数,其按位数的n次方之和等于该数本身) #153 = i 1**3 + 5**3 + 3**3 = i ''' 个位数 a = i % 10 十位数 b = i % 100 // 10 百位 c = i // 100 ''' # def sxh(): # for i in range(100,1000): # a = i % 10 # b = i % 100 // 10 # c = i // 100 # if a**3 + b**3 + c**3 == i: # print(i) # sxh() # 16、用递归的方法求出n的阶乘?4的阶乘结果为? 递归函数:在函数的内部调用函数本身 # 0! = 1 # 1! = 1 # 2! = 1*2 # 3! = 1*2*3 # 4! = 1*2*3*4 # def func(n): # if n == 0 or n == 1: # return 1 # else: # return n * func(n-1) #递归的时候会一直循环调用自己本身,知道为0的时候结束 # print(func(6)) # 17、有如下URL地址,要求实现截取'?'后面的参数,并将参数以key value 的键值形式保存起来,并最终通过get(key)的方式取出对应的value值 # url = 'http://ip:port/extername/get_account_trade_record.json?page_size=20&page_index=1&user_id=203317&trade_type=0' # url = 'http://ip:port/extername/get_account_trade_record.json?page_size=20&page_index=1&user_id=203317&trade_type=0' # def get_valie(): # dict1 = {} # str1 = url.split('?') # del str1[0] # for i in str1: # str2 = i.split('&') # for j in str2: # str3 = j.split('=') # dict1.setdefault(str3[0],str3[1]) # # print(str3) # # print(j) # # print(str2) # # print(str1) # # print(str1) # # print(dict1) # return dict1 # # get_valie() # 打印结果为:{'page_size': '20', 'page_index': '1', 'user_id': '203317', 'trade_type': '0'} # print(get_valie()) # def login(): # username = input('请输入您的账号:') # if username == "admin": # password = input('请输入您的密码:') # if password == "123456": # return '登录成功' # # else: # return '您输入的密码错误,请重新输入!' # else: # return '您输入的账户有误,请重新输入!' # # def select_amou(): # value = login() # if value == '登录成功': # print('您的余额为¥:8888888.88') # # elif value == '您输入的密码错误,请重新输入!': # print('您输入的密码错误,请重新输入!') # # else: # print('请登录后查询余额!!!') # # if __name__ == '__main__': # select_amou() # format()函数 # str1 = '{},{}'.format('xiaoli','12','hello') # print(str1) # 打印结果为:xiaoli,12 # # str1 = '{0},{2}'.format('xiaoli','12','hello') #0,2 表示索引位 # print(str1) # 打印结果为:xiaoli,hello # # # 通过参数名格式化 # str1 = '姓名:{name} 年纪:{age}'.format(name = '小李',age= 18) # print(str1) # 打印结果为:姓名:小李 年纪:18 # # list1 = ['xiaoli',89] # list2 = ['class',1833] # str1 = '姓名:{0[0]} 年龄:{1[0]}'.format(list1,list2) # print(str1) # 打印结果为:姓名:xiaoli 年龄:class # # # dict1 = {'name':'xiaoli','age':19,'class':1833} # str1 = '姓名:{name} 年龄:{age}'.format(**dict1) # print(str1) # 打印结果为:姓名:xiaoli 年龄:19 # python中的zip() # list1 = ['name','age','sex'] # list2 = ['xiaoli',12,'男'] # list3 = zip(list1,list2) # list4 = list(list3) # list5 = dict(list4) # print(list3) # 打印结果为:<zip object at 0x00000263A1586F08> # print(list4) # 打印结果为:[('name', 'xiaoli'), ('age', 12), ('sex', '男')] # print(type(list4)) # 打印结果为:<class 'list'> # print(list5) # 打印结果为”:{'name': 'xiaoli', 'age': 12, 'sex': '男'} # print(type(list5)) # 打印结果为:<class 'dict'> # # # python中的open()函数 # # 路径前面加一个r表示不转义 # # r表示:read 表示读 # a = open(r'D:\PycharmProjects\untitled\dcs23\lesson\lesson.txt','r',encoding='utf-8') # all = a.read() # 表示显示文件的所有内容 并且以字符串类型展示: <class 'str'> # all = a.readline() # 表示显示文件的第一行内容 并且以字符串类型展示: <class 'str'> # all = a.readlines() # 表示显示文件中的所有内容 并且是以列表的类型展示: <class 'list'> # print(all) # print(type(all)) # # # w :表示写入的意思,write # a = open(r'D:\PycharmProjects\untitled\dcs23\lesson\lesson.txt','w',encoding='utf-8') # a.write(123) # 打印结果为:TypeError: write() argument must be str, not int 表示字符串中不能添加整型 # a.write('1223') # 在写入的时候,会覆盖之前的内容 # a.writelines('duoceshi1') # w表示写入,写入的数据会被覆盖 # a.close() # 写入完成之后需要手动关闭 # # # a 表示追加 # a = open(r'D:\PycharmProjects\untitled\dcs23\lesson\lesson.txt','a',encoding='utf-8') # a.write('123344\n') # a.close() # # # with open() 对比open的时候会有一个自动关闭 # with open(r'D:\PycharmProjects\untitled\dcs23\lesson\lesson.txt','r',encoding='utf-8') as f: # # a = f.read() # a = f.readlines() # print(a)
python中一些基础的函数和循环例题
最新推荐文章于 2024-06-18 15:07:16 发布