python基礎操做

pycharm python編輯工具
ctrl + D 複製當前行
ctrl + x 刪除行
ctrl + / 註釋行

佔位符
%s 字符串
%d 數字    %f小數,用得少
name=123
print("test %s" % name)
~~~~~~~~
info = '3abc'
info2 = '''test {hook}'''.format(hook=info)
info2 = '''test {0}'''.format(info)  #或者
print(info2)

讀取原始字符串
str=r'C:\work\音樂基地\報表'
#########################
_pycache_ 目錄
 python的編譯文件,還不能被機器能直接識別,相似於預編譯,python是一門先編譯後解釋的語言,就是邊編譯邊執行
.pyc 文件是python編譯後保存在內存中的pycodeobject中,當執行結束後python解釋器將pycodeobject寫回pyc文件中,第2次再執行則直接讀取該pyc文件

>>> import sys
>>> print(sys.path)            #系統默認路徑
                            #建議放在/usr/local/python3/lib/python3.6/site-packages目錄下

#####################
dir(__builtins__)
help(hex)
import random
secret = random.randint(1,10)
dir(list)
字符串處理的方法
http://bbs.fishc.com/forum.php?mod=viewthread&tid=38992&extra=page%3D1%26filter%3Dtypeid%26typeid%3D403
多行註釋''' 這裏是註釋的內容'''
##########模塊###################
import sys
print(sys.path)
print(sys.argv[2]) 1 2 3 4   -> 3

import os
cmd-res = os.system("df -h")   執行系統命令,不保存結果,直接輸出到屏幕上
print("-->",cmd-res)    -->  0  

cmd-res = os.popen("dir")
print("-->",cmd-res)    -->  內存地址

cmd-res = os.popen("dir").read()
print("-->",cmd-res)    -->  讀取內存地址

os.mkdir("new_dir")

import getpass
pwd = getpass.getpass("請輸入密碼:")

#####################
a,b,c=3,5,7
>>> a,b,c = 1,3,5
>>> d = a if a> b else c
>>> d
5
>>>result = 值1 if 條件 else 值2
#####################
讀取多行字符串
str="""
aaa
bbb
ccc
"""
print(str)
#####################
2者類型一至判斷
isinstance(a,str)
#####################
** 冪運算 2 ** 3 = 8    -3 ** 2 = -9
// 求整數 5 // 2 = 2
3 < 4 < 5   ==  3 < 4 and 4 < 5
#####################
smart = x if x<y else y
assert 3 > 4 程序自動退出,並拋出assertionerror的異常
#####################
favourite = 'abcde'
for i in favourite:
    print(i,end=' ')
結果a b c d e
#####################
for i in favourite:
    print(i + ' ')
結果爲
a
b
c
d
e
f
g
#####################
list(range(5))
結果
[0, 1, 2, 3, 4]
#####################
>>> for i in range(2,9):
...     print(i)
...
2
3
4
5
6
7
8
#####################
>>> for i in range(2,9,2):
...     print(i)
...
2
4
6
8
######################
init = 'haha'
answer = input('請輸入一段話:')

while True:
        if answer == init:
                break
        answer = input('no,no,no,go on:')

