字符串是双引号包括的
t = ('a', 'b', str("A,B"))
l=t[2]
print l #A,B
print l[0],l[1],l[2]#A , B
t = ('a', 'b', str("A,B"))
l=t[2]
print l #A,B
print l[0],l[1],l[2]#A , B
2015-03-28
L = ['Adam', 'Lisa', 'Bart']
L[-1]=L[-3]
L[-3]='Bart'
print L
L[-1]=L[-3]
L[-3]='Bart'
print L
2015-03-28
如果想让一个函数能接受任意个参数,我们就可以定义一个可变参数:
def fn(*args):
print args
可变参数的名字前面有个 * 号,我们可以传入0个、1个或多个参数给可变参数:
def fn(*args):
print args
可变参数的名字前面有个 * 号,我们可以传入0个、1个或多个参数给可变参数:
2015-03-27
def greet(strs='world'):
print 'hello,',strs,'.'
greet()
greet('Bart')
print 'hello,',strs,'.'
greet()
greet('Bart')
2015-03-27
def move(n, a, b, c):
if n == 1:
print a, '-->', c
else:
move(n-1, a, c, b)
move(1, a, b, c)
move(n-1, b, a, c)
move(4, 'A', 'B', 'C')
if n == 1:
print a, '-->', c
else:
move(n-1, a, c, b)
move(1, a, b, c)
move(n-1, b, a, c)
move(4, 'A', 'B', 'C')
2015-03-27
和 for 循环不同的另一种循环是 while 循环,while 循环不会迭代 list 或 tuple 的元素,而是根据表达式判断循环是否结束。
that is important to distinguish the difference between "for" and "while".
that is important to distinguish the difference between "for" and "while".
2015-03-27
sum = 0
x = 1
n = 1
while True:
if n>=20:
break
sum=sum+x
x=x*2
n=n+1
print sum
x = 1
n = 1
while True:
if n>=20:
break
sum=sum+x
x=x*2
n=n+1
print sum
2015-03-26