1. 程式人生 > >我的python基礎總結

我的python基礎總結

python基礎知識

1.mac下給python3安裝requests庫和scrapy庫

https://blog.csdn.net/u011413922/article/details/78026866

pip3 install requests

pip3 install Scrapy #註意S大寫

兩種安裝方式 https://blog.csdn.net/cighao/article/details/47860041

最新安裝步驟:https://blog.csdn.net/qq_36305327/article/details/60144614



2.重啟

https://blog.csdn.net/wen_1108/article/details/78682578


3.最新版的下載地址

http://www.pc6.com/mac/170572.html

requests:https://pypi.org/project/requests/。 安裝方式多樣。。。


4.mac版本的切換

https://blog.csdn.net/blue_zy/article/details/69568240

pycharm切換python版本:https://blog.csdn.net/sgfmby1994/article/details/77876873


5.基礎知識

1>中文編碼

1>#!/usr/bin/python

# -*- coding: UTF-8 -*-

print "你好,世界";

同時idea File Encoding需要設置



2>交互式編程 腳本式編程

獨有的標識符和保留字

多行語句\ 註釋# 多引號、單引號、三引號的使用 空行 python -h



3>運算符、條件表達式、循環表達式、break、continue、pass


4>變量類型:字符串、Number、原祖、字典(json)、日期時間、列表(數組)



5>函數

def 函數名(參數列表):

函數體



在 python 中,strings, tuples, 和 numbers 是不可更改的對象,而 list,dict 等則是可以修改的對象。 list、dict參數->指針

默認參數:def printinfo( name, age = 35 ):


不定長參數:

#!/usr/bin/python3

# 可寫函數說明

def printinfo( arg1, *vartuple ):

"打印任何傳入的參數"

print ("輸出: ")

print (arg1)

for var in vartuple:

print (var)

return

# 調用printinfo 函數

printinfo( 10 )

printinfo( 70, 60, 50 )


匿名函數:lambda [arg1 [,arg2,.....argn]]:expression


變量的作用域:L--->E--->G--->B Python 中只有模塊(module),類(class)以及函數(def、lambda)才會引入新的作用域,其它的代碼塊(如 if/elif/else/、try/except、for/while等)是不會引入新的作用域的


global 和 nonlocal關鍵字:

num = 1

def fun1():

global num # 需要使用 global 關鍵字聲明

print(num)

num = 123

print(num)

fun1()

print(num)


def outer():

num = 10

def inner():

nonlocal num # nonlocal關鍵字聲明

num = 100

print(num)

inner()

print(num)

outer()






6>模塊



7>文件io操作。file操作文件open seek tell read write等方法,os操作目錄



8>支持對異常的處理



9>時間的處理

獲取時間戳:ticks = time.time()

時間元組:time.struct_time(tm_year=2018, tm_mon=5, tm_mday=7, tm_hour=10, tm_min=19, tm_sec=50, tm_wday=0, tm_yday=127, tm_isdst=0)

日期的轉換:

print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

print (time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))

