Learning Python

本文介绍了Python的基础知识,包括如何查看Python及Numpy的版本和安装位置、设置PYTHONPATH环境变量、进行交互式和脚本式编程等。同时,还详细讲解了Python字符串、列表、元组和字典的基本操作。

说明:这是我的学习笔记,很多内容转自网络,请查阅文章末尾的参考资料。


Python是一种 解释型、面向对象、动态数据类型的高级程序设计语言。Python最具特色的就是 使用缩进来表示代码块。缩进的空格数是可变的,但是同一个代码块的语句必须包含相同的缩进空格数。

准备工作

安装Anaconda

下载地址

  • 查看conda环境
conda env list
  • 创建/删除conda环境
conda create -n langchain2 python=3.8
conda remove --name langchain2 --all

危险:可以导出所有的第三方库至文件列表, 然后按照该列表卸载所有库

pip list
pip freeze > requirements.txt
pip uninstall -r requirements.txt -y
  • 从已有的环境中复制新的conda环境
conda create --name new_name --clone old_name

注意:如果是跨电脑复制,可以从源电脑导出配置,然后在目标电脑利用这个配置创建新环境

conda activate xxx
conda env export > xxx.yml
...
conda env create -f xxx.yml
  • 进入/退出conda环境
conda activate langchain2
...
conda deactivate

安装venv

如果无法使用Anaconda,可以使用Python自己提供的虚拟环境venv

cd ~
mkdir .venv
cd .venv
python3.11 -m venv .

可以使用如下命令进入/退出venv环境

source ~/.venv/bin/activate
...
deactivate

更多内容可以参考廖雪峰的venv教程

安装Python

  • 查看Python的版本
python --version
python -V
  • 查看Python的安装位置
which python
python -c "import sys; print(sys.executable)"
python -c "import os; print(os.sys.executable)"
  • 查看/修改Python的搜索路径
python -c "import sys; print(sys.path)"
import sys
sys.path.append('/home/someone/')

注意:我们也在命令窗口中添加路径 (这是默认的Python模块搜索路径,它的格式和系统PATH是一样的)。但是,这种方法只在当前命令窗口生效,即如果打开一个新的Terminal 窗口,定位到当前目录,打印PYTHONPATH 是没有刚才加入的路径的.

export PYTHONPATH=$PYTHONPATH:/home/someone

安装Python第三方库

  • 安装 graphviz (MacOS)
brew install graphviz
pip install graphviz
  • 更新 numpy 以及查看版本和安装位置
pip list | grep numpy
pip install --upgrade numpy
python -c "import numpy; print(numpy.version.version)"
python -c "import numpy; print(numpy.__version__)"
python -c "import numpy; print(numpy.__file__)"
  • 在conda环境中,安装LangChain开发包
pip install langchain==0.2.0
pip install langchain-community
pip install langchain-openai
pip install langchain-huggingface
pip install faiss-cpu
pip install sentence-transformers
pip install docx2txt
pip install python-dotenv
pip install ragas==0.1.7

注意:不要使用 pip install ragas==0.0.22 (不能指定 llm & embedding)
安装完成后,版本如下所示

$ pip list | grep -e lang -e openai -e transformers -e ragas
langchain                0.2.0
langchain-community      0.2.4
langchain-core           0.2.43
langchain-huggingface    0.0.3
langchain-openai         0.1.25
langchain-text-splitters 0.2.4
langsmith                0.1.145
openai                   1.55.0
ragas                    0.1.7
sentence-transformers    3.2.1
transformers             4.46.3

Python环境的备份和复制

$ pip freeze > requirements.txt
...
$ pip install -r requirements.txt

工作模式

交互式编程

命令行中输入 Python 命令即可启动交互式编程

