python基礎整理----基本概念和知識,python----
整理一下python的基本概念和知識, 主要用python3為文法標準.
python介紹
一種物件導向的解釋性電腦設計語言,具有豐富和強大的庫。
- python定位:“優雅”、“明確”、“簡單”
- 多種應用情境:可以寫工具,後台服務,移動端等等。
運行環境搭建
下載對應版本的Python安裝,官網地址如下
https://www.python.org/
運行get-pip.py安裝pip (Py3內建pip)
https://bootstrap.pypa.io/get-pip.py
添加python目錄及Scripts子目錄添加至環境變數中
安裝虛擬環境
pip install virtualenv
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32Type "help", "copyright", "credits" or "license" for more information.>>>
編輯器選擇
Sublime Text,Wing IDE,pycharm
基本文法和概念
變數
>>> x=3>>> x*26>>> x*9992997
整數、浮點數
x=567(整數)y=5.67(浮點數)x/y(預設返回浮點數)x//y(預設返回整數)
函數
def checkIsHasCapitalLetter(filename): if filename.islower() == True: return True else: common.outError(filename + "檔案名稱存在大寫.................") return Fasle
縮排,流程式控制制語句for,if,while等都靠縮排識別,還有函數也是
for i in range(0, 5): print(i)
模組
>>>import common(允許使用命名空間訪問common裡的東西)>>>common.count()>>>from common import count(添加到當前命名空間)>>>count()
執行python程式
python.exe test.py
常用資料結構
- 列表(list)
- 字典(dict)
- 集合(set)
- 元組(tuple)
常用資料類型
字串基礎
字串是不可以改變的,和java一樣,如若賦值,其實是建立了一個字串
字串定義,可以是單引號,也可以是雙引號,字元拼接用+號
>>> "hello world"'hello world'>>> 'hello world''hello world'>>> "hello" + "world"'helloworld'>>> "don't let go""don't let go">>> 'don\'t let go'(轉義單引號)"don't let go">>> "don\'t let go""don't let go">>> r"don\'t let go"(前面帶"r"不轉義)"don\\'t let go"
字串尋找
>>> "nothing to see here".find("see")11>>> "nothing to see here".find("ok")-1
字串格式化
>>> filename = "notepad">>> "get file: %s ok"%filename'get file: notepad ok'
UNICODE,python3字串是以Unicode編碼的
1.將其他字串格式轉為unicode:
ret=str.decode("gb2312")ret=str.decode("ascii")ret=str.decode("utf-8")
2.將unicode字元轉為其他字串格式:
ret=str.encode(“gb2312”)ret=str.encode("ascii")ret=str.encode("utf-8")
Regex
http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html
擴充
字串內建函數,len(), split(), upper(), strip()多行字串字串下標[0],[-2]