【写在前面】可以参看我的视频进行学习,喜欢的话关注,点赞,分享📚 前言
算术运算符是Python编程的基础,掌握它们对于编写高效的代码至关重要。本文将详细介绍Python中的算术运算符,并通过丰富的实例帮助您深入理解。
🔢 Python算术运算符一览表
| 运算符 | 名称 | 描述 | 示例 |
|---|
+ | 加法 | 两个数相加 | 3 + 5 = 8 |
- | 减法 | 两个数相减 | 10 - 4 = 6 |
* | 乘法 | 两个数相乘 | 6 * 7 = 42 |
/ | 除法 | 两个数相除(结果为浮点数) | 15 / 3 = 5.0 |
// | 整除 | 两个数相除,向下取整 | 17 // 5 = 3 |
% | 取模 | 返回除法的余数 | 17 % 5 = 2 |
** | 幂运算 | 返回x的y次幂 | 2 ** 3 = 8 |
📝 详细讲解与实例
1️⃣ 加法运算符 (+)
# 数字相加
a=10
b=20
result=a+b
print(f"{a} + {b} = {result}") # 输出: 10 + 20 = 30
# 字符串拼接(+的特殊用法)
str1="Hello"
str2="World"
print(str1+" "+str2) # 输出: Hello World
2️⃣ 减法运算符 (-)
# 数字相减
x=50
y=30
print(f"{x} - {y} = {x-y}") # 输出: 50 - 30 = 20
# 负数表示
temperature=-5
print(f"当前温度: {temperature}°C") # 输出: 当前温度: -5°C
3️⃣ 乘法运算符 (*)
# 数字相乘
price=25.5
quantity=3
total=price*quantity
print(f"总价: ¥{total}") # 输出: 总价: ¥76.5
# 字符串重复(*的特殊用法)
message="Python"
print(message*3) # 输出: PythonPythonPython
4️⃣ 除法运算符 (/)
# 普通除法(总是返回浮点数)
pizza=8
people=3
per_person=pizza/people
print(f"每人分到: {per_person} 片披萨") # 输出: 每人分到: 2.6666666666666665 片披萨
# 注意:除数不能为0
# print(10 / 0) # 会抛出 ZeroDivisionError
5️⃣ 整除运算符 (//)
# 向下取整除法
total_candies=17
children=5
candies_per_child=total_candies//children
remaining=total_candies%children
print(f"{total_candies}颗糖分给{children}个小朋友:")
print(f"每人分到: {candies_per_child}颗") # 输出: 每人分到: 3颗
print(f"剩余: {remaining}颗") # 输出: 剩余: 2颗
6️⃣ 取模运算符 (%)
# 判断奇偶数
number=27
ifnumber%2==0:
print(f"{number} 是偶数")
else:
print(f"{number} 是奇数") # 输出: 27 是奇数
# 循环计数器
foriinrange(1, 11):
ifi%3==0:
print(f"{i} 是3的倍数")
7️⃣ 幂运算 (**)
# 计算平方
side=5
area=side**2
print(f"边长为{side}的正方形面积: {area}") # 输出: 边长为5的正方形面积: 25
# 计算复利
principal=10000# 本金
rate=0.05# 年利率
years=3# 年数
amount=principal* (1+rate) **years
print(f"{years}年后的金额: ¥{amount:.2f}") # 输出: 3年后的金额: ¥11576.25
🎯 综合实例:简单计算器
defsimple_calculator():
print("=== Python简单计算器 ===")
# 获取用户输入
num1=float(input("请输入第一个数字: "))
operator=input("请选择运算符 (+, -, *, /, //, %, **): ")
num2=float(input("请输入第二个数字: "))
# 执行运算
ifoperator=='+':
result=num1+num2
elifoperator=='-':
result=num1-num2
elifoperator=='*':
result=num1*num2
elifoperator=='/':
ifnum2!=0:
result=num1/num2
else:
print("错误:除数不能为0!")
return
elifoperator=='//':
ifnum2!=0:
result=num1//num2
else:
print("错误:除数不能为0!")
return
elifoperator=='%':
ifnum2!=0:
result=num1%num2
else:
print("错误:除数不能为0!")
return
elifoperator=='**':
result=num1**num2
else:
print("错误:无效的运算符!")
return
print(f"计算结果: {num1}{operator}{num2} = {result}")
# 运行计算器
simple_calculator()
💡 实用技巧
1. 运算符优先级
# 优先级从高到低: ** → *, /, //, % → +, -
expression=2+3*4**2-8/4
print(f"2 + 3 * 4 ** 2 - 8 / 4 = {expression}")
# 计算步骤: 4**2=16 → 3*16=48 → 8/4=2 → 2+48-2=48
2. 复合赋值运算符
# 等同于: count = count + 1
count=10
count+=5# count = 15
count-=3# count = 12
count*=2# count = 24
count//=4# count = 6
count**=2# count = 36
3. 实际应用场景
# 购物车计算
items= [
{"name": "苹果", "price": 5.5, "quantity": 3},
{"name": "香蕉", "price": 3.8, "quantity": 2},
{"name": "橙子", "price": 6.2, "quantity": 4}
]
total=0
foriteminitems:
subtotal=item["price"] *item["quantity"]
total+=subtotal
print(f"{item['name']}: ¥{item['price']} × {item['quantity']} = ¥{subtotal}")
print(f"总计: ¥{total}")
🎓 总结
Python的算术运算符虽然简单,但功能强大。掌握这些基础知识,您就能:
✅ 进行各种数学计算
✅ 处理字符串操作
✅ 编写实用的程序逻辑
✅ 为更复杂的编程概念打下基础
记住:编程是一门实践性很强的技能,多动手练习才能真正掌握!
📱 关注我们
想了解更多Python编程技巧?欢迎关注我们的微信公众号,获取更多编程干货!
本文适合Python初学者,建议收藏保存!