2020-08-26
python学习
00
请注意,本文编写于 1502 天前,最后修改于 1496 天前,其中某些信息可能已经过时。

目录

习题1:给一个圆的半径,求圆的周长和面积
习题2:截取字符串
习题3:冒泡法排序
习题4:九九乘法表
习题5:求素数
习题6:杨辉三角
习题7:打印转置矩阵
习题8.字典扁平化
习题9:给定一个不超过5位的正整数,判断其有几位?使用input函数
习题10:打印一个正方形
习题11:求100以内的奇数和
习题12: 求5的阶乘
习题13:打印菱形
习题14:生成0000.zhuajgmncp 形式的的字符串,点号前有序,之后为无序的字符串
习题1:给一个圆的半径,求圆的周长和面积
import math #调用math函数,使用pi进行周长和面积的计算 r = int(input('圆的半径为:')) if r > 0: #判断条件可以为True C = 2 * math.pi * r S = math.pi * r * r print ("圆周长是{}".format(C) ) print ("圆面积是{}".format(S) ) else: print ('输入的半径无意义,请输入一个正数。')
圆的半径为:11 圆周长是69.11503837897544 圆面积是380.1327110843649
习题2:截取字符串
#截取'JAVA_HOME=/usr/bin' 中的 变量名和值 key,value='JAVA_HOME=/usr/bin'.split(sep='=') print(key) print(value)
JAVA_HOME /usr/bin
# 截取基名前一个名称(父目录) lst=input("请输入合法的路径:") *_,basename,_= lst.split(sep='/') print(basename)
请输入合法的路径:/etc/sysconfig/network-script/ifcfg-eth network-script
习题3:冒泡法排序
# 对列表[1,9,8,5,6,7,4,3,2]使用冒泡法进行排序,要求使用封装和解构来交换数据 lst = [1,9,8,5,6,7,4,3,2] for i in range(len(lst)): for j in range(8-i): if lst[j]>lst[j+1]: #靠前位置的值大于靠后位置的值,前位置获取小的值。 lst[j],lst[j+1]=lst[j+1],lst[j] print(lst)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
习题4:九九乘法表
##方法一: for i in range(1,10): for j in range(1,i+1): print(j,'*',i,'=',j*i,'\t ',end=' ') print()
1 * 1 = 1 1 * 2 = 2 2 * 2 = 4 1 * 3 = 3 2 * 3 = 6 3 * 3 = 9 1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16 1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25 1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36 1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49 1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64 1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
##方法二: for i in range(1,10): #i的值是[1,10) i=1 ; i=2 line = ' ' #定义一个空的字符串 for j in range(1,i+1): #[1,i+1) j=1 ; j=1 j=2 line += '{0}*{1}={2} '.format(j,i,i*j) #jxi = j*i =1 x1 =1 ; 1x2=2 2x2=4 print(line)
1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