print('game over!!!')
#####################列表操做#####################
刪除列表變量del member
>>> member = ['haha','hehe','heihei','youxi','youxiwa','youxiwang']
>>> member.append('goodnight')
>>> member
['haha', 'hehe', 'heihei', 'youxi', 'youxiwa', 'youxiwang', 'goodnight']
#####################
>>> member.extend([123,3.14])
>>> member
['haha', 'hehe', 'heihei', 'youxi', 'youxiwa', 'youxiwang', 'goodnight', 123, 3.14]
#####################
>>> member.insert(0,'個人第一')
>>> member
['個人第一', 'haha', 'hehe', 'heihei', 'youxi', 'youxiwa', 'youxiwang', 'goodnight', 123, 3.14]
#####################
>>> temp = member[0]
>>> member[0] = member[1]
>>> member[1] = temp
>>> member
['haha', '個人第一', 'hehe', 'heihei', 'youxi', 'youxiwa', 'youxiwang', 'goodnight', 123, 3.14]
#####################
member.remove('haha')
>>> del member[1]
>>> del member
剔除
>>> member=['個人第一', 'heihei', 'youxi', 'youxiwa', 'youxiwang', 'goodnight', 123, 3.14]
>>> member
['個人第一', 'heihei', 'youxi', 'youxiwa', 'youxiwang', 'goodnight', 123, 3.14]
>>> name=member.pop()
>>> name
3.14
>>> member
['個人第一', 'heihei', 'youxi', 'youxiwa', 'youxiwang', 'goodnight', 123]
>>> member
['個人第一', 'heihei', 'youxi', 'youxiwa', 'youxiwang', 'goodnight', 123]
>>> member.pop(4)
'youxiwang'
>>> member
['個人第一', 'heihei', 'youxi', 'youxiwa', 'goodnight', 123]
>>> member[0:5]
['個人第一', 'heihei', 'youxi', 'youxiwa', 'goodnight']
>>> member
['個人第一', 'heihei', 'youxi', 'youxiwa', 'goodnight', 123]
>>> member[:2]
['個人第一', 'heihei']
>>> member[2:]
['youxi', 'youxiwa', 'goodnight', 123]
>>> member[:]                #這個和member是2個對象,原使用sort排序後,member[:]不受影響
['個人第一', 'heihei', 'youxi', 'youxiwa', 'goodnight', 123]
>>> list1=[123,456]
>>> list2=[234,123]
>>> list3=[123,456]
>>> (list1 < list2) and (list1 == list3)
True
>>> list4 = list1 + list2
>>> list4
[123, 456, 234, 123]
>>> list3 *=2
>>> list3
[123, 456, 123, 456]
>>> 123 in list3
True
>>> '遊戲' not in list3
True
>>> list5 = [123,['小甲魚','牡丹'],234]
>>> '小甲魚' in list5
False
>>> '小甲魚' in list5[1]
True
>>> list5
[123, ['小甲魚', '牡丹'], 234]
>>> list5[1][1]
'牡丹'
>>> list3 *=4
>>> list3
[123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456]
>>> list3.count(123)
8
>>> list3.index(123,2,4)    #從第2位開始數,到第4位
2
以下
>>> list3.index(123,1,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 123 is not in list
>>> list3.index(123,1)
2

[].copy()只能copy一層結構,由於在內存中,列表實際存的是一層指針,以下
>>> name
['遊戲', ['good', 'night'], 'ddd', 'bbb', 'ccc', 'heihei']
>>> name2
['aaa', 'ddd', 'bbb', 'ccc', 'heihei']
>>> name2=name.copy()
>>> name2
['遊戲', ['good', 'night'], 'ddd', 'bbb', 'ccc', 'heihei']
>>> name[0]='aaa'
>>> name[1][1]='youxiwang'
>>> name
['aaa', ['good', 'youxiwang'], 'ddd', 'bbb', 'ccc', 'heihei']
>>> name2
['遊戲', ['good', 'youxiwang'], 'ddd', 'bbb', 'ccc', 'heihei']
~~~~~~~~~~~~~淺copy三種方式~~~~~~~~~
>>> person=['name',['a',100]]
>>> p1=copy.copy(person)
>>> p2=person[:]
>>> p3=list(person)
>>> p1
['name', ['a', 100]]
>>> p2
['name', ['a', 100]]
>>> p3
['name', ['a', 100]]
~~~~~~~~~~~~~完整copy~~~~~~~~~
>>> name
['aaa', 'bbb', 'ccc', 'ddd', ['123', '遊戲', '345'], 'eee']
>>> import copy;
>>> name2=copy.copy(name)
>>> name2
['aaa', 'bbb', 'ccc', 'ddd', ['123', '遊戲', '345'], 'eee']
>>> name2[4][2]="玩笑"
>>> name
['aaa', 'bbb', 'ccc', 'ddd', ['123', '遊戲', '玩笑'], 'eee']
>>> name2
['aaa', 'bbb', 'ccc', 'ddd', ['123', '遊戲', '玩笑'], 'eee']
>>> del name2
>>> name2=copy.deepcopy(name)                                    deepcopy
>>> name
['aaa', 'bbb', 'ccc', 'ddd', ['123', '遊戲', '玩笑'], 'eee']
>>> name2
['aaa', 'bbb', 'ccc', 'ddd', ['123', '遊戲', '玩笑'], 'eee']
>>> name2[4][0]="神話"
>>> name
['aaa', 'bbb', 'ccc', 'ddd', ['123', '遊戲', '玩笑'], 'eee']
>>> name2
['aaa', 'bbb', 'ccc', 'ddd', ['神話', '遊戲', '玩笑'], 'eee']
~~~~~~~~~~~~~列表循環~~~~~~~~~
>>> for i in name2:
...     print(i)
>>>
~~~~~~~~~~~~~跳着切片~~~~~~~~~
>>> name
['aaa', 'bbb', 'ccc', 'ddd', ['123', '遊戲', '玩笑'], 'eee']
>>> print(name[0:-1:2])
['aaa', 'ccc', ['123', '遊戲', '玩笑']]
>>> print(name[::2])
['aaa', 'ccc', ['123', '遊戲', '玩笑']]

-----重要的2個參數-------
>>> list5
[123, ['小甲魚', '牡丹'], 234]
>>> list5.reverse()
>>> list5
[234, ['小甲魚', '牡丹'], 123]
>>> list6.sort(reverse=True)      #倒序排列
>>> list6
[9, 6, 5, 4, 2, 1]
##############元組沒法更改#######
元組只有2個方法count()和index()
>>> temp2 = 2,3,4,5        #建立元祖,逗號是關鍵
>>> type(temp2)
<class 'tuple'>
>>> temp2 = ()
>>> type(temp2)
<class 'tuple'>
>>> 8 * 8,
(64,)
>>> 8 * (8,)
(8, 8, 8, 8, 8, 8, 8, 8)
>>> temp2 = 8 * 8,
>>> type(temp2)
<class 'tuple'>
>>> temp = ('小甲魚','黑夜','迷途','小布丁')
>>> temp = temp[:2] + ('怡靜',) + temp[2:]
>>> temp
('小甲魚', '黑夜', '怡靜', '迷途', '小布丁')
>>> del temp
#####################
>>> "{0} love {b}.{c}".format('i',b='fishc',c='com')
'i love fishc.com'    #位置參數必須在變量參數以前
>>> "{0:.1f}{1}".format(27.658,'GB')
'27.7GB'        #f定點數==浮點數
>>> "%c %c %c" % (97,98,99)
'a b c'
字符串格式化符號含義
http://bbs.fishc.com/forum.php?mod=viewthread&tid=92997&extra=page%3D1%26filter%3Dtypeid%26typeid%3D403

###########在列表中加入一列編號#############
for i in product_list:
    print(product_list.index(i),i)
另外一種方式
for index,i in enumerate(product_list):
    print(index,i)


###################字符串string轉化二進制bytes##########################
網絡傳輸都是二進制bytes傳輸
string -encode-> bytes
bytes -decode-> string
>>> msg='我'
>>> msg.encode()
b'\xe6\x88\x91'
>>> msg.encode('utf-8')
b'\xe6\x88\x91'
>>> msg.encode('utf-8').decode('utf-8')
>>> print(msg.encode('utf-8').decode('utf-8'))
'我'
>>> print(msg.encode(encoding="utf-8").decode(encoding="utf-8"))

##############################字典#################################################
id_db = {
    123:{1:'abc',
         2:'bcd',
         3:'cde',
         },
    1234:{1:'abc',2:'haha',3:'cde',},
    123:{1:'abc',2:'hahaha',3:'cde',},
}

print(id_db)
id_db[1234][2]='wanxiao'
print(id_db)
print(id_db[1234])
~~~~~~~~~~輸出~~~~~~~~~~~~~~~
id_db = {
    123:{1:'abc',
         2:'bcd',
         3:'cde',
         },
    1234:{1:'abc',2:'haha',3:'cde',},
    123:{1:'abc',2:'hahaha',3:'cde',},
}

print(id_db)
id_db[1234][2]='wanxiao'        #修改
print(id_db)
print(id_db[1234])
id_db[1234][4]='wanxiao'        #增長
print(id_db[1234])
del id_db[1234][4]                #刪除 另一種方式id_db[1234].pop("key_name")
print(id_db[1234
for k,v in id_db.items():        循環一個字典,效率低,有一個字典轉換爲列表的過程
    print(k,v)
for key in id_db:
    print(key,id_db[key])        循環,效率高,適合大的數據量
字典格式
{key:value}
字典自動去重,只保留重複最後的key對應的value值



php

相關文章
相關標籤/搜索