文章主要内容如下:
1,打印空心正方形
代码如下:
for i in range(1,12):
if i == 1 or i == 11:
print('+' * 20)
else:
print('+' + (' ' * 18) + '+')
输出结果:
PS D:\Software\python_object>
++++++++++++++++++++
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
++++++++++++++++++++
PS D:\Software\python_object>
2,求100以内所有奇数的和
代码如下:
sum = 0
for i in range(1,100,2):
sum +=i
print(sum)
输出结果:
PS D:\Software\python_object>
2500
PS D:\Software\python_object>
3,判断学生成绩,成绩等级A-E,其中90分以上为A, 80分-89分为B, 70-79分为C,60-69分为D,60分以下为E
代码如下:
score=int(input("pleas input your socre: "))
if score >= 90:
print("A")
elif score >= 80 and score <= 89:
print("B")
elif score >= 70 and score <= 79:
print("C")
elif score >= 60 and score <= 69:
print("D")
else:
print("E")
输出结果:
PS D:\Software\python_object>
pleas input your socre: 89
B
PS D:\Software\python_object>
pleas input your socre: 53
E
PS D:\Software\python_object>
4,求1到5的阶乘之和
代码如下:
s = 1
sum = 0
for i in range(1,6):
s = s*i
sum += s
print(s)
print(sum)
输出结果:
PS D:\Software\python_object>
120
153
PS D:\Software\python_object>
5,求5以内的素数
代码如下
num = int(input("plese ainput a prime number: "))
for i in range(2,num):
if num % i == 0:
print(i," is not prime number!")
break
else:
print(i,"is a prime number!")
输出结果:
PS D:\Software\python_object>
plese ainput a prime number: 5
2 is a prime number!
3 is a prime number!
4 is a prime number!
PS D:\Software\python_object>
6,给一个半径,求圆的面积和周长
代码如下:
r =int(input("请输入半径: "))
print("面积:",2*3.14*r)
print("周长:",3.14*(r**2))
输出结果:
PS D:\Software\python_object>
请输入半径: 5
面积: 31.400000000000002
周长: 78.5
PS D:\Software\python_object>
7,输入2个数,比较大小后,从小到大排序打印
代码如下:
a=int(input("please input a first number: "))
b=int(input("please input a second number: "))
if a > b:
print(b,a)
else:
print(a,b)
输出结果:
PS D:\Software\python_object>
please input a first number: 4
please input a second number: 1
1 4
PS D:\Software\python_object>
please input a first number: 3
please input a second number: 1
1 3
8,打印九九乘法表
第一种,代码如下:
for i in range(1,10):
for j in range(1,i+1):
print("%d*%d=%d "%(j,i,j*i)+"\t",end=(""))
print("")
输出结果:
PS D:\Software\python_object>
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
PS D:\Software\python_object>
第二种,利用列表解析式
print('\n'.join([''.join(['%s*%s=%-3s' % (x,y,y*x) for x in range(1,y+1)]) for y in range(1,10)]))
输出结果:
PS D:\Software\python_object>
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
9,打印闪电符号
代码如下:
for i in range(-3,4):
if i < 0:
a = -i
print(" "*a+(4-a)*"@")
elif i == 0:
print("@"*7)
else:
print(" "*3+"@"*(4-i))
输出结果:
PS D:\Software\python_object>
@
@@
@@@
@@@@@@@
@@@
@@
@
PS D:\Software\python_object>
10,求10项目内斐波那契数
代码如下:
def feibo(n):
if n <= 1:
return n
else:
return(feibo(n-1)+feibo(n-2))
num =int(input("你需要打印到第几项: "))
for i in range(num):
print(feibo(i))
输出结果:
PS D:\Software\python_object>
你需要打印到第几项: 5
0
1
1
2
3
PS D:\Software\python_object>
编写一个函数,能够接收2个参数,返回最大值和最小值
代码如下:
def sum_numbers(x,y,*args):
print('最大值',min(x,y,*args))
print('最小值',max(x,y,*args))
print(sum_numbers(5,6,2,4,9,0))
输出结果:
PS D:\Software\python_object>
最小是: 0
最大是: 9
编写一个函数,接收一个参数n,n为正整数,要求数字必须对齐
def trangles(n):
for i in range(1,n+1):
for j in range(n,0,-1):
if i < j:
print(' ' * len(str(j)),end=' ')
else:
print(j,end=' ')
print()
PS D:\Software\python_object> trangles(10)
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1