您的位置:首页 > 编程语言 > Python开发

Python初学笔记~

2011-08-29 15:30 351 查看
工作需要,要接触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是首当其冲的选择啊~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: