python sytex a instance文法一例
#title:python sytex a instance#------------------------------#pathfilename:test.py#vp:hsg#create date:2011-10-20#------------------------------from __future__ import division#from __future__ import unicode_literals#note:not write china language word!print('Hello World')#set value to x,yx,y=1,2print xprint y#calute x(0~9) sum(x^2)print sum(x*x for x in range(10))#get substr methodstr = 'ABCDEFG'print str[0:3+0]#get substr method2nums=[1, 3, 5, 7, 8, 13, 20]print nums[2:5] #[5,7,8]print nums[1:] #[3, 5, 7, 8, 13, 20]print nums[:-3] #[1, 3, 5, 7]print nums[:] #[1, 3, 5, 7, 8, 13, 20]print nums[1:5:2] #[3,7]#str array foreach methodstrs=['a','b','c']for s in strs: pass print s#up this code have output value a b c#down this bottom code no value outputfor s in strs: continue print s#if check statementsage=18if age < 21: print("you can't buy jiu") print("but you can buy kou xiang tang")print("if out")#if elif use methodx=1if x>0: print("x>0")elif x<=0: print("x<=0") import mathprint "math.sin(math.pi/2)=1="print(math.sin(math.pi/2)) #define function method OKdef randint(a,b): "return random integer in range[a,b],including both end points." if a>=0 and b>=a: return (a+b)/2 else: return -1#def endprint randint(1,5)print randint(-1,5)help(randint)#define function method 2 def reverse(str): rstr='' lstr=list(str) for i in range(len(str),0,-1): rstr +=''.join(lstr[i-1]) return rstrdef reverse2(str): lstr=list(str) lstr.reverse() rstr=''.join(lstr) return rstrdef reverse3(str): return str[::-1]#call reverse function methodprint "----Fishhat---"print "reverse(string)"string="Fishhat"print list(string)print reverse(string)print "----ABCDE---"string="ABCDE"print reverse2(string)print reverse3(string)print "-------------"#class define and call methodclass Fish: def eat(self,food): if food is not None: self.hungry=False else: self.hungry=True#class end class User: def __init__(myself,name): myself.name=name#class end#use top code instance:f=Fish()#call class.methodFish.eat(f,"earchworm")print "earchworm"print f.hungryprint "-------------"f.eat(None)print "None="print f.hungryu=User("Tom talk hsg")print u.name #requre from __futrue__ import division#only return double numbersval=3/2print valprint "3/2=1.5="print(3/2)#array ->string method#return apple-banana-chinastring=('apple','banana','china')print '-'.join(string)print '-'.join("fishhat")#return f-i-s-h-h-a-tstring ='I am Fishhat'print stringprint "find rfind function"print string.find("F")print string.find("f")print string.find("h")print string.rfind('h')print "------------"str='AAAABBBBBDDDDD'print strprint str.replace('D','C')print str.replace('A','a',3)print "------------"#about time datetimeimport timestr=time.strftime('%Y-%m-%d %X',time.localtime())print strstr='2009/12/09'print type(str)str=time.strptime(str,'%Y/%m/%d')print strprint type(str)import datetimestr=datetime.datetime(2009,12,9)print strprint type(str)print "------------"#stopstr="s"#getc(stopstr)#import msvcrt,sysstop=raw_input("input Stop:")print stopimport getpasspwd=getpass.getpass('password:')print pwd#define console get password method functionimport msvcrt, sysdef pwd_input(): chars = [] while True: newChar = msvcrt.getch() if newChar in '\r\n': # swap line print '' break elif newChar == '\b': # back block if chars: del chars[-1] sys.stdout.write('\b') # else: chars.append(newChar) sys.stdout.write('*') # display * char print ''.join(chars)pwd =pwd_input()print pwd#---the----end----
arcgis python 例子
用法1:求圖形面積運算式類型:PYTHON運算式 float(!shape.area!)用法2:求圖形長度運算式類型:PYTHON運算式 float(!shape.Length!)用法3:求兩欄位相乘運算式類型:PYTHON運算式 float(!kd!)*float(!cd!)用法3:自訂函數# Calculation is based on a custom getclass definition expression = "getclass(float(!shape.area!))" codeblock = "def getclass(area): if area <= 1000: return 1 if area > 1000 and area <= 10000: return 2 else: return 3" CalculateField(inputFC, "areaclass", expression, "PYTHON", codeblock)用法4:getclass(!dlmc!)def getclass(dlmc) : if(dlmc=='灌溉水田') : return "111" if(dlmc=='菜地') : return "115" if(dlmc=='其他園地') : return "125" if(dlmc=='水澆地') : return "113" if(dlmc=='果園') : return "121" if(dlmc=='可調整果園') : return "121K" if(dlmc=='可調整其他園地') : return "125K" if(dlmc=='有林地') : return "131" if(dlmc=='灌木林地') : return "132" if(dlmc=='疏林地') : return "133" if(dlmc=='曬穀場等用地') : return "158" if(dlmc=='設施農業用地') : return "152" if(dlmc=='畜禽飼養地') : return "151" if(dlmc=='農村道路') : return "153" if(dlmc=='可調整養殖水面') : return "155K" if(dlmc=='養殖水面') : return "155" if(dlmc=='農田水利用地') : return "156" if(dlmc=='建制鎮') : return "202" if(dlmc=='農村居民點') : return "203" if(dlmc=='獨立工礦') : return "204" if(dlmc=='公路用地') : return "262" if(dlmc=='港口碼頭用地') : return "264" if(dlmc=='水工建築用地') : return "272" if(dlmc=='特殊用地') : return "206" if(dlmc=='河流水面') : return "321" if(dlmc=='灘涂') : return "324" if(dlmc=='荒草地') : return "311" else : return "000"
取子字串方法在Arcgis中
!bhkbh![0:12+1] 取欄位bhkbh的前12位值
---the end----