$ python
Python 3.8.20 (default, Oct  3 2024, 10:25:41) 
[Clang 14.0.6 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

脚本式编程

将以下的源代码拷贝至test.py文件中。

print("Hello, Python!");

这里,假设你已经设置了Python解释器PATH变量。使用以下命令运行程序:

$ python test.py

输出结果:

Hello, Python!

让我们尝试另一种方式来执行Python脚本。修改test.py文件,如下所示:

#!/usr/bin/python

print("Hello, Python!");

这里,假定您的Python解释器在/usr/bin目录中,使用以下命令执行脚本:

$ chmod +x test.py     # 脚本文件添加可执行权限
$./test.py

输出结果:

Hello, Python!

基本概念

Python字符串

  1. 使用单引号(')
    你可以用单引号指示字符串,就如同’Quote me on this’这样。所有的空白,即空格和制表符都照原样保留。

  2. 使用双引号(“)
    在双引号中的字符串与单引号中的字符串的使用完全相同,例如"What’s your name?”。

  3. 使用三引号(‘’'或"“”)
    利用三引号,你可以指示一个多行的字符串。你可以在三引号中自由的使用单引号和双引号。

  • 字符串查找
  1. find()方法:
info = 'abca'
print(info.find('a')) ##从下标0开始,查找在字符串里第一个出现的子串,返回结果:0

info = 'abca'
print(info.find('a',1)) ##从下标1开始,查找在字符串里第一个出现的子串:返回结果3

info = 'abca'
print(info.find('333')) ##返回-1,查找不到返回-1
  1. index()方法:

python的index方法是在字符串里查找子串第一次出现的位置,类似字符串的find方法,不过比find方法更好的是,如果查找不到子串,会抛出异常,而不是返回-1

info = 'abca'
print(info.index('a'))
print(info.index('33'))
  • 字符串翻转

通过步进反转[::-1]

a = 'abcd'
b = a[::-1]##[::-1]通过步进反转
print(b)

Python列表

List(列表) 是 Python 中使用最频繁的数据类型。列表用[ ]标识
列表中的值得分割也可以用到变量[头下标:尾下标],就可以截取相应的列表,从左到右索引默认0开始的,从右到左索引默认-1开始,下标可以为空表示取到头或尾。加号(+)是列表连接运算符,星号(*)是重复操作。

Python元组

元组是另一个数据类型,类似于List(列表)。元组用( )标识
元组内部元素用逗号隔开。但是元素不能二次赋值,相当于只读列表。

Python字典

字典(dictionary) 用{ }标识。字典由索引(key)和它对应的值value组成。
两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。列表是有序的对象结合,字典是无序的对象集合。

参考资料

  1. Python 基础教程
  2. Python Cookbook
  3. A Byte of Python
  4. Anaconda官网
  5. Linux下安装Anaconda(64位)详细过程
  6. Linux(redhat)安装Anaconda
  7. mac下conda的安装使用
  8. Conda 安装(MacOS 系统)
2016新书,和另一个Learning Python(最新第5版)不是同一个,书面相同 Paperback: 140 pages Publisher: Packt Publishing - ebooks Account (January 1, 2016) Language: English ISBN-10: 178528424X ISBN-13: 978-1785284243 Key Features Find out how to interact with Jenkins from within Eclipse, NetBeans, and IntelliJ IDEA Develop custom solutions that act upon Jenkins information in real time A step-by-step, practical guide to help you learn about extension points in existing plugins and how to build your own plugin Book Description Jenkins CI is the leading open source continuous integration server. It is written in Java and has a wealth of plugins to support the building and testing of virtually any project. Jenkins supports multiple Software Configuration Management tools such as Git, Subversion, and Mercurial. This book explores and explains the many extension points and customizations that Jenkins offers its users, and teaches you how to develop your own Jenkins extensions and plugins. First, you will learn how to adapt Jenkins and leverage its abilities to empower DevOps, Continuous Integration, Continuous Deployment, and Agile projects. Next, you will find out how to reduce the cost of modern software development, increase the quality of deliveries, and thereby reduce the time to market. We will also teach you how to create your own custom plugins using Extension points. Finally, we will show you how to combine everything you learned over the course of the book into one real-world scenario.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值