python语法学习练习1
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. **************************************************************** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. **************************************************************** IDLE 1.2.1 >>> ================================ RESTART ================================ >>> s ='abc' >>> for a in s: print a ; SyntaxError: invalid syntax >>> print s abc >>> for a in a:print a Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> for a in a:print a NameError: name 'a' is not defined >>> for a in s:print a a b c >>> str='akakak' >>> str=str.replace('k',' 8') >>> print str a 8a 8a 8 >>> str='akakak'str=str.replace('k',' 8')str='akakak' SyntaxError: invalid syntax >>> str='akakak' >>> str=str.replace('k','8') >>> print str a8a8a8 >>> str='a,hello' >>> print str.find('hello') 2 >>> str='a,b,c,d' >>> strlist=str.split(',') >>> for value in strlist: print value a b c d >>> def getclass(txt_s): str_array=txt_s.split('#') return str_array[0] >>> print getclass('chen3#4.6men') chen3 >>> print getclass('鍩庨晣寤烘柊鍖?#锛? 鎬婚潰绉細38.42浜?) SyntaxError: EOL while scanning single-quoted string >>> print getclass('鍩庨晣寤烘柊鍖?#锛?) 鍩庨晣寤烘柊鍖? >>> t=getclass('鍩庨晣寤烘柊鍖?#锛?) >>> print t 鍩庨晣寤烘柊鍖? >>> for a in t:print a ? ? ? ? ? ? ? ? ? ? 1 >>> num=-1 >>> print num -1 >>> for a in t: if(a>=0 && a<=9): SyntaxError: invalid syntax >>> for a in t: if(a>=0 and a<=9): print a >>> for a int t: SyntaxError: invalid syntax >>> for a in t: if(a==1) print a SyntaxError: invalid syntax >>> for a in t: if(a=='1'): print a 1 >>> dkbh='' >>> for a in t: if(a=='1'): if(dkbh==''): dkbh=a else SyntaxError: invalid syntax >>> i=1 >>> while i<10: i+=1 if i%2>0: continue print i 2 4 6 8 10 >>> print i 10 >>> i=1 >>> while 1: print i i+=1 if(i>10): break 1 2 3 4 5 6 7 8 9 10 >>> def getclass(txt_s): dkbh='' for a in t: if(a=='1' or a=='2' or a=='3' or a=='4'): if(dkbh==''): dkbh=a else: dkbh+=a return dkbh File "<pyshell#68>", line 2 dkbh='' ^ IndentationError: expected an indented block >>> str='a,b,c,d' >>> strlist=str.split(',') >>> for value in strlist: print value a b c d >>> def myadd(a=1,b=100): result=0 i=a while i<=b: result+=i i+=1 return result >>> print myadd(1,10) 55 >>> print myadd() 5050 >>> print myadd(50) 3825 >>> def getclass(txt_s): dkbh='' for a in txt_s: if(a=='1' or a=='2' or a=='3' or a=='4'): if(dkbh==''): dkbh=a else: dkbh+=a return dkbh File "<pyshell#85>", line 1 def getclass(txt_s): ^ IndentationError: unexpected indent >>> ================================ RESTART ================================ >>> a=1 >>> a.isnumeric() Traceback (most recent call last): File "<pyshell#87>", line 1, in <module> a.isnumeric() AttributeError: 'int' object has no attribute 'isnumeric' >>> a.isnumeric Traceback (most recent call last): File "<pyshell#88>", line 1, in <module> a.isnumeric AttributeError: 'int' object has no attribute 'isnumeric' >>> txt_s='sfsfsd3#2.6' >>> print txt_s sfsfsd3#2.6 >>> t=txt_s[0:txt_s.find('#')] >>> print t sfsfsd3 >>> print t.isdigit() False >>> for a in t: print a.isdigit() False False False False False False True >>> print t sfsfsd3 >>> dkbh='' >>> for a in t: if(a.isdigit()==true): if(dkbh==''): dkbh=a else SyntaxError: invalid syntax >>> for a in t: if(a.isdigit()==true): if(dkbh==''): dkbh=a else: dkbh+=a Traceback (most recent call last): File "<pyshell#105>", line 2, in <module> if(a.isdigit()==true): NameError: name 'true' is not defined >>> for a in t: if(a.isdigit()==True): if(dkbh==''): dkbh=a else: dkbh+=a >>> print dkbh 3 >>>
getclass5(!RefName!)
def getclass5(txt_s):
t=txt_s
t=t[0:t.find('#')]
dkbh=''
for a in t:
if(a.isdigit()==True):
if(dkbh==''):
dkbh=a
else:
dkbh+=a
return dkbh
def getclass5(txt_s):
t=txt_s
t=t[0:t.find('#')]
dkbh=''
for a in t:
if(a.isdigit()==True):
if(dkbh==''):
dkbh=a
else:
dkbh+=a
return dkbh
--the--end--
---2012-03-09---