>>> range(0, 10, 2)
[0, 2, 4, 6, 8]
-------break.py-----------------
from math import sqrt
for n in range(99, 0, -1) :
root = sqrt(n)
if root == int(root) :
print n
break
sample output 81
-------while1.py----------------
x = 1
while x <= 100 :
print x
x += 1
-------while2.py----------------
name = ''
while not name.strip():
name = raw_input('Please enter your name: ')
print 'Hello, %s!' % name
-------while3.py---------------
word = 'dummy'
while word :
word = raw_input('Please enter a word : ')
print 'The word was ' + word
-------while4.py---------------
while True :
word = raw_input('Please enter a word : ')
if not word : break
print 'hehe show ' + word
列表推导式 -- 轻量级循环
>>> [x*x for x in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> [x*x for x in range(10) if x % 3 == 0]
[0, 9, 36, 81]
python中空代码块是非法的
name = 'robby'
if name == 'Jim' :
print 'welcome'
elif name == 'robby' :
pass
elif name == 'Gates' :
print 'Bill'
--------del.py-------
x = ["hello", "world"]
y = x
y[1] = "python"
print x
del x
print y
hp@ubuntu:~/py$ python del.py
['hello', 'python']
['hello', 'python']
注 : 元组用 (), 序列用[], 字符串就是字符串。元组就是圆的。