feat: 初始化 Python 零基础教学课程
This commit is contained in:
97
01_python基础/1_4_运算符/operators.py
Normal file
97
01_python基础/1_4_运算符/operators.py
Normal file
@@ -0,0 +1,97 @@
|
||||
# 第 1-4 课示例:运算符
|
||||
#
|
||||
# 本文件按类别演示常用运算符。
|
||||
# 运行时请对照每段注释,观察不同运算符产生的结果和数据类型。
|
||||
|
||||
|
||||
# 一、算术运算符
|
||||
first_number = 17
|
||||
second_number = 7
|
||||
|
||||
print(f"加法:{first_number + second_number}")
|
||||
print(f"减法:{first_number - second_number}")
|
||||
print(f"乘法:{first_number * second_number}")
|
||||
print(f"普通除法:{first_number / second_number}")
|
||||
print(f"整除:{first_number // second_number}")
|
||||
print(f"取余:{first_number % second_number}")
|
||||
print(f"幂运算:{2 ** 3}")
|
||||
|
||||
|
||||
# 二、整除与取余的实际含义
|
||||
# 17 天中包含 2 个完整星期,另外还剩 3 天。
|
||||
learning_days = 17
|
||||
full_weeks = learning_days // 7
|
||||
remaining_days = learning_days % 7
|
||||
|
||||
print(f"{learning_days} 天等于 {full_weeks} 个完整星期加 {remaining_days} 天。")
|
||||
|
||||
|
||||
# 三、字符串支持的部分运算
|
||||
# 加号连接字符串,乘号让字符串重复指定次数。
|
||||
role_name = "Agent" + " " + "开发者"
|
||||
separator = "=" * 20
|
||||
|
||||
print(role_name)
|
||||
print(separator)
|
||||
|
||||
|
||||
# 四、比较运算符
|
||||
completed_hours = 12
|
||||
target_hours = 10
|
||||
|
||||
print(f"是否等于目标:{completed_hours == target_hours}")
|
||||
print(f"是否不等于目标:{completed_hours != target_hours}")
|
||||
print(f"是否超过目标:{completed_hours > target_hours}")
|
||||
print(f"是否少于目标:{completed_hours < target_hours}")
|
||||
print(f"是否达到目标:{completed_hours >= target_hours}")
|
||||
print(f"是否未超过目标:{completed_hours <= target_hours}")
|
||||
|
||||
|
||||
# 五、复合赋值运算符
|
||||
# completed_tasks += 1 等价于 completed_tasks = completed_tasks + 1。
|
||||
completed_tasks = 3
|
||||
completed_tasks += 1
|
||||
print(f"新增一个完成项后:{completed_tasks}")
|
||||
|
||||
# 下面把预计时间扩大为原来的两倍。
|
||||
estimated_hours = 5
|
||||
estimated_hours *= 2
|
||||
print(f"调整后的预计时间:{estimated_hours}")
|
||||
|
||||
|
||||
# 六、逻辑运算符
|
||||
course_finished = True
|
||||
practice_finished = True
|
||||
uses_pycharm = True
|
||||
uses_vscode = False
|
||||
|
||||
can_start_next_course = course_finished and practice_finished
|
||||
has_code_editor = uses_pycharm or uses_vscode
|
||||
is_still_learning = not course_finished
|
||||
|
||||
print(f"课程和练习是否都完成:{can_start_next_course}")
|
||||
print(f"是否至少使用一种编辑器:{has_code_editor}")
|
||||
print(f"是否仍未完成课程:{is_still_learning}")
|
||||
|
||||
|
||||
# 七、比较运算与逻辑运算组合
|
||||
daily_hours = 2.5
|
||||
completed_tasks = 4
|
||||
|
||||
is_plan_healthy = (daily_hours >= 1) and (completed_tasks >= 3)
|
||||
print(f"学习时长和任务数是否同时达标:{is_plan_healthy}")
|
||||
|
||||
|
||||
# 八、运算优先级
|
||||
# 第一个结果先计算乘法;第二个结果先计算括号中的加法。
|
||||
result_without_parentheses = 2 + 3 * 4
|
||||
result_with_parentheses = (2 + 3) * 4
|
||||
|
||||
print(f"没有括号:{result_without_parentheses}")
|
||||
print(f"使用括号:{result_with_parentheses}")
|
||||
|
||||
|
||||
# 九、观察浮点数精度现象
|
||||
# 这个结果说明部分十进制小数无法用二进制浮点数精确表示。
|
||||
floating_point_result = 0.1 + 0.2
|
||||
print(f"0.1 + 0.2 的结果:{floating_point_result}")
|
||||
Reference in New Issue
Block a user