if语句

计算机之所以能做很多自动化的任务,因为它可以自己做条件判断。

比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,可以用if语句实现:

age = 20
if age >= 18:
print 'your age is', age
print 'adult'
print 'END'

注意: Python代码的缩进规则。具有相同缩进的代码被视为代码块,上面的3,4行 print 语句就构成一个代码块(但不包括第5行的print)。如果 if 语句判断为 True,就会执行这个代码块。

缩进请严格按照Python的习惯写法:4个空格,不要使用Tab,更不要混合Tab和空格,否则很容易造成因为缩进引起的语法错误。

注意: if 语句后接表达式,然后用:表示代码块开始。

如果你在Python交互环境下敲代码,还要特别留意缩进,并且退出缩进需要多敲一行回车:

>>> age = 20
>>> if age >= 18:
... print 'your age is', age
... print 'adult'
...
your age is 20
adult

任务

如果成绩达到60分或以上,视为passed。

假设Bart同学的分数是75,请用if语句判断是否能打印出 passed:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjAAAAB6CAIAAAAuz/4bAAAMfUlEQVR4nO2dT0tb6R6A/RCzmUVGXFwoQ3ExIBSjlgbnOiploFDvvVKjtMXrbKpMFKRSOi200oJ30Sr+oZRZiKuKWChdJBa6UPwU+QB+idzF+feeeE7+6Jv390vyPDwLPcYk0vT38J5zktPzw48/ISIiitsj/gwQERF/+PGnnouLi0xvH9qyBwAghQrUhCARJABwhPTA1w5BIkgA4Ajpga8dgkSQAMAR0gNfOwSJIAGAI6QHvnYIEkECAEdID3ztECSCBACOkB742iFIBAkAHCE98LVDkAgSADhCeuBrhyARJABwhPTA1w5BIkgA4Ajpga8dgkSQAMAR0gNfOwSJIAGAI6QHvk1a8RcRJIIEAI6wO77Fsf4XESSCBACOsDu+xbH+FxEkggQAjrA7vsWx/hcRJIIEAI6wM7WDu0q8z7SHS3smjWxv8CEs/GkEiSABgBusDW7j3nriFam6md3ttR/u+hAkggQAjrA2uNODUUlawaQ9dI3tic+89uNeH4JEkADAEdYGd/MrmLSnkbg97anWeFwrtDxIhVKlUlpp9JaVSqW8NyEdFYIEAK3A2uB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PSjlfzgje9D1OakC0rvTA1871gzS+dtwu70OKXwDQaExKkNIuGFgrSLHfusZZdtH+NLMcXie8nWzTYx/9YMRvH1UkbXv6Q4R78w7GsuyyQ7St9MDXTld9UkPVMSS1Vh1DQsQOUXrga4cgKZQgIXam0gNfOwRJoQQJsTOVHvja6aogISJKKj3wtUOQEBEdKT3wtUOQEBEdKT3wtUOQEBEdKT3wtUOQEBEdKT3wtUOQEBEdKT3wtUOQEBEdKT3wtUOQEBEdKT3wtUOQEBEdKT3wtUOQEBEdKT3wtUOQEBEdKT3wtUOQEBEdKT3wtUOQEBEdKT3wtUOQEBEdKT3wtUOQEBEdKT3wtUOQEBEdKT3wtUOQEBEdKT3wtUOQEBEdKT3wtUOQEBEdKT3wtUOQEBEdKT3wtfN/SAa5tm0AKOQAAAAASUVORK5CYII=" alt="" />

if-else

当 if 语句判断表达式的结果为 True 时,就会执行 if 包含的代码块:

if age >= 18:
print 'adult'

如果我们想判断年龄在18岁以下时,打印出 'teenager',怎么办?

方法是再写一个 if:

if age < 18:
print 'teenager'

或者用 not 运算:

if not age >= 18:
print 'teenager'

细心的同学可以发现,这两种条件判断是“非此即彼”的,要么符合条件1,要么符合条件2,因此,完全可以用一个 if ... else ... 语句把它们统一起来:

if age >= 18:
print 'adult'
else:
print 'teenager'

利用 if ... else ... 语句,我们可以根据条件表达式的值为 True 或者 False ,分别执行 if 代码块或者 else 代码块。

注意: else 后面有个“:”。

任务

如果成绩达到60分或以上,视为passed,否则视为failed。

