我用的是ArcGIS中自带的python,版本是2.6.5
print "Hello world";
i=9; print i; a = "Hello world" print a;
可以在三引号中自由使用单引号和双引号。
转义字符 \n 和其他语言类似。
变量的命名和其他语言类似。
使用变量时只需要给它们赋一个值,不需要声明和定义数据类型,与shell编程一样。
一、单行注释
单行注释以#开头,例如: print 6 #输出6
二、多行注释
(Python的注释只有针对于单行的注释(用#),这是一种变通的方法)
多行注释用三引号'''将注释括起来,例如: ''' 多行注释 多行注释 '''
三、中文注释
在文件头上写入: #coding=gbk或:#coding=utf-8
然#这个符号在python中表示注释,其实如果用pydev或者别的什么IDE来编写程序的时候,如果开头不声明保存编码格式,会默认使用ASKII码保存,那么代码中有中文就会有问题,即使你的中文是在注释里面。
a=True #逻辑型,单行注释,多行注释用三引号
#coding=utf-8 length =5; width = 6; area = length * width; print 'area',area#这里的逗号不可以省略
#coding=utf-8 #输入整数 i=int(raw_input("输入一个整数:")); if i>0: print("正数"); print("木有报错哦!"); elif i==0: print("0"); print("你输入一个0干嘛?"); else: print("负数"); print("完成");
#coding=utf-8 a=raw_input("输入一个字符串:"); print (a);
#coding=utf-8 number=23; running=True; while running: guess=int(raw_input("输入一个正数:")); if guess==number: print("你猜对了"); running=False elif guess<number: print("再大一点"); else: print("再小一点"); print("程序结束");
#coding=utf-8 for i in range(1,5): print(i); #打印1 2 3 4 else: #这里可以不要else print("循环结束"); for i in range(1,10,2):#步长为2 print(i); #打印1 3 5 7 9
#coding=utf-8 while True: s=raw_input("输入一个字符串:"); if s=="exit": break; if len(s)<3: continue; print("你输入的字符串的长度为"),len(s); print("程序结束");
#coding=utf-8 def sayHello(): #定义函数 print("Hello world!"); sayHello(); #调用函数
#coding=utf-8 def printMax(a,b): if(a>b): print a,"最大"; else: print b,"最大"; printMax(3,4);
#coding=utf-8 #局部变量 def func(x): print "x的值是",x; x=2; print "x的值变成了",x; x=50; func(x); print(x);
# coding=utf-8 def func(): global x; #定义全局变量 print "x的值是", x; x = 2; print "改变x的值为", x; x = 50; func(); print"x的值为", x;
带返回值的函数 #coding=utf-8 def max(x,y): if x>y: return x; else: return y; print max(2,3);
#使用DocStrings #coding=utf-8 def printmax(x,y): "输出最大值"; x=int(x)#转成整型 y=int(y) if x>y: print x,"是最大值"; else: print y,"是最大值"; printmax(3,5);
#coding=utf-8 #使用sys模块 import sys; print "The comm and lineargum entsare"; for i in sys.argv: print i;
#coding=utf-8 if __name__ == '_main_': print "This program is being run by itself" else: print "I am being imported from another module";
#coding=utf-8 shoplist=["banana","apple","orange"]; print len(shoplist);#3 for item in shoplist: print item; shoplist.append("rice"); print shoplist;#['banana', 'apple', 'orange', 'rice'] shoplist.sort(); print shoplist;#['apple', 'banana', 'orange', 'rice'] print "第一个元素是",shoplist[0];#第一个元素是 apple olditem = shoplist[0]; del shoplist[0]; print shoplist;#['banana', 'orange', 'rice']
# coding=UTF-8 #元组 zoo = ('wolf', 'elephant', 'penguin'); print len(zoo) #3 new_zoo = ('monkey', 'dolphin', zoo) print len(new_zoo); #3 print new_zoo #('monkey', 'dolphin', ('wolf', 'elephant', 'penguin')) print new_zoo[2] #('wolf', 'elephant', 'penguin') print new_zoo[2][2] #penguin
# coding=UTF-8 # 元组与打印语句 age = 22 name = "umgsai" print "%s is %d years old" % (name, age) #umgsai is 22 years old print "why is %s playing with that python" % name #why is umgsai playing with that python
# coding=UTF-8 # 元组与打印语句 ab = {'swaroop':'swaroopch@byte of python.info', 'Larry':'[email protected]' } print "Swaroop's address id %s " % ab['swaroop'] ab['Guido'] = '[email protected]' del ab['Larry'] print len(ab) for name, address in ab.items(): print '%s %s' % (name, address) if 'Guido' in ab: print "\nGuido's address is %s"%ab['Guido'] if ab.has_key('Guido'): print "\nGuido's address is %s"%ab['Guido']
本文出自 “优赛工作室” 博客,谢绝转载!