本文共 18879 字,大约阅读时间需要 62 分钟。
基础语法
Python 是一门高阶、动态类型的多范式编程语言;定义 Python 文件的时候我们往往会先声明文件编码方式:
#!/usr/bin/env python
人生苦短,请用 Python,大量功能强大的语法糖的同时让很多时候 Python 代码看上去有点像伪代码。譬如我们用 Python 实现的简易的快排相较于 Java 会显得很短小精悍:
def quicksort(arr):if len(arr) <= 1:return arrpivot = arr[len(arr) / 2]left = [x for x in arr if x < pivot]middle = [x for x in arr if x == pivot]right = [x for x in arr if x > pivot]return quicksort(left) + middle + quicksort(right)print quicksort([3,6,8,10,1,2,1])
控制台交互
可以根据 name 关键字来判断是否是直接使用 python 命令执行某个脚本,还是外部引用;Google 开源的 fire 也是不错的快速将某个类封装为命令行工具的框架:
import fireclass Calculator(object):
"""A simple calculator class."""def double(self, number):
return 2 * numberif name == 'main':
fire.Fire(Calculator)Python 2 中 print 是表达式,而 Python 3 中 print 是函数;如果希望在 Python 2 中将 print 以函数方式使用,则需要自定义引入:
from future import print_function我们也可以使用 pprint 来美化控制台输出内容:import pprintstuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
pprint.pprint(stuff)pp = pprint.PrettyPrinter(depth=6)
tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',('parrot', ('fresh fruit',))))))))pp.pprint(tup)模块Python 中的模块(Module)即是 Python 源码文件,其可以导出类、函数与全局变量;当我们从某个模块导入变量时,函数名往往就是命名空间(Namespace)。而 Python 中的包(Package)则是模块的文件夹,往往由 init.py 指明某个文件夹为包:
someDir/
main.pysiblingModule.pydef siblingModuleFun():
print('Hello from siblingModuleFun')def siblingModuleFunTwo():
print('Hello from siblingModuleFunTwo')import siblingModule
import siblingModule as sibModsibMod.siblingModuleFun()
from siblingModule import siblingModuleFun
siblingModuleFun()try:
import someModuleA
except ImportError:
try:import someModuleBexcept ImportError:
Package 可以为某个目录下所有的文件设置统一入口:
someDir/main.pysubModules/init.pysubA.pysubSubModules/init.pysubSubA.pydef subAFun():
print('Hello from subAFun')def subAFunTwo():
print('Hello from subAFunTwo')def subSubAFun():
print('Hello from subSubAFun')def subSubAFunTwo():
print('Hello from subSubAFunTwo')from .subA import *
from .subSubDir.subSubA import *
from .subSubDir import *
from .subSubA import *
import subDir
subDir.subAFun() # Hello from subAFun
subDir.subAFunTwo() # Hello from subAFunTwosubDir.subSubAFun() # Hello from subSubAFunsubDir.subSubAFunTwo() # Hello from subSubAFunTwo表达式与控制流条件选择
Python 中使用 if、elif、else 来进行基础的条件选择操作:
if x < 0:x = 0print('Negative changed to zero')elif x == 0:print('Zero')else:print('More')Python 同样支持 ternary conditional operator:a if condition else b也可以使用 Tuple 来实现类似的效果:(falseValue, trueValue)[test]
(falseValue, trueValue)[test == True]
(falseValue, trueValue)[bool(<expression>)]
循环遍历for-in 可以用来遍历数组与字典:
words = ['cat', 'window', 'defenestrate']for w in words:
print(w, len(w))for w in words[:]:
if len(w) > 6:words.insert(0, w)如果我们希望使用数字序列进行遍历,可以使用 Python 内置的 range 函数:
a = ['Mary', 'had', 'a', 'little', 'lamb']for i in range(len(a)):
print(i, a[i])基本数据类型可以使用内建函数进行强制类型转换(Casting):
int(str)float(str)str(int)str(float)Number: 数值类型x = 3print type(x) # Prints "<type 'int'>"print x # Prints "3"print x + 1 # Addition; prints "4"print x - 1 # Subtraction; prints "2"print x * 2 # Multiplication; prints "6"print x 2 # Exponentiation; prints "9"x += 1print x # Prints "4"x = 2print x # Prints "8"y = 2.5print type(y) # Prints "<type 'float'>"print y, y + 1, y 2, y 2 # Prints "2.5 3.5 5.0 6.25"布尔类型Python 提供了常见的逻辑操作符,不过需要注意的是 Python 中并没有使用 &&、|| 等,而是直接使用了英文单词。
t = Truef = Falseprint type(t) # Prints "<type 'bool'>"print t and f # Logical AND; prints "False"print t or f # Logical OR; prints "True"print not t # Logical NOT; prints "False"print t != f # Logical XOR; prints "True"String: 字符串Python 2 中支持 Ascii 码的 str() 类型,独立的 unicode() 类型,没有 byte 类型;而 Python 3 中默认的字符串为 utf-8 类型,并且包含了 byte 与 bytearray 两个字节类型:
type("Guido") # string type is str in python2from future import unicode_literals
type("Guido") # string type become unicodePython 字符串支持分片、模板字符串等常见操作:
var1 = 'Hello World!'var2 = "Python Programming"print "var1[0]: ", var1[0]
print "var2[1:5]: ", var2[1:5]print "My name is %s and weight is %d kg!" % ('Zara', 21)
str[0:4]
len(str)string.replace("-", " ")
",".join(list)"hi {0}".format('j')str.find(",")str.index(",") # same, but raises IndexErrorstr.count(",")str.split(",")str.lower()
str.upper()str.title()str.lstrip()
str.rstrip()str.strip()str.islower()
re.sub('[^A-Za-z0-9]+', '', mystring)
如果需要判断是否包含某个子字符串,或者搜索某个字符串的下标:if "blah" not in somestring:
continues = "This be a string"
if s.find("is") == -1:print "No 'is' here!"else:print "Found 'is' in the string."Regex: 正则表达式import rere.match(r'^[aeiou]', str)
re.sub(r'^[aeiou]', '?', str)
re.sub(r'(xyz)', r'\1', str)expr = re.compile(r'^...$')
expr.match(...)expr.sub(...)下面列举了常见的表达式使用场景:re.search('<[^/>][^>]*>', '<a href="#label">')
re.match('^[a-zA-Z0-9-]{3,16}$', 'Foo') is not Nonere.match('^\w|[-]{3,16}$', 'Foo') is not None
re.match('^([a-z0-9_.-]+)@([\da-z.-]+).([a-z.]{2,6})$', 'hello.world@example.com')
exp = re.compile(r'''^(https?:\/\/)? # match http or https
([\da-z.-]+) # match domain.([a-z.]{2,6}) # match domain([\/\w .-]*)\/?$ # match api or file''', re.X)exp.match('www.google.com')exp = re.compile(r'''^(?:(?:25[0-5]
|2[0-4][0-9]|[1]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[1]?[0-9][0-9]?)$''', re.X)exp.match('192.168.1.1')集合类型List: 列表
Operation: 创建增删
list 是基础的序列类型:
l = []l = list()str.split(".")
list1 = ['1', '2', '3']
str1 = ''.join(list1)list1 = [1, 2, 3]
str1 = ''.join(str(e) for e in list1)可以使用 append 与 extend 向数组中插入元素或者进行数组连接x = [1, 2, 3]x.append([4, 5]) # [1, 2, 3, [4, 5]]
x.extend([4, 5]) # [1, 2, 3, 4, 5],注意 extend 返回值为 None
可以使用 pop、slices、del、remove 等移除列表中元素:myList = [10,20,30,40,50]myList.pop(1) # 20
myList.pop()
a = [ 1, 2, 3, 4, 5, 6 ]
index = 3 # Only Positive indexa = a[:index] + a[index+1 :]myList = [10,20,30,40,50]
rmovIndxNo = 3del myList[rmovIndxNo] # myList: [10, 20, 30, 50]letters = ["a", "b", "c", "d", "e"]
numbers.remove(numbers[1])print(letters) # used a to make it unpack you don't have toIteration: 索引遍历你可以使用基本的 for 循环来遍历数组中的元素,就像下面介个样纸:
animals = ['cat', 'dog', 'monkey']for animal in animals:print animal如果你在循环的同时也希望能够获取到当前元素下标,可以使用 enumerate 函数:
animals = ['cat', 'dog', 'monkey']for idx, animal in enumerate(animals):print '#%d: %s' % (idx + 1, animal)Python 也支持切片(Slices):
nums = range(5) # range is a built-in function that creates a list of integersprint nums # Prints "[0, 1, 2, 3, 4]"print nums[2:4] # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]"print nums[2:] # Get a slice from index 2 to the end; prints "[2, 3, 4]"print nums[:2] # Get a slice from the start to index 2 (exclusive); prints "[0, 1]"print nums[:] # Get a slice of the whole list; prints ["0, 1, 2, 3, 4]"print nums[:-1] # Slice indices can be negative; prints ["0, 1, 2, 3]"nums[2:4] = [8, 9] # Assign a new sublist to a sliceprint nums # Prints "[0, 1, 8, 9, 4]"Comprehensions: 变换Python 中同样可以使用 map、reduce、filter,map 用于变换数组:
items = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, items))def multiply(x):
return (x*x)def add(x):return (x+x)funcs = [multiply, add]
for i in range(5):value = list(map(lambda x: x(i), funcs))print(value)reduce 用于进行归纳计算:from functools import reduce
product = reduce((lambda x, y: x * y), [1, 2, 3, 4])filter 则可以对数组进行过滤:
number_list = range(-5, 5)less_than_zero = list(filter(lambda x: x < 0, number_list))print(less_than_zero)字典类型
创建增删
d = {'cat': 'cute', 'dog': 'furry'} # 创建新的字典print d['cat'] # 字典不支持点(Dot)运算符取值如果需要合并两个或者多个字典类型:z = {
x, y}def merge_dicts(*dict_args):
"""Given any number of dicts, shallow copy and merge into a new dict,precedence goes to key value pairs in latter dicts."""result = {}for dictionary in dict_args:result.update(dictionary)return result索引遍历可以根据键来直接进行元素访问:
print 'cat' in d # Check if a dictionary has a given key; prints "True"
print d['monkey'] # KeyError: 'monkey' not a key of d
print d.get('monkey', 'N/A') # Get an element with a default; prints "N/A"
print d.get('fish', 'N/A') # Get an element with a default; prints "wet"d.keys() # 使用 keys 方法可以获取所有的键
可以使用 for-in 来遍历数组:for key in d:
for k in dict.keys(): ...
for value in dict.itervalues(): ...
for key, value in d.iteritems():
for key, value in d.items():
其他序列类型集合
normal_set = set(["a", "b","c"])
normal_set.add("d")
print("Normal Set")
print(normal_set)frozen_set = frozenset(["e", "f", "g"])
print("Frozen Set")
print(frozen_set)函数
函数定义
Python 中的函数使用 def 关键字进行定义,譬如:
def sign(x):if x > 0:return 'positive'elif x < 0:return 'negative'else:return 'zero'for x in [-1, 0, 1]:
print sign(x)Python 支持运行时创建动态函数,也即是所谓的 lambda 函数:
def f(x): return x**2g = lambda x: x**2
参数Option Arguments: 不定参数
def example(a, b=None, *args, **kwargs):print a, bprint argsprint kwargsexample(1, "var", 2, 3, word="hello")
a_tuple = (1, 2, 3, 4, 5)
a_dict = {"1":1, "2":2, "3":3}example(1, "var", *a_tuple, **a_dict)生成器
def simple_generator_function():yield 1yield 2yield 3for value in simple_generator_function():
print(value)our_generator = simple_generator_function()
next(our_generator)next(our_generator)
next(our_generator)
#3def get_primes(number):
while True:if is_prime(number):yield numbernumber += 1装饰器装饰器是非常有用的设计模式:
from functools import wraps
def decorator(func):@decorator
def example(*a, **kw):passexample.name # attr of function preserve
from functools import wraps
def decorator_with_argument(val):def decorator(func):@decorator_with_argument(10)
def example():print "This is example function."example()
def example():
print "This is example function."example = decorator_with_argument(10)(example)
example()类与对象
类定义
Python 中对于类的定义也很直接:
class Greeter(object):# Constructordef __init__(self, name): self.name = name # Create an instance variable# Instance methoddef greet(self, loud=False): if loud: print 'HELLO, %s!' % self.name.upper() else: print 'Hello, %s' % self.name
g = Greeter('Fred') # Construct an instance of the Greeter class
g.greet() # Call an instance method; prints "Hello, Fred"g.greet(loud=True) # Call an instance method; prints "HELLO, FRED!"ex = 10
isinstance(ex,int)Managed Attributes: 受控属性class Example(object):
def init(self, value):self._val = valueex = Example(123)
ex.val = "str"类方法与静态方法
class example(object):ex = example()
ex.clsmethod()ex.stmethod()
ex.instmethod()
example.clsmethod()
example.stmethod()
example.instmethod()
对象
实例化
属性操作Python 中对象的属性不同于字典键,可以使用点运算符取值,直接使用 in 判断会存在问题:
class A(object):a = A()
print "'prop' in a.dict =", 'prop' in a.dictprint "hasattr(a, 'prop') =", hasattr(a, 'prop')print "a.prop =", a.prop建议使用 hasattr、getattr、setattr 这种方式对于对象属性进行操作:
class Example(object):def init(self):self.name = "ex"def printex(self):print "This is an example"ex = Example()
hasattr(ex,"name")hasattr(ex,"printex")
hasattr(ex,"print")
getattr(ex,'name')
setattr(ex,'name','example')
ex.name异常与测试
异常处理
Context Manager - with
with 常用于打开或者关闭某些资源:
host = 'localhost'port = 5566with Socket(host, port) as s:while True:conn, addr = s.accept()msg = conn.recv(1024)print msgconn.send(msg)conn.close()单元测试from future import print_functionimport unittest
def fib(n):
return 1 if n<=2 else fib(n-1)+fib(n-2)def setUpModule():
print("setup module")def tearDownModule():print("teardown module")class TestFib(unittest.TestCase):
def setUp(self): print("setUp") self.n = 10def tearDown(self): print("tearDown") del self.n@classmethoddef setUpClass(cls): print("setUpClass")@classmethoddef tearDownClass(cls): print("tearDownClass")def test_fib_assert_equal(self): self.assertEqual(fib(self.n), 55)def test_fib_assert_true(self): self.assertTrue(fib(self.n) == 55)
if name == "main":
unittest.main()存储文件读写
路径处理
Python 内置的 file 关键字会指向当前文件的相对路径,可以根据它来构造绝对路径,或者索引其他文件:
dir = os.path.dirname(file) # src\app
dirname1 = os.path.basename(dir)
dirname2 = os.path.split(dir)[1] ## if you look at the documentation, this is exactly what os.path.basename does.os.path.abspath(os.path.dirname(file)) # D:\WorkSpace\OWS\tool\ui-tool-svn\python\src\app
os.path.dirname(os.path.realpath(file)) # D:\WorkSpace\OWS\tool\ui-tool-svn\python\src\app
os.getcwd()
可以使用 listdir、walk、glob 模块来进行文件枚举与检索:from os import listdir
from os.path import isfile, joinonlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]from os import walk
f = []
for (dirpath, dirnames, filenames) in walk(mypath):f.extend(filenames)breakimport glob
print(glob.glob("/home/adam/*.txt"))简单文件读写
mode = 'a' if os.path.exists(writepath) else 'w'
with open("file.dat",mode) as f:
f.write(...)...f.close()
message = f.read()
复杂格式文件JSON
import jsonwith open('data.json', 'w') as f:
json.dump(data, f)with open('data.json', 'r') as f:
data = json.load(f)XML我们可以使用 lxml 来解析与处理 XML 文件,本部分即对其常用操作进行介绍。lxml 支持从字符串或者文件中创建 Element 对象:
from lxml import etreexml = '<a xmlns="test"><b xmlns="test"/></a>'
root = etree.fromstring(xml)etree.tostring(root)tree = etree.parse("doc/test.xml")
root = etree.fromstring(xml, base_url="")
其提供了迭代器以对所有元素进行遍历:for tag in tree.iter():
if not len(tag):print tag.keys() # 获取所有自定义属性print (tag.tag, tag.text) # text 即文本子元素值for e in root.iter():
print tree.getpath(e)lxml 支持以 XPath 查找元素,不过需要注意的是,XPath 查找的结果是数组,并且在包含命名空间的情况下,需要指定命名空间:root.xpath('//page/text/text()',ns={prefix:url})el.getparent()
lxml 提供了 insert、append 等方法进行元素操作:st = etree.Element("state", name="New Mexico")
co = etree.Element("county", name="Socorro")st.append(co)node.insert(0, newKid)
Excel可以使用 [xlrd]() 来读取 Excel 文件,使用 xlsxwriter 来写入与操作 Excel 文件。
sh.cell(rx, col).value
workbook = xlsxwriter.Workbook(outputFile)
worksheet = workbook.add_worksheet()row = 0
for rowData in array:
for col, data in enumerate(rowData):worksheet.write(row, col, data)row = row + 1workbook.close()
文件系统对于高级的文件操作,我们可以使用 Python 内置的 shutil
shutil.rmtree(appName)
网络交互Requests
Requests 是优雅而易用的 Python 网络请求库:
import requestsr = requests.get('')
r = requests.get('', auth=('user', 'pass'))r.status_code
r.headers['content-type']
r.encoding
r.text
r.json()
r = requests.put('', data = {'key':'value'})
r = requests.delete('')r = requests.head('')r = requests.options('')数据存储MySQL
import pymysql.cursorsconnection = pymysql.connect(host='localhost',
user='user',password='passwd',db='db',charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)try:
with connection.cursor() as cursor:sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)" cursor.execute(sql, ('webmaster@python.org', 'very-secret'))# connection is not autocommit by default. So you must commit to save# your changes.connection.commit()with connection.cursor() as cursor: # Read a single record sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s" cursor.execute(sql, ('webmaster@python.org',)) result = cursor.fetchone() print(result)
finally:
connection.close()最后,如果你跟我一样都喜欢python,也在学习python的道路上奔跑,欢迎你加入python学习群:839383765 群内每天都会分享最新业内资料,企业项目案例,分享python免费课程,共同交流学习,让学习变(编)成(程)一种习惯!
转载于:https://blog.51cto.com/14186420/2377384