python if 语句
格式: (必须注意 if 、elif 、 else后面均带冒号,并且语句块必须缩进)
if 表达式 :
语句块
elif 表达式:
语句块
else:
语句块
eg:
tmp = int(raw_input("please input a integer"))
if tmp < 10:
print tmp ,' < 10'
elif tmp >=10 and tmp < 20:
print '10 <= ' ,tmp , ' < 20'
else:
print '20 <= ' , tmp
---------------------------------------------------------------------------------------------------
python for 语句
格式1:
str = ['Monday', 'Tuesday', 'Thirsday', 'Wendesday', 'Friday']
for x in str:
print x, len(x)
格式2:
str = ['Monday', 'Tuesday', 'Thirsday', 'Wendesday', 'Friday']
for x in str[:]:
print x, len(x)
------------------------------------------------------------------------------------------------------
range 产生等差级数列
>>>range(10)
[0,1,2,3,4,5,6,7,8,9]
结合range、len遍历链表
for i in (range(len(str))):
print str[i], len(str[i])
-----------------------------------------------------------------------------------------------------
python while 语句
while 表达式:
语句块
eg:
tmp = 10
while tmp > 0:
print tmp
tmp = tmp - 1
----------------------------------------------------------------------------------------------------------
格式: (必须注意 if 、elif 、 else后面均带冒号,并且语句块必须缩进)
if 表达式 :
语句块
elif 表达式:
语句块
else:
语句块
eg:
tmp = int(raw_input("please input a integer"))
if tmp < 10:
print tmp ,' < 10'
elif tmp >=10 and tmp < 20:
print '10 <= ' ,tmp , ' < 20'
else:
print '20 <= ' , tmp
---------------------------------------------------------------------------------------------------
python for 语句
格式1:
str = ['Monday', 'Tuesday', 'Thirsday', 'Wendesday', 'Friday']
for x in str:
print x, len(x)
格式2:
str = ['Monday', 'Tuesday', 'Thirsday', 'Wendesday', 'Friday']
for x in str[:]:
print x, len(x)
------------------------------------------------------------------------------------------------------
range 产生等差级数列
>>>range(10)
[0,1,2,3,4,5,6,7,8,9]
结合range、len遍历链表
for i in (range(len(str))):
print str[i], len(str[i])
-----------------------------------------------------------------------------------------------------
python while 语句
while 表达式:
语句块
eg:
tmp = 10
while tmp > 0:
print tmp
tmp = tmp - 1
----------------------------------------------------------------------------------------------------------