Python初學筆記~
阿新 • • 發佈:2019-01-24
工作需要,要接觸python的程式,學習一下~
使用的3.2版本~話說比2.5變化還真多~print都變了~
總體感覺,py比較perl來說,特點還是非常之強烈的~
1、py可以自動字串複製:
>>> x='3'
>>> x*3
'333'
>>>
2、py是強型別變數,和perl不同,型別變數不能混用~
3、字串連線+號和join等string函式:
>>> x='z' >>> y='zr' >>> x=x+y >>> print(x) zzr >>> print(x,":",y) zzr : zr
>>> aa =['1','2','3']
>>> split = '**'
>>> aftersplit=split.join(aa)
>>> print (aftersplit)
1**2**3
>>> x = 'zzrqwe' >>> if 'qwe' in x: print ('Match') Match >>> if x.find('qwe') != -1: print ('Match') Match >>> print (x.find('qwe')) 3
>>> y = x.replace('q','qqq')
>>> y
'zzrqqqwe'
>>> a ['zrxrcrvr'] >>> a=y.split('r') >>> a ['z', 'x', 'c', 'v', ''] >>> a.pop(4) #use index '' >>> a ['z', 'x', 'c', 'v'] >>> a.remove('v') #use value >>> a ['z', 'x', 'c'] >>>
4、type/id函式:
>>> name=type(x)
>>> print(name)
<class 'str'>
>>> type(None)
<type 'NoneType'>
>>> id(None)
504026256
5、數字介於比較:
>>> x
6.333333333333333
>>> if 0<x<9:
print("normal")
normal
6、3.2版本的raw_input變化為input,不過active3.2整合的IDE功能真的很強~
7、python的正則表示式,封裝為class就是給力~
'123.456.789'
>>> patt=re.compile(r"(\d+)\.(\d*)\.(\d+)")
>>> r=patt.match(age)
>>> print(r.group())
123.456.789
>>> print(r.lastindex)
3
>>> print(r.group(1))
123
>>>
8、for的range,切片,string索引等py用法都不包含尾:
>>> name='i am jason'
>>> print(name[2:4])
am
>>> for number in range(0,4,2):
... print(number)
...
0
2
9、可使用分號標明py的邏輯行和物理行的對應關係
10、__doc__和__name__用法,給力啊~~
11、del用法:
>>> x='123'
>>> print(x)
123
>>> del x
>>> x
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
x
NameError: name 'x' is not defined
12、列表用法:
>>> print(list)
[1, 2, 3, 4, 1]
>>> list.remove(1)
>>> print(list)
[2, 3, 4, 1]
>>> list.reverse()
>>> print(list)
[1, 4, 3, 2]
>>> list.sort()
>>> print(list)
[1, 2, 3, 4]
>>> list.reverse()
>>> print(list)
[4, 3, 2, 1]
>>> for listitem in list:
print(listitem)
4
3
2
1
>>>
13、元組(),元組不可修改:
>>> stringlist = ''
>>> for listitem in list:
stringlist = stringlist + str(listitem)
>>> print (stringlist)
4321
>>> stringlist = ''
>>> for listitem in list:
stringlist = stringlist , str(listitem)
>>> print (stringlist)
(((('', '4'), '3'), '2'), '1')
>>> stringlist[0]='1'
Traceback (most recent call last):
File "<pyshell#161>", line 1, in <module>
stringlist[0]='1'
TypeError: 'tuple' object does not support item assignment
14、奇妙的層次感鮮明的元組,:
>>> print(stringlist[0][0])
(('', '4'), '3')
>>> print(stringlist[0][1])
2
>>> print(stringlist[0][0][0])
('', '4')
15、雜湊序列及其排序:
>>> bb={}
>>> bb['Gary'] = {'email':'ad','qq':'23'}
>>> bb
{'Gary': {'qq': '23', 'email': 'ad'}}
>>> bb.values()
dict_values([{'qq': '23', 'email': 'ad'}])
>>> bb.items()
dict_items([('Gary', {'qq': '23', 'email': 'ad'})])
>>> bb.get('Gary')
{'qq': '23', 'email': 'ad'}
>>> qq='qq'
>>> if qq in ab['Gary']:
print(ab['Gary']['qq'])
23
>>> bb['Jason'] = {'qq':11,'email':"dd"}>>> bb
{'Jason': {'qq': 11, 'email': 'dd'}, 'Gary': {'qq': '23', 'email': 'ad'}}
>>> bb.items()
dict_items([('Jason', {'qq': 11, 'email': 'dd'}), ('Gary', {'qq': '23', 'email': 'ad'})])
>>> for item in sorted(bb.keys()):
for items in sorted(bb[item].keys()):
print (item,items,bb[item][items])
Gary email ad
Gary qq 23
Jason email dd
Jason qq 11
>>> bb.pop('Gary')
{'qq': '23', 'email': 'ad'}
>>> bb
{'Jason': {'qq': 11, 'email': 'dd'}}
>>> for name in bb.keys():
for items,value in bb[name].items():
print (name ,"+++",items,"+++",value)
Jason +++ qq +++ 11
Jason +++ email +++ dd
16、繼承類使用(父類),~所有皆是物件~
17、檔案操作r,w,a:
string = '''BAS
(Broadband Access Server/ Broadband Remote Access Server)
End PPPoE
'''
f = open('Bas.txt', 'w')
f.write(string)
f.close()
line = open('Bas.txt').read()
print (line)
18、匿名函式列印列表中偶數的2倍數:
>>> aa = [1,2,3,4,5,6]
>>> g= lambda x : [num*2 for num in x if num % 2 ==0]
>>> g(aa)
[4, 8, 12]
先學習到這吧,差不多程式都能讀懂修改了~
感覺要寫大型專案而不是系統資料維護工作,python是首當其衝的選擇啊~