假设Bart同学的分数是55,请用if语句打印出 passed 或者 failed:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkIAAACgCAIAAABv1pT3AAAQUElEQVR4nO2dz08bZx6H+SP2sgc24lApqlIOlZCiugEFUVFAuUQK2nWDk22iiF4CKo6UBq1IIgXUqOyhIeKHEOoBcQpFRKI5AKlyAPFX+A/opdtWObOHsWfesWf8A2bm46/zPHoO9tiMjUL86H3n9UzX3/7+D0RERKN2yd8BIiLimSVjiIho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AAAAElFTkSuQmCC" alt="" />

if-elif-else

有的时候,一个 if ... else ... 还不够用。比如,根据年龄的划分:

条件1:18岁或以上:adult
条件2:6岁或以上:teenager
条件3:6岁以下:kid

我们可以用一个 if age >= 18 判断是否符合条件1,如果不符合,再通过一个 if 判断 age >= 6 来判断是否符合条件2,否则,执行条件3:

if age >= 18:
print 'adult'
else:
if age >= 6:
print 'teenager'
else:
print 'kid'

这样写出来,我们就得到了一个两层嵌套的 if ... else ... 语句。这个逻辑没有问题,但是,如果继续增加条件,比如3岁以下是 baby:

if age >= 18:
print 'adult'
else:
if age >= 6:
print 'teenager'
else:
if age >= 3:
print 'kid'
else:
print 'baby'

这种缩进只会越来越多,代码也会越来越难看。

要避免嵌套结构的 if ... else ...,我们可以用 if ... 多个elif ... else ... 的结构,一次写完所有的规则:

if age >= 18:
print 'adult'
elif age >= 6:
print 'teenager'
elif age >= 3:
print 'kid'
else:
print 'baby'

elif 意思就是 else if。这样一来,我们就写出了结构非常清晰的一系列条件判断。

特别注意: 这一系列条件判断会从上到下依次判断,如果某个判断为 True,执行完对应的代码块,后面的条件判断就直接忽略,不再执行了。

请思考下面的代码:

age = 8
if age >= 6:
print 'teenager'
elif age >= 18:
print 'adult'
else:
print 'kid'

当 age = 8 时,结果正确,但 age = 20 时,为什么没有打印出 adult?

如果要修复,应该如何修复?--互换条件

任务

如果按照分数划定结果:

90分或以上:excellent

80分或以上:good

60分或以上:passed

60分以下:failed

请编写程序根据分数打印结果。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjEAAADzCAIAAAD4stA1AAAeIklEQVR4nO2dwUsj2dqH54/4Nt/CT7K4MHyIiwFBbq42BofcKLMROtwJJva93YizaWXaBjF8eG1QacG7aCPaTSOzEFeG4ID0wrbBheJfkX+lv0Wlqk5VqmJK6+TNa54fzyJ1UqnE6cz7cE6dnPPDf/33/wAAAPQDP4h/AgAAAIcfhoYz8Eh+IISQmHwnSYKTcBIhxGKki7yy4CScRAixGOkiryw4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K8vAOgkAoBdIF3llwUkAABaRLvLKgpMAACwiXeSVBScBAFhEusgrC04CALCIdJFXFpwEAGAR6SKvLDgJAMAi0kVeWXASAIBFpIu8suAkAACLSBd5ZcFJAAAWkS7yyoKTAAAsIl3klQUnAQBYRLrIKwtOAgCwiHSRVxacBABgEekiryw4CQDAItJFXllwEgCARaSLvLLgJAAAi0gXeWXBSQAAFpEu8sqCkwAALCJd5JUFJwEAWES6yCsLTgIAsIh0kVcWnAQAYBHpIq8s/w+CTrFCtdG52gAAAABJRU5ErkJggg==" alt="" />

for循环

list或tuple可以表示一个有序集合。如果我们想依次访问一个list中的每一个元素呢?比如 list:

L = ['Adam', 'Lisa', 'Bart']
print L[0]
print L[1]
print L[2]

如果list只包含几个元素,这样写还行,如果list包含1万个元素,我们就不可能写1万行print。

这时,循环就派上用场了。

Python的 for 循环就可以依次把list或tuple的每个元素迭代出来:

L = ['Adam', 'Lisa', 'Bart']
for name in L:
print name

注意:  name 这个变量是在 for 循环中定义的,意思是,依次取出list中的每一个元素,并把元素赋值给 name,然后执行for循环体(就是缩进的代码块)。