打印倒置的九九乘法表

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()
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 6*6=36 6*7=42 6*8=48 6*9=54 7*7=49 7*8=56 7*9=63 8*8=64 8*9=72 9*9=81
习题5:求素数
## 方法一: #求100内的素数 #思考: #1,什么是素数? 又称为质素,是在大于1的自然数中,除了1和它本身以外不再有其他因数。 #对正整数n,如果用2到根号n的之间的所有整数去除,均无法整除,则n为质数。 import math p1=[2,3,5,7] n = 100 for x in range(10,n): for i in range(2,math.ceil(math.sqrt(x))): if x % i == 0: break else: p1.append(x) print(p1)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 37, 41, 43, 47, 49, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
## 方法二: #合数一定可以分解成几个质素的乘积 import math n = 100 p = [] for x in range(2,n): for i in p: if x % i == 0: break else: print(x,end=' ') p.append(x)
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
## 方法三: # 带flag判断,提高程序效率。 import math p = [] flag = False for x in range(2,100): #x=13 for i in p: #p=[2,3,5,7] if x % i ==0: # x%i != 0 flag = True break if i >= math.ceil(math.sqrt(x)): #i< math.ceil(math.sqrt(x)) flag = False break if not flag: #flag = false ==> true print(x,end=' ') p.append(x)
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
习题6:杨辉三角
## 方法一: # 每个数等于它上方两数之和 # 每行数字左右对称,由1开始逐渐变大。 # 第n行的数字有n项。 # 第n行数字和为2n-1。 ## 第一次循环 i=2 t = [[1],[1,1]] for i in range(2,6): #i=2 cur = [1] # print(cur) pre = t[i-1] # t[1] ==> pre = [1,1] len(pre)=2 # print(pre) for j in range(len(pre)-1): # len(pre)-1 =1 ==> for j in [0,1) cur.append(pre[j]+pre[j+1]) # cur = [1].append ([0]+[1])=2 ==>[1,2] cur.append(1) #[1,2,1] t.append(cur)# [[1], [1, 1], [1, 2, 1]] print(t) ## 第二次循环 i=3 t_i3 = [[1],[1,1]] for i in range(2,6): #i=3 cur = [1] # print(cur) pre = t_i3[i-1] # t[2] ==> pre = [1,2,1] ==> len(pre)=3 # print(pre) for j in range(len(pre)-1): # len(pre)-1 = 2 ==> for j in [0,1,2) 循环2次 j=0 and j=1 cur.append(pre[j]+pre[j+1]) # cur第一次循环 = [1].append ([0]+[1])=3(pre[0]=1 and pre[1]=2 )) ==>[1,3] # cur第二次循环 cur=[1,3].append([1]+[2]=3(pre[1=2 and pre [2]=1 )) ==> [1,3,3] cur.append(1) #[1,3,3,1] t_i3.append(cur)# [[1], [1, 1], [1, 2, 1],[1, 3, 3, 1]] print(t_i3)
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1]] [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1]]
## 方法二: def YangHui (num = 6): LL = [[1]] for i in range(1,num): LL.append([(0 if j== 0else LL[i-1][j-1])+ (0 if j ==len(LL[i-1]) else LL[i-1][j]) for j in range(i+1)]) return LL a = YangHui() print(a)
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1]]
习题7:打印转置矩阵
## 方法一: def trans(m): a = [[] for i in m[0]] #打印两个原始列表 count = 0 for i in m: #i=[1,2],拿出第一个列表 count += 1 print(a) print(i) print(m) for j in range(len(i)): # j in (0,1),将列表的数据分别插入到新的列表中 a[j].append(i[j]) #a[0].append(i[0]) ==> []+1 = [1] #a[1].append(i[1]) ==> []+2 = [2] print(count ) return a m = [[1, 2,3,5], [3, 4,4,5], [5,4, 6],[0,4,5,3]] print(trans(m))
[[], [], [], []] [1, 2, 3, 5] [[1, 2, 3, 5], [3, 4, 4, 5], [5, 4, 6], [0, 4, 5, 3]] 1 [[1], [2], [3], [5]] [3, 4, 4, 5] [[1, 2, 3, 5], [3, 4, 4, 5], [5, 4, 6], [0, 4, 5, 3]] 2 [[1, 3], [2, 4], [3, 4], [5, 5]] [5, 4, 6] [[1, 2, 3, 5], [3, 4, 4, 5], [5, 4, 6], [0, 4, 5, 3]] 3 [[1, 3, 5], [2, 4, 4], [3, 4, 6], [5, 5]] [0, 4, 5, 3] [[1, 2, 3, 5], [3, 4, 4, 5], [5, 4, 6], [0, 4, 5, 3]] 4 [[1, 3, 5, 0], [2, 4, 4, 4], [3, 4, 6, 5], [5, 5, 3]]
习题8.字典扁平化

要求:把源字典转换为目标字典 源字典:{'a':{'b':1,'c':2},'d':{'e':3,'f':{'g':4}}} 目标字典:{'a.b': 1, 'a.c': 2, 'd.e': 3, 'd.f.g': 4}

