1.3.3 练习
1.3.3.1 输入求值
给一个半径,求圆的面积和周长。圆周率3.14
# In:
import math
radius = int(input('radius = '))
print('area = ', 3.14 * radius ** 2)
print('circumference =', 3.14 * 2 * radius)
# 输入:
radius = 2
# 打印结果:
area = 12.56
circumference = 12.56
# In:
r = int(input('r='))
print('area=' + str(3.14 * r *r))
print('circumference=' + str(2 * 3.14 * r))
# 输入:
r= 5
# 打印结果:
area=78.5
circumference=31.400000000000002
# In:
import math
radius = int(input('radius= '))
area = math.pi * radius ** 2
print("area = {:.2f}".format(area))
print('circumference =', 3.14 * 2 * radius)
# 输入:
radius= 5
# 打印结果:
area = 78.54
circumference = 31.400000000000002
1.3.3.2 排序并打印
输入两位数,比较大小后,从小到大升序打印
# In:
a = int(input('first: '))
b = int(input('second: '))
if a > b:
print(b, a)
else:
print(a, b)
# 输入:
first: 11
second: 9
# 打印结果:
9 11
三元表达式
python没有三目运算符,但提供了三元表达式 真值 if 条件 else 假值
# In:
a = int(input('first: '))
b = int(input('second: '))
print(b, a) if a>b else print(a, b)
# 输入:
first: 10
second: 85
# 打印结果:
10 85
1.3.3.3 获取最大值
依次输入若干个整数,打印出最大值。如果输入为空,则退出程序
# In:
x = int(input('Please Input your first number : '))
m = x
while True:
x = input('Please Input a number >>>')
if x:
x = int(x)
if x > m:
m = x
print('Current Nax Value = ', m)
else:
break
# 输入:
Please Input your first number : 5
Please Input a number >>>7
# 打印结果:
Current Nax Value = 7
# 输入:
Please Input a number >>>9
# 打印结果:
Current Nax Value = 9
# 输入:
Please Input a number >>>10
# 打印结果:
Current Nax Value = 10
# 输入:
Please Input a number >>>
# In:
m = int(input('Please Input your first number: '))
while True:
c = int(input('Please Input a number: ')
if c:
n = int(c)
if n > m:
m = n
print('Max is', m)
else: #输入为空
print('Exit.')
break #退出
# 输入:
Input your first number: 8
Input a number: 15
# 打印结果:
Max is 15
# 输入:
Input a number: 6
# 打印结果:
Max is 15
# 输入:
Input a number: 14
# 打印结果:
Max is 15
# 输入:
Input a number: 96
# 打印结果:
Max is 96
# 输入:
Input a number:
# 打印结果:
Exit.
1.3.3.4 计算整数位数并打印
给定一个不超过5位的正整数(不转换为字符串),判断该数的位数,依次打印出万位、千位、百位、十位、个位的数字
# In:
n = int('54321') # 5 * 10 ** 4 + 4 * 10 ** 3 + ...
w = 10000
x = n // w # 5
y = n % w # 4321
print(x, y)
w = w // 10 # 1000
x = y // w # 4321 // 1000 4
y = y % w # 4321 % 1000 321
print(x, y)
w //= 10
x = y // w # 321 // 100 3
y = y % w # 321 % 100 21
print(x, y)
w //= 10
x = y // w # 21 // 10 2
y = y % w # 21 % 10 1
print(x, y)
w //= 10
x = y // w # 1 // 1 1
y = y % w # 1 % 1 0
print(x, y)
# 打印结果:
5 4321
4 321
3 21
2 1
1 0
# In:
y = int('54321') # 5 * 10 ** 4 + 4 * 10 ** 3 + ...
w = 10000
for i in range(5):
x = y // w
y = y % w
print(x, y)
w = w // 10
# 打印结果:
5 4321
4 321
3 21
2 1
1 0
# In:
y = int('54321') # 5 * 10 ** 4 + 4 * 10 ** 3 + ...
w = 10000
for i in range(4):
x = y // w
y = y % w
print(x, y)
w = w // 10
else:
print(y) # 少计算一次
# 打印结果:
5 4321
4 321
3 21
2 1
1
# In:
y = int('54321') # 5 * 10 ** 4 + 4 * 10 ** 3 + ...
for i in range(4):
w = 10 ** (4-i)
x = y // w
y = y % w
print(x, y)
else:
print(y)
# 打印结果:
5 4321
4 321
3 21
2 1
1
# In:
y = int('54321') # 5 * 10 ** 4 + 4 * 10 ** 3 + ...
for i in range(4, 0, -1):
w = 10 ** i
x = y // w
y = y % w
print(x, y)
else:
print(y)
# 打印结果:
5 4321
4 321
3 21
2 1
1
# In:
y = int('54321') # 5 * 10 ** 4 + 4 * 10 ** 3 + ...
for i in range(4, 0, -1):
w = 10 ** i
x = y // w
y = y % w
print(x, y)
print(y)
# 打印结果:
5 4321
4 321
3 21
2 1
1
# In:
y = int('54321') # 10000. 00100
w = 10000
length = 5
for i in range(5):
x = y // w
if x: # !0
pass
else:
length -= 1
y = y % w
print(x, y)
w = w // 10
print('The length of the number is ', length)
# 打印结果:
5 4321
4 321
3 21
2 1
1 0
The length of the number is 5
# In:
y = int('00300') # 10000. 00100
w = 10000
length = 5
for i in range(5):
x = y // w
if x: # !0
pass
else:
length -= 1
y = y % w
print(x, y)
w = w // 10
print('The length of the number is ', length)
# 打印结果:
0 300
0 300
3 0
0 0
0 0
The length of the number is 1 # 这样就不对了
# In:
y = int('00300') # 10000. 00100
w = 10000
length = 5
flag = False # 没有找到第一个有效数字
for i in range(5):
x = y // w
if not flag:
if x: # !0
flag = True # 打开开关,或者叫做打标记
else:
length -= 1
y = y % w
print(x, y)
w = w // 10
print('The length of the number is ', length)
# 打印结果:
0 300
0 300
3 1
0 1
0 0
The length of the number is 3
# In:
y = int('00300') # 10000. 00100
w = 10000
length = 5
flag = False # 没有找到第一个有效数字
for i in range(5):
x = y // w
if not flag:
if x: # !0
flag = True # 打开开关,或者叫做打标记
else:
length -= 1
if flag:
print(x)
y = y % w
w = w // 10
print('The length of the number is ', length)
# 打印结果:
3
0
0
The length of the number is 3
# In:
y = int('00300') # 10000. 00100
w = 10000
length = 5
flag = False # 没有找到第一个有效数字
for i in range(5):
x = y // w
if not flag:
if x: # !0
flag = True # 打开开关,或者叫做打标记
else:
length -= 1
else: # 这样写是错误的,你要注意打开开关的这一回它用不用,打开开关的目的是以后再也不进来了,但是你打开开关之后要输出,但是你别忘了如果刚才进来之后这作为这个分支来讲,你进了这个分支就不再走else的分支了,这不就出错了吗?
print(x)
y = y % w
w = w // 10
print('The length of the number is ', length)
# 打印结果:
0
0
The length of the number is 3
# In:
y = int('00300') # 10000. 00100
w = 10000
length = 5
flag = False # 没有找到第一个有效数字
while w:
x = y // w
if not flag:
if x: # !0
flag = True # 打开开关,或者叫做打标记
else:
length -= 1
if flag:
print(x)
y = y % w
w = w // 10
print('The length of the number is ', length)
# 打印结果:
3
0
0
The length of the number is 3
给定一个不超过5位的正整数(不转换为字符串),判断该数的位数,依次打印出万位、千位、百位、十位、个位的数字
注:这不是高效的算法,只是为了练习
难点在于如何对一个正整数进行迭代处理(暂不考虑使用字符串处理方式)
假设有数字54321,目标打印出5、4、3、2、1
第一趟 54321//10000=5 54321%10000=4321
第二趟 4321 //1000 =4 4321 %1000 =321
第三趟 321 //100 =3 321 %100 =21
第四趟 21 //10 =2 21 %10 =1
第五趟 1 //1 =1 1 %1 =0
# In:
c = int('54321') #不超过5位数
w = 10000
x = c // w # 5
y = c % w # 4321
print(x, y)
w = w // 10 # 1000
x = y // w # 4
y = y % w # 321
print(x, y)
w = w // 10 # 100
x = y //w # 3
y = y % w # 21
print(x, y)
w = w // 10 # 10
x = y // w # 2
y = y % w # 1是余数
print(x, y)
w = w // 10 # 1
x = y // w # 1
y = y % w # 0
print(x, y)
# 打印结果:
5 4321
4 321
3 21
2 1
1 0
每一趟中,整除就能获得最高位置的数,取模就能获得除去最高位的数的剩余数字,且取模后的数字作为下一趟的待处理数字。
c = 54321 w = 10000
第一趟 out = c //w c = c % w w = w // 10
第一趟 out = c //w c = c % w w = w // 10
......
# In:
c = int('54321') #不超过5位数
w = 10000
for i in range(5):
x = c // w
c = c % w
print(x)
w //= 10
# 打印结果:
5
4
3
2
1
# In:
c = int('54321') #不超过5位数
w = 10000
for i in range(4): # 计算第四次后,余数就是个位数
x = c // w
c = c % w
print(x)
w //= 10
else:
print(c) #余数
# 打印结果:
5
4
3
2
1
长度处理
从左到右处理数据,遇到0则长度减1,遇到第一个非0数字才开始打印并不在减1
# In:
c = int('54321') #不超过5位数 # 00451, 01230
w = 10000 # 先拿10000来试
length = 5 # 假设长度为5
flag = False # 开关量,打标记,假定没找到第一个非0
for i in range(5):
x = c // w
if not flag:
if x: # 找到第一个非0数字,从此才开始打印
flag = True
else:
length -= 1
if flag:
print(x)
c = c % w
w //= 10
print('The length of the number is', length)
# 打印结果:
5
4
3
2
1
The length of the number is 5
# In:
c = int('00451') #不超过5位数 # 00451, 01230
w = 10000 # 先拿10000来试
length = 5 # 假设长度为5
flag = False # 开关量,打标记,假定没找到第一个非0
for i in range(5):
x = c // w
if not flag:
if x: # 找到第一个非0数字,从此才开始打印
flag = True
else:
length -= 1
if flag:
print(x)
c = c % w
w //= 10
print('The length of the number is', length)
# 打印结果:
4
5
1
The length of the number is 3
# In:
c = int('01230') #不超过5位数 # 00451, 01230
w = 10000 # 先拿10000来试
length = 5 # 假设长度为5
flag = False # 开关量,打标记,假定没找到第一个非0
for i in range(5):
x = c // w
if not flag:
if x: # 找到第一个非0数字,从此才开始打印
flag = True
else:
length -= 1
if flag:
print(x)
c = c % w
w //= 10
print('The length of the number is', length)
# 打印结果:
1
2
3
0
The length of the number is 4
1.3.3.5 求平均数
输入n个数,求每次输入后的算数平均数
1 + 2 + 3 +4 =10 /4=2.5
1 + 2 /2 =1.5
1.5 + 3 / 2 = 2.25
2.25 + 4 /2 =
# In:
n = 0
s = 0
while True:
x = input('>>')
if x == '':
break
s += int(x) # 累计,平均数 s / n
n += 1
print('avg =', s / n)
# 输入:
>>1
# 打印结果:
avg = 1.0
# 输入:
>>2
# 打印结果:
avg = 1.5
# 输入:
>>3
# 打印结果:
avg = 2.0
# 输入:
>>4
# 打印结果:
avg = 2.5
# 输入:
>>
# In:
sum = 0 # 和
count = 0 # 次数
while True:
num = input('please input a number,or input 'quit' to exit')
if num == 'quit':
break
count += 1
sum += int(num)
avg = sum / count
print(avg)
# 输入:
>>>1
# 打印结果:
1.0
# 输入:
>>>2
# 打印结果:
1.5
# 输入:
>>>3
# 打印结果:
2.0
# 输入:
>>>4
# 打印结果:
2.5
# 输入:
>>>5
# 打印结果:
3.0
# 输入:
>>>quit
1.3.3.6 打印空心正方形
打印一个边长为n的空心正方形
# In:
char = '*'
n = 4
print(char * n)
for i in range(n-2):
print(char + ' ' * (n-2) + char)
print(char * n)
# 打印结果:
****
* *
* *
****
# In:
char = '*'
n = 4
for i in range(n):
if i == 0 or i == n-1: # 首尾是特例
print(char * n)
else:
print(char + ' ' * (n-2) + char)
# 打印结果:
****
* *
* *
****
# In:
char = '*'
n = 4
for i in range(n):
if i % (n-1) == 0:
print(char * n)
else:
print(char + ' ' * (n-2) + char)
# 打印结果:
****
* *
* *
****
# In:
n = int(input(">>>"))
print('*' * n)
for i in range(n-2):
print('*' + ' ' * (n-2) + '*')
print('*' * n)
# 输入:
>>>6
# 打印结果:
******
* *
* *
* *
* *
******
# In:
n = int(input(">>>"))
septop = "*"
sepmid = "*"
for i in range(0, n-1):
septop += "\t*"
sepmid += "\t"
else:
sepmid += "*"
print(septop)
for i in range(0, n-2):
print("\n")
print(sepmid)
else:
print("\n")
print(septop)
# 输入:
>>>5
# 打印结果:
* * * * *
* *
* *
* *
* * * * *
边为3,则-1 0 1 => range(-1,2)
边长4,则 -2 -1 0 1 => range(-2,2)
边长5,则-2 -1 0 1 2 => range(-2,3)
下面循环语句块中并没有使用到这个range中的e,这只是为了提供一种解决复杂问题的公式推导的思路,例如对称性问题。打印正方形的题目体现不出来而已
# In:
n = int(input(">>>"))
e = -n // 2
for i in range(e,n+e):
if i == e or i == n + e - 1:
print("*" * n)
else:
print("*" + " " * (n -2) + "*")
# 输入:
>>>5
# 打印结果:
*****
* *
* *
* *
*****
# In:
n = int(input(">>>"))
for i in range(n):
if i == 0 or i == n - 1:
print("*" * n)
else:
print("*" + " " * (n -2) + "*")
# 输入:
>>>5
# 打印结果:
*****
* *
* *
* *
*****
# In:
n = 5
char = '*'
print(char * n)
for i in range(n-2):
print(char + ' '*(n-2) + char)
print(char * n)
# 打印结果:
*****
* *
* *
* *
*****
# In:
n = 5
char = '*'
for i in range(n):
if i == 0 or i == n-1:
print(char * n)
else:
print(char + ' '*(n-2) + char)
# 打印结果:
*****
* *
* *
* *
*****
另一种思路
取模,能整数n-1的就打印所有*
# In:
n = 5
for i in range(5):
if i % (n-1) == 0:
print('*'*n)
else:
print('*' + ' '*(n-2) + '*')
# 打印结果:
*****
* *
* *
* *
*****
1.3.3.7 求100内所有奇数的和
# In:
s = 0
for i in range(1, 100):
if i & 1:
s += i
print(s)
# 打印结果:
2500
# In:
s = 0
for i in range(1, 100, 2):
s += i
print(s)
# 打印结果:
2500
# sum 内建函数,求和
# In:
sum(range(1, 100, 2))
# Out:
2500
使用for循环是为了练习,以后建议使用内建函数sum求和
# In:
print(sum(range(1, 100, 2)))
# 打印结果:
2500
1.3.3.8 成绩问题
判断学生成绩,成绩等级A至E。其中,90分以上为‘A’,80-89分为‘B’,70-79分为‘C’,60-69分为‘D’,60分以下为‘E’
# In:
score = 88
if score >= 90:
print('A')
elif score >= 80: # < 90
print('B')
elif score >= 70: # < 80
print('C')
elif score >= 60: # < 70
print('D')
else:
print('E')
# 打印结果:
B
# In:
score = int(input("请输入一个在 0-100 之间的数字:"))
grade = ""
if score > 100 or score < 0:
score = int(input("输入错误!请重新输入一个在 0-100 之间的数字:"))
else:
if score >= 90:
grade = "A"
elif score >= 80:
grade = 'B'
elif score >= 70:
grade = 'C'
elif score >= 60:
grade = 'D'
else:
grade = 'E'
print("分数为{0},等级为{1}".format(score, grade))
# 输入:
请输入一个在 0-100 之间的数字:94
# 打印结果:
分数为94,等级为A
1.3.3.9 求1到5阶乘之和
# In:
n = 5
p = 1
for j in range(1, n+1):
p = p * j
print(p)
# 打印结果:
120
# In:
n = 5
p = 1
for i in range(1, n+1):
p = 1 # 每一趟重置
for j in range(1, i+1):
p = p * j
print(p)
# 打印结果:
1
2
6
24
120
# In:
n = 5
s = 0 # 总和
for i in range(1, n+1):
p = 1 # 每一趟重置,每一个i的阶乘重新算,非常没有效率
for j in range(1, i+1):
p = p * j
print(p)
s += p
print(s) # 它的时间复杂度是O(n ** 2)
# 打印结果:
1
2
6
24
120
153
# In:
n = 5
p = 1
s = 0 # 总和
for i in range(1, n+1): # 它的时间复杂度是0(n)
p = p * i
print(p)
s += p
print(s)
# 打印结果:
1
2
6
24
120
153
# In:
# 方法1
n = 5
s = 0
for i in range(1, n+1):
tmp = 1
for j in range(2, i+1):
tmp *= j
s += tmp
print(tmp)
print(s)
# 打印结果:
1
2
6
24
120
153
# 方法1中发现了大量的重复操作,后一次的阶乘等于前一次阶乘的值*当次值
# In:
# 方法2
n = 5
s = 0
tmp = 1
for i in range(1, n+1):
tmp *= i
s += tmp
print(s)
# 打印结果:
153
1.3.3.10 求素数
给一个数,判断它是否是素数(质数)
质数:一个大于1的自然数只能被1和它本身整除
# In:
x = 7793
for i in range(2, x): # 它的范围是 [2, x-1]
if x % i == 0: # 被整除说明是合数
print('not a prime number')
break # 只要在测试途中被任何一个数整除,它就是合数
else:
print('a prime number')
# 打印结果:
a prime number
# In:
x = 7793
flag = False # 假设它不是合数
for i in range(2, x): # 它的范围是 [2, x-1]
if x % i == 0: # 被整除说明是合数
flag = True # 是合数
break # 只要在测试途中被任何一个数整除,它就是合数
if flag:
print('a composite number')
else:
print('a prime number')
# 打印结果:
a prime number
方法1
最简单的思路,按照定义来,假设有一个数n(n>1),从2开始判断,一直判断到n-1。
# In:
n = 12577 # 避开3、5、10、2的倍数
flag = False # 假定不是合数,一旦整除就是合数
for i in range(2, n): # 对这个数从2开始测试到n-1
if n % i == 0: # 整除就是合数
flag = Ture
break
if flag:
print(n, 'is a composite number')
else:
print(n, 'is a prime number')
# 打印结果:
12577 is a prime number
与2取模,就可以直接判断它是否是一个偶数。
使用else子句
# In:
n = 12577 # 避开3、5、10、2的倍数
for i in range(2, n): # 对这个数从2开始测试到n-1
if n % i == 0: # 整除就是合数
print(n, 'is a composite number')
break
else:
print(n, 'is a prime number')
# 打印结果:
12577 is a prime number
从2到n的开平方的优化,以后实现。
# In:
num = int(input('>>>'))
for i in range(2, int((num ** 0.5) + 1)):
if num % i == 0:
print(num, '这不是质数')
break
else:
print(num, '这就是质数')
# 输入:
12577
# 打印结果:
12577 这就是质数
# In:
n = int(input('>>>'))
i = 0
for m in range(1, n+1):
if n % m == 0: #判断是否能被整除
i += m
if i == (1+n): #如果只能被1和自身整除,是素数
print('yes')
else:
print('no')