print (time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y")))


元祖tuple:tup1 = (50,) tup1 = ('Google', 'Runoob', 1997, 2000);

訪問元祖:print ("tup1[0]: ", tup1[0]) print ("tup2[1:5]: ", tup2[1:5])

元組元素不可修改,但是可以進組合

# 以下修改元組元素操作是非法的。

# tup1[0] = 100

# 創建一個新的元組

tup3 = tup1 + tup2;

print (tup3)

刪除元組:del tup;

元祖運算符:len + *(復制) in for in


10>保留字

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']



11>import

將整個模塊(somemodule)導入,格式為: import somemodule

從某個模塊中導入多個函數,格式為: from somemodule import firstfunc, secondfunc, thirdfunc

將某個模塊中的全部函數導入,格式為: from somemodule import *


12>叠代器和生成器

叠代器:

list=[1,2,3,4]

it = iter(list) # 創建叠代器對象

for x in it:

print (x, end=" ")

生成器:自定義的叠代器

import sys

def fibonacci(n): # 生成器函數 - 斐波那契

a, b, counter = 0, 1, 0

while True:

if (counter > n):

return

yield a

a, b = b, a + b

counter += 1

f = fibonacci(10) # f 是一個叠代器,由生成器返回生成

while True:

try:

print (next(f), end=" ")

except StopIteration:

sys.exit()







6.操作mysql

面向對象、可以創建一個類


1>連接mysql數據庫 PyMysql(python3)

安裝PyMysql,鏈接mysql的庫

import pymysql

db = pymysql.connect("localhost","root","root","jiqi" )

cursor = db.cursor()

cursor.execute("SELECT VERSION()")

data = cursor.fetchone()

print ("Database version : %s " % data)

db.close()


2>創建表:

# 打開數據庫連接

db = pymysql.connect("localhost","testuser","test123","TESTDB" )

# 使用 cursor() 方法創建一個遊標對象 cursor

cursor = db.cursor()

# 使用 execute() 方法執行 SQL,如果表存在則刪除

cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")

# 使用預處理語句創建表

sql = """CREATE TABLE EMPLOYEE (

FIRST_NAME CHAR(20) NOT NULL,

LAST_NAME CHAR(20),

AGE INT,

SEX CHAR(1),

INCOME FLOAT )"""

cursor.execute(sql)

# 關閉數據庫連接

db.close()


3>數據庫插入操作:

import pymysql

# 打開數據庫連接

db = pymysql.connect("localhost","testuser","test123","TESTDB" )

# 使用cursor()方法獲取操作遊標

cursor = db.cursor()

# SQL 插入語句

sql = """INSERT INTO EMPLOYEE(FIRST_NAME,

LAST_NAME, AGE, SEX, INCOME)

VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""

try:

# 執行sql語句

cursor.execute(sql)

# 提交到數據庫執行

db.commit()

except:

# 如果發生錯誤則回滾

db.rollback()

# 關閉數據庫連接

db.close()


4>數據查詢操作:

# 打開數據庫連接

db = pymysql.connect("localhost","testuser","test123","TESTDB" )

# 使用cursor()方法獲取操作遊標

cursor = db.cursor()

# SQL 查詢語句

sql = "SELECT * FROM EMPLOYEE \

WHERE INCOME > '%d'" % (1000)

try:

# 執行SQL語句

cursor.execute(sql)

# 獲取所有記錄列表

results = cursor.fetchall()

for row in results:

fname = row[0]

lname = row[1]

age = row[2]

sex = row[3]

income = row[4]

# 打印結果

print ("fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \

(fname, lname, age, sex, income ))

except:

print ("Error: unable to fetch data")

# 關閉數據庫連接

db.close()



5>數據庫更新操作:

import pymysql

# 打開數據庫連接

db = pymysql.connect("localhost","testuser","test123","TESTDB" )

# 使用cursor()方法獲取操作遊標

cursor = db.cursor()

# SQL 更新語句

sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M')

try:

# 執行SQL語句

cursor.execute(sql)

# 提交到數據庫執行

db.commit()

except:

# 發生錯誤時回滾

db.rollback()

# 關閉數據庫連接

db.close()


6>刪除操縱

import pymysql

# 打開數據庫連接

db = pymysql.connect("localhost","testuser","test123","TESTDB" )

# 使用cursor()方法獲取操作遊標

cursor = db.cursor()

# SQL 刪除語句

sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20)

try:

# 執行SQL語句

cursor.execute(sql)

# 提交修改

db.commit()

except:

# 發生錯誤時回滾

db.rollback()

# 關閉連接

db.close()



7.網絡編程


服務器端口:

# 導入 socket、sys 模塊

import socket

import sys

# 創建 socket 對象

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 獲取本地主機名

host = socket.gethostname()

# 設置端口好

port = 9999

# 連接服務,指定主機和端口

s.connect((host, port))

# 接收小於 1024 字節的數據

msg = s.recv(1024)

s.close()

print (msg.decode('utf-8'))


客戶端端口:

# 導入 socket、sys 模塊

import socket

import sys

# 創建 socket 對象

serversocket = socket.socket(

socket.AF_INET, socket.SOCK_STREAM)

# 獲取本地主機名

host = socket.gethostname()

port = 9999

# 綁定端口號

serversocket.bind((host, port))

# 設置最大連接數,超過後排隊

serversocket.listen(5)

while True:

# 建立客戶端連接

clientsocket, addr = serversocket.accept()

print("連接地址: %s" % str(addr))

msg = '歡迎訪問菜鳥教程!' + "\r\n"

clientsocket.send(msg.encode('utf-8'))

clientsocket.close()



8.多線程



創建多線程

import _thread

import time

# 為線程定義一個函數

def print_time( threadName, delay):

count = 0

while count < 5:

time.sleep(delay)

count += 1

print ("%s: %s" % ( threadName, time.ctime(time.time()) ))

# 創建兩個線程

try:

_thread.start_new_thread( print_time, ("Thread-1", 2, ) )

_thread.start_new_thread( print_time, ("Thread-2", 4, ) )

except:

print ("Error: 無法啟動線程")

while 1:

pass



使用 threading 模塊創建線程

import threading

import time

exitFlag = 0

class myThread (threading.Thread):

def __init__(self, threadID, name, counter):

threading.Thread.__init__(self)

self.threadID = threadID

self.name = name

self.counter = counter

def run(self):

print ("開始線程:" + self.name)

print_time(self.name, self.counter, 5)

print ("退出線程:" + self.name)

def print_time(threadName, delay, counter):

while counter:

if exitFlag:

threadName.exit()

time.sleep(delay)

print ("%s: %s" % (threadName, time.ctime(time.time())))

counter -= 1

# 創建新線程

thread1 = myThread(1, "Thread-1", 1)

thread2 = myThread(2, "Thread-2", 2)

# 開啟新線程

thread1.start()

thread2.start()

thread1.join()

thread2.join()

print ("退出主線程")



線程的同步:

class myThread (threading.Thread):

def __init__(self, threadID, name, counter):

threading.Thread.__init__(self)

self.threadID = threadID

self.name = name

self.counter = counter

def run(self):

print ("開啟線程: " + self.name)

# 獲取鎖,用於線程同步

threadLock.acquire()

print_time(self.name, self.counter, 3)

# 釋放鎖,開啟下一個線程

threadLock.release()

def print_time(threadName, delay, counter):

while counter:

time.sleep(delay)

print ("%s: %s" % (threadName, time.ctime(time.time())))

counter -= 1

threadLock = threading.Lock()

threads = []

# 創建新線程

thread1 = myThread(1, "Thread-1", 1)

thread2 = myThread(2, "Thread-2", 2)

# 開啟新線程

thread1.start()

thread2.start()

# 添加線程到線程列表

threads.append(thread1)

threads.append(thread2)

# 等待所有線程完成

for t in threads:

t.join()

print ("退出主線程")



線程的優先級隊列:

import queue

import threading

import time

exitFlag = 0

class myThread (threading.Thread):

def __init__(self, threadID, name, q):

threading.Thread.__init__(self)

self.threadID = threadID

self.name = name

self.q = q

def run(self):

print ("開啟線程:" + self.name)

process_data(self.name, self.q)

print ("退出線程:" + self.name)

def process_data(threadName, q):

while not exitFlag:

queueLock.acquire()

if not workQueue.empty():

data = q.get()

queueLock.release()

print ("%s processing %s" % (threadName, data))

else:

queueLock.release()

time.sleep(1)

threadList = ["Thread-1", "Thread-2", "Thread-3"]

nameList = ["One", "Two", "Three", "Four", "Five"]

queueLock = threading.Lock()

workQueue = queue.Queue(10)

threads = []

threadID = 1

# 創建新線程

for tName in threadList:

thread = myThread(threadID, tName, workQueue)

thread.start()

threads.append(thread)

threadID += 1

# 填充隊列

queueLock.acquire()

for word in nameList:

workQueue.put(word)

queueLock.release()

# 等待隊列清空

while not workQueue.empty():

pass

# 通知線程是時候退出

exitFlag = 1

# 等待所有線程完成

for t in threads:

t.join()

print ("退出主線程")




9.xml解析

dom解析

#!/usr/bin/python3

from xml.dom.minidom import parse

import xml.dom.minidom

# 使用minidom解析器打開 XML 文檔

DOMTree = xml.dom.minidom.parse("movies.xml")

collection = DOMTree.documentElement

if collection.hasAttribute("shelf"):

print ("Root element : %s" % collection.getAttribute("shelf"))

# 在集合中獲取所有電影

movies = collection.getElementsByTagName("movie")

# 打印每部電影的詳細信息

for movie in movies:

print ("*****Movie*****")

if movie.hasAttribute("title"):

print ("Title: %s" % movie.getAttribute("title"))

type = movie.getElementsByTagName('type')[0]

print ("Type: %s" % type.childNodes[0].data)

format = movie.getElementsByTagName('format')[0]

print ("Format: %s" % format.childNodes[0].data)

rating = movie.getElementsByTagName('rating')[0]

print ("Rating: %s" % rating.childNodes[0].data)

description = movie.getElementsByTagName('description')[0]

print ("Description: %s" % description.childNodes[0].data)



sax解析:

#!/usr/bin/python3

import xml.sax

class MovieHandler( xml.sax.ContentHandler ):

def __init__(self):

self.CurrentData = ""

self.type = ""

self.format = ""

self.year = ""

self.rating = ""

self.stars = ""

self.description = ""

# 元素開始調用

def startElement(self, tag, attributes):

self.CurrentData = tag

if tag == "movie":

print ("*****Movie*****")

title = attributes["title"]

print ("Title:", title)

# 元素結束調用

def endElement(self, tag):

if self.CurrentData == "type":

print ("Type:", self.type)

elif self.CurrentData == "format":

print ("Format:", self.format)

elif self.CurrentData == "year":

print ("Year:", self.year)

elif self.CurrentData == "rating":

print ("Rating:", self.rating)

elif self.CurrentData == "stars":

print ("Stars:", self.stars)

elif self.CurrentData == "description":

print ("Description:", self.description)

self.CurrentData = ""

# 讀取字符時調用

def characters(self, content):

if self.CurrentData == "type":

self.type = content

elif self.CurrentData == "format":

self.format = content

elif self.CurrentData == "year":

self.year = content

elif self.CurrentData == "rating":

self.rating = content

elif self.CurrentData == "stars":

self.stars = content

elif self.CurrentData == "description":

self.description = content

if ( __name__ == "__main__"):

# 創建一個 XMLReader

parser = xml.sax.make_parser()

# 關閉命名空間

parser.setFeature(xml.sax.handler.feature_namespaces, 0)

# 重寫 ContextHandler

Handler = MovieHandler()

parser.setContentHandler( Handler )

parser.parse("movies.xml")


我的python基礎總結