Python初學筆記~

工作需要,要接觸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是首當其衝的選擇啊~

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章