标签:des style blog http color os
python位列3P(perl,php,python)之一。业界这样定义它:1)脚本语言:2)胶水语言:
阅读了<a byte of python>之后,留下这篇文章做个备忘录,
这本99页的小册子描述的是老版本的python2.3,我的ubuntu自带python2.7.6
a=‘hello world‘
print‘hello world‘ python3.x不支持老版本
print(‘hello world‘) 新版本
print(a)
| |hello worldpython就像string类一样,自动执行+=操作,join操作为括号里的每个字符执行一个+=操作
print(‘hhh‘,a,‘hhh‘)
print(‘hhh‘,a,‘aaa‘.join(‘hhh‘))
||hhh hello world hhh
||hhh hello world haaahaaah
注意下面的逗号和百分号
print(‘what is a:‘,a)
print(‘print twice a :%s,%s‘ %(a,a))
| |what is a: hello world
| |print twice a :hello world,hello world1.2 数据类型:>>> for i in range(1,5):
print(i)
else:
print(‘loop over‘)
1
2
3
4
loop overusrName=[‘allen‘,‘allen2‘] #这是一个List数据结构
while True:
s=input(‘please enter your name :‘)
for checkName in usrName:
if s==checkName:
print(‘weclome:%s‘ %s)
break
elif s==‘root‘:
print(‘weclime:‘,s)
break
s2=input(‘enter q to quit:‘)
if s2==‘q‘:
print(‘get away!~!‘)
breakdef syHi():
print(‘i am allen.lidh‘)
syHi()
>>>
i am allen.lidh函数之缺省值参数:缺省参数的右边的参数必须全部都有缺省值。(编译器从左至右匹配参数,只有在被传递的参数匹配完毕后,def syHi(a=1,b,c)
#这样定义是错的
def syHi(a,b=4,c=5):
print(‘a:‘,a,‘b:‘,b,‘c:‘,c)
syHi(1,2)
>>>
a: 1 b: 2 c: 5参数之局部和全局变量def syHi(x):
x=3
global y
y=22
x=7
y=77
syHi(x)
print(‘x:‘,x,‘y:‘,y)输出:import two
two.sayhi()
print(two.version)
>>>
i am from two.py
0.2
from two import sayhi,version sayhi() print(version) >>> i am from two.py 0.2
>>> myList=[‘item1‘,‘item2‘] >>> myList.append(‘item3‘) >>> print(‘length:‘,len(myList),‘list[0]:‘,myList[0]) (‘length:‘, 3, ‘list[0]:‘, ‘item1‘) >>> del myList[0] >>> myList.sort() >>> for i in myList:print i ... item2 item3Tuple:
>>> myTuple=(‘item1‘,‘item2‘) >>> yourTuple=(‘item3‘,myTuple) >>> print(yourTuple[1][0]) item1DIctionary:
>>> myDic={‘happy‘:‘laughing‘,
... ‘sad‘:‘laughed‘}
>>> #add a key-value item
... myDic[‘sb‘]=‘no-brain‘
>>> del myDic[‘sad‘]
>>> for me,you in myDic.items():
... print(‘me:‘,me,‘ while you ‘,you)
...
(‘me:‘, ‘sb‘, ‘ while you ‘, ‘no-brain‘)
(‘me:‘, ‘happy‘, ‘ while you ‘, ‘laughing‘)>>> a=tuple() >>> a () >>> a=tuple(‘hahah‘) >>> a (‘h‘, ‘a‘, ‘h‘, ‘a‘, ‘h‘) >>> b=a+a >>> b (‘h‘, ‘a‘, ‘h‘, ‘a‘, ‘h‘, ‘h‘, ‘a‘, ‘h‘, ‘a‘, ‘h‘) >>> for i in a: print i ... h a h a h >>> b==a False >>> b>=a True >>>
import os source=‘/home/allen-li/‘ fileName=raw_input(‘enter the file you want to operate on:‘) #这里其实应该有个检测文件名合法性的操作 src_file=source+fileName+‘.gif‘ des_file=source+‘allen_lidh‘+‘.gif‘ myCommand="mv ‘%s‘ ‘%s‘ " %(src_file,des_file) #unix命令行 print(myCommand) if os.system(myCommand)==0: print‘return True‘ else: print ‘return False‘输出:输出的结果就是这张gif的名字发生改变
allen-li@allenli-U80V:~$ python /home/allen-li/mypy1.py enter the file you want to operate on:hhh mv ‘/home/allen-li/hhh.gif‘ ‘/home/allen-li/allen_lidh.gif‘ return True这张gif让我笑到爆,不过图片太大传不了,只好截图吧
class Person:
def __init__(self,name,sex):
self.name=name
self.sex=sex
def syHi(self):
print ("name:‘%s‘ sex:‘%s‘" %(self.name,self.sex));
class Student(Person):
def __init__(self,name,sex,sid):
Person.__init__(self,name,sex)
self.sid=sid
def syHi(self):
Person.syHi(self)
print("sid:‘%s‘" %self.sid)
class Teacher(Person):
def __init__(self,name,sex,tid):
Person.__init__(self,name,sex)
self.tid=tid
def syHi(self):
Person.syHi(self)
print("tid:‘%s‘ " %self.tid)
s=Student(‘allen.lidh2‘,‘boy2‘,100)
t=Teacher(‘allen.lidh3‘,‘boy3‘,100100)
s.syHi()
t.syHi();
输出:allen-li@allenli-U80V:~$ python /home/allen-li/桌面/mypy1.py name:‘allen.lidh2‘ sex:‘boy2‘ sid:‘100‘ name:‘allen.lidh3‘ sex:‘boy3‘ tid:‘100100‘
import time
try:
f=file(‘/home/allen-li/桌面/allen.lidh.txt‘,‘wb+‘) #这里的file和下面的open有什么区别?
s="hello i am allen.lidh \n in shu"
# f.write(bytes(s,"utf-8")) #这是python 3.X的写法
f.write(s) #写和读不能同时进行,学过os就应该知道这是缓冲区没有及时刷新
f=open(‘/home/allen-li/桌面/allen.lidh.txt‘,‘wb+‘)
while True:
#line=f.readline()
line=f.read(16) #16个字节读一次
if len(line)==0:
break
print(line)
finally:
f.close()
print(‘bye:%s‘ %time.strftime(‘%Y-%m-%d-%H-%M‘)) #时间函数
>>> allen-li@allenli-U80V:~$ python /home/allen-li/桌面/mypy1.py hello i am allen .lidh in shu bye:2014-04-30-00-42
隔壁的同学买了一本关于python library超厚的书,我不禁缩了。。。
作为弥补,贴了Lambda的例子吧,C++11把这个特性列为标准了。
def makeTwice(n): return lambda s:s*n twice=makeTwice(2) print(twice(7)) print(twice(‘hhh‘)) >>> allen-li@allenli-U80V:~$ python /home/allen-li/桌面/mypy1.py 14 hhhhhh #感觉这个跟正则一样走的非主流,
标签:des style blog http color os
原文地址:http://blog.csdn.net/amazing24/article/details/24526181