1. 程式人生 > >筆記1 python入門學習筆記

筆記1 python入門學習筆記

目錄


官方手冊

https://docs.python.org/3.7/tutorial/index.html

菜鳥站手冊地址:

https://www.runoob.com/python3/python3-basic-operators.html

python的執行方法

  1. Sublime編輯器執行python程式碼,用Ctrl+b
  2. cmd黑窗口裡 D:\phpStudy\PHPTutorial\WWW\python>python test.py

註釋

確保對模組, 函式, 方法和行內註釋使用正確的風格

Python中的註釋有單行註釋和多行註釋:

Python中單行註釋以 # 開頭,例如:

三個單引號,或者三個雙引號

    '''這是一個區塊連的函式,
        money:交易的金額
        last_value 獲取到陣列的最後一個單元(預設值為[1])
        Docstring'''

參考手冊:

https://www.runoob.com/python3/python3-comment.html

變數

  1. 變數可以理解為一個空盒子,向變數賦值可以理解為向空盒子子裡存東西,向變數重新賦值可以理解為把盒子裡原有的東西拿出來,放入新的東西
  2. 宣告變數前不需加任何宣告.php前需要加$,JavaScript前需要加var,python不需要
  3. 變數分為區域性變數全域性變數,區域性變數可以通過global轉為全域性變數
name = 'lisi'
def get_name():
    global name
    name = input('please input your are name:')
    
get_name() #執行函式區域性變數就會通過global轉為全域性變數

print(name) #輸出的是input裡您輸入的值

字串

字串的拼接

a= 'hello'
b='word'
c = a + b
print ("值為:", c) # 'helloword'

字串的換行

用三個單引號或者三個雙引號

str= '''asdfasdfasdf
asdfasdfasdfdfasdf
asdfasdfasdfdfasdf
'''
print (str)

列表 (相當於PHP裡的索引陣列)

參考菜鳥手冊
https://www.runoob.com/python3/python3-list.html

  • 相當裡php裡的陣列
  • 序列中的每個元素都分配一個數字 - 它的位置,或索引,第一個索引是0,第二個索引是1,依此類推。
list1 = ['Google', 'Runoob', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
 
print ("list1[0]: ", list1[0]) #list1[0]:  Google 
print ("list2[1:5]: ", list2[1:5]) #list2[1:5]:  [2, 3, 4, 5]

列表的新增

列表相當於php中的一維陣列,裡面元素是有序的,可以重複的.

  1. 向列表的最後面追加單元資料

列表的追加(向陣列的右邊追加) append()方法

list1 = ['Google', 'Runoob', 'Taobao']
list1.append('Baidu')
print ("更新後的列表 : ", list1) #['Google', 'Runoob', 'Taobao', 'Baidu']
  1. 向陣列後面拼接元素
list1 = ['Google', 'Runoob', 'Taobao']
n = ['a','b','c']
list1 += n
print(list1) #['Google', 'Runoob', 'Taobao', 'a', 'b', 'c']
  1. insert() 函式用於向列表指點任意列表的插入值。

    insert()方法 list.insert(index, obj)

list1 = ['Google', 'Runoob', 'Taobao']
list1.insert(1, 'Baidu')
print ('列表插入元素後為 : ', list1) #['Google', 'baidu', 'Runoob', 'Taobao']
  1. extend() 函式用於在列表末尾一次性追加另一個序列中的多個值(用新列表擴充套件原來的列表)。

extend()方法語法:

list.extend(seq)

list1 = ['Google', 'Runoob', 'Taobao']
list2=list(range(5)) # 建立 0-4 的列表
list1.extend(list2)  # 擴充套件列表
print ("擴充套件後的列表:", list1) # ['Google', 'Runoob', 'Taobao', 0, 1, 2, 3, 4]

5.extend 與 append 的區別

  • extend 與 append 方法的相似之處在於都是將新接收到引數放置到已有列表的後面。
  • 而 extend 方法只能接收 list,且把這個 list 中的每個元素新增到原 list 中。
  • 而 append 方法可以接收任意資料型別的引數,並且簡單地追加到 list 尾部。
list1 = ['Google', 'Runoob', 'Taobao']
list1.extend(['a','b'])  # 擴充套件列表
print (list1) # ['Google', 'Runoob', 'Taobao', 'a', 'b']

list2 = ['Google', 'Runoob', 'Taobao']
list2.append(['a','b'])  # 擴充套件列表
print (list2) # ['Google', 'Runoob', 'Taobao', ['a', 'b']]

https://www.runoob.com/python3/python3-att-list-extend.html

列表的刪除

方法一:可以使用 del 語句來刪除列表的指定元素,如下例項:

list = ['Google', 'Runoob', 1997, 2000]
print ("原始列表 : ", list) #原始列表 :  ['Google', 'Runoob', 1997, 2000]
del list[2]
print ("刪除第三個元素 : ", list) #刪除第三個元素 :  ['Google', 'Runoob', 2000]

方法二:remove()

描述

remove() 函式用於移除列表中某個值的第一個匹配項。

語法

remove()方法語法:
list.remove(obj)

list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']
list1.remove('Taobao')
print ("列表現在為 : ", list1) #列表現在為 :  ['Google', 'Runoob', 'Baidu']
list1.remove('Baidu')
print ("列表現在為 : ", list1) #列表現在為 :  ['Google', 'Runoob']

方法三:pop(列表索引)

描述
pop(列表索引) 函式用於移除列表中某索引的值

不列索引,預設刪除最後一個元素

list = ['Google', 'Runoob', 1997, 2000]
list.pop(2) 
print(list) #['Google', 'Runoob', 2000]

列表的更新 指定列表裡元素的索引下標重新賦值

你可以對列表的資料項進行修改或更新,如下所示:
指定列表裡元素的索引下標重新賦值

list = ['Google', 'Runoob', 1997, 2000]
print ("第三個元素為 : ", list[2]) #第三個元素為 :  1997
list[2] = 2001
print ("更新後的第三個元素為 : ", list[2]) #更新後的第三個元素為 :  2001

列表擷取與拼接

Python的列表擷取與字串操作型別.

如下所示:

L=['Google', 'Runoob', 'Taobao']

Python 表示式 結果 描述
L[2] 'Taobao' 讀取第三個元素
L[-2] Runoob' 從右側開始讀取倒數第二個元素: count from the right
L[1:] ['Runoob', 'Taobao'] 輸出從第二個元素開始後的所有元素

拼接

list1 = ['Google', 'Runoob', 'Taobao']
n = ['a','b','c']
list1 += n
print(list1) #['Google', 'Runoob', 'Taobao', 'a', 'b', 'c']

巢狀列表

把兩個一維列表巢狀成一個兩維列表

a = ['Google', 'Runoob', 'Taobao']
b = ['a','b','c']
c = [a,b]
print(c) #[['Google', 'Runoob', 'Taobao'], ['a', 'b', 'c']]

列表的賦值

copy()和直接=賦值的區別:

  1. 使用=直接賦值,是引用賦值,更改一個,另一個同樣會變
  2. copy() 則顧名思義,複製一個副本,原值和新複製的變數互不影響

例子:
https://www.runoob.com/python3/python3-att-list-copy.html

元組

Python 的元組與列表類似,不同之處在於元組的元素不能修改。

元組使用小括號,列表使用方括號。

相同之處,它們的元素都是有序的,可以重複的

元組建立很簡單,只需要在括號中新增元素,並使用逗號隔開即可。

手冊:
https://www.runoob.com/python3/python3-tuple.html


字典

可以理解為鍵值對形式的json格式字串形式

  1. 字典的每個鍵值(key=>value)對用冒號(:)分割,每個對之間用逗號(,)分割,整個字典包括在花括號({})中 ,

    格式如下所示:

    d = {key1 : value1, key2 : value2 }

  2. 字典是另一種可變容器模型,且可儲存任意型別物件。
  3. 鍵必須是唯一的,但值則重複。
  4. 值可以取任何資料型別,但鍵必須是不可變的,如字串,數字或元組。
  5. 元素是無序的

一個簡單的字典例項:

dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}

