python内置函数代码练习
Posted jiangzuofenghua
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python内置函数代码练习相关的知识,希望对你有一定的参考价值。
练习1:
# 输入两个数,比较大小后,从小到大升序打印 a = input(‘fisrt:‘) b = input(‘secend:‘) if a > b: print(b,a) else: print(a,b)
练习2:
#九九乘法表 for i in range(1,10): for j in range(1,i+1): print(str(j)+‘*‘+str(i)+"="+str(i*j),end=‘ ‘) print()
:
练习3:
#打印倒着的九九乘法表 for i in range(1,10): print(‘ ‘*7*(i-1), end=‘‘) for j in range(i,10): product = i*j if product<10: end = ‘ ‘ else: end = ‘ ‘ print(str(i)+‘*‘+str(j)+‘=‘+str(i*j),end=end) print()
练习4:
#依次接手用户输入的3个数,排序后打印 #方法1:转换成int后判断大小排序 nums = [] for i in range(3): nums.append(int(input(‘number:‘.format(i)))) if nums[0] > nums[1]: if nums[0] > nums[2]: i3 = nums[0] if nums[1] > nums[2]: i2 = nums[1] i1 = nums[2] else: i2 = nums[2] i1 = nums[1] else: i2 = nums[0] i3 = nums[2] i1 = nums[1] else: # nums[0] < nums[1] if nums[0] > nums[2]: i3 = nums[1] i2 = nums[0] i1 = nums[2] else: if nums[1] < nums[2]: #1<2 i1 = nums[0] i2 = nums[1] i3 = nums[2] else: #1>2 i1 = nums[0] i2 = nums[2] i3 = nums[1] print(i1,i2,i3)
练习5:
#依次接手用户输入的3个数,排序后打印 #方法2:max min 方法实现 nums = [] out = None for i in range(3): nums.append(int(input(‘number:‘.format(i)))) while True: cur = min(nums) print(cur) nums.remove(cur) if len(nums) == 1: print(nums[0]) break # sort方法实现 nums = [] out = None for i in range(3): nums.append(int(input(‘number:‘.format(i)))) nums.sort() print(nums)
练习6:
# 冒泡法实现 num_list = [ [1,9,8,5,6,7,4,3,2], [1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,9,8] ] nums = num_list[2] print(nums) length = len(nums) count_swap = 0 count = 0 for i in range(length): flag = False for j in range(length-i-1): count += 1 if nums[j] > nums[j+1]: tmp = nums[j] nums[j] = nums[j+1] nums[j+1] = tmp flag = True count_swap += 1 if not flag: break print(nums,count_swap,count)
练习7:
# 用户输入一个数字 ‘‘‘ 1、判断是几位数,去掉前面空格和前面的0 2、打印每一位数字及其重复的次数 3、依次打印每一位数字,顺序个十百千万 ‘‘‘ num = ‘‘ while True: num = input(‘Input a positive number >>>‘).strip().lstrip(‘0‘) # strip删除前面空格换行0等 if num.isdigit(): #判断是否全部数字 break print("The length of is .".format(num, len(num))) #倒序打印 for i in range(len(num),0,-1): print(num[i-1],end=‘‘) print() #判断0-9的数字在字符串中出现的次数,每一次迭代都是用count,都是O(n)问题 counter = [0]*10 for i in range(10): counter[i] = num.count(str(i)) # 统计num字符串中数字i的个数 if counter[i]: print("The count of is ".format(i, counter[i])) # 迭代字符串本身的字符 counter = [0]*10 for x in num: i = int(x) counter[i] += 1 for i in range(len(counter)): if counter[i]: print("The count of is ".format(i, counter[i]))
练习8:
#输入5个数字,打印每个数字的位数,将这些数字排序打印,要求升序打印 nums = [] while len(nums) < 5: num = input(‘Input a positive number >>>‘).strip().lstrip(‘0‘) if not num.isdigit(): continue print(‘The length of is ‘.format(num, len(num))) nums.append(int(num)) print(nums) # 冒泡法排序 for i in range(len(nums)): flag = False for j in range(len(nums)-i-1): if nums[j] > nums[j+1]: tmp = nums[j] nums[j] = nums[j+1] nums[j+1] = tmp flag = True if not flag: break print(nums)
练习9:
# 随机产生10个数字 ‘‘‘ 要求每个数字取值范围【1,20】 统计重复的数字有几个,分别是什么 统计不重复的数字有几个,分别是什么 ‘‘‘ import random nums = [] for _ in range(10): nums.append(random.randrange(21)) print("Origin numbers = ".format(nums)) print() length = len(nums) samenums = [] # 记录相同的数字 diffnums = [] # 记录不同的数字 states = [0]*length # 记录不同的索引异同状态 print(states) for i in range(length): flag = False # 假定没有重复 if states[i] == 1: continue for j in range(i+1,length): if states[j] == 1: continue if nums[i] == nums[j]: flag = True states[j] = 1 if flag: samenums.append(nums[i]) states[i] = 1 else: diffnums.append(nums[i]) print("Same numbers = 1, Counter=0".format(len(samenums), samenums)) print("Diff numbers = 1, Counter=0".format(len(diffnums), diffnums))
以上是关于python内置函数代码练习的主要内容,如果未能解决你的问题,请参考以下文章