python基礎整理----基本概念和知識,python----

來源:互聯網
上載者:User

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子目錄添加至環境變數中

 

安裝虛擬環境

  • 安裝virtualenv
pip install virtualenv
  • 配置正確的話,會進入python解譯器介面
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]

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.