Python编程基础复习(一)
人机交互,输入输出
- 编写一个程序,用户输入一个字符串,按每行一个字符的形式进行输出。
string = input()
for char in string:
print(char)
2.用户输入一个浮点数,用str.format格式化输出,保留3位小数。
my_num = float(input())
print('{:.3f}'.format(my_num))
3.输入一个整数 n,输出所有小于 n 的非负整数 i 的平方( 0 <= i < n )。
def loop(n):
for i in range(n):
print(n**2)
if __name__ == '__main__':
n = int(input())
loop(n)
4.print()函数输入总结
print(*objects,seq=,end=,file=sys.stdout,flush=False)
常用控制输出的是seq和end,前者控制多个输出间隔元素(默认空格分隔),后者控制输出结束后的操作(默认换行),file控制输出到显示器的形式
print(1,2,3,4,5)
print(1,2,3,4,5,sep = ',')
print(2019,11,18,sep = '/')
print(23,59,36,sep = ':')
print([1,2,3,4,5])
print(*[1,2,3,4,5])
print('12345')
print(*'12345')
for i in range(5):
print(i)
for i in range(5):
print(i,end = ',')
for i in range(5):
print(i,end = ' ')
流程控制(判断、循环)
1.输入一个正整数 n ,计算从 1 到 n 各数字的和,要求用一行代码实现
print(sum(range(1,int(input())+1)))
2.用户输入一个小于10的正整数,求1 + 12 + 123 + 1234 + …… 的前n项的和
numString = '123456789'
n = eval(input())
numSum = 0
for i in range(n):
numSum = numSum + eval(numString[:i+1])
print(numSum)
3.1-1/2+2/3-3/5+4/8-5/13+…的前n项和,n由用户输入(n>0),结果用str.format()方法保留小数点后6位数字输出。
sum0 = 1
sign = -1
a,b = 1,1
n = int(input())
for i in range(1,n):
a , b = b, a+b
sum0 = sum0 + sign*(i/b)
sign = -sign
print("{:.6f}".format(sum0))
4.十进制转二进制
n = int(input())
s = ''
if n == 0:
s = '0'
while n != 0:
s = s+str(n%2)
n = n//2
print(s[::-1])
5.百钱买百鸡问题系列 问题描述(1):我国古代数学家张丘建在《算经》一书中提出的数学问题:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。百钱买百鸡,如果要求鸡翁、鸡母、鸡雏都不为零,问鸡翁、鸡母、鸡雏各几何? 输出要求:每行输出一组结果,按鸡翁数、鸡母数、鸡雏数的顺序输出,数字之间用空格分隔;如果有多组解,按照鸡翁的数量由小到大输出。
for i in range(1,101):
for j in range(1,101):
for k in range(1,101):
if k%3 == 0:
if i + j + k == 100 and 5*i+3*j+k//3==100:
print(i, j, k)
问题描述(2):用户输入鸡的数量和钱数,鸡翁、鸡母、鸡雏各为多少?如果有解,输出全部解,并按鸡翁数量由少到多的顺序输出;如果无解则输出“无解”。
num,money = map(int,input().split(" "))
flag = 0
for i in range(1,num+1):
for j in range(1,num+1):
for k in range(1,num+1):
if i+j+k == num:
if 5*i+3*j+k/3==money:
flag = 1
print(i,j,k)
if flag == 0:
print("无解")
num,money = map(int,input().split(" "))
flag = 0
for i in range(1,num//5+1):
for j in range(1,num//3+1):
k = num - i - j
if k%3==0 and k > 0 and 5*i+3*j+k//3==money:
flag = 1
print(i,j,k)
if flag == 0:
print("无解")
6.一元二次方程求解情况判断
a = eval(input())
b = eval(input())
c = eval(input())
if a == 0:
if b == 0:
print("Data error!")
else:
print("{:.2f}".format(-1*c/b))
else:
sqrt = pow(b,2)-4*a*c
if sqrt > 0:
x1 = (-1*b - pow(sqrt,0.5))/(2*a)
x2 = (-1*b + pow(sqrt,0.5))/(2*a)
print("{:.2f} {:.2f}".format(x2,x1))
elif sqrt == 0:
x = -1*b/(2*a)
print("{:.2f}".format(x))
else:
print("该方程无实数解")
7.判断闰年和非闰年
year = int(input())
if year % 400 == 0 and year % 100 == 0:
return True
elif year % 4 == 0 and year % 100 != 0:
return True
else:
return False
8.判断输入日期是当年的第几天
date = input()
year,month,day = date.split('/')
year = eval(year)
month = eval(month)
day = eval(day)
normal = [31,28,31,30,31,30,31,31,30,31,30,31]
unnormal = [31,29,31,30,31,30,31,31,30,31,30,31]
if (year%4==0 and year%100!=0) or (year%400==0 and year%100==0):
days = sum(unnormal[:month-1])+day
else:
days = sum(normal[:month-1])+day
print("{:}年{:}月{:}日是{:}年第{:}天".format(year,month,day,year,days))
9.计算n的阶乘
import math
n=eval(input())
if n>=0 and type(n)==int:
print(math.factorial(n))
else:
print("ERROR")
10.字母大小写转换
s = input()
for c in s:
if c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
print(c.lower(),end="")
elif c in 'abcdefghijklmnopqrstuvwxyz':
print(c.upper(),end="")
else:
print(c,end="")
s = input()
for c in string:
if c.islower():
print(c.upper(),end="")
elif c.isupper():
print(c.lower(),end="")
else:
print(c)
print(''.join([i.lower() if i.isupper() else i.upper() for i in input()]))
import string
def swap_case(s):
ls = []
for c in s:
if c in string.ascii_lowercase:
ls.append(c.upper())
elif c in string.ascii_uppercase:
ls.append(c.lower())
else:
ls.append(c)
return ''.join(ls)
if __name__ == '__main__':
s = input()
result = swap_case(s)
print(result)
11.阶乘和数判断:一个正整数如果等于组成它的各位数字的阶乘之和,则该正整数称为阶乘和数
import math
numStr = input()
total = 0
for char in numStr:
total = total + math.factorial(int(char))
if total == eval(numStr):
print("YES")
else:
print("NO")
???????????????????????????????????????????????????????????????????????????????????????????
|