## 六、Python的程序流程
----From a high schoolstudent's view to learn Python
关键字:
python 高中生学编程 MIT公开课视频 Python语言程序流程 判断 循环
一、条件和分支
在Python中代码的逻辑顺序有几种,而条件和分支是很重要的一类。最典型的体现这一逻辑顺序的语句就是if语句。
if语句:
在Python中if语句看起来十分的简单和熟悉,它由三部分组成:关键字,用于判断的条件表达式,以及在表达式为真或假是应该执行的代码。在之前的内容中已经向大家介绍了简单的if语句的写法并且举了几个例子,那么下面就向大家介绍一下一些复杂的if语句。
1.多重条件表达式
一般大家在写程序的时候只会在条件行中写一个条件,但是通过使用逻辑操作符:and,or,not可以将不同的几个判断条件连接在一起从而形成多重判断条件或是否定判断条件,这样做的好处是可以把一些需要用几个if语句表达的条件和在一个里面从而提高了效率。例如:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 109.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">6</span></p></td><td valign="top" style="width: 447.4px; height: 109.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>i=[1,2,3,4]</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>x=3</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>if x<9 and x in i:</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr>print('trueanswer')</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">true answer</span></p></td></tr></tbody></table>
这样就把需要用两句话表达的式子连接在了一起从而不用大家在写第二遍。
2.单一语句的代码块
虽然大家一般把if语句的开头和条件部分写在两行中,但是其实可以合起来写在一行中,但是我并不推荐大家这么写,虽然这样可能会省空间(省了也没用……)但是这很难区分,而且这样也不能分清逻辑的主次关系,并且如果你要添加新的代码,还是要把它移动到另一行。
else语句
在Python中,大家可能会发现写一个if语句只能给条件的一种可能设置该向下执行的代码,如果要给另一种可能设置该怎么办?这里就引入了和if语句搭配的else语句。如果if语句中的条件表达式的结果为false,那么就会执行else语句中的代码,代码和if语句基本相同:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 109.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">6</span></p></td><td valign="top" style="width: 447.4px; height: 109.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>if x<4:</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>print('small')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr>else:</wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>print('big')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">big</span></p></td></tr></tbody></table>
需要注意的是,一般在else之后并不需要像if语句那样添加一些条件,因为else的条件就是if语句条件false时的情况。还有就是要注意else语句的摆放,也就是缩进问题。在Python中,else语句应和if语句搭配使用,但是如果有好几个if语句,而你只想把else语句与其中的一个if语句搭配怎么办?这其实很简单,只需要把else语句和你想搭配的if语句对齐,这就是大家不得不再次注意缩进的问题,因为这是一个十分重要的表示代码的逻辑顺序的问题,例如:
第一个
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 147.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">8</span></p></td><td valign="top" style="width: 447.4px; height: 147.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>x=7</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>if x>0:</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>ifx<8:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>print('small')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>else:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>print('big')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">small</span></p></td></tr></tbody></table>
第二个
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 147.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">8</span></p></td><td valign="top" style="width: 447.4px; height: 147.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>x=7</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>if x>0:</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>ifx<8:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>print('small')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr>else:</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>print('big')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">small</span></p></td></tr></tbody></table>
以上这两种代码的含义是不同的,因为第一个代码中的else语句对应的是第二个语句因为它是和其对其的,但是第二个代码中else和最上面的if语句对齐,因此这就体现出了缩进的重要性,如果缩进错了,结果就可能完全不相同。
elif语句
elif顾名思义,就是else-if语句,用它可以检查多个表达式是否为真,elif语句和else语句一样,也是可加可不加,但是不同的是,一个if语句中最多有一个else语句,但是却可以有很多歌elif语句,例如:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 166.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">8</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">9</span></p></td><td valign="top" style="width: 447.4px; height: 166.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>x=9</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>if x>10:</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>print('big')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr> <wbr>elifx>6:</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>print('middle')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr> <wbr>else:</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>print('small')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">middle</span></p></td></tr></tbody></table>
这就是条件分支的主要内容。
二、循环
循环也是在Python中十分常见的逻辑顺序,而这里一般是通过while语句和for语句来体现。
1. While语句
While语句实际上是一个条件循环语句,但是和if语句的条件不同,while中的代码会一直循环执行,直到循环的条件为false。While语句的语法其实和if语句有些相似,但是为了让程序循环起来,就必须要给一个每次循环结束时的指令来让下次循环进行,也就是所谓的自增语句。例如:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 223.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">8</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">9</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">10</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">11</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">12</span></p></td><td valign="top" style="width: 447.4px; height: 223.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>x=0</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>while x<7:</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>print("thenumber is " , x)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>x+=1</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">the numberis <wbr> <wbr>0</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">the numberis <wbr> <wbr>1</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">the numberis <wbr> <wbr>2</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">the numberis <wbr> <wbr>3</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">the numberis <wbr> <wbr>4</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">the numberis <wbr> <wbr>5</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">the numberis <wbr> <wbr>6</wbr></wbr></span></p></td></tr></tbody></table>
这就是一个while语句计数循环的例子。当然有计数循环就会有无限循环,在其中你必须相信的使用while循环,因为他有可能无限次的循环下去,所以大家要看清楚。
2.for语句
我们现在要介绍的就是Python中的另一个循环语句:for语句。它是Python中最为实用并且强大的循环语句。For循环不同于while循环,它可以提供一个可循环的对象,并将对象(列表,元组等刻可迭代对象)全部执行完才会结束循环。例如:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 166.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">8</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">9</span></p></td><td valign="top" style="width: 447.4px; height: 166.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>alist=[1, 2, 3, 4, 5]</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>for eachnumber in alist:</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>print(eachnumber,"is a integer")</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1 is a integer</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2 is a integer</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3 is a integer</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4 is a integer</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5 is a integer</span></p></td></tr></tbody></table>
这就是简单的for语句的例子,而不仅仅可以以列表等为循环对象,连字符串都可以,例如:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 261.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">8</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">9</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">10</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">11</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">12</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">13</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">14</span></p></td><td valign="top" style="width: 447.4px; height: 261.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>for eachletter in 'HelloPython':</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>print("this is letter", eachletter)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter H</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter e</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter l</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter l</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter o</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter P</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter y</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter t</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter h</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter o</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter n</span></p></td></tr></tbody></table>
3. while与for的区别
来看例子:
1到n得自然数进行累加,求累加和小于10000时的最大自然数n的值?
在循环次数未知时,如果需要使用循环,就需要使用while
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 128.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">6</span></p></td><td valign="top" style="width: 447.4px; height: 128.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">sum = 0</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">i = 1</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">while sum + i <=10000:</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> sum += i;</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> i += 1</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">print "from 1 to %d, the sum is%d(near 10000)" % (i, sum)</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br/></p></td></tr></tbody></table>
再看例子:
从1累加到10000,求累加和:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 90.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p></td><td valign="top" style="width: 447.4px; height: 90.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">sum = 0</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">for i inrange(1,10001):</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px"> <wbr> <wbr> sum += i</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144); min-height: 14px;"><span style="letter-spacing: 0.0px"> <wbr> <wbr> <wbr/></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">print "sum 1 to 10000 is equal%d" % sum</span></p></td></tr></tbody></table>
4. range的使用说明
range是一个built-in函数,函数定义如下:
**range([start],stop[,step])**
This is a versatile function tocreate lists containing arithmetic progressions. It is most oftenused in [**for**](http://www.python.org/doc//current/reference/compound_stmts.html) loops.The arguments must be plain integers. Ifthe *step* argument isomitted, it defaults to 1.If the *start* argument isomitted, it defaults to 0.The full form returns a list of plainintegers [start, start + step, start + 2 * step, ...].If *step* is positive, thelast element is the largest start + i * step lessthan *stop*;if *step* is negative, thelast element is the smallest start + i * step greaterthan *stop*. *step* mustnot be zero (or else [**ValueError**](http://www.python.org/doc//current/library/exceptions.html) israised). Example:
~~~
range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8,9]
range(1, 11)
[1, 2, 3, 4, 5, 6, 7, 8, 9,10]
range(0, 30, 5)
[0, 5, 10, 15, 20,25]
range(0, 10, 3)
[0, 3, 6,9]
range(0, -10, -1)
[0, -1, -2, -3, -4, -5, -6, -7,-8, -9]
range(0)
[]
range(1, 0)
[]
~~~
注意以下几点:
- range()返回的是一个list
~~~
type(range(10))
<type'list'>
~~~
- start的缺省参数为0,step的缺省参数为1(*缺省参数将在函数的章节中介绍)
- 如果step的值为负数,则产生的序列为倒序
这就是Python中的条件分支与循环的主要内容,下节会向大家讲解函数的知识。
再次强调,对于循环、条件分支语句在写程序的时候千万注意缩进,如果在程序出错的时候,首先要检查的也是缩进是否准确,然后再找其他的问题。
我的更多文章:
- [Python程序调试的一些体会](http://blog.sina.com.cn/s/blog_d6cca93e0101ewc9.html)(2013-10-06 22:57:35)
- [十四、Python编程计算24点(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101euxx.html)(2013-10-03 22:18:28)
- [十三、Python编程计算24点(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101eukc.html)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含图片")
(2013-10-02 22:15:46)
- [十二、Python简单数据结构应用(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101euk8.html)(2013-10-02 22:10:41)
- [十一、Python简单数据结构应用(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101ep9z.html)(2013-09-23 23:31:49)
- [十、Python编程解决组合问题(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101entc.html)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含图片")
(2013-09-21 23:37:27)
- [九、Python编程解决组合问题(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101ent7.html)(2013-09-21 23:32:54)
- [八、Python的函数编程(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101ekwj.html)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含视频")
(2013-09-20 23:09:39)
- [七、Python的函数编程(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101ekwg.html)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含视频")
(2013-09-20 23:09:10)
- [高中生如何学编程](http://blog.sina.com.cn/s/blog_d6cca93e0101e8fn.html)(2013-09-02 19:26:01)