08_python_练习题——乘法表

本文介绍了一个简单的Python程序,用于打印9*9乘法口诀表。通过双重循环实现,外层循环控制行数,内层循环控制列数。每行输出格式整齐,便于阅读。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#题目:输出9*9乘法口诀表。

#程序分析:分行与列考虑,共9行9列,i控制行,j控制列。


# _*_ coding:utf-8 _*_

#这两行只能直接输出最终结果
#a=[k * l for k in range (1,10) for l in range(1,10)]
#print a
for i in range(1,10): 
	for j in range(1,10): 
		result = i*j 
		print '%d * %d = %d\t' % (i,j,result),
 print ''




### Python 函数编程练习题 #### 定义简单函数 创建一个简单的加法器函数,接收两个参数 `num1` 和 `num2` 并返回其和。这有助于理解如何定义带有多个参数的函数以及执行基本运算[^3]。 ```python def add_numbers(num1, num2): """计算两个数相加""" result = num1 + num2 return result print(add_numbers(5, 7)) ``` #### 使用默认参数值 编写一个具有默认参数值的函数来展示当未提供某些参数时程序的行为。此功能允许更灵活地调用函数而无需总是指定所有可能的输入项。 ```python def greet(name="Guest"): """向用户提供问候,默认称呼为'Guest'""" print(f"Hello {name}!") greet() # 输出 Hello Guest! greet("Alice") # 输出 Hello Alice! ``` #### 实现带列表推导式的成本计算器 构建一个用于处理商品价格信息的成本计算器,它接受包含单位价格和数量的信息元组列表作为输入,并利用列表推导式快速完成总价计算工作[^4]。 ```python def calculate_total_cost(info_list): """ 计算总费用 :param info_list: 商品详情表[(名称, 单价字符串, 数量字符串), ...] :return: 总金额浮点数值 """ total = sum(float(unit_price) * int(count) for _, unit_price, count in info_list) return round(total, 2) items = [("apple", "2.5", "3"), ("banana", ".8", "6")] print(calculate_total_cost(items)) # 结果应接近于 9.30 ``` #### 创建高阶函数 尝试实现更高层次的功能——即能够操作其他函数或将它们作为参数传递给另一个函数的能力。这里给出的例子是一个名为 `apply_operation` 的通用处理器,它可以应用于任何二元运算符函数上[^2]。 ```python from operator import mul, truediv def apply_operation(func, a, b): """应用给定的操作到两个数字上""" try: outcome = func(a, b) return f"The operation's result is {outcome}" except Exception as e: return str(e) # 测试乘法与除法 print(apply_operation(mul, 4, 5)) # The operation's result is 20 print(apply_operation(truediv, 10, 2)) # The operation's result is 5.0 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值