也可如此建立字典:

dict1 = { 'abc': 456 };
dict2 = { 'abc': 123, 98.6: 37 };

手冊:
https://www.runoob.com/python3/python3-dictionary.html


列表 集合 元組 字典 的特性

# animal = ['pig','dog','cat','dog','pig'] #列表
# animal = {'pig','dog','cat','dog','pig'} #集合
# animal = ('pig','dog','cat','dog','pig') #元組
# animal = {'name':'lisi','age':28,'weight':120} #字典
型別 特性 符號
陣列 可編輯 有序 允許重複 []
集合 可編輯 無序 不可重複 {}
元組 不可編輯 有序 可重複 ()
字典 無序 可編輯 值:可重複 鍵:不可重複 鍵值對形式 {}

函式

def 自定義一函式

def test():
    print('My name is haima')
test() #My name is haima

迴圈

for迴圈

blockchian = [[[1], 12.0], [[[1], 12.0], 13.0], [[[[1], 12.0], 13.0], 14.0]]
for block in blockchian:
    '''這是一個區塊連的函式,
        money:交易的金額
        last_value 獲取到陣列的最後一個單元(預設值為[1])
        Docstring'''
    print(block)

while迴圈

while True:
    print('請輸入數字:')
    print('1:繼續交易')
    print('2:列印當前區塊鏈')
    print('q:退出當前操作')
    userChoice_num = get_userChoice_num()
    if userChoice_num == '1':
        money = get_input_money()
        add_value(money,get_last_value())
    elif userChoice_num == '2':
        print_blockchian()
    elif userChoice_num == 'q':
        break #continue
    else:
        print('請輸入列表裡的數字!')

range()函式迴圈列表

也可以使range以指定數字開始並指定不同的增量(甚至可以是負數,有時這也叫做'步長'):

for i in range(0, 10, 3) :
    print(i)

列印結果:

0
3
6
9

手冊參考

https://www.runoob.com/python3/python3-loop.html

判斷if..elif..else

if userChoice_num == '1':
    money = get_input_money()
    add_value(money,get_last_value())
elif userChoice_num == '2':
    print_blockchian()
elif userChoice_num == 'q':
    break #continue
else:
    print('請輸入列表裡的數字!')

手冊參考

https://www.runoob.com/python3/python3-conditional-statements.html

breakcontinue的區別

break終止本次迴圈
continue跳過當前操作,執行下一次迴圈