首页 > 代码库 > 2、循环判断

2、循环判断

IF 判断
 
 
FOR循环
 
for i in range(1,5):        print i
for i in range(5):        print i

 

 
 
用变量控制循环次数:
  1. #!/usr/bin/pythonlist=[lk,tom,jack]a=4for name in range(1,a):        print name
 
小练习:for和if结合用
  1. [root@likun python_scripts]# cat 4for.py #!/usr/bin/pythonlist=[lk,tom,jack]for name in list:        if name==lk:                print name, is very good!        else:                print nameprint loop over![root@likun python_scripts]# python 4for.py lk  is very good!tomjackloop over!

     

 
WHILE循环
 
  1. [root@likun python_scripts]# cat 5while.py #!/usr/bin/pythonwhileTrue:        input=raw_input((alive|dead|other):)        if input==alive:                print The process is ,input        else:                print The process is ,input                break

     

 

2、循环判断