`
venceinfo
  • 浏览: 38245 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

基础(1)

阅读更多
模块导入:
xx.py
def getName():
    return "xuxu"

yy.py
//调用方法与java的类调用相同
import xx
ret = xx.getName()
print ret
-------------------------------------------------------------------------------------------
def testList():
   typeList = []   
   typeList.append("a")
   return typeList

ll=testList()
print ll   
-------------------------------------------------------------------------------------------
//当不指定函数的返回值时,默认 None
def testP():
	print "ok"
	
ret = testp()
print ret //返回值为None
-------------------------------------------------------------------------------------------
//运算符
if not (1 == 2):
	print "ok"

if "a"=="a":
	print "first ok"

try:
	if "a"=="a" and ("c"=="c" or  \
		"b"=="b"):
		print "ok"
except:
	print "fail"
print str(1)
-------------------------------------------------------------------------------------------
//字符串操作

//截取子串
str="abcdef"
print str[2:]

//判断是否以某串开头
str = "BeiJing_XUX"
if str.startswith("BeiJing"):
	print "ok"

//拼接字符串
str = "a" + "|"+"b"
print str

list操作:

li = ["a","b","c"]

//利用in判断最好,不会抛出异常
if "a" in li:
	print "ok"
//True为内置变量
if True:
	print "true"

try:
	li.index("a")
	print "ok"
except:
	print "fail"

//求长度
print len(li)

//遍历操作
li = ["a","b","c"]
for i in li
	print i
	
li = ["a","b","c"]
print "begin".join(li) //结果为一个字符串

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics