@@ -545,7 +545,7 @@ print(f"四舍五入: {round(num, 2)}")
545545
546546## 随机数
547547
548- 在 Web 开发中经常需要生成随机数, JavaScript 使用` Math.random() ` :
548+ JavaScript 使用 ` Math.random() ` 生成 0-1 之间的随机浮点数,然后通过数学运算得到需要的范围 :
549549
550550``` javascript runner
551551// JavaScript 随机数
@@ -568,14 +568,22 @@ let j = Math.floor(Math.random() * numbers.length);
568568console .log (" 打乱后:" , numbers);
569569```
570570
571- Python 使用 random 模块:
571+ Python 的 random 模块提供了更丰富和便利的随机数函数:
572+
573+ * ** ` random.random() ` ** - 与 JavaScript 相同,生成 0-1 之间的随机浮点数
574+ * ** ` random.randint(a, b) ` ** - 生成 a 到 b 之间的随机整数,** 两端都包含**
575+ * ** ` random.randrange(start, stop) ` ** - 生成 start 到 stop 之间的随机整数,** 含左不含右** (类似 ` range() ` 函数)
576+ * ** ` random.uniform(a, b) ` ** - 生成 a 到 b 之间的随机浮点数
577+ * ** ` random.choice(seq) ` ** - 从序列中随机选择一个元素
578+ * ** ` random.shuffle(list) ` ** - 随机打乱列表(原地修改)
572579
573580``` python runner
574581import random
575582
576583print (" === 随机数生成 ===" )
577584print (f " 0-1 之间: { random.random():.4f } " )
578- print (f " 1-10 之间: { random.randint(1 , 10 )} " )
585+ print (f " 1-10 之间: { random.randint(1 , 10 )} " ) # 包含 1 和 10
586+ print (f " 1-9 之间: { random.randrange(1 , 10 )} " ) # 包含 1,不包含 10
579587print (f " 浮点数 (1-10): { random.uniform(1 , 10 ):.2f } " )
580588
581589# 从列表中随机选择
@@ -671,21 +679,11 @@ print(f"float('nan') 是 NaN: {math.isnan(float('nan'))}")
671679| 乘除赋值 | ` *= ` ` /= ` | ` *= ` ` /= ` |
672680| 幂运算赋值 | ` **= ` | ` **= ` |
673681| 取余赋值 | ` %= ` | ` %= ` |
674- | 整除赋值 | ` Math.floor(x / y) ` | ` x //= y ` |
675- | ** 数学函数** |
676- | 绝对值 | ` Math.abs(x) ` | ` abs(x) ` |
677- | 平方根 | ` Math.sqrt(x) ` | ` math.sqrt(x) ` |
678- | 向上取整 | ` Math.ceil(x) ` | ` math.ceil(x) ` |
679- | 向下取整 | ` Math.floor(x) ` | ` math.floor(x) ` |
680- | 四舍五入 | ` Math.round(x) ` | ` round(x, 位数) ` |
682+ | 整除赋值 | ` x = Math.floor(x / y) ` | ` x //= y ` |
683+ | ** 统计函数** |
681684| 最大值 | ` Math.max(a, b, c) ` | ` max(a, b, c) ` |
682685| 最小值 | ` Math.min(a, b, c) ` | ` min(a, b, c) ` |
683- | 三角函数 | ` Math.sin(x) ` ` Math.cos(x) ` | ` math.sin(x) ` ` math.cos(x) ` |
684- | 对数 | ` Math.log(x) ` ` Math.log10(x) ` | ` math.log(x) ` ` math.log10(x) ` |
685- | 指数 | ` Math.exp(x) ` | ` math.exp(x) ` |
686- | ** 常数** |
687- | 圆周率 | ` Math.PI ` | ` math.pi ` |
688- | 自然常数 | ` Math.E ` | ` math.e ` |
686+ | 求和 | ` array.reduce((a,b)=>a+b, 0) ` | ` sum(iterable) ` |
689687| ** 特殊值** |
690688| 正无穷 | ` Infinity ` | ` float('inf') ` |
691689| 负无穷 | ` -Infinity ` | ` float('-inf') ` |
@@ -712,7 +710,18 @@ print(f"float('nan') 是 NaN: {math.isnan(float('nan'))}")
712710| 转八进制 | ` num.toString(8) ` | ` oct(num)[2:] ` |
713711| 转十六进制 | ` num.toString(16) ` | ` hex(num)[2:] ` |
714712| 解析进制 | ` parseInt(str, base) ` | ` int(str, base) ` |
715-
713+ | ** 数学函数** |
714+ | 绝对值 | ` Math.abs(x) ` | ` abs(x) ` |
715+ | 平方根 | ` Math.sqrt(x) ` | ` math.sqrt(x) ` |
716+ | 向上取整 | ` Math.ceil(x) ` | ` math.ceil(x) ` |
717+ | 向下取整 | ` Math.floor(x) ` | ` math.floor(x) ` |
718+ | 四舍五入 | ` Math.round(x) ` | ` round(x, 位数) ` |
719+ | 三角函数 | ` Math.sin(x) ` ` Math.cos(x) ` | ` math.sin(x) ` ` math.cos(x) ` |
720+ | 对数函数 | ` Math.log(x) ` ` Math.log10(x) ` | ` math.log(x) ` ` math.log10(x) ` |
721+ | 指数函数 | ` Math.exp(x) ` | ` math.exp(x) ` |
722+ | ** 常数** |
723+ | 圆周率 | ` Math.PI ` | ` math.pi ` |
724+ | 自然常数 | ` Math.E ` | ` math.e ` |
716725## 小结
717726
718727Python 的数值系统相比 JavaScript 更加丰富和精确:
@@ -729,50 +738,80 @@ Python 的数值系统相比 JavaScript 更加丰富和精确:
729738
730739## 练习
731740
732- 完成购物价格计算系统,输出详细的计算结果 :
741+ 完成一个餐厅订单计算系统,练习数字运算和格式化 :
733742
734743```
735- === 购物计算器 ===
736- 商品单价: ¥29.99
737- 购买数量: 3 件
738- 商品总价: ¥89.97
739- 折扣金额: ¥9.00 (10% 折扣)
740- 最终价格: ¥80.97
741- === 支付计算 ===
742- 每人平均: ¥26.99 (3人分摊)
743- 找零金额: ¥19.03 (支付 ¥100)
744- 积分奖励: 80 分 (1元=1分)
745- === 数据分析 ===
746- 价格平方根: 5.48
747- 税前价格: ¥73.61 (税率10%)
748- 随机抽奖号: 8847
744+ === 餐厅订单计算系统 ===
745+ 商品价格: [28.5, 32.8, 45.0]
746+ 商品名称: ["牛肉面", "宫保鸡丁", "红烧肉"]
747+
748+ === 价格统计 ===
749+ 最贵菜品: ¥45.00
750+ 最便宜菜品: ¥28.50
751+ 总计金额: ¥106.30
752+ 平均价格: ¥35.43
753+
754+ === 优惠计算 ===
755+ 满100减10元: ¥96.30
756+ 服务费 (10%): ¥9.63
757+ 最终金额: ¥105.93
758+
759+ === 支付处理 ===
760+ 向上取整金额: ¥106
761+ 向下取整金额: ¥105
762+ 四舍五入金额: ¥106
763+ 2人分摊: 每人 ¥52.97
764+
765+ === 随机活动 ===
766+ 随机折扣: 85%
767+ 折扣后金额: ¥90.04
768+ 抽奖编号: 7392
749769```
750770
751771``` python runner
752- # 商品信息:单价29.99元,购买3件,9折优惠,3人分摊,支付100元
772+ import math
773+ import random
774+
775+ # 商品数据
776+ prices = [28.5 , 32.8 , 45.0 ]
777+ items = [" 牛肉面" , " 宫保鸡丁" , " 红烧肉" ]
778+
779+ print (" === 餐厅订单计算系统 ===" )
780+ print (f " 商品价格: { prices} " )
781+ print (f " 商品名称: { items} " )
782+
783+ print (" \n === 价格统计 ===" )
784+ # 1. 使用内置函数计算价格统计
785+
786+
787+ # 2. 计算总金额和平均价格
788+
753789
754- # 1. 创建基本数据并导入必要模块
790+ print (" \n === 优惠计算 ===" )
791+ # 3. 满100减10元优惠
755792
756793
757- # 2. 计算原价、折扣后价格
794+ # 4. 计算10%服务费
758795
759796
760- # 3. 计算人均分摊金额
797+ # 5. 计算最终金额
761798
762799
763- # 4. 计算找零
800+ print (" \n === 支付处理 ===" )
801+ # 6. 使用不同取整方式处理金额
764802
765803
766- # 5. 计算积分奖励
804+ # 7. 计算2人分摊金额
767805
768806
769- # 6. 计算价格平方根
807+ print (" \n === 随机活动 ===" )
808+ # 8. 生成随机折扣 (75%-95%,整数百分比)
770809
771810
772- # 7. 反推税前价格(假设现价含10%税)
811+ # 9. 计算折扣后金额
773812
774813
775- # 8. 生成随机抽奖号
814+ # 10. 生成4位随机抽奖编号
776815
777816
778817```
@@ -782,51 +821,64 @@ Python 的数值系统相比 JavaScript 更加丰富和精确:
782821import math
783822import random
784823
785- # 1. 创建基本数据并导入必要模块
786- unit_price = 29.99
787- quantity = 3
788- discount_rate = 0.9 # 9折
789- people_count = 3
790- payment = 100
791-
792- print (" === 购物计算器 ===" )
793- print (f " 商品单价: ¥ { unit_price} " )
794- print (f " 购买数量: { quantity} 件 " )
795-
796- # 2. 计算原价、折扣后价格
797- original_total = unit_price * quantity
798- discount_amount = original_total * (1 - discount_rate)
799- final_price = original_total - discount_amount
800-
801- print (f " 商品总价: ¥ { original_total:.2f } " )
802- print (f " 折扣金额: ¥ { discount_amount:.2f } ( { int ((1 - discount_rate)* 100 )} % 折扣) " )
803- print (f " 最终价格: ¥ { final_price:.2f } " )
804-
805- print (" === 支付计算 ===" )
806- # 3. 计算人均分摊金额
807- per_person = final_price / people_count
808- print (f " 每人平均: ¥ { per_person:.2f } ( { people_count} 人分摊) " )
809-
810- # 4. 计算找零
811- change = payment - final_price
812- print (f " 找零金额: ¥ { change:.2f } (支付 ¥ { payment} ) " )
813-
814- # 5. 计算积分奖励
815- points = int (final_price) # 取整数部分
816- print (f " 积分奖励: { points} 分 (1元=1分) " )
817-
818- print (" === 数据分析 ===" )
819- # 6. 计算价格平方根
820- price_sqrt = math.sqrt(final_price)
821- print (f " 价格平方根: { price_sqrt:.2f } " )
822-
823- # 7. 反推税前价格
824- tax_rate = 0.1
825- price_before_tax = final_price / (1 + tax_rate)
826- print (f " 税前价格: ¥ { price_before_tax:.2f } (税率 { int (tax_rate* 100 )} %) " )
827-
828- # 8. 生成随机抽奖号
829- lottery_num = random.randint(1000 , 9999 )
830- print (f " 随机抽奖号: { lottery_num} " )
824+ # 商品数据
825+ prices = [28.5 , 32.8 , 45.0 ]
826+ items = [" 牛肉面" , " 宫保鸡丁" , " 红烧肉" ]
827+
828+ print (" === 餐厅订单计算系统 ===" )
829+ print (f " 商品价格: { prices} " )
830+ print (f " 商品名称: { items} " )
831+
832+ print (" \n === 价格统计 ===" )
833+ # 1. 使用内置函数计算价格统计
834+ max_price = max (prices)
835+ min_price = min (prices)
836+ print (f " 最贵菜品: ¥ { max_price:.2f } " )
837+ print (f " 最便宜菜品: ¥ { min_price:.2f } " )
838+
839+ # 2. 计算总金额和平均价格
840+ total_amount = sum (prices)
841+ avg_price = total_amount / len (prices)
842+ print (f " 总计金额: ¥ { total_amount:.2f } " )
843+ print (f " 平均价格: ¥ { avg_price:.2f } " )
844+
845+ print (" \n === 优惠计算 ===" )
846+ # 3. 满100减10元优惠
847+ discounted_amount = total_amount - 10
848+ print (f " 满100减10元: ¥ { discounted_amount:.2f } " )
849+
850+ # 4. 计算10%服务费
851+ service_fee = discounted_amount * 0.1
852+ print (f " 服务费 (10%): ¥ { service_fee:.2f } " )
853+
854+ # 5. 计算最终金额
855+ final_amount = discounted_amount + service_fee
856+ print (f " 最终金额: ¥ { final_amount:.2f } " )
857+
858+ print (" \n === 支付处理 ===" )
859+ # 6. 使用不同取整方式处理金额
860+ ceil_amount = math.ceil(final_amount)
861+ floor_amount = math.floor(final_amount)
862+ round_amount = round (final_amount)
863+ print (f " 向上取整金额: ¥ { ceil_amount} " )
864+ print (f " 向下取整金额: ¥ { floor_amount} " )
865+ print (f " 四舍五入金额: ¥ { round_amount} " )
866+
867+ # 7. 计算2人分摊金额
868+ per_person = final_amount / 2
869+ print (f " 2人分摊: 每人 ¥ { per_person:.2f } " )
870+
871+ print (" \n === 随机活动 ===" )
872+ # 8. 生成随机折扣 (75%-95%,整数百分比)
873+ discount_rate = random.randint(75 , 95 )
874+ print (f " 随机折扣: { discount_rate} % " )
875+
876+ # 9. 计算折扣后金额
877+ discounted_final = final_amount * (discount_rate / 100 )
878+ print (f " 折扣后金额: ¥ { discounted_final:.2f } " )
879+
880+ # 10. 生成4位随机抽奖编号
881+ lottery_number = random.randint(1000 , 9999 )
882+ print (f " 抽奖编号: { lottery_number} " )
831883```
832884:::
0 commit comments