目录
用python计算1 + 2 + 3 + 4 + 5 + ... + 100之和(两种方法)
水仙花数是指一个n位数(n>=3)它的每位上的数字的n次幂之和等于它本身。用python求100~999之间的所有的水仙花数
用python输入学生学号与姓名,数据用字典存储,最终学生信息(按学号由小到大显示)
用python输出三位整数的百位,十位和个位上的数字
#输出三位整数的百位,十位和个位上的数字
x = input('请输入一个三位整数:')
x = int(x)
a = x // 100
b = x // 10 % 10
c = x % 10
print('百位:', a, '十位:', b, '个位:', c)
用python使用map函数获取一个三位整数
#使用map函数获取一个三位整数
x = input('请输入一个三位整数:')
a, b, c =map(int, x)
print('百位:', a, '十位:', b, '个位:', c)
用python实现交换两个变量的值
#方法1:
def demo1(a, b):
temp = a
a = b
b = temp
print(a, b)
demo1(11, 22)
#方法2:
a = 11
b = 22
a, b =b, a
print(a, b)
用python输入一个百分之成绩,根据输出等级成绩
"""输入一个百分之成绩,根据输出等级成绩
x >= 90
优秀
90 > x >= 80
良好
80 > x >= 60
及格
x < 60
补考
"""
num = int(input("请输入百分制成绩:"))
if num > 100 or num < 0:
print("数据错误,请输入0-100范围内数字!")
elif num >= 90:
print("优秀")
elif num >= 80:
print("良好")
elif num >= 60:
print("及格")
else:
print("补考")
如何用python求100以内的素数?
num=[];
i=2
for i in range(2,100):
j=2
for j in range(2,i):
if(i%j==0):
break
else:
num.append(i)
print(num)
用python计算1 + 2 + 3 + 4 + 5 + ... + 100之和(两种方法)
# while 循环
sum = 0
i = 1
while i <= 100:
sum += i
i += 1
print("sum = ", sum)
# for循环
sum = 0
for i in range(101):
sum += i
print("sum = ", sum)
# 结果:
# sum = 5050
用python实现九九乘法表
#九九乘法表
for i in range(1, 10):
for j in range(1, i + 1):
print(f'{j}x{i}={i * j}\t', end='\t')
print()
#while-while
i = 1
while i <= 9:
j = 1
while j <= i: # j的大小是由i来控制的
print(f'{i}*{j}={i*j}', end='\t')
j += 1
print('')
i += 1
#while-for
i = 1
while i <= 9:
for j in range(1, i+1): # range()函数左闭右开
print(f'{i}*{j}={i*j}', end=' ')
i += 1
print()
#for-while
for i in range(1,10):
j = 0
while j < i:
j += 1
print(f"{i}*{j}={i*j}", end=' ')
print()
#定义一个变量
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in a:
j = 1
while j <= i:
print(f'{i}*{j}={ i *j}', end='\t')
j += 1
print()
#使用递归
def multiplication(n):
if n < 10:
for m in range(1, n+ 1):
print(f"{m}*{n}={m * n}", end="\t")
print()
multiplication(n + 1)
multiplication(1)
#使用一行语句
print('\n'.join([' '.join(["%2s x%2s = %2s" % (j, i, i*j) for j in range(1, i+1)]) for i in range(1, 10)]))
print('\n'.join([' '.join([f"{j}x{i}={i*j}" for j in range(1, i + 1)]) for i in range(1, 10)]))
用python实现鸡兔同笼问题(35头,94足)
#鸡兔同笼问题(35头,94足)
for chicken in range(0, 36):
if 2 * chicken + (35 - chicken) * 4 == 94:
print('小鸡:', chicken, '小兔:', 35 - chicken)
用python求学生成绩的平均值,最大值最小值
#求学生成绩的平均值,最大值最小值
num, sum, max, min =0, 0, 0, 100
while True:
str = input('请输入第%d位学生成绩:'%(num + 1))
if str == 'Q':
print('输入结束!')
break
score = int(str)
if score < 0 or score > 100:
print('输入有误,重新输入!')
continue
sum += score
num += 1
if score > max:
max = score
if score < min:
min =score
print('平均成绩:', sum * 1.0 / num)
print('最大值:', max, '最小值:', min)"""
用python根据用户输入的密码判断安全级别
num = '1234567890'
W = 'ABCDEFGHIJKLMNOPQRSTUVMXYZ'
w = 'abcdefghijklmnopqrstuvmxyz'
sym = r'''`!@#$%^&*()_-+=/|\'":;?,.<>'''
pwd = input('请输入密码:')
length = len(pwd)
while pwd.isspace() or length == 0:
pwd = input("您输入的密码为空(或空格),请重新输入;")
if length <= 8:
lenGrade = 1
elif 8 < length < 16:
lenGrade = 2
elif not pwd.isalnum():
print("您的密码中可能包含非法字符!")
else:
lenGrade = 3
charGrade = 0
for each in pwd:
if each in num:
charGrade +=1
break
for each in pwd:
if each in W:
charGrade += 1
break
for each in pwd:
if each in w:
charGrade += 1
break
for each in pwd:
if each in sym:
charGrade += 1
break
if lenGrade == 1 or charGrade == 1 :
print("你的密码安全级别为:低")
elif lenGrade == 2 or charGrade == 2 :
print('你的密码安全级别为:中')
else :
print('你的密码安全级别为:高')
#输入一个字符串,分别统计其中字母、数字、和其它字符的个数
str = input("请输入一个字符串:")
letter = 0
digit = 0
others = 0
for i in str:
if i.isalpha():
letter += 1
elif i.isdigit():
digit += 1
else:
others += 1
print("字母有%d个,数字有%d个,其它字符有%d个"%(letter, digit, others))
用python求选手平均分
sorce = []
for i in range(1, 6):
num = float(input('%d号评委打分:'%i))
sorce.append(num)
min =min(sorce)
max =max(sorce)
sorce.remove(min)
sorce.remove(max)
ave = sum(sorce) / len(sorce)
print('选手最终得分为%.2f'%ave)
水仙花数是指一个n位数(n>=3)它的每位上的数字的n次幂之和等于它本身。
用python求100~999之间的所有的水仙花数
for i in range(100, 1000):
ge = i% 10
shi = i // 10 % 10
bai = i //100
if ge ** 3 + shi ** 3 + bai ** 3 == i:
print('水仙花数:\n', i)
用python统计所输入的英文中出现的字母及次数
s = input('所输入一句英文:')
s = s.upper()
dict1 = {chr(n): 0 for n in range(65,91)}
for char in s:
if 'A' <= char <= 'Z':
dict1[char] += 1
list1 = list(dict1.items())
for ele in list1:
if ele[1] != 0:
print(ele[0] + ':', ele[1])
用python输入学生学号与姓名,数据用字典存储,最终学生信息(按学号由小到大显示)
students = {}
while 1:
student = input('请输入学号(输入q停止输入信息):')
if student == 'q':
break
student_name = input('请输入学生姓名:')
students[student] = student_name
print(students)
list_xh = list(students.keys())
print(list_xh)
list_xh.sort()#用sort()函数排序
for i in list_xh:
print(i,':', students[i])
用python输出杨辉三角的前num行数据
#生成杨辉三角
def triangle(num):
triangle = [[1]]
for i in range(2, num + 1):
triangle.append([1]*i)
for j in range(1, i - 1):
triangle[i-1][j] = triangle[i-2][j] + triangle[i-2][j-1]
return triangle
#打印杨辉三角
def printtriangle(triangle):
width = len(str(triangle[-1][len(triangle[-1]) // 2])) + 3
column = len(triangle[-1]) * width
for sublist in triangle:
result = []
for contents in sublist:
result.append('{0:^{1}}'.format(str(contents), width))
print('{0:^{1}}'.format(''.join(result), column))
num = int(input('请输入行数:'))
triangle = triangle(num)
printtriangle(triangle)
用python解决汉诺塔问题
def han(n, a, b, c):
if n == 1:
# 当仅有一个圆盘,直接将圆盘从柱子a移动到柱子c上
print(n, a, '->', c)
else:
# 将n-1个圆盘从柱子a移动到柱子b上
han(n - 1, a, c, b)
# 将最大的圆盘从柱子a移动到柱子c上
print(n, a, '->', c)
# 将n-1个圆盘从柱子b移动到柱子c上
han(n - 1, b, a, c)
n = int(input('请输入圆盘数:'))
han(n, 'A', 'B', 'C')