Python 3.3 Tutorial Notes - 1:Basic

本文深入探讨了Python编程的基础概念、关键特性及其在不同领域的应用。从基本语法到高级功能,包括运算符、表达式、字符串操作、类型转换、控制流程、函数定义等核心知识点。同时,详细介绍了Python中列表、元组、字典等数据结构的使用方法及注意事项,旨在为读者提供全面的Python编程技能提升指南。
1. "/": Division, always return float
2. "//": Floor division, return integer
3. "%": Reminder, return integer
 
1. "**": Power
 

1. Interactive mode, "_" means the value of the last printed expression. "_" is read only.

 

1. Complex number, use "j" or "J" to indicate the imaginary part -- "5+3j"

 

1. "..." And '...' are the same in Python

2."\" can be used to escape quotes

3. '"Yes"' will print out "Yes".

4. r"....", mean no escape in the string.

5. '"""..."""' or "'''...'''" to indicate a multiple line string. To avoid this, add "\" at the end of the line

6. String concatenation: "+" or side by side two string literals next to each other.

7. String repeat: <repeat times> * <the string>

8. String[i], the i-th char in string. Negative means from the end to the beginning.

9. String slicing: Str[0:2], 0 is included, 2 is excluded. Str[:j]: From beginning to j but not include j. Str[k:], from k to the end.

10. Python string is immutable

 

1. List: a=[1, 2, 3]
2. the type of element could be different: b=[1, 'a', 5.3]
3. string is immutable, list is mutable. For list, assign to a slice is also correct: a[3:5]=["hello", "world"]

1. Multiple-assignment: a,b=0,1
2. The right-hand expressions evaluate BEFORE assignment
3. All right-hand expressions evaluated from left to right.

1. Anything not zero means true in a place where boolean is expected

1. Indentation is important. It is the way to group statements.
2. When a compound statement is entered interactively, it must be followed by a blank line to indicate completion (since the parser cannot guess when you have typed the last line).
3. Note that each line within a basic block must be indented by the same amount.

1. print(), to avoid new line, use "end" argument: print(b, end=',')

<think>嗯,用户想查看mysqlclient包的详细信息,特别是关于Python方面的。首先,我需要回忆一下mysqlclient的基本情况。mysqlclient是Python连接MySQL的一个驱动程序,支持Python3,这点用户提供的引用里也提到了[^1][^2]。接下来,我应该整理出它的主要特点,比如兼容性、依赖库、功能支持等。 然后,安装方法部分,用户可能想知道不同系统下的安装命令。记得在Linux上需要安装python3-dev和默认的libmysqlclient-dev,引用中的例子提到了CentOS可能遇到的问题[^3]。Windows用户可能需要预编译的二进制包,这里可以提到pip安装的命令。 接下来是基本用法,需要给出一个简单的代码示例,展示如何连接数据库、执行查询,这部分要确保代码正确,比如使用cursor.execute和fetchall方法。用户可能还关心错误处理和性能优化,所以高级特性部分要提到上下文管理器和事务支持。 常见问题方面,用户可能会遇到依赖缺失或者版本冲突,比如Python.h找不到,这时候需要指出安装python3-dev。另外,引用中提到了MySQL-python只支持Python2,而mysqlclient是分支支持Python3,这点要强调兼容性问题[^2]。 最后,相关资源部分需要列出项目地址和文档链接,引用里的GitCode和PyPI页面[^2][^4]。这样用户可以直接访问官方资源获取更多信息。整个回答要保持结构清晰,分点说明,确保用户能一步步了解mysqlclient的各个方面。</think>### mysqlclient 包详情(Python) #### 1. **概述** - **mysqlclient** 是 Python 连接 MySQL 数据库的常用驱动库,支持 Python 3.x,是 MySQL-python(仅支持 Python 2)的替代分支。 - 提供高效的原生 SQL 操作接口,底层基于 MySQL C API 实现。 #### 2. **主要特性** - 兼容 Python 3.x 和 Python 2.7+。 - 依赖 MySQL 客户端库(如 `libmysqlclient-dev` 或 `mysql-connector-c`)。 - 支持事务、连接池、参数化查询等数据库操作。 - 提供与 MySQL-python 高度兼容的 API。 #### 3. **安装方法** - **Linux/Unix**: ```bash # 安装依赖 sudo apt-get install python3-dev default-libmysqlclient-dev # Debian/Ubuntu sudo yum install python3-devel mysql-devel # CentOS/RHEL # 安装包 pip install mysqlclient ``` - **Windows**:需预安装 MySQL 客户端库或使用预编译的二进制包: ```bash pip install mysqlclient ``` #### 4. **基本用法示例** ```python import MySQLdb # 连接数据库 conn = MySQLdb.connect( host="localhost", user="root", password="password", database="test_db" ) # 执行查询 cursor = conn.cursor() cursor.execute("SELECT * FROM users") results = cursor.fetchall() for row in results: print(row) # 关闭连接 cursor.close() conn.close() ``` #### 5. **高级特性** - **事务管理**: ```python try: conn.begin() cursor.execute("INSERT INTO users (name) VALUES (&#39;Alice&#39;)") conn.commit() except Exception as e: conn.rollback() ``` - **上下文管理器**(Python 3+): ```python with conn.cursor() as cursor: cursor.execute("SELECT @@version") print(cursor.fetchone()) ``` #### 6. **常见问题** - **依赖缺失错误**(如 `Python.h not found`):需安装 `python3-dev` 或 `mysql-devel`。 - **兼容性问题**:若项目原使用 MySQL-python,可无缝替换为 mysqlclient。 - **性能优化**:通过 `cursorclass=MySQLdb.cursors.DictCursor` 返回字典格式结果。 #### 7. **相关资源** - 项目地址:[GitCode 仓库](https://gitcode.com/gh_mirrors/mysqlclient) - 文档:[PyPI 页面](https://pypi.org/project/mysqlclient/)[^4] ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值