IF 判断
FOR循环
for i in range(1,5): print i
for i in range(5): print i
用变量控制循环次数:
-
#!/usr/bin/python list=[‘lk‘,‘tom‘,‘jack‘] a=4 for name in range(1,a): print name
小练习:for和if结合用
-
[[email protected] python_scripts]# cat 4for.py #!/usr/bin/python list=[‘lk‘,‘tom‘,‘jack‘] for name in list: if name==‘lk‘: print name,‘ is very good!‘ else: print name print ‘loop over!‘ [[email protected] python_scripts]# python 4for.py lk is very good! tom jack loop over!
WHILE循环
-
[[email protected] python_scripts]# cat 5while.py #!/usr/bin/python whileTrue: input=raw_input(‘(alive|dead|other):‘) if input==‘alive‘: print ‘The process is ‘,input else: print ‘The process is ‘,input break
时间: 2024-11-15 22:54:31