# #两个数字之和标签:01,python,list1,sum,list,number,item,print From: https://www.cnblogs.com/baibiaotong/p/16917189.html
number1 = 2.5
number2 = 3.6
sum = number1 + number2
print (f"{number1} + {number2} == {sum}")
# #数字的阶乘
def get_jiecheng(number):
result = 1
while number > 0:
result *= number
number -=1
return result
print("jiecheng 6 = ",get_jiecheng(6))
print("jiecheng 5 = ",get_jiecheng(5))
print("jiecheng 80 = ",get_jiecheng(80))
#求园的面积
import math
def computer_area_of_circel(r):
return round(math.pi * r * r,2)
print("area of 5 is: ",computer_area_of_circel(5))
print("area of 2.5 is: ",computer_area_of_circel(2.5))
print("area of 5.6 is: ",computer_area_of_circel(5.6))
#区间内所有素数 一个数字只能被自己和1整除
def is_prime(number):
if number in(1,2):
return True
for idx in range (2,number):
if number % idx == 0:
return False
return True
def print_primes(begin,end):
for number in range(begin,end+1):
if is_prime(number):
print(f"{number} is a prime")
begin = 1
end = 8
print_primes(begin,end)
#求前n个数字的平方和
def sum_of_square(n):
result = 0
for number in range(1,n+1):
result += number * number
return result
print("number 3 square is:",sum_of_square(3))
print("number 5 square is:",sum_of_square(5))
print("number 8 square is:",sum_of_square(8))
#列表数字之和
def sum_of_list(parm_list):
total = 0
for item in parm_list:
total += item
return total
list1 = {1,2,5,8}
list2 = {15,85,95,865}
print(f"sum of {list1}:",sum_of_list(list1) )
print(f"sum of {list2}:",sum_of_list(list2) )
print(f"sum of {list1}:",sum(list1) ) #sum函数一步到位
print(f"sum of {list2}:",sum(list2) )
#计算数字范围内所有偶数
def get_even_number(begin,end):
result = []
for item in range(begin,end+1):
if item % 2 ==0:
result.append(item)
return result
begin = 12
end = 17
print(f"begin={begin},end={end},even number",get_even_number(begin,end))
date = [item for item in range(begin,end+1) if item % 2 == 0] #date函数一步到位
print(f"begin={begin},end={end},even number",date)
#移除列表中的多个元素
def remove_elements_from_list(list1,list2):
for item in list2:
list1.remove(item)
return list1
list1 = [45,5,6,85,99]
list2 = [5,6]
print(f"from {list1} remove {list2 },result",remove_elements_from_list(list1,list2))
list1 = [45,5,6,85,99]
list2 = [5,6]
date = [item for item in list1 if item not in list2]
print(f"from {list1} remove {list2 },result",date)
#对列表元素去重
def get_unique_list(list1):
result = []
for item in list1:
if item not in result:
result.append(item)
return result
list1 = [1,2,3,3,5]
print(f"source list {list1},unique list:",get_unique_list(list1))
print(f"source list {list1},unique list:",list(set(list1))) #set 函数是没有重复元素的 想转换set为list 则需要在 set 前加上一个列表list。
#怎么对简单列表元素进行排序
list1 = [1,2,56,4,5,6,878]
#list2 = [kk,jj,h,u,t]
list1.sort(reverse = True ) #list1.sort 函数一样包括判断条件reverse = Ture 来进行升序或者降序的判断。
list3 = sorted(list1,reverse = True ) # sorted 函数中列表后可以加判断reverse = True 或者是False 来进行升序或者降序排列。
print(f"list1 is {list1}")
print(f"list1 is {list3}")
#实现学生成绩排序:lambda 函数做字典排序 用到一个 x:X 第一个x代表字典里面的一个列表,后面一个x代表当前列表中的一个可以用作比较的参数值作为比较参数,在元组列表中,可以把x : x 比作x0:xn这样的顺序去对列表中的元素进行排序。
#sort_student_list
student = [
{"sno":101,"name":"小张","sgrade":88},
{"sno":102,"name":"小王","sgrade":95},
{"sno":103,"name":"小李","sgrade":66},
{"sno":104,"name":"小赵","sgrade":55},
]
student_sort = sorted(student,
key=lambda x : x["sgrade"],
reverse=True)
print(f"source {student},"
f"sort result:{student_sort}")
#读取成绩文件排序数据:
def read_file():
result = []
with open("./student_grade_input.txt",encoding="utf-8") as fin:
for line in fin:
line = line[:-1]
result.append(line.split(","))
return result
def sort_grades(dates):
return sorted(datas,
key=lambda x: int(x[2]),
reverse =True )
def write_file(datas):
with open("./student_grade_output.txt","w",encoding="utf-8") as fout:
for data in datas:
fout.write(",".join(data) + "\n")
# 读取student_grade_input.txt文本文件内容
datas = read_file()
print("read_file datas:",datas)
# 对读取出来的内容可以进行排序
datas = sort_grades(datas)
print("sort_grades datas:",datas)
# #对排序整理过的文件我们进行输出到新的文本文档保存
write_file(datas)