这样一来,遍历一个list或tuple就非常容易了。

任务

班里考试后,老师要统计平均成绩,已知4位同学的成绩用list表示如下:

L = [75, 92, 59, 68]

请利用for循环计算出平均成绩。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAicAAACTCAIAAADa9i7AAAAQWUlEQVR4nO2d30sb6R6H+0ecm3ORI7lYWA7lQBcE2e4qDS5FpVAola3VtGdthd4U0VaKYfGkoLCFrtCmVKUXS5DtTUX0pheRQC8i/hX5VzwXk/npTDIzmTff7yTPw3ORH5OZt2v4fPadmcxc+8c//4WIiDgYr4mPABERR0daBxERB+e1wlgR+/EaAEAEl3AFWofWAQBTSCe8RmgdWgcATCGd8BqhdWgdADCFdMJrhNahdQDAFNIJrxFah9YBAFNIJ7xGaB1aBwBMIZ3wGqF1aB0AMIV0wmuE1qF1AMAU0gmvEVqH1gEAU0gnvEZoHVoHAEwhnfAaoX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yO4REff1KCcrR94pi+PiB998peOtzS++hcvv7HbxNpDndXeBsI9HGto67pGt2be9j/qwhw0xD0onvEYy2MPW2nsSVTwp97AFY9cN5VjzAL/ObjHPRKT06ti/t80yGOXuYuFzplubX4KX3746fwqR1kEcEaUTXiN9nzm9/DHRTaxD/jBJ5zoJW8e29OrYKR7fnjTX7nOdkL1n/rlObGkdxBFROuE10mfrzFROIuc66VvHf1zn/mrDd1wnZev4miNiz5uvjRY/nPuO64Qds4l/LMer4dbhuA6iFqUTXiMpWmemcpLp73XCz2FzTujy/bQlYev4TmALBLG75813Tlr5XdjykWclBDbRo4G8J+Z5z2RL1ToXUf80WgdRi9IJrxEd1yZARBxGpRNeI7QOIqIppRNeI7QOIqIppRNeI7QOIqIppRNeI7QOIqIppRNeI7QOIqIppRNeI7QOIqIppRNeI7QOIqIppRNeI320zu2to4vzRJfDoXUQcaSUTniNpG6dmcrJeW0v2UXYaB1EHCmlE14jKVtneuu4dbI1nfDSnxKtM3Vvt1mtd1y+675oPy4W7u5WdzduWA82N6zll+8+XK43q/XP9ybyN55f/vy6+XIy3nh03fUOcciUTniNpGqd21tHF8eV24kvOD341rnx9HN18+GV16NTvt5cvlssbVr57l8sP+OJ3zo673qHODRKJ7xG0rSOe9fqXLROfbcUfD065Xc3brjdMHVvt7n2tMtNsh88a3594/XvtR8kx9MxWevou+sd4tAonfAaSd463qZR3zqFTtA3q/WmZ5KRccprGc/CH28CLdj8+myhx3gU3vUOcWiUTniNJG4d597VXmvLelvHdurerhP0YnMdw+PpmOS4jqOiu94hDo3SCa+R/n6vk4e5jqP3mEpp0078iY21enOQc50BjCdV6yi66x3i0Cid8BoZ7tbxnTDWiXJLK9zrzWp9tzSxsTag1hnQeGK3jra73iEOm9IJrxGuTYCIaErphNcIrYOIaErphNcIrYOIaErphNcIrYOIaErphNfIte///Z9BKv4lGAJ//OnnH3/6WXwYiNhT6YTXCK2TP2kdxLwonfAaoXXyJ62DmBelE14jeWydqeevG6evX9yU/j71abrfchZoHcT8KJ3wGknTOre3jr2XwznamtPcOvdXG5/Kpq4vkN7Jtc3mH7/EXHi89O39o3a1ND1WLNA6iPlROuE1krJ1EjWN7B42na3zw8u/Yk90ru9UHx2uztE6iLlTOuE1Its6U89fN7ZnF7ZrjdNa47R2+Hy889bN8uGn8tTN8uFprXFac2vDeeV0daHnetyFHX2fCnUwd4F78Kz516+Tsb6100vz7WppeobWQcyf0gmvEfnWOa01tmeLBask7P1mncKwno6/+OQppM67V1rHVzaedxPNdQZz17UfXv715s8HsYY0Xvr2fn5nvFigdRBzqHTCa6Tv4zon/7vd71zHebqw7W0O98iN+7plaOu465l96z3qk7h1jN91bfLXv3vf9sZyZfXRt6XrhTFaBzGXSie8Rvo9h+23vWTF4/+TdG2d6L1h5lqnMIC7wC38Efc2PJ6moXUQ86h0wmuk7zOnn3xsXXz8LZPWGX/xqfb2/lixINo6zjoN3XUt/gnTK6uP2u+DHs7QOoi5UTrhNdJn68z9fnLe2lvJZA/b/VX3aH+GrePfWZdAI3ddS3TCtFfmOog5VDrhNZKideZ+P0n5Y53Q1gk9wSyidfzLux/p1jq+T/U4h834XdcSnEcQkNZBzKHSCa8R2WsTBI7rDL0JTpjuIq2DmBelE14jtE7+pHUQ86J0wmuE1smftA5iXpROeI3k8eqfoy6tg5gXpRNeI9xLFBHRlNIJrxFaBxHRlNIJrxFaBxHRlNIJrxFaBxHRlNIJrxFaBxHRlNIJrxFaBxHRlNIJrxFaBxHRlNIJrxFaBxHRlNIJrxFaBxHRlNIJr5H/A54Whot7qYiVAAAAAElFTkSuQmCC" alt="" />

