第一练
第一小题
第一题:请打印输出以下格式的结果
print('Twinkle, twinkle, little star,')
print('\tHow I wonder what you are! ')
print("\t\tUp above the world so high,")
print("\t\tLike a diamond in the sky.")
print('Twinkle, twinkle, little star,')
print('\tHow I wonder what you are! ')
第二小题
第二题:编写一个 Python 程序来获取你正在使用的 Python 版本
import sys
print("Python version")
print(sys.version)
print("Version info.")
print(sys.version_info)
第三小题
第三题:编写一个 Python 程序来显示当前的日期和时间。
print("当前的日期是")
import datetime
time=datetime.datetime.now()
print(time)
第四小题
第四题:编写一个 Python 程序,接受用户提供的圆的半径并计算面积。
R=int(input("请输入圆的半径:"))
π=3.14
S=π*R**2
Y=str(S)
print(f"圆的半径为{Y}")
第五大题
第五题:编写一个 Python 程序,它接受用户的名和姓,并以相反的顺序打印它们,它们之间有一个空格。
first_name=input("请输入你的姓")
last_name=input("请输入你的名")
print(f"{first_name} {last_name}")
print(f"{last_name} {first_name}")
第二练
第一题
:编写一个 Python 程序,它接受来自用户的逗号分隔数字序列,并生成一个列表和一个包含这些数字的元组:
X=input()
number=[]
Y=len(X)
i=0
while i<Y:
N=X[i:i+1]
if N==",":
i+=1
continue
else:
number.append(N)
i+=1
print(number)
第二题
第二题:编写一个 Python 程序来接受来自用户的文件名并打印其扩展名。
X=input()
for i,x in enumerate(X):
if x=='.':
print(X[i+1:])
第三题
第三题:编写一个 Python 程序来显示以下列表中的第一种和最后一种颜色。
color_list = ["Red","Green",'White' ,'Black']
print(color_list[0])
print(color_list[-1])
第四题
第四题:编写一个 Python 程序,它接受一个整数 n并计算 n+nn+nn*n 的值
x=input()
i=int(x)
n=i*10+i
X=i+n+n*i
print(X)
第五题
编写一个 Python 程序来打印给定月份和年份的日历。
参考模块:calendar
第三练
第一题:
编写一个 Python 程序来计算两个日期之间的天数。
R=int(input())
π=3.14
V=3/4*π*R**3
print(V)
第二题:
编写一个 Python 程序来获取给定数字与 17 之间的差,如果该数字大于 17,则返回绝对差的两倍。
X=int(input())
if X<17:
print(17-X)
else:
print(3*(X-17))
第三题:
编写Python程序,测试一个数是在1000或2000的100以内。
第四题
编写一个 Python 程序来计算三个给定数字的总和,如果值相等,则返回它们总和的三倍。
X=int(input())
Y=int(input())
Z=int(input())
if X==Y and Y==Z:
print(X*3)
else:
print(X+Y+Z)
第四练
第一题:编写一个 Python 程序来计算给定列表中的数字 4的个数。
比如列表:
def num(name):
x=0
for i in name:
if i==4:
x+=1
print(x)
第二题
:编写一个Python程序来检查一组值中是否包含指定的值。
测试数据
def number(list,X):
if X in list:
print('True')
else:
print("False")
第三题
:编写一个 Python 程序,将列表中的所有元素连接成一个字符串并返回。
def number(list):
number=''
for i in list:
number=number+str(i)
return number
第四题
编写一个 Python 程序,接受三角形的底和高并计算面积。
def number(c,h):
S=c*h/2
return S
第五题
编写一个 Python 程序来计算两个正整数的最大公约数
def index(x,y):
first=[]
if x>y:
for i in range(1, x + 1):
if x % i == 0 and y % i == 0:
first.append(i)
elif x==y:
first.append(y)
else:
for i in range(1, y + 1):
if x % i == 0 and y % i == 0:
first.append(i)
print(max(first))
第五练
第一题:
编写一个 Python 程序来获取两个正整数的最小公倍数
def index(x,y):
first=[]
if x>y:
for i in range(1, x + 1):
if x % i == 0 and y % i == 0:
first.append(i)
elif x==y:
first.append(y)
else:
for i in range(1, y + 1):
if x % i == 0 and y % i == 0:
first.append(i)
N=max(first)
Y=x*y/N
print(Y)
第二题:
编写一个 Python 程序来求三个给定整数的和。但是,如果两个值相等,总和将为零。
def index(x,y,z):
num=0
if x==y or y==z or z==x:
return num
else:
num=x+y+z
return num
第三题
编写一个 Python 程序来求两个给定整数的和。但是,如果总和在 15 到 20 之间,它将返回 20。
def index(x,y):
num=x+y
if 14<num<21:
return 0
else:
return num
第四题:
编写一个 Python 程序,如果两个给定的整数值相等或它们的和或差为 5,则返回 true
def index(x,y):
if x == y or x+y == 5 or abs(y-x)==5:
return True
第五题
如果两个对象都是整数类型,编写一个 Python 程序来计算两个对象的和
def index(x,y):
if type(x) == type(y)== int:
num=x+y
return num