码迷,mamicode.com
首页 > 编程语言 > 详细

我的python基础总结

时间:2018-05-07 11:52:53      阅读:138      评论:0      收藏:0      [点我收藏+]

标签: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基础总结

标签:python基础知识

原文地址:http://blog.51cto.com/13496382/2113434

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!