一、知识点
- 中文编码:在文件头部加
# -*- coding: UTF-8 -*- - 标准类型:Numbers、String、List、Tuple、Dictionary
- 字符串索引:
str = “Python” => str[1:3] = 'yt' - 运算符:and、or、not(逻辑);in、not in(成员);is、not is(自身)
- 条件语句:if…、if…else…、if…elif…else…
- 文件操作:
with open(filename, 'r+') as filehandle: filehandle.read()
- 异常处理:try…except xxx:…else:…
- 正则表达式:pattern = re.compile(r’’); m = pattern.match(‘String’);
- 数据库连接:
import MySQLdb
#打开数据库连接
db = MySQLdb.connect(“localhost”,“testuser”,“test123”,“TESTDB” )
#使用cursor()方法获取操作游标
cursor = db.cursor()
#使用execute方法执行SQL语句
cursor.execute(“SELECT VERSION()”)
#使用 fetchone() 方法获取一条数据库。
data = cursor.fetchone()
print "Database version : %s " % data
#关闭数据库连接
db.close()
二、面试题
- 垃圾回收机制
Python中的垃圾回收是以引用计数为主,分代收集为辅。引用计数的缺陷是循环引用的问题。
在Python中,如果一个对象的引用数为0,Python虚拟机就会回收这个对象的内存。
- 深拷贝浅拷贝
浅拷贝是对于一个对象的顶层拷贝,通俗的理解是:拷贝了引用,并没有拷贝内容。深拷贝是对于一个对象所有层次的拷贝(递归)。