while循环

和 for 循环不同的另一种循环是 while 循环,while 循环不会迭代 list 或 tuple 的元素,而是根据表达式判断循环是否结束。

比如要从 0 开始打印不大于 N 的整数:

N = 10
x = 0
while x < N:
print x
x = x + 1

while循环每次先判断 x < N,如果为True,则执行循环体的代码块,否则,退出循环。

在循环体内,x = x + 1 会让 x 不断增加,最终因为 x < N 不成立而退出循环。

如果没有这一个语句,while循环在判断 x < N 时总是为True,就会无限循环下去,变成死循环,所以要特别留意while循环的退出条件。

任务

利用while循环计算100以内奇数的和。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjUAAACrCAIAAAARhPrVAAAQzklEQVR4nO2dX0sj9xqA90Ocm3ORI7lYWA5ywEJAaruhwYNEWRDEpXW3uqfZFXojQV0pkeKJoNCCJ1BT/IMXJYTuTUX0Zi8igb2I+CnyAXrVP/TaczEzyUwyEzNm8nvn1efhuUjGSWox+z78JpPMo7/9/R+IiIhx85H4b4CIiNgtfUJExDhKnxARMY7SJ0REjKP0CRER4+ij33777ffff//jjz9+/fXXxEgSo/IRAEAAN9AH9Ik+AYBppCe/DugTfQIA00hPfh3QJ/oEAKaRnvw6oE/0CQBMIz35dUCf6BMAmEZ68uuAPtEnADCN9OTXAX2iTwBgGunJrwP6RJ8AwDTSk18Hj/7880/6RJ8AwCTSk18Hj/766y/6RJ8AwCTSk18HrJ/oEwCYRnry64D3n+gTAJgmyiHu97S9/4tBv0Pkv9uA0Cf6BACmiWyCd4XHd3s/+/TzWMPQJ/oEAKaJbIKHbEyf+0f4Gw4CfaJPAGCayCY4fUL6BAARMpRp7n3/6cbv/aSgu713k4I+0ScAMM1QpnnX+RG+230fErRdFvpEnwDANNGP8p7P+ei2DgVtl4U+0ScAME3Ec/y2J3xEn3q7dnlzc/m23z1vbm6ax9PSjaFPADAMohziAZ9k8r0bdrsscexTYiSZWLukTwBwX4lsgvfRm3726bFdkLge36NPAHB/iWyCBz9zj/9W2O1SmOvT9FHTXj9Zx+8u39rH8W6ahzPObjPHzdav1uqTs5/n4c3jaftI4OWa9z9k/bx2ZD9Vbc3a/uyw2Xpa+7bzI/oEAEYxMNzvAYb6NH3UvLlpH9+z7tbWnGzY29/WnGasXbb69LZmB6z908TapZWltcvOOLVTZD187dLVv9ZTJRMjbw+Png21x9KvfwCIL8KDXwkS66eA29NHTc+aqXk87d24dnnTdKKydnnTvLw8DFgArV16lk2eR1m3147bizb6BABmkZ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dCyn6hIh3UXry6yDi9ZPr5Ijo+pRaPynvzY8kE6J9aj3nXKmVKPqEiHdRevLrIOLzIxrnW5NPRh8/GZ3cOmvdHrBP8/n2+QsR9sl7qDCE7veiMgWnVeMbq5U6fULEfpSe/DqI9vNP7Q8/hYqTb598T64L6JN3//ZDevXJ86hbzt/znLxnR8jSylKlXqyUMuMbq/QJEftQevLrIJ7fH9Hx/hMi4r1SevLrgD4hIppWevLrgD4hIppWevLrIJ59QkS8z0pPfh3QJ0RE00pPfh3QJ0RE00pPfh3QJ0RE00pPfh3QJ0RE00pPfh3QJ0RE00pPfh3QJ0RE00pPfh3QJ0RE00pPfh3QJ0RE00pPfh3QJ0RE00pPfh3QJ0RE00pPfh3QJ0RE00pPfh3QJ0RE00pPfh3QJ0RE00pPfh38H0l6KuN/+cSIAAAAAElFTkSuQmCC" alt="" />