#dst = {'d.e': 3, 'a.c': 2, 'a.b': 1, 'd.f.g': 4} #a,v_a; #b,v_a #为什么需要个前缀呢? #中心思想,递归 #判断v是否为字典,如果是字典则继续分解,如果不是字典就输出 source = {'a':{'b':1,'c':2},'d':{'e':3,'f':{'g':4}}} def flatmap(src,dest=None,prefix=''): if dest == None: dest = {} for k,v in src.items(): if isinstance(v,(list,dict)): #判断v是否为字典,如果是字典则继续分解,如果不是字典就输出 flatmap(v,prefix=prefix+k+'.') #把v再次传入flatmap函数,此时参数已经为prfix+.了 else: target[prefix+k] = v return dest flatmap(source) print(target)
{'a.b': 1, 'a.c': 2, 'd.e': 3, 'd.f.g': 4}
source = {'a':{'b':1,'c':2},'d':{'e':3,'f':{'g':4}}} def flatmap(src): def _flatmap(src,dest=None,prefix=''): for k,v in src.items(): key = prefix + k if isinstance(v,(list,dict)): #判断v是否为字典,如果是字典则继续分解,如果不是字典就输出 _flatmap(v,dest,key+'.') #把v再次传入flatmap函数,此时参数已经为prfix+.了 else: dest[key] =v dest = {} _flatmap(src,dest) return dest print(flatmap(source))
{'a.b': 1, 'a.c': 2, 'd.e': 3, 'd.f.g': 4}
习题9:给定一个不超过5位的正整数,判断其有几位?使用input函数
num = int(input()) if (num /10000)>=1 and (num /10000) < 10: print('5') elif (num /1000)>=1 and (num /1000) < 10: print('4') elif (num /100)>=1 and (num /100) < 10: print('3') elif (num /10)>=1 and (num /10) < 10: print('2') elif num >=1 and num < 10: print('1') else: print('error')
1123 4
num = int(input()) if num <= 9 and num >=0: print("1") elif num <= 99: print('2') elif num <= 999: print("3") elif num <= 9999: print("4") else: print('error')
112 3
num = int(input()) if num < 100: if av <10 and av > 0: print('1') elif av <100 and av >= 10: print('2') elif 100<=num <=9999: av = num /100 if av >= 1 and av < 10: print('3') elif av >= 10 and av <100: print('4') else: print('error')
111111 error

while 实现

num =1000000 count = 0 while num >=10: if num / 10 >= 1: num = num / 10 count +=1 print(count+1)
7

统计位数,并且倒序打印出每一位

num1 = num = 12345678 count = 0 while num >= 10: if num / 10 >= 1: count +=1 num = num // 10 print(num1-num*10) num1 = num num1 - num * 10 if num < 10 : print(num) print('total:',count+1)
8 7 6 5 4 3 2 1 total: 8
习题10:打印一个正方形
for i in range(4): print('* '*4)
* * * * * * * * * * * * * * * *
for i in range(1,10): if i == 1: print('* '*10) elif i<9: print('*'+' '*17+'*') else: print('* '*10)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
习题11:求100以内的奇数和
sum = 0 for i in range(1,100,2): sum += i print(sum)
2500
sum = 0 for i in range(1,100): if i % 2 !=0: sum += i print(sum)
2500
习题12: 求5的阶乘
num = 1 for i in range(1,5): num *=i print(num)
24

求1到5的阶乘之和

num = 1 a = 0 for i in range(1,6): #5 for n in range(1,i+1): #1-5 num *=n a += num num = 1 print(a)
153
num = 1 a = 0 for n in range(1,6): #n= 1 , n =2 n =3 num *=n # num = 1, num = 2 *1 = 2 num = 2 x 3 a += num # a= 1 a = 1+2 a = 1+2 +6 print(a)
153
习题13:打印菱形
for i in range(1,8): if i <=4: print((4-i)*' '+'*'*(2*i-1)) else: print((i-4)*' '+'*'*(2*(8-i)-1))
* *** ***** ******* ***** *** *
for i in range(-3,4): #[-3,3] if i < 0: p = -i else: p = i print(' '*p+'*'*(7-p*2))
* *** ***** ******* ***** *** *
for i in range(1,8): print((4-i)*' '+'*'*(2*i-1)) if i <=4 else print((i-4)*' '+'*'*(2*(8-i)-1))
* *** ***** ******* ***** *** *
a, b, c = 1, 2, 3 if a>b: c = a else: c = b ## 变异写法==> c = a if a>b else b
习题14:生成0000.zhuajgmncp 形式的的字符串,点号前有序,之后为无序的字符串
import random random_str = '' str_word = 'abcdefghijklmnopqlstuvwxyz' for i in range(10): str_i=("%04d" % i) lst = list(str_word) random_list = random.choices(lst,k=10) for tmp_str in random_list: random_str+=tmp_str new = str_i + '.'+random_str random_str = '' print(new)
0000.dsjvxdnkww 0001.sjlaeyjpcl 0002.ucypwqeoah 0003.ofkllwkdmi 0004.bujjgomwsl 0005.ouzvmfngti 0006.wsqvpxayob 0007.oqobuqongf 0008.aqiplnlljx 0009.mwpmnjjgmj
['{:04},{}'.format(n,''.join([random.choice(bytes(range(97,123)).decode()) for _ in range(10)])) for n in range(1,10)]
['0001,mhtflcfjcj', '0002,aiedozjflp', '0003,wtnddoeiof', '0004,jwimjzeant', '0005,nahqnwsozj', '0006,jbfbeykmke', '0007,brhqtxyits', '0008,fzdrthuhuf', '0009,slfwnjpfmg']

本文作者:mykernel

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!