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,y 

x,y=1,2 

print x 

print y 

 

#calute x(0~9) sum(x^2) 

print sum(x*x for x in range(10)) 

 

#get substr method 

str = 'ABCDEFG' 

print str[0:3+0] 

 

#get substr method2 

nums=[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 method 

strs=['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 output 

for s in strs: 

    continue 

    print s 

 

#if check statements 

age=18 

if age < 21: 

    print("you can't buy jiu") 

    print("but you can buy kou xiang tang") 

print("if out") 

#if elif  use method 

x=1 

if x>0:     

    print("x>0") 

elif x<=0:     

    print("x<=0") 

     

import math 

print "math.sin(math.pi/2)=1=" 

print(math.sin(math.pi/2)) 

     

#define function method OK 

def 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 end 

print 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 rstr 

def reverse2(str): 

    lstr=list(str) 

    lstr.reverse() 

    rstr=''.join(lstr) 

    return rstr 

def reverse3(str): 

    return str[::-1] 

 

#call reverse function method 

print "----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 method 

class 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.method 

Fish.eat(f,"earchworm") 

print "earchworm" 

print f.hungry 

print "-------------" 

f.eat(None) 

print "None=" 

print f.hungry 

 

u=User("Tom talk hsg") 

print u.name    

 

#requre from __futrue__ import division 

#only return  double numbers 

val=3/2 

print val 

print "3/2=1.5=" 

print(3/2) 

 

#array ->string method 

#return apple-banana-china 

string=('apple','banana','china') 

print '-'.join(string) 

print '-'.join("fishhat") 

#return f-i-s-h-h-a-t 

 

string ='I am Fishhat' 

print string 

print "find rfind function" 

print string.find("F") 

print string.find("f") 

print string.find("h") 

print string.rfind('h') 

print "------------" 

 

str='AAAABBBBBDDDDD' 

print str 

print str.replace('D','C') 

print str.replace('A','a',3) 

print "------------" 

#about time datetime 

import time 

str=time.strftime('%Y-%m-%d %X',time.localtime()) 

print str 

 

str='2009/12/09' 

print type(str) 

str=time.strptime(str,'%Y/%m/%d') 

print str 

print type(str) 

import datetime 

str=datetime.datetime(2009,12,9) 

print str 

print type(str) 

 

print "------------" 

 

#stopstr="s" 

#getc(stopstr) 

#import msvcrt,sys 

stop=raw_input("input Stop:") 

print stop 

 

import getpass 

pwd=getpass.getpass('password:') 

print pwd 

 

#define console get password method  function 

import msvcrt, sys 

def 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 例子

 

view plain

用法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----

 

摘自:hsg77的专栏

相关内容

    暂无相关文章

评论关闭