break退出循环

用 for 循环或者 while 循环时,如果要在循环体内直接退出循环,可以使用 break 语句。

比如计算1至100的整数和,我们用while来实现:

sum = 0
x = 1
while True:
sum = sum + x
x = x + 1
if x > 100:
break
print sum

咋一看, while True 就是一个死循环,但是在循环体内,我们还判断了 x > 100 条件成立时,用break语句退出循环,这样也可以实现循环的结束。

任务

利用 while True 无限循环配合 break 语句,计算 1 + 2 + 4 + 8 + 16 + ... 的前20项的和。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkYAAADwCAIAAABqszx6AAAYx0lEQVR4nO2d32tj552H/UfszV4oRheBsLgLExCYThsT4eDIJjAwzLD1zMjeesaQm0GMPW5qUxQZbEioYxjLa88wgSBMB0Jc47noXNioNVu5WnbbJk3b3YRGZLdslyQNkyb42ntxdKSjH0f2sSV9pK+eh4dBko+ONHDsh/f8evv+7u+fQURENGCf/BsgIiI2RZKGiIhGJGmIiGhEkoaIiEYkaYiIaESShoiIRux7+vTpV1995fxb4m9/+9vXX3/9+eefh/rD2Cz7AAB8OIZmQNJIGgDoUbfACCSNpAGAHnULjEDSSBoA6FG3wAgkjaQBgB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udu1AO4dDl1eydW+zkROw51S0wgmqU5sSstH+yNUnz30nYuqSFilXLpjJZz3CNpCFiI9UtMIIiaSPJbe+exiYcS3OfRmYfpleu9IdD0qSV1nl5tVQ1koaIjVS3wAiKpFWc8TgwuXmeMx4DT6Hp89NGSavchxlA73G16Lybt8G5O5ksSUNEr+oWGKG1SStffFa0FDbnYrXDXD5Yz+omLcgUmpXLl9/SKGkV7zrhjMeK0x2L3XJ0SpbJpjKr0cG5OyQNET2qW2CEbr97SNWxNETErlTdAiOQNEREveoWGIGkISLqVbfACN2eNEREC6pbYASShoioV90CI5A0RES96hYYgaQhIupVt8AIJA0RUa+6BUYgaYiIetUtMAJJQ0TUq26BEUgaIqJedQuMQNIQEfWqW2AEkoaIqFfdAiOQNEREveoWGIGkISLqVbfACCQNEVGvugVGIGmIiHrVLTDC/wOYdDB20IfKcQAAAABJRU5ErkJggg==" alt="" />

continue继续循环

在循环过程中,可以用break退出当前循环,还可以用continue跳过后续循环代码,继续下一次循环。

假设我们已经写好了利用for循环计算平均分的代码:

L = [75, 98, 59, 81, 66, 43, 69, 85]
sum = 0.0
n = 0
for x in L:
sum = sum + x
n = n + 1
print sum / n

现在老师只想统计及格分数的平均分,就要把 x < 60 的分数剔除掉,这时,利用 continue,可以做到当 x < 60的时候,不继续执行循环体的后续代码,直接进入下一次循环:

for x in L:
if x < 60:
continue
sum = sum + x
n = n + 1

任务

对已有的计算 0 - 100 的while循环进行改造,通过增加 continue 语句,使得只计算奇数的和:

