教6個月前的Neil學Python

@[Python][ch01]

教6個月前的自己學Python


Python 數據類型

  • python3有六大類數據 Number(數字)、String(字符串/廖雪峰)、List(列表)、Tuple(元組)、Sets(集合)、Dictionary(字典)

數據類型練習:

  • Dictionary
    name = {1:'zhang', 3:'nai', 'a': 'tao', 3: 'neil', 4: 'tao', 1: 'Neil'}
    print (name)

    print (name.values())
    print (name.keys())

    b = set(name.values())
    print (b)

    c = set(name.keys())
    print (c)
    d = b&c
    print (d)

    diff = b^c
    print (diff)
  • Dictionary練習結果輸出:
    {1: 'Neil', 3: 'neil', 'a': 'tao', 4: 'tao'}
    dict_values(['Neil', 'neil', 'tao', 'tao'])
    dict_keys([1, 3, 'a', 4])
    {'neil', 'Neil', 'tao'}
    {1, 3, 'a', 4}
    set()
    {1, 'tao', 3, 'a', 4, 'neil', 'Neil'}


  • List : for ch0 for random 4 different numbers

# random 4 different numbers - 02
# -*- coding: utf-8 -*-

import random

guess_number = []

list = [i for i in range(10)]

print ("Original list =", list)

# compare above out put style,

for i in range(4):

    print (("i = %s") % i)

    j = random.randint(0, len(list) - 1)
    print (("j = %s") % j)

    guess_number.append(list[j])
    print(("list['j' = %s] = %s ") % (j, list[j]))
    print("guess_number = %s " % guess_number)

# you may can optimize above output code.

    print(("list.pop('j' = %s) = %s") % (j,list.pop(j)))

##    list.pop(j)
# It very import to cancel above code, because list.pop(j) already excute in code "print.....

    print(("list after pop ['j' = %s]) = %s") % (j, list))
    print ("*"*10)
    print ("*"*20)

print(f'method 2 (list): guess_number = {guess_number}')
print (len(list)-1)
print (list.pop(j))

# you can guess why abve list.pop(j) = 7?

print (guess_number[1:3])


  • List練習結果輸出 : ch0 for random 4 different numbers
PS C:\Users\CNNEZHA2\mystuff\Python-exercises_2017_07> py36 ex10.py

Original list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
i = 0
j = 5
list['j' = 5] = 5
guess_number = [5]
list.pop('j' = 5) = 5
list after pop ['j' = 5]) = [0, 1, 2, 3, 4, 6, 7, 8, 9]
**********
********************
i = 1
j = 2
list['j' = 2] = 2
guess_number = [5, 2]
list.pop('j' = 2) = 2
list after pop ['j' = 2]) = [0, 1, 3, 4, 6, 7, 8, 9]
**********
********************
i = 2
j = 6
list['j' = 6] = 8
guess_number = [5, 2, 8]
list.pop('j' = 6) = 8
list after pop ['j' = 6]) = [0, 1, 3, 4, 6, 7, 9]
**********
********************
i = 3
j = 4
list['j' = 4] = 6
guess_number = [5, 2, 8, 6]
list.pop('j' = 4) = 6
list after pop ['j' = 4]) = [0, 1, 3, 4, 7, 9]
**********
********************
method 2 (list): guess_number = [5, 2, 8, 6]
5
7
[2, 8]





  • List code2
# random 4 differernt numbers-01

import random
guess_number = []

list = [i for i in range(10)]
guess_number = random.sample(list, 4)

# for test
print (guess_number)

while guess_number[0] == 0:
    guess_number = random.sample(list, 4)

# for test
    print("new_guess_number", guess_number)

print (f'method 1(random.sample): guess_number = {guess_number}')

# for test different print code
print (guess_number)

  • code2 輸出結果
    For comparing the code 1 and 2 , which achieve the sam function, and code 2 even better on functions, for example, code 2 sucessfully avoid the first num = 0

