Python学习笔记——条件和循环

1.条件表达式

>>> x = 3
>>> x = 1 if x<3 else 2
>>> x
2

2.for语句用于序列类型

  <1>通过序列项迭代

>>> List = ['a','b','c','d']
>>> for eachList in List:
...     print eachList
... 
a
b
c
d

  <2>通过序列索引迭代

>>> for eachList in range(len(List)):
...     print List[eachList]
... 
a
b
c
d

  <3>使用项和索引迭代

>>> for i,eachList in enumerate(List):
...     print "%d %s" % (i,eachList)
... 
0 a
1 b
2 c
3 d

 3.else语句

#coding:utf-8
#!/usr/bin/env python
'maxFact.py -- 寻找一个数的最大约数'

def showMaxFactor(num):
	count = num/2
	while count > 1:
		if num % count == 0:
			print '%d 的最大约数是 %d' % (num,count)
			break
		count -= 1
	else:
		print num,'没有最大公约数'
		
		
for eachNum in range(10,21):
	showMaxFactor(eachNum)
原文地址:https://www.cnblogs.com/tonglin0325/p/5748603.html