python基础语法测评_对比测试脚本语言Tcl和Python的语法以及处理(基础篇)

在编写测试自动化脚本中,由于项目变更需要使用Tcl和python语言,所以在学习中对比两个学习笔记,先统计一些常用的基础对比.

期间因为个人学习深入度不足,所以未录入Tcl的info,跟踪,历史相关指令.也未录入python相关class的知识.同时由于时间不足,没列举ui和制作工具方面的对比.在后续发布深入对比时会以测试的实例作对比.同时本文是以个人学习和工作的经验编写,所以有错误的地方希望指出,我作出更新.(同时部分对比我未寻找到,标为缺省中)

Tcl的花括号中,所有特殊字符都将成为普通字符,失去其特殊意义,TCL解释器不会对其作特殊处理。

打印:

Tcl:puts "hello world"

Python:print 'hello world'

数字和表达式:

Tcl:expr 2+2

Python:2+2

获取用户输入:

Tcl:缺省中

Python:input("please input:")

赋值:

Tcl:set a 0和set a "abc"打印puts $a,

sety [expr2+100],使用unset删除变量

Python:

a =0和a = 'abc'打印print a或者直接a ,

y = 2+100,使用del删除变量

数组:

Tcl:

set a(0) 1,set a(1) 2,.......

array get a

array name a

array size a

array exists a

Pyhton:

Pyhton中有元组的概念,虽然个人学习的确在列表和数组间做出很清晰的解释,暂时以元组来作对比

a =(1,2,3,4,5,6),a[0],a[1]...

加到一个变量和整数:

tcl:

append X AAA;append X BBB

set a 1;incr a 1

Pyhton:

a = [1,2,3,4,5];a.append(5)

a = ['a','b'];a.append('c')

列表:

tcl:

set alist [list 1 2 3 4 5 6 7 8]

合成list :blist [concat $alist {3 4 5 6 7}]

返回索引:lindex $blist 0

返回长度:llength $blist

插入数据:set blist [linsert $blist 0 {a b}]

替换数据:set blist [lreplace $blist 0 0 c d]

返回串:lrange $blist 0 end

拆分和连接指令: set blist {1,2,3,4,5,6}

set blist[split $blist ,]

setblist[join $blist ,]

python:

alist = [1,2,3,4,'a',(1,2,'b')]

blist = alist + [1,2,3,4,5]

blist[0]

len[blist]

blist[1:1] = ['a','b']

blist[1]='a'

print blist[1:4]

注意:在python中拆分函数支持字符串的拆分,所以数字需以下解决:

blist = ['a','2']

clist ='_'.join(blist)

clist.split('_')

控制流:

tcl:

控制语句中记住大括号需要空格,包括if、while、for、foreach、switch、break、continue等命令

set a 0

if {$a != 0} {

puts "fail"

}else {

puts $a

}

for {set i 0} {$i < 10} {incr i 1} {

puts $i

}

switch$x {

a-

b{incrt1}

c{incrt2}

default{incrt3}

}

python:

Python中的缩进是用来表示语法块的,控制语句有if,while,for,case,loop,until,yield.以空行表示循环结束

if x < 0:

...x = 0

...print 'Negative changed to zero'

... elif x == 0:

...print 'Zero'

... eles:

... print 'Zero'

...

相关的函数有range()函数.还有pass关键字是不做任何操作.

迭代索引列表

>>> a = ['Mary', 'had', 'a', 'little', 'lamb']

>>> for i in range(len(a)):

...print i, a[i]

类似于中的for循环效果:

for(int i=0;i

System.out.println(i+""+a[i]);

}

执行命令eval:

tcl:

set cmd "puts /"This is a tcltk example/""

eval $cmd

python:

此函数也是支持字符串的命令:

eval('3+4')

索引文件:

tcl:

source e:/tcl&c/hello.tcl

python:

import test.py

from sys import argv,path

过程(procedure),全局变量:

个人觉得在脚本语言中,其实在运用过程中,函数和过程这2个东西其实很少真正意义实现,主要还是封装成易读的模块.

注意:过程使用了全局变量后,修改会影响到全局上.

tcl:

set c 4

proc test {a {d 2}} {

global c

set $f [expr $a + $c +$d]

return $f

}

pyhton:

注意:过程也是要缩进的.

b = 2

def add1(x):

global b

x = b + 1

return x

引用:

Tcl:

proctemp{ arg } {

upvar $argb

setb[expr$b+2]

}

procmyexp{var} {

seta4

temp a

return[expr $var+$a]

}

pyhton:暂时缺省中

格式化输出:

tcl:

setmsg[format "%s is %d years old"wyj27]

scan命令可以认为是format命令的逆

scan"some26 34""some %d %d" a b

python:

print 'www.%s.%s'%('qq'.'com')

正则表达式:

tcl:

regexp {^((0x)?[0-9a-fA-F]+|[0-9]+)$}ab

regsub there"They live there lives " theirx

python:

import re

re.findall(r’\bf[a-z]*’, ’which foot or hand fell fastest’)

’tea for too’.replace(’too’, ’two’)

字符串处理:

Tcl:

索引字符串:string index "0123456" 6

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值