一个初学者的第一手学习记录

hello,大家好!今天是我学习Python的第一天,收获满满,赶紧记录下来和大家分享!

📝 为什么选择Python?

之前也接触过JavaScript,但Python给我的感觉完全不一样。JS只有number类型,没有浮点数和高精度类型,更适合做页面展示和交互。而Python特别适合机器学习、爬虫和数据分析,这也是我转学Python的主要原因。

不得不说,JS确实借鉴了Python的很多特性,比如动态数组。但Python的list用起来更加灵活!

📋 Python List - 比数组更灵活的容器

Python中很少用传统的array,最常用的是list。它和JS的Array很像:

# 不用提前指定容量,甚至可以混装不同类型
L = ["江妮", 500,"林一", "周文", "徐志", "洪忠"]

list的特点:

  • 不需要提前指定容量

  • 不约束元素类型(可以混合存放)

  • 可变、有序、通用

✂️ Slice切片 - 简化操作的利器

切片是我今天学的最实用的技巧!大大简化了取元素的操作:

L = ["江妮", "林一", "周文", "徐志", "洪忠"]

# 基本切片
L[0:3]    # ['江妮', '林一', '周文']
L[:3]     # 省略开头,同上
L[1:3]    # ['江妮', '周文']
L[-2:]    # 倒数两个:['徐志', '洪忠']

# 带步长的切片
list(range(100))[::5]  # 每5个取一个:[0, 5, 10, ...]

字符串也同样支持切片操作!

1.不用内置的strip(),而是用双指针+切片:

python

def trim(s):
    left = 0
    while left < len(s) and s[left] == ' ':
        left += 1
    right = len(s)
    while right > left and s[right - 1] == ' ':
        right -= 1
    return s[left:right]

print(trim("   hello world "))  # "hello world"

2.在学习切片实现trim()函数时,其实Python已经提供了更简单的方法——strip():

strip()的基本用法

# strip() 默认去除字符串首尾的空格、换行符、制表符等空白字符

"   hello world "
def trim(s):
return s.strip()
print(trim("   hello worlld"))

# 也可以指定要去除的字符
text2 = "***hello***"
print(text2.strip('*'))  # "hello"

# 同时去除多种字符
text3 = "  \t\n  hello  \t\n  "
print(text3.strip())  # "hello"

相关方法家族

text = "   hello world   "

# lstrip() - 只去除左侧(开头)的空白
print(text.lstrip())  # "hello world   "

# rstrip() - 只去除右侧(结尾)的空白
print(text.rstrip())  # "   hello world"

# strip() - 同时去除两侧
print(text.strip())   # "hello world"

🤖 初探LLM接口 - 最兴奋的部分!

今天最让我兴奋的是调用了大语言模型接口!用的是DeepSeek(兼容OpenAI接口)。

什么是Transformer?

Google开源的Transformer架构,在2022年底引领了生成式AI浪潮,现在已经成为业内标准。各大厂商基本都兼容OpenAI接口:

  • DeepSeek(我今天用的)

  • Gemini(Google)

  • Claude(Anthropic)

ModelScope(阿里魔搭社区)

阿里推出的ModelScope是一个开源模型社区:

  • Mode = 模型

  • Scope = 空间

这里可以找到:

  • 开源模型(models)

  • 数据集(datasets)

  • NLP实验环境

实战:调用Completion接口

python

# python 不用new,直接调用构造函数运行实例化

client = OpenAI(

    api_key="your-api-key",

    base_url="your-base-url"

)

COMPLETION_MODEL = "deepseek-chat"

💡 Prompt工程小技巧

今天学到的写prompt的要点:

  1. 清晰详细地表达目标(比如具体说明要用于Amazon)

  2. 分步骤(1, 2, 3...)

  3. 约束返回格式(JSON格式)

prompt = """

Consideration product: 工厂现货PVC充气青蛙夜市地摊热卖充气玩具发光蛙儿童水上玩具

1. Compose human readable product title used on

Amazon in english within 20 words.

2.Write 5 selling points for the products in Amazon

3.Evaluate a price range for this prodect in u.s.

Output the result in json format with

three properties called title,selling_point and price_range

"""

def get_response(prompt):

    response = client.chat.completions.create(

    model =COMPLETION_MODEL,

        messages=[

            {"role":"user", "content": prompt}

        ]

    )

    return response.choices[0].message.content

print(get_response(prompt))

返回的结果非常棒!得到了结构化的JSON数据,包含标题、卖点和价格区间:

{ "title": "Inflatable PVC Glow Frog Toy for Night Market, Water Play, and Kids’ Outdoor Fun", "selling_points": [ "Bright LED glowing design attracts attention at night, perfect for夜市 (night markets) and evening beach parties.", "Made from durable, non-toxic PVC material safe for children and resistant to punctures during water play.", "Lightweight and easy to inflate/deflate for convenient storage and portability to pools, lakes, or backyards.", "Versatile toy suitable for both land and water use, including bath time, swimming pools, and outdoor play.", "Fun frog shape with vibrant colors enhances imaginative play and encourages active outdoor entertainment for kids." ], "price_range": "$9.99 – $14.99" }

📓 Jupyter Notebook - 边写边记的神器

今天全程用的就是Notebook文件(.ipynb):

  • Markdown单元格:写笔记、说明文档

  • Code单元格:写代码、运行实验

  • 边写代码边记录,非常适合数据分析、学习和写报告

🎯 今日总结

  1. Python的list切片操作非常灵活,值得熟练掌握

  2. Prompt工程很重要:清晰、分步骤、约束格式

  3. LLM接口调用比想象中简单,兼容OpenAI的都能快速上手

  4. Notebook是学习和实验的好工具

明天继续深入学习!一起加油吧🚀

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