PS C:\Users\CNNEZHA2\mystuff\Python-exercises_2017_07> py36 ex09.py
[5, 7, 9, 2]
method 1(random.sample): guess_number = [5, 7, 9, 2]
[5, 7, 9, 2]
PS C:\Users\CNNEZHA2\mystuff\Python-exercises_2017_07> py36 ex09.py
[4, 1, 9, 6]
method 1(random.sample): guess_number = [4, 1, 9, 6]
[4, 1, 9, 6]
PS C:\Users\CNNEZHA2\mystuff\Python-exercises_2017_07> py36 ex09.py
[9, 4, 6, 8]
method 1(random.sample): guess_number = [9, 4, 6, 8]
[9, 4, 6, 8]
PS C:\Users\CNNEZHA2\mystuff\Python-exercises_2017_07> py36 ex09.py
[0, 4, 7, 2]
new_guess_number [2, 7, 8, 9]
method 1(random.sample): guess_number = [2, 7, 8, 9]
[2, 7, 8, 9]
PS C:\Users\CNNEZHA2\mystuff\Python-exercises_2017_07>



函數

菜鳥教程


文本記錄(Markdown)

1. 目前文檔發布流程 (使用maxiang 編輯,簡書發布)

流程圖(maxing代碼):
st=>start: 在maxiang編輯
op1=>operation: maxiang直接同步到evernote
cond=>condition: 檢查,沒有錯誤
op2=>operation: 復制maxing代碼到簡書發布
e2=>end: 最終結束所有發布與保存!

st->op1->cond
cond(yes)->op2->e2
cond(no)->op1


  • 流程圖如下:
最終簡書發布流程圖

2. 目前 maxiang 編輯中的代碼編輯流程

  • 要在maxing中加入整片的代碼段,代碼段的拷貝來源要從MarkdownPad2來,Atom上直接拷貝始終會出現順序行的問題, 相關流程如下:
流程圖(maxing代碼):
st=>start: 從Atom拷貝到MarkdownPad2 
op1=>start: 拷貝代碼從MarkdownPad2 到 maxing
e1=>end: 檢查相關代碼間距
e2=>end: 發布

MarkdwonPad2 => maxing

st->op1->e1->e2

code upload process

Class 類.....累 /面向對象編程......對象

定義:

  • Class是用來描述具有相同的屬性和方法的對象的集合。它定義了該集合中每個對象所共有的屬性和方法。

  • 對象是類的實例 & 類是抽象的模板

  1. 實例變量:定義在方法中的變量,只作用于當前實例的類

  2. 方法:類中定義的函數。

  3. 對象:通過類定義的數據結構實例。對象包括兩個數據成員(類變量和實例變量)和方法

    面向對象 (Object Oriented Programming) 的設計思想是從自然界中來的,因為在自然界中,類(Class)實例(Instance)的概念是很自然的。Class是一種抽象概念,比如我們定義的Class——Student,是指學生這個概念,而實例(Instance)則是一個個具體的Student,比如,Bart Simpson和Lisa Simpson是兩個具體的Student。所以,面向對象的設計思想是抽象出Class,根據Class創建Instance。面向對象的抽象程度又比函數要高,因為一個Class既包含數據,又包含操作數據的方法。

實例:

  • 實例化:創建一個類的實例,類的具體對象。灰常重要,類始終是要服務與對象,也就是要實例化。

  • 第一種方法init()方法是一種特殊的方法,被稱為類的構造函數或初始化方法,當創建了這個類的實例時就會調用該方法. 也就是說在實例代碼出來時,馬上就對對象按照init() (方法)進行了初始化。

  • init方法的第一個參數永遠是self,表示創建的實例本身,因此,在init方法內部,就可以把各種屬性綁定到self,因為self就指向創建的實例本身

  • Generator functions A function or method which uses the yield statement (see section The yield statement) is called a generator function. Such a function, when called, always returns an iterator object which can be used to execute the body of the function: calling the iterator’s iterator.next() method will cause the function to execute until it provides a value using the yield statement. When the function executes a return statement or falls off the end, a StopIteration exception is raised and the iterator will have reached the end of the set of values to be returned.

self : init方法的第一個參數

  • 類的方法與普通的函數只有一個特別的區別——它們必須有一個額外的第一個參數名稱, 按照慣例它的名稱是 self.

  • self 代表類的實例,self 在定義類的方法時是必須有的,雖然在調用時不必傳入相應的參數。

  • self 代表的是類的實例,代表當前對象的地址,而 self.class 則指向類。self 不是 python 關鍵字,我們把他換成 runoob 也是可以正常執行的.

  • When an instance method object is called, the underlying function (func) is called, inserting the class instance (self) in front of the argument list. For instance, when C is a class which contains a definition for a function f(), and x is an instance of C, calling x.f(1) is equivalent to calling C.f(x, 1).

