AI时代学习python-使用ai工具

AI时代,善于使用ai会大大加速学习效率,从今天起我将用字节的IDE trace来记录学习python的过程。

我在ide中发送如下提示词,他自动生成了第一课的内容。

我现在要学习python以便后续开发大模型相关,我需要你充当一个老师的角色,详细的安排课程通过与我互动的方式学习,按照lesson1往后安排每一天的课程,我希望周期在半个月掌握python相关知识,现在你可以安排第一天的课程了。你需要具备老师以及工程师的开发思维帮助我。

Python学习课程 - Lesson 1

主题:Python环境搭建与基础语法

目标:掌握Python基本语法,能够编写简单的Python程序

-------------------------------

1. Python环境搭建

-------------------------------

1.1 下载安装Python

- 访问 https://www.python.org/downloads/

- 选择适合你操作系统的最新版本(推荐3.10+)

- 安装时勾选"Add Python to PATH"

1.2 验证安装

- 打开命令提示符(Windows)或终端(Mac/Linux)

- 输入:python --version

- 如果显示版本号,说明安装成功

1.3 运行Python程序的方式

- 交互式模式:在命令行输入python进入,直接输入代码执行

- 脚本模式:将代码保存为.py文件,然后用python filename.py执行

-------------------------------

2. 基础语法

-------------------------------

2.1 注释

使用#表示单行注释

多行注释使用三个单引号或双引号

2.2 变量与数据类型

变量不需要声明类型,直接赋值即可

name = "张三" # 字符串
age = 25 # 整数
height = 1.75 # 浮点数
is_student = True # 布尔值

查看变量类型

print(type(name)) # <class 'str'>
print(type(age)) # <class 'int'>
print(type(height)) # <class 'float'>
print(type(is_student)) # <class 'bool'>

2.3 基本运算

算术运算

a = 10
b = 3
print(a + b) # 加法 13
print(a - b) # 减法 7
print(a * b) # 乘法 30
print(a / b) # 除法 3.333...
print(a // b) # 整除 3
print(a % b) # 取模 1
print(a ** b) # 幂运算 1000

比较运算

print(a > b) # True
print(a < b) # False
print(a == b) # False
print(a != b) # True

逻辑运算

x = True
y = False
print(x and y) # False
print(x or y) # True
print(not x) # False

-------------------------------

3. 控制流

-------------------------------

3.1 条件语句

age = 18
if age >= 18:
print("成年人")
elif age >= 13:
print("青少年")
else:
print("儿童")

3.2 循环语句

for循环 - 遍历序列

fruits = ["苹果", "香蕉", "橙子"]
for fruit in fruits:
print(fruit)

for循环 - 使用range

for i in range(5):
print(i) # 0, 1, 2, 3, 4

while循环

count = 0
while count < 3:
print(f"计数: {count}")
count += 1

break和continue

for i in range(10):
if i == 3:
continue # 跳过当前循环
if i == 7:
break # 终止循环
print(i)

-------------------------------

4. 函数

-------------------------------

定义函数

def greet(name):
"""这是函数文档字符串(docstring),用于描述函数功能"""
return f"你好,{name}!"

调用函数

result = greet("李四")
print(result)

带默认参数的函数

def add(a, b=10):
return a + b

print(add(5)) # 15
print(add(5, 20)) # 25

-------------------------------

5. 基本数据结构 - 列表

-------------------------------

创建列表

numbers = [1, 2, 3, 4, 5]

访问元素

print(numbers[0]) # 第一个元素 1
print(numbers[-1]) # 最后一个元素 5

切片操作

print(numbers[1:3]) # [2, 3]
print(numbers[:3]) # [1, 2, 3]
print(numbers[3:]) # [4, 5]

修改元素

numbers[0] = 10
print(numbers) # [10, 2, 3, 4, 5]

列表方法

numbers.append(6) # 添加元素到末尾
print(numbers) # [10, 2, 3, 4, 5, 6]
numbers.insert(1, 15) # 在指定位置插入
print(numbers) # [10, 15, 2, 3, 4, 5, 6]
numbers.remove(3) # 删除指定值的元素
print(numbers) # [10, 15, 2, 4, 5, 6]
popped = numbers.pop() # 移除并返回末尾元素
print(popped) # 6
print(numbers) # [10, 15, 2, 4, 5]

-------------------------------

6. 基本数据结构 - 字典

-------------------------------

创建字典

person = {
"name": "张三",
"age": 25,
"city": "北京"
}

访问元素

print(person["name"])

修改元素

person["age"] = 26

添加新键值对

person["email"] = "zhangsan@example.com"

字典方法

print(person.keys()) # 获取所有键
print(person.values()) # 获取所有值
print(person.items()) # 获取所有键值对

遍历字典

for key, value in person.items():
print(f"{key}: {value}")

-------------------------------

7. 实践练习

-------------------------------

练习1:编写一个函数,计算两个数的和与差

要求:函数接收两个参数,返回一个包含和与差的元组

def calculate_sum_diff(a, b):
# 在这里编写你的代码
pass

练习2:编写一个程序,统计列表中正数的个数

例如:numbers = [-1, 2, -3, 4, 5],输出:3

numbers = [-1, 2, -3, 4, 5]

在这里编写你的代码

练习3:编写一个程序,将字典中的键值对反转

例如:{'a': 1, 'b': 2} →

my_dict = {'a': 1, 'b': 2, 'c': 3}

在这里编写你的代码

-------------------------------

8. 作业

-------------------------------

1. 完成上面的三个练习

2. 编写一个程序,输入一个字符串,统计其中每个字符出现的次数(使用字典)

3. 预习第二天的内容:字符串处理、文件操作

-------------------------------

明天预告:

Lesson 2 - 字符串处理与文件操作

学习Python字符串的各种操作方法,以及如何读写文件

posted @ 2026-01-08 10:15  岚岚1990  阅读(2)  评论(0)    收藏  举报