正整数的因子展开式
【题目描述】
编写程序,输出一个给定正整数x(x>1)的质因子展开式。
num = int(input()) newnum = num text = "" counter = 2 while counter * counter <= newnum: if newnum % counter == 0: # 判断是否能够整除2 text = text + str(counter) # 将质因子组合起来 newnum = int(newnum / counter) else: counter += 1 if newnum != 1: # 如果结果不为1,就加上目前的newnum质因子 text = text + str(newnum) if text == "" + str(newnum): # 判断质因子就是其本身 text = str(newnum) print(str(num) + "=" + text)

牛顿迭代法
【题目描述】
编写程序,使用牛顿迭代法求方程在x附近的一个实根。
num = input() n1 = num.split(" ") n = [] for i in n1[::]: if i == '': n1.remove(i) for i in n1: n.append(float(i)) def f(x): return n[0] * pow(x, 3) + n[1] * pow(x, 2) + n[2] * pow(x, 1) + n[3] def fd(x): return 3 * n[0] * pow(x, 2) + 2 * n[1] * pow(x, 1) + n[2] def newtonMethod(assum): x = assum a = f(x) b = fd(x) if f(x) == 0.0: print(round(x, 2)) return x else: next = x - a / b # print('next x = ' + str(next)) # 输出下一条切线的值 if a - f(next) < 1e-6: print(round(next, 2)) # 设置跳出条件,同时输出满足f(x) = 0 的x的值 else: return newtonMethod(next) # 递归 newtonMethod(n[4])

字符串和元组属于不可变数据,即创建后不可修改。列表、字典、集合属于可变数据,即创建后可以修改元素。列表使用eval()或list()或中括号[]进行创建,元素之间使用逗号分隔。若不添加任何元素,则创建空列表。
# 使用[]创建
list_ = [1, 2, 3, 4, 5]
print(type(list_)) # <class 'list'>
# 使用eval()创建,eval()方法用来执行一个字符串表达式,并返回表达式的值
list_ = eval("[1,2,3,4,5]")
print(type(list_)) # <class 'list'>
# 使用list()创建,list()方法用于将元组转换为列表
list_ = list((1, 2, 3, 4, 5))
print(type(list_)) # <class 'list'>
元组使用eval()或tuple()或小括号()进行创建,元素之间使用逗号分隔。若不添加任何元素,则创建空元组。如果元组只有一个元素,则必须在这个元素后面加上逗号。
# 使用()创建
tuple_ = (1, 2, 3, 4, 5)
print(type(tuple_)) # <class 'tuple'>
# 使用eval()创建
tuple_ = eval("(1,2,3,4,5)")
print(type(tuple_)) # <class 'tuple'>
# 使用tuple()创建,tuple()函数用于将列表转换为元组
tuple_ = tuple([1, 2, 3, 4, 5])
print(type(tuple_)) # <class 'tuple'>
字典使用eval()或dict()函数或者大括号{}创建,元素之间用逗号分隔。每个元素必须是一个键(key)值(value)对。若不添加任何元素,则创建空字典。
# 使用{}创建
dict_ = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
print(type(dict_)) # <class 'dict'>
# 使用eval()创建
dict_ = eval("{'a':1,'b':2,'c':3,'d':4,'e':5}")
print(type(dict_)) # <class 'dict'>
# 使用dict()创建
dict_ = dict(a=1, b=2, c=3, d=4, e=5) # 传入关键字方式来构造字典
print(type(dict_)) # <class 'dict'>
dict_ = dict(zip(['a', 'b', 'c', 'd', 'e'], [1, 2, 3, 4, 5])) # 映射函数方式来构造字典
print(type(dict_)) # <class 'dict'>
dict_ = dict([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]) # 可迭代对象方式来构造字典
print(type(dict_)) # <class 'dict'>
集合使用eval()或set()或{}进行创建,元素之间使用逗号分隔。由于{}用于创建空字典,所以只能通过不传参的set()来创建空集合。
# 使用{}创建
set_ = {1, 2, 3, 4, 5}
print(type(set_)) # <class 'set'>
# 使用eval()创建
set_ = eval("{1,2,3,4,5}")
print(type(set_)) # <class 'set'>
# 使用set()创建,参数为可迭代对象
set_ = set("python")
print(type(set_)) # <class 'set'>
字符串使用str()或单引号''或双引号""进行创建,引号内元素为一个整体,不需要使用分隔符。若引号内不添加字符或str()不传参,则创建空字符串
# 使用引号创建
str_ = '12345'
print(type(str_)) # <class 'str'>
# 使用str()创建
str_ = str(12345)
print(type(str_)) # <class 'str'>
列表内的元素可以为任意类型。元素之间可以重复。元素顺序遵循创建时的顺序。
list_ = [1, 2.5, 1, "a", ["b"], ("c",), {"d": "e"}, {"f"}]
print(list_) # [1, 2.5, 1, 'a', ['b'], ('c',), {'d': 'e'}, {'f'}]
元组内的元素可以为任意类型。元素之间可以重复。元素顺序遵循创建时的顺序。
tuple_ = (1, 2.5, 1, "a", ["b"], ("c",), {"d": "e"}, {"f"})
print(tuple_) # (1, 2.5, 1, 'a', ['b'], ('c',), {'d': 'e'}, {'f'})
字典中的键只能为数字、字符串、元组类型,值可以为任意类型。键(key)是唯一的,可以多次赋值(value)。若对应的键多次赋值,则后面赋的值会覆盖前面赋的值。元素顺序遵循创建时的顺序。
浙公网安备 33010602011771号