0% found this document useful (0 votes)
27 views2 pages

Python Basics Notes Depth

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Python Basics Notes Depth

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

标题: Python 编程基础笔记

课程: 编程入门
日期: 2025 年 3 月
目标: 掌握 Python 基础并完成小型项目
字数: 约 1000 字

### 第一部分:基础语法
- **变量与数据类型**:
- 整数:x = 10
- 浮点数:y = 3.14
- 字符串:name = "Python"
- 列表:numbers = [1, 2, 3]
- **运算符**:
- 算术:+、-、*、/、%(取余)。
- 比较:>、<、==、!=。
- **输入输出**:
- 输出:print("Hello")
- 输入:name = input("请输入姓名:")

### 第二部分:控制结构
1. **条件语句**:
- 语法:
if x > 0:
print("正数")
elif x == 0:
print("零")
else:
print("负数")
- 练习:判断年份是否为闰年。
year = 2024
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("闰年")
else:
print("平年")
2. **循环**:
- For 循环:
for i in range(1, 6):
print(i) # 输出 1 到 5
- While 循环:
count = 0
while count < 5:
print(count)
count += 1

### 第三部分:函数
- **定义**:
def greet(name):
return f"你好,{name}!"
print(greet("Alice")) # 输出:你好,Alice!
- **参数**:
- 默认参数:def add(a, b=5): return a + b
- 调用:add(3) 输出 8,add(3, 4) 输出 7。

### 第四部分:项目 - 简单计算器


- **任务**:实现加减乘除功能。
- **代码**:
def calculator():
num1 = float(input("输入第一个数:"))
op = input("输入运算符(+,-,*,/):")
num2 = float(input("输入第二个数:"))
if op == "+":
result = num1 + num2
elif op == "-":
result = num1 - num2
elif op == "*":
result = num1 * num2
elif op == "/":
result = num1 / num2 if num2 != 0 else "除数不能为 0"
else:
result = "无效运算符"
print("结果:", result)
calculator()
- **扩展**:添加错误处理、循环运行。

### 总结
Python 简单强大,适合初学者。通过练习条件、循环和函数,可为数据分析、AI 开发打基
础。推荐资源:Python 官方文档。

You might also like