sum = 0
x = 1
while True:
sum = sum + x
x = x + 1
if x > 100:
break
print sum

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjIAAADvCAIAAABnkanWAAAZeElEQVR4nO2dwUsjad6A+4/4Lt8hX8jhg2HpHZiGgKy7hg4ObpSBhsZmJm1HZ+wR+iJB7aY3YclGUGgZR2gj2o2HQcLMpZ2gh/UQyeIhIX9F/oC9zLe79NnvUKlUVVIVU1qp3y/p5+E5JGWMoU378Fa9ed97//Xf/4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RcSBla6AOljlARFRUukKqIMsISJKKl0BdZAlRERJpSugDrKEiCipdAXUQZYQESWVroA6yBIioqTSFVAHWUJElFS6Aur41LKUWN2snm9uTEq/ERERDaUroI6hZsl9MdZbf6I2iHeAvyzNZ6vHmWGt1ICIGCFLPQwtS8uH9WYlX+heYWjpqFOjVP7M3wa14b9dyBIiDlvpCqhjSFlK5c8Olz7rXfjuealZyc+Yd2cKp/a7/rKUWN2sbs2mt0rV81L1vFRejbe/NJkpH2cSk5nyeal6XrK60jlynk3f+DzWgzs6vstVbdsGIuIIKF0BdQz52lJXlux3zR0uzO2XbpOl81J1azYWMSpinpprF8W4G984thWr/dWeLDlqZPuqr9GStm0DEXEklK6AOkLP0llhuh2kSn4mlT+7U5aMJkWisUg0vWVPi3X1yDpu6Jol63lmd+1Xnnxnafjb9CHimCldAXWEP1oygtQ51zecLHmfcBteliL6tg1ERP1KV0Ad4WZppnBqP2sXwLUl825847i0Ox+NRUSz1HnO4W3Th4hjpnQF1BFulhwz8e4vHd1lJp4jJ/NZa0pCgFlyng/0obZtAxFRrdIVUMewsmR9OKltJ07Gh5ka9aa/JrlmyXWanEeWnI+3vqVflhzfdcNMPG3bBiLiaChdAXWM7ioPXdeWEBFHUukKqIMsISJKKl0BdZAlRERJpSugjtHNEiLiOChdAXWQJURESaUroA6yhIgoqXQF1EGWEBElla6AOsgSIqKk0hVQB1lCRJRUugLqIEuIiJJKV0AdZAkRUVLpCqiDLCEiSipdAXWQJURESaUroA6yhIgoqXQF1EGWEBElla6AOsgSIqKk0hVQB1lCRJRUugLqIEuIiJJKV0AdZAkRUVLpCqjj/wHfWGvdb8r1swAAAABJRU5ErkJggg==" alt="" />

多重循环

在循环内部,还可以嵌套循环,我们来看一个例子:

for x in ['A', 'B', 'C']:
for y in ['1', '2', '3']:
print x + y

x 每循环一次,y 就会循环 3 次,这样,我们可以打印出一个全排列:

A1
A2
A3
B1
B2
B3
C1
C2
C3

任务

对100以内的两位数,请使用一个两重循环打印出所有十位数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LRAwAANiiSrPOqzLG5WMAAN5RwvazLL7ataaJAQAAWxi6ftqpM2MguEkMAAB4h7HvJ51aHwO6gpQFGicGAAAsYez7Wa4tq0tcoEFiAADAEmUtX+namlVFiQEAAO8oa/lpyxaMDhVukxgAALBEKctP+7XmBKHcc0lzmiUGAAAsUYX5Txy/5AbaHYkBAABLmPlyrsVLClIWaJwYAACwhJkvKy0+Y/CHQSEAAE+pKAXsihgAALCFa4cXiRgAALCFa4cXiRgAALCFa4cXiRgAALCFa4cXiRgAALCFa4cXiRgAALCFa4cXiRgAALBFlWatujiAxSQAALymwgxQ3jS+cjjWGjEAAGCJ8h4dOjUxAAAwi5T36NCpiQEAgFmkvEcrXDtVY6BMPfohMQAAYI8KXD/t2qkp4vT9xRokBgAALFGB6ycsO9PrzZLg/wH5TpqsNoxmVQAAAABJRU5ErkJggg==" alt="" />
 