Python內置類屬性:

  1. dict : 類的屬性(包含一個字典,由類的數據屬性組成)
  2. doc : 類的文檔字符串
  3. name: 類名
  4. module: 類定義所在的模塊(類的全名是 main.className,如果類位于一個導入模塊mymod中,那么className.module 等于 mymod
  5. bases : 類的所有父類構成元素(包含了一個由所有父類組成的元組)

類的繼承:

  • 繼承語法 class 派生類名(基類名)://... 基類名寫在括號里,基本類是在類定義的時候,在元組之中指明的
  1. 在繼承中基類的構造init()方法)不會被自動調用,它需要在其派生類的構造中親自專門調用
  2. 在調用基類的方法時,需要加上基類的類名前綴,且需要帶上self參數變量。區別于在類中調用普通函數時并不需要帶上self參數
  3. Python總是首先查找對應類型的方法,如果它不能在派生類中找到對應的方法,它才開始到基類中逐個查找。(先在本類中查找調用的方法,找不到才去基類中找)。
  4. 通常,如果沒有合適的繼承類,就使用object類,這是所有類最終都會繼承的類.

類的私有屬性:

  • __private_attrs:兩個下劃線開頭,聲明該屬性為私有,不能在類的外部被使用或直接訪問。在類內部的方法中使用時 self.__private_attrs
  • Python不允許實例化的類訪問私有數據,但你可以使用 object._className__attrName 訪問屬性

python對象銷毀(垃圾回收):

Python 使用了引用計數這一簡單技術來跟蹤和回收垃圾。在 Python 內部記錄著所有使用中對象各有多少引用。一個內部跟蹤變量,稱為一個引用計數器。當對象被創建時, 就創建了一個引用計數, 當這個對象不再需要時, 也就是說, 這個對象的引用計數變為 0 時, 它被垃圾回收。但是回收不是"立即"的, 由解釋器在適當的時機,將垃圾對象占用的內存空間回收。

垃圾回收機制不僅針對引用計數為0的對象,同樣也可以處理循環引用的情況。循環引用指的是,兩個對象相互引用,但是沒有其他變量引用他們。這種情況下,僅使用引用計數是不夠的。Python 的垃圾收集器實際上是一個引用計數器和一個循環垃圾收集器。作為引用計數的補充, 垃圾收集器也會留心被分配的總量很大(及未通過引用計數銷毀的那些)的對象。 在這種情況下, 解釋器會暫停下來, 試圖清理所有未引用的循環。

Python code style

1. Immediately inside parentheses, brackets or braces.
Yes: spam(ham[1], {eggs: 2})
No:  spam( ham[ 1 ], { eggs: 2 } )

2. Immediately before a comma, semicolon, or colon:
Yes: if x == 4: print x, y; x, y = y, x
No:  if x == 4 : print x , y ; x , y = y , x


3.  However, in a slice the colon acts like a binary operator, and should have equal amounts on either side (treating it as the operator with the lowest priority).  In an extended slice, both colons must have the same amount of spacing applied.  Exception: when a slice parameter is omitted, the space is omitted

Yes:
ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
ham[lower:upper], ham[lower:upper:], ham[lower::step]
ham[lower+offset : upper+offset]
ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
ham[lower + offset : upper + offset]

No:
ham[lower + offset:upper + offset]
ham[1: 9], ham[1 :9], ham[1:9 :3]
ham[lower : : upper]
ham[ : upper]


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1.元類 1.1.1類也是對象 在大多數編程語言中,類就是一組用來描述如何生成一個對象的代碼段。在Python中這...
    TENG書閱讀 1,315評論 0 3
  • 轉至元數據結尾創建: 董瀟偉,最新修改于: 十二月 23, 2016 轉至元數據起始第一章:isa和Class一....
    40c0490e5268閱讀 1,776評論 0 9
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,967評論 19 139
  • 自從有了簡書,記了手帳,自己還是蠻自律的,說每天一副水彩畫,再忙也都克服而沒有間斷,只是很久沒有發布了,進步也說不...
    一筆一畫一世界閱讀 217評論 5 4
  • 假如時間可以回溯 我希望能夠回到1995 在那年的昆明待到八月初八 就可以和你們一起共賞春城飛花 假如時間可以繼續...
    飄蕩的貓咪哥閱讀 431評論 0 0