新聞中心
本文是對(duì)于 現(xiàn)代 Python 開發(fā):語法基礎(chǔ)與工程實(shí)踐的總結(jié),更多 Python 相關(guān)資料參考 Python 學(xué)習(xí)與實(shí)踐資料索引;本文參考了 Python Crash Course - Cheat Sheets,pysheeet 等。本文僅包含筆者在日常工作中經(jīng)常使用的,并且認(rèn)為較為關(guān)鍵的知識(shí)點(diǎn)與語法,如果想要進(jìn)一步學(xué)習(xí) Python 相關(guān)內(nèi)容或者對(duì)于機(jī)器學(xué)習(xí)與數(shù)據(jù)挖掘方向感興趣,可以參考程序猿的數(shù)據(jù)科學(xué)與機(jī)器學(xué)習(xí)實(shí)戰(zhàn)手冊(cè)。

站在用戶的角度思考問題,與客戶深入溝通,找到浦北網(wǎng)站設(shè)計(jì)與浦北網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國際域名空間、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋浦北地區(qū)。
基礎(chǔ)語法
Python 是一門高階、動(dòng)態(tài)類型的多范式編程語言;定義 Python 文件的時(shí)候我們往往會(huì)先聲明文件編碼方式:
- # 指定腳本調(diào)用方式
- #!/usr/bin/env python
- # 配置 utf-8 編碼
- # -*- coding: utf-8 -*-
- # 配置其他編碼
- # -*- coding:
-*- - # Vim 中還可以使用如下方式
- # vim:fileencoding=
人生苦短,請(qǐng)用 Python,大量功能強(qiáng)大的語法糖的同時(shí)讓很多時(shí)候 Python 代碼看上去有點(diǎn)像偽代碼。譬如我們用 Python 實(shí)現(xiàn)的簡易的快排相較于 Java 會(huì)顯得很短小精悍:
- def quicksort(arr):
- if len(arr) <= 1:
- return arr
- pivot = 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])
- # Prints "[1, 1, 2, 3, 6, 8, 10]"
控制臺(tái)交互
可以根據(jù) __name__ 關(guān)鍵字來判斷是否是直接使用 python 命令執(zhí)行某個(gè)腳本,還是外部引用;Google 開源的 fire 也是不錯(cuò)的快速將某個(gè)類封裝為命令行工具的框架:
- import fire
- class Calculator(object):
- """A simple calculator class."""
- def double(self, number):
- return 2 * number
- if __name__ == '__main__':
- fire.Fire(Calculator)
- # python calculator.py double 10 # 20
- # python calculator.py double --number=15 # 30
Python 2 中 print 是表達(dá)式,而 Python 3 中 print 是函數(shù);如果希望在 Python 2 中將 print 以函數(shù)方式使用,則需要自定義引入:
- from __future__ import print_function
我們也可以使用 pprint 來美化控制臺(tái)輸出內(nèi)容:
- import pprint
- stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
- pprint.pprint(stuff)
- # 自定義參數(shù)
- pp = pprint.PrettyPrinter(depth=6)
- tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',('parrot', ('fresh fruit',))))))))
- pp.pprint(tup)
模塊
Python 中的模塊(Module)即是 Python 源碼文件,其可以導(dǎo)出類、函數(shù)與全局變量;當(dāng)我們從某個(gè)模塊導(dǎo)入變量時(shí),函數(shù)名往往就是命名空間(Namespace)。而 Python 中的包(Package)則是模塊的文件夾,往往由 __init__.py 指明某個(gè)文件夾為包:
- # 文件目錄
- someDir/
- main.py
- siblingModule.py
- # siblingModule.py
- def siblingModuleFun():
- print('Hello from siblingModuleFun')
- def siblingModuleFunTwo():
- print('Hello from siblingModuleFunTwo')
- import siblingModule
- import siblingModule as sibMod
- sibMod.siblingModuleFun()
- from siblingModule import siblingModuleFun
- siblingModuleFun()
- try:
- # Import 'someModuleA' that is only available in Windows
- import someModuleA
- except ImportError:
- try:
- # Import 'someModuleB' that is only available in Linux
- import someModuleB
- except ImportError:
Package 可以為某個(gè)目錄下所有的文件設(shè)置統(tǒng)一入口:
- someDir/
- main.py
- subModules/
- __init__.py
- subA.py
- subSubModules/
- __init__.py
- subSubA.py
- # subA.py
- def subAFun():
- print('Hello from subAFun')
- def subAFunTwo():
- print('Hello from subAFunTwo')
- # subSubA.py
- def subSubAFun():
- print('Hello from subSubAFun')
- def subSubAFunTwo():
- print('Hello from subSubAFunTwo')
- # __init__.py from subDir
- # Adds 'subAFun()' and 'subAFunTwo()' to the 'subDir' namespace
- from .subA import *
- # The following two import statement do the same thing, they add 'subSubAFun()' and 'subSubAFunTwo()' to the 'subDir' namespace. The first one assumes '__init__.py' is empty in 'subSubDir', and the second one, assumes '__init__.py' in 'subSubDir' contains 'from .subSubA import *'.
- # Assumes '__init__.py' is empty in 'subSubDir'
- # Adds 'subSubAFun()' and 'subSubAFunTwo()' to the 'subDir' namespace
- from .subSubDir.subSubA import *
- # Assumes '__init__.py' in 'subSubDir' has 'from .subSubA import *'
- # Adds 'subSubAFun()' and 'subSubAFunTwo()' to the 'subDir' namespace
- from .subSubDir import *
- # __init__.py from subSubDir
- # Adds 'subSubAFun()' and 'subSubAFunTwo()' to the 'subSubDir' namespace
- from .subSubA import *
- # main.py
- import subDir
- subDir.subAFun() # Hello from subAFun
- subDir.subAFunTwo() # Hello from subAFunTwo
- subDir.subSubAFun() # Hello from subSubAFun
- subDir.subSubAFunTwo() # Hello from subSubAFunTwo
表達(dá)式與控制流
條件選擇
Python 中使用 if、elif、else 來進(jìn)行基礎(chǔ)的條件選擇操作:
- if x < 0:
- x = 0
- print('Negative changed to zero')
- elif x == 0:
- print('Zero')
- else:
- print('More')
Python 同樣支持 ternary conditional operator:
- a if condition else b
也可以使用 Tuple 來實(shí)現(xiàn)類似的效果:
- # test 需要返回 True 或者 False
- (falseValue, trueValue)[test]
- # 更安全的做法是進(jìn)行強(qiáng)制判斷
- (falseValue, trueValue)[test == True]
- # 或者使用 bool 類型轉(zhuǎn)換函數(shù)
- (falseValue, trueValue)[bool(
)]
循環(huán)遍歷
for-in 可以用來遍歷數(shù)組與字典:
- words = ['cat', 'window', 'defenestrate']
- for w in words:
- print(w, len(w))
- # 使用數(shù)組訪問操作符,能夠迅速地生成數(shù)組的副本
- for w in words[:]:
- if len(w) > 6:
- words.insert(0, w)
- # words -> ['defenestrate', 'cat', 'window', 'defenestrate']
如果我們希望使用數(shù)字序列進(jìn)行遍歷,可以使用 Python 內(nèi)置的 range 函數(shù):
- a = ['Mary', 'had', 'a', 'little', 'lamb']
- for i in range(len(a)):
- print(i, a[i])
基本數(shù)據(jù)類型
可以使用內(nèi)建函數(shù)進(jìn)行強(qiáng)制類型轉(zhuǎn)換(Casting):
- int(str)
- float(str)
- str(int)
- str(float)
Number: 數(shù)值類型
- x = 3
- print type(x) # Prints "
" - 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 += 1
- print x # Prints "4"
- x *= 2
- print x # Prints "8"
- y = 2.5
- print type(y) # Prints "
" - print y, y + 1, y * 2, y ** 2 # Prints "2.5 3.5 5.0 6.25"
布爾類型
Python 提供了常見的邏輯操作符,不過需要注意的是 Python 中并沒有使用 &&、|| 等,而是直接使用了英文單詞。
- t = True
- f = False
- print type(t) # Prints "
" - 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() 類型,獨(dú)立的 unicode() 類型,沒有 byte 類型;而 Python 3 中默認(rèn)的字符串為 utf-8 類型,并且包含了 byte 與 bytearray 兩個(gè)字節(jié)類型:
- type("Guido") # string type is str in python2
- #
- # 使用 __future__ 中提供的模塊來降級(jí)使用 Unicode
- from __future__ import unicode_literals
- type("Guido") # string type become unicode
- #
Python 字符串支持分片、模板字符串等常見操作:
- var1 = 'Hello World!'
- var2 = "Python Programming"
- print "var1[0]: ", var1[0]
- print "var2[1:5]: ", var2[1:5]
- # var1[0]: H
- # var2[1:5]: ytho
- print "My name is %s and weight is %d kg!" % ('Zara', 21)
- # My name is Zara and weight is 21 kg!
- str[0:4]
- len(str)
- string.replace("-", " ")
- ",".join(list)
- "hi {0}".format('j')
- str.find(",")
- str.index(",") # same, but raises IndexError
- str.count(",")
- str.split(",")
- str.lower()
- str.upper()
- str.title()
- str.lstrip()
- str.rstrip()
- str.strip()
- str.islower()
- # 移除所有的特殊字符
- re.sub('[^A-Za-z0-9]+', '', mystring)
如果需要判斷是否包含某個(gè)子字符串,或者搜索某個(gè)字符串的下標(biāo):
- # in 操作符可以判斷字符串
- if "blah" not in somestring:
- continue
- # find 可以搜索下標(biāo)
- s = "This be a string"
- if s.find("is") == -1:
- print "No 'is' here!"
- else:
- print "Found 'is' in the string."
Regex: 正則表達(dá)式
- import re
- # 判斷是否匹配
- re.match(r'^[aeiou]', str)
- # 以第二個(gè)參數(shù)指定的字符替換原字符串中內(nèi)容
- re.sub(r'^[aeiou]', '?', str)
- re.sub(r'(xyz)', r'\1', str)
- # 編譯生成獨(dú)立的正則表達(dá)式對(duì)象
- expr = re.compile(r'^...$')
- expr.match(...)
- expr.sub(...)
下面列舉了常見的表達(dá)式使用場景:
- # 檢測是否為 HTML 標(biāo)簽
- re.search('<[^/>][^>]*>', '')
- # 常見的用戶名密碼
- re.match('^[a-zA-Z0-9-_]{3,16}$', 'Foo') is not None
- re.match('^\w|[-_]{3,16}$', 'Foo') is not None
- re.match('^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$', 'hello.world@cdxwcx.com')
- # Url
- 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')
- # IP 地址
- 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: 創(chuàng)建增刪
- l = []
- l = list()
- # 使用字符串的 split 方法,可以將字符串轉(zhuǎn)化為列表
- str.split(".")
- # 如果需要將數(shù)組拼裝為字符串,則可以使用 join
- list1 = ['1', '2', '3']
- str1 = ''.join(list1)
- # 如果是數(shù)值數(shù)組,則需要先進(jìn)行轉(zhuǎn)換
- list1 = [1, 2, 3]
- str1 = ''.join(str(e) for e in list1)
可以使用 append 與 extend 向數(shù)組中插入元素或者進(jìn)行數(shù)組連接
- 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]
- # 彈出第二個(gè)元素
- myList.pop(1) # 20
- # myList: myList.pop(1)
- # 如果不加任何參數(shù),則默認(rèn)彈出***一個(gè)元素
- myList.pop()
- # 使用 slices 來刪除某個(gè)元素
- a = [ 1, 2, 3, 4, 5, 6 ]
- index = 3 # Only Positive index
- a = a[:index] + a[index+1 :]
- # 根據(jù)下標(biāo)刪除元素
- myList = [10,20,30,40,50]
- rmovIndxNo = 3
- del myList[rmovIndxNo] # myList: [10, 20, 30, 50]
- # 使用 remove 方法,直接根據(jù)元素刪除
- letters = ["a", "b", "c", "d", "e"]
- numbers.remove(numbers[1])
- print(*letters) # used a * to make it unpack you don't have to
Iteration: 索引遍歷
你可以使用基本的 for 循環(huán)來遍歷數(shù)組中的元素,就像下面介個(gè)樣紙:
- animals = ['cat', 'dog', 'monkey']
- for animal in animals:
- print animal
- # Prints "cat", "dog", "monkey", each on its own line.
如果你在循環(huán)的同時(shí)也希望能夠獲取到當(dāng)前元素下標(biāo),可以使用 enumerate 函數(shù):
- animals = ['cat', 'dog', 'monkey']
- for idx, animal in enumerate(animals):
- print '#%d: %s' % (idx + 1, animal)
- # Prints "#1: cat", "#2: dog", "#3: monkey", each on its own line
Python 也支持切片(Slices):
- nums = range(5) # range is a built-in function that creates a list of integers
- print 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 slice
- print nums # Prints "[0, 1, 8, 9, 4]"
Comprehensions: 變換
Python 中同樣可以使用 map、reduce、filter,map 用于變換數(shù)組:
- # 使用 map 對(duì)數(shù)組中的每個(gè)元素計(jì)算平方
- items = [1, 2, 3, 4, 5]
- squared = list(map(lambda x: x**2, items))
- # map 支持函數(shù)以數(shù)組方式連接使用
- 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 用于進(jìn)行歸納計(jì)算:
- # reduce 將數(shù)組中的值進(jìn)行歸納
- from functools import reduce
- product = reduce((lambda x, y: x * y), [1, 2, 3, 4])
- # Output: 24
filter 則可以對(duì)數(shù)組進(jìn)行過濾:
- number_list = range(-5, 5)
- less_than_zero = list(filter(lambda x: x < 0, number_list))
- print(less_than_zero)
- # Output: [-5, -4, -3, -2, -1]
字典類型
創(chuàng)建增刪
- d = {'cat': 'cute', 'dog': 'furry'} # 創(chuàng)建新的字典
- print d['cat'] # 字典不支持點(diǎn)(Dot)運(yùn)算符取值
如果需要合并兩個(gè)或者多個(gè)字典類型:
- # python 3.5
- z = {**x, **y}
- # python 2.7
- 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
索引遍歷
可以根據(jù)鍵來直接進(jìn)行元素訪問:
- # Python 中對(duì)于訪問不存在的鍵會(huì)拋出 KeyError 異常,需要先行判斷或者使用 get
- print 'cat' in d # Check if a dictionary has a given key; prints "True"
- # 如果直接使用 [] 來取值,需要先確定鍵的存在,否則會(huì)拋出異常
- print d['monkey'] # KeyError: 'monkey' not a key of d
- # 使用 get 函數(shù)則可以設(shè)置默認(rèn)值
- 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 來遍歷數(shù)組:
- # 遍歷鍵
- for key in d:
- # 比前一種方式慢
- for k in dict.keys(): ...
- # 直接遍歷值
- for value in dict.itervalues(): ...
- # Python 2.x 中遍歷鍵值
- for key, value in d.iteritems():
- # Python 3.x 中遍歷鍵值
- for key, value in d.items():
其他序列類型
集合
- # Same as {"a", "b","c"}
- normal_set = set(["a", "b","c"])
- # Adding an element to normal set is fine
- normal_set.add("d")
- print("Normal Set")
- print(normal_set)
- # A frozen set
- frozen_set = frozenset(["e", "f", "g"])
- print("Frozen Set")
- print(frozen_set)
- # Uncommenting below line would cause error as
- # we are trying to add element to a frozen set
- # frozen_set.add("h")
函數(shù)
函數(shù)定義
Python 中的函數(shù)使用 def 關(guān)鍵字進(jìn)行定義,譬如:
- 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)
- # Prints "negative", "zero", "positive"
Python 支持運(yùn)行時(shí)創(chuàng)建動(dòng)態(tài)函數(shù),也即是所謂的 lambda 函數(shù):
- def f(x): return x**2
- # 等價(jià)于
- g = lambda x: x**2
參數(shù)
Option Arguments: 不定參數(shù)
- def example(a, b=None, *args, **kwargs):
- print a, b
- print args
- print kwargs
- example(1, "var", 2, 3, word="hello")
- # 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)
- # 1 var
- # (1, 2, 3, 4, 5)
- # {'1': 1, '2': 2, '3': 3}
生成器
- def simple_generator_function():
- yield 1
- yield 2
- yield 3
- for value in simple_generator_function():
- print(value)
- # 輸出結(jié)果
- # 1
- # 2
- # 3
- our_generator = simple_generator_function()
- next(our_generator)
- # 1
- next(our_generator)
- # 2
- next(our_generator)
- #3
- # 生成器典型的使用場景譬如***數(shù)組的迭代
- def get_primes(number):
- while True:
- if is_prime(number):
- yield number
- number += 1
裝飾器
裝飾器是非常有用的設(shè)計(jì)模式:
- # 簡單裝飾器
- from functools import wraps
- def decorator(func):
- @wraps(func)
- def wrapper(*args, **kwargs):
- print('wrap function')
- return func(*args, **kwargs)
- return wrapper
- @decorator
- def example(*a, **kw):
- pass
- example.__name__ # attr of function preserve
- # 'example'
- # Decorator
- # 帶輸入值的裝飾器
- from functools import wraps
- def decorator_with_argument(val):
- def decorator(func):
- @wraps(func)
- def wrapper(*args, **kwargs):
- print "Val is {0}".format(val)
- return func(*args, **kwargs)
- return wrapper
- return decorator
- @decorator_with_argument(10)
- def example():
- print "This is example function."
- example()
- # Val is 10
- # This is example function.
- # 等價(jià)于
- def example():
- print "This is example function."
- example = decorator_with_argument(10)(example)
- example()
- # Val is 10
- # This is example function.
類與對(duì)象
類定義
Python 中對(duì)于類的定義也很直接:
- class Greeter(object):
- # Constructor
- def __init__(self, name):
- self.name = name # Create an instance variable
- # Instance method
- def 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!"
- # isinstance 方法用于判斷某個(gè)對(duì)象是否源自某個(gè)類
- ex = 10
- isinstance(ex,int)
Managed Attributes: 受控屬性
- # property、setter、deleter 可以用于復(fù)寫點(diǎn)方法
- class Example(object):
- def __init__(self, value):
- self._val = value
- @property
- def val(self):
- return self._val
- @val.setter
- def val(self, value):
- if not isintance(value, int):
- raise TypeError("Expected int")
- self._val = value
- @val.deleter
- def val(self):
- del self._val
- @property
- def square3(self):
- return 2**3
- ex = Example(123)
- ex.val = "str"
- # Traceback (most recent call last):
- # File "", line 1, in
- # File "test.py", line 12, in val
- # raise TypeError("Expected int")
- # TypeError: Expected int
類方法與靜態(tài)方法
- class example(object):
- @classmethod
- def clsmethod(cls):
- print "I am classmethod"
- @staticmethod
- def stmethod():
分享名稱:Python語法速覽與實(shí)戰(zhàn)清單
網(wǎng)站鏈接:http://www.dlmjj.cn/article/dpjoejg.html


咨詢
建站咨詢