Python 入门(五)条件判断和循环的更多相关文章

  1. python学习第六天 条件判断和循环

    总归来讲,学过C语言的同学,对条件判断和循环并不陌生.这次随笔只是普及一下python的条件判断和循环对应的语法而已. 条件判断: 不多说,直接贴代码: age = 23 if age >= 6 ...

  2. Python基础:条件判断与循环的两个要点

    一.条件判断: Python中,条件判断用if语句实现,多个条件判断时用if...elif实现:看下面一段程序 #python 3.3.5 #test if...elif age = 20 if ag ...

  3. Python学习之条件判断和循环

    #coding= utf-8 # 条件判断和循环 # 如果if语句判断是True,就把缩进的两行print语句执行了,否则,什么也不做 age1 = 20 if age1 >= 18: prin ...

  4. python基础知识--条件判断和循环

    一.输入输出 python怎么来接收用户输入呢,使用input函数,python2中使用raw_input,接收的是一个字符串,输出呢,第一个程序已经写的使用print,代码入下: 1 name=in ...

  5. Python基础(条件判断,循环,占位符等)

    Python 自动化 系统开发用的语言和自动化脚本可以不同 学习peython可用于: 网路爬虫,数据分,web开发,人工智能,自动化运维,自动化测试,嵌入式,黑客 第三方库比较全 脚本语言:功能单一 ...

  6. python基础之条件判断和循环

    1.条件判断 age = 3 if age >= 18: print('adult') elif age >= 6: print('teenager') else: print('kid' ...

  7. Python基础教程-条件判断和循环

    Python条件判断 在Python中用if语句实现: age = 20 if age >= 18: print 'your age is :',age print 'adult' 根据Pyth ...

  8. Python中的条件判断、循环以及循环的终止

    条件判断 条件语句是用来判断给定条件是否满足,并根据判断所得结果从而决定所要执行的操作,通常的逻辑思路如下图: 单次判断 形式 if <判断条件>: <执行> else: &l ...

  9. Python学习笔记—条件判断和循环

    条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= ...

  10. python学习第六天:python基础(条件判断、循环)

    条件判断 格式 if <条件判断1>: <执行1> elif <条件判断2>: <执行2> elif <条件判断3>: <执行3> ...

随机推荐

  1. 使用yum时出现的404

    今天使用yum升级nss yum update nss 结果爆出错误: http://people.centos.org/tru/devtools-2/6Server/x86_64/RPMS/repo ...

  2. c++之——重载、重写、重定义

    函数重载: 必须在同一个类中进行: 子类无法重载父类的函数,父类同名函数将被子类名称覆盖: 重载是在编译期间根据参数类型和个数决定函数的调用(静态联编). 函数重写与重定义: 重写: 必须发生在基类和 ...

  3. CentOS 挂载NTFS

    直接在CentOS上挂载NTFS,报错支持ntfs格式: mount: unknown filesystem type 'ntfs' 原因:无法使用Kernel NTFS Module挂载Window ...

  4. git hook部署代码

    git 提供了钩子功能,当某个操作发生时,可以执行某个动作. ftp上传时没有文件比较,虽然可以winscp提供了同步功能但是不够强大,而且文件多了,会花费比较长的时间. 1.先在主机上搭建一个git ...

  5. FreeBSD下安装lua

    额,一行命令搞定啊 make freebsd CC=clang && make install 花了我一晚上,问题是啥呢? 我在Makefile中压根没有看到CC的定义在哪儿,然后直接 ...

  6. am335x USB 驱动框架记录

    参考: http://processors.wiki.ti.com/index.php/AM335x_USB_Driver%27s_Guide http://processors.wiki.ti.co ...

  7. linux中的ll(转)

    linux中的ll(转) 操作系统:ubuntu 9.04 ll并不是linux下一个基本的命令,它实际上是ls -l的一个别名. Ubuntu默认不支持命令ll,必须用 ls -l,这样使用起来不是 ...

  8. Netstat命令(windows下)

    功能: 一般用于检验本机各端口的网络连接情况. 例子:检查本机3389远程连接端口是否可用 netstat -nao|find  "3389" 查看某进程使用的端口号: netst ...

  9. e595. Drawing an Image

    See also e575 The Quintessential Drawing Program and e594 Reading an Image or Icon from a File. publ ...

  10. linux命令详解之netstat

    今天在使用linux的时候,要查看端口号,但是不知道要使用哪一个命令所以就学习了一下,原来是使用netstat,接下来给大家一起来学习. 一.netstat介绍 1.1.简介 Netstat 命令用于 ...