💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 四、Python语言简单介绍 ----From a high school student'sview to learn Python 关键字:高中生 学编程 Python语言 Python语言安装 (一)、下载和安装Python 下载Python最直接的方法就是访问它的官网([www.python.org](http://www.python.org)),然后点击屏幕左侧的 “download”选项。点击之后就会进入如下的界面: [![四、Python语言简单介绍](https://box.kancloud.cn/d697f9b22a73843d39c21a67e360978b_955x540.jpg "四、Python语言简单介绍")](http://photo.blog.sina.com.cn/showpic.html#blogid=d6cca93e0101ejcj&url=http://album.sina.com.cn/pic/d6cca93egx6DbGnJAch91&690) 接下来大家就可以根据自己电脑的系统来选取各自的下载资源,在电脑平台中,主要还是针对Win,Unix, Mac OSX这三大平台。但是Python可应用的平台十分广泛,不仅仅是前面所提到的电脑平台,手机平台上也可以进行Python语言的学习与使用,例如:Android,IOS等等。 注意:因为这个教程针对的是Python3.0以下的版本,所以大家最好选择Python 2.7.5来下载。而为什么选择Python2.7.5呢,是因为Python3.0的升级是革命性的,把以前的一些特性抛弃了,当然也引入了很多新的特性,因此并不具备向前的兼容性,而且许多的第三方软件并没有针对Python3.0进行移植,所以还是Python2.7.x更实用。 在下载完成之后就直接进行安装,安装时我们尽量选择按照提示的缺省目录来进行操作,然后我们就可以来运行Python了。 (二)、运行Python 总体来说有三种方法来运行Python,今天先向大家介绍前两种较为简单的运行方法。 (1)     在命令行上启动解释器 按Windows键+R,输入CMD [![四、Python语言简单介绍](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "四、Python语言简单介绍")](http://photo.blog.sina.com.cn/showpic.html#blogid=d6cca93e0101ejcj&url=http://album.sina.com.cn/pic/d6cca93egx6DbG8NJzz49&690) ![](https://box.kancloud.cn/fcfb77cec2bdc5ca68dcb8a7f92a5a44_723x364.jpg) 之后找到python目录,运行python [![四、Python语言简单介绍](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "四、Python语言简单介绍")](http://photo.blog.sina.com.cn/showpic.html#blogid=d6cca93e0101ejcj&url=http://album.sina.com.cn/pic/d6cca93egx6DbG8RPsx8d&690) ![](https://box.kancloud.cn/a237a696a464a4d7c30d3a269b2ad379_846x690.jpg) 显示如下: [![四、Python语言简单介绍](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "四、Python语言简单介绍")](http://photo.blog.sina.com.cn/showpic.html#blogid=d6cca93e0101ejcj&url=http://album.sina.com.cn/pic/d6cca93egx6DbG8Mx8Na5&690) ![](https://box.kancloud.cn/3f965443db4d6cd9a0eddbf7be5cd0ff_846x165.jpg) 我们就可以打开命令行来编写最简单的代码了。但是这种命令行只能每次输入一行代码来执行,效率太低。 (2)    在简单的集成开发环境内运行 这里所说的简单集成环境就是IDLE,它适合去编辑一些简单短小的代码,对于初学者来说是完全够用的。 IDLE在哪呢?如果按照缺省目录安装则在:C:\Python27\Lib\idlelib [![四、Python语言简单介绍](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "四、Python语言简单介绍")](http://photo.blog.sina.com.cn/showpic.html#blogid=d6cca93e0101ejcj&url=http://album.sina.com.cn/pic/d6cca93egx6DbG8MW7Qce&690) ![](https://box.kancloud.cn/29f706f87d84713ae38018192d23428d_846x319.jpg) 或者直接在搜索目录中查找IDLE然后运行,就会出现如下窗口: [![四、Python语言简单介绍](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "四、Python语言简单介绍")](http://photo.blog.sina.com.cn/showpic.html#blogid=d6cca93e0101ejcj&url=http://album.sina.com.cn/pic/d6cca93egx6DbG8M6R7f5&690) ![](https://box.kancloud.cn/76bca3949ab47e7232946f45bd537c8e_846x221.jpg) 接下来点击左上角的File然后选择NewWindow就可以通过新的File来编写代码了。 [![四、Python语言简单介绍](https://box.kancloud.cn/bfea4b6df70752b35575f7fbed8dfdd8_846x217.jpg "四、Python语言简单介绍")](http://photo.blog.sina.com.cn/showpic.html#blogid=d6cca93e0101ejcj&url=http://album.sina.com.cn/pic/d6cca93egx6DbG8VAwJ35&690) 在编写完一个程序后,将文件保存为类型为.py的文件,点击菜单“run”,再点击”RunModule”或者直接按“F5”键运行。 [![四、Python语言简单介绍](https://box.kancloud.cn/839da6280bc0ae75cd31e5fce148ceb9_846x269.jpg "四、Python语言简单介绍")](http://photo.blog.sina.com.cn/showpic.html#blogid=d6cca93e0101ejcj&url=http://album.sina.com.cn/pic/d6cca93egx6DbG8XZog7c&690) 之后,如果运行正常,弹出的窗口中就会显示运行的结果: [![四、Python语言简单介绍](https://box.kancloud.cn/1fe1aa59209d96ac148aff0adfe50cfc_857x246.jpg "四、Python语言简单介绍")](http://photo.blog.sina.com.cn/showpic.html#blogid=d6cca93e0101ejcj&url=http://album.sina.com.cn/pic/d6cca93egx6DbG8WLoE64&690) (3)第三种方法 还有一些第三方的专门的软件可以用来进行Python的开发,而且比上面介绍的两种方法都高级,但是工具的安装比较复杂,我们还是先把精力用在语言学习上吧,等后续有必要的时候在介绍。 作为一门解释性的语言,Python的简洁与纯粹使用提供了一个更加快速的开发环境,Python的语法十分简单,更加易懂,相比于C++和JAVA来说用Python编程可以生许多时间。 并且Python适用范围广泛,它最大的优势就是在网页的编辑上,但是在和计算机的“底层”打交道时(例如直接操控一些硬件),Python的效率就会相当低下,但是它是十分适合没有计算机语言基础的初学者来学习。 (三)、Python的快速介绍 Python包含的主要元素: 1. 输入与输出 一般的,在Python中想要输入一些内容,只需要在平台中打出你想要的内容即可,而输出程序则是用print函数来进行。 2. 注释  注释就是在写代码的过程中,程序员希望用一些语句来帮助他在以后返回看自己所写的程序,而这些语句并不能够出现在真正的代码当中。所以在Python中,语句一般用#标示注释,从#开始到一行结束都是注释。 例: <table cellspacing="0" cellpadding="0" style="background-color:#ffffff; border-collapse:collapse"><tbody><tr><td valign="top" style="width:30.1px; height:14px; border-style:solid; border-width:1px 3px 1px 1px; border-color:rgb(203,203,203) rgb(66,146,135) rgb(203,203,203) rgb(203,203,203); padding:2.5px"><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">1</span></p></td><td valign="top" style="width:400.4px; height:14px; border-style:solid; border-width:1px 1px 1px 3px; border-color:rgb(203,203,203) rgb(203,203,203) rgb(203,203,203) rgb(66,146,135); padding:2.5px"><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">print (“hello”) # commentone</span></p></td></tr></tbody></table> 注释还有一种方式就是使用三个”””,在后面的程序中会有使用及说明。 3. 操作符 操作符就是大家日常学习工作中会用到的一些符号,像+、-、*、/、//、**(乘方)、%等标准操作符,还有一些比较操作符,像<  > <= >=  == !=(不等于)<>(判断两个数不相等)。 还有一些逻辑操作符:and or not。这些操作符大家可以在命令行中或者IDLE中自己试一下。 例: <table cellspacing="0" cellpadding="0" style="background-color:#ffffff; border-collapse:collapse"><tbody><tr><td valign="top" style="width:30.1px; height:185px; border-style:solid; border-width:1px 3px 1px 1px; border-color:rgb(203,203,203) rgb(66,146,135) rgb(203,203,203) rgb(203,203,203); padding:2.5px"><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><span style="letter-spacing:0.0px">10</span></p></td><td valign="top" style="width:400.4px; height:185px; border-style:solid; border-width:1px 1px 1px 3px; border-color:rgb(203,203,203) rgb(203,203,203) rgb(203,203,203) rgb(66,146,135); padding:2.5px"><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;3+5</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">8</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;3&lt;4</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">True</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;3!=4</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">True</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;2&gt;4 and 3&lt;4</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">False</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;2&gt;4 or 3&lt;4</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">True</span></p></td></tr></tbody></table> 4. 变量和赋值  变量简单来说就是类似于方程中的x,y这一类有名字的元素,变量名就是一些字母开头的标示符,中间可以带有数字,下划线等。变量的赋值就是把变量和数字用等号连接起来,从而给变量一个具体的值。赋的值并不一定要是数字,也可以是字符串(接下来会介绍)。 <table cellspacing="0" cellpadding="0" style="background-color:#ffffff; border-collapse:collapse"><tbody><tr><td valign="top" style="width:30.1px; height:109px; border-style:solid; border-width:1px 3px 1px 1px; border-color:rgb(203,203,203) rgb(66,146,135) rgb(203,203,203) rgb(203,203,203); padding:2.5px"><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><span style="letter-spacing:0.0px">6</span></p></td><td valign="top" style="width:400.4px; height:109px; border-style:solid; border-width:1px 1px 1px 3px; border-color:rgb(203,203,203) rgb(203,203,203) rgb(203,203,203) rgb(66,146,135); padding:2.5px"><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;start=2</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;start</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">2</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;second="two"</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;second</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">'two'</span></p></td></tr></tbody></table> 5. 数字  Python中支持五中基本数字类型,分别是: 1) 有符号整型:int  如:231   56  -342 2) 长整数 <table cellspacing="0" cellpadding="0" style="background-color:#ffffff; border-collapse:collapse"><tbody><tr><td valign="top" style="width:26.6px; height:128px; border-style:solid; border-width:1px 3px 1px 1px; border-color:rgb(203,203,203) rgb(66,146,135) rgb(203,203,203) rgb(203,203,203); padding:2.5px"><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><span style="letter-spacing:0.0px">6</span></p></td><td valign="top" style="width:361.9px; height:128px; border-style:solid; border-width:1px 1px 1px 3px; border-color:rgb(203,203,203) rgb(203,203,203) rgb(203,203,203) rgb(66,146,135); padding:2.5px"><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;a=1234569l</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;type(a)</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&lt;type'long'&gt;</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;b=-12345678901234567890123l<wbr/></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;type(b)</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&lt;type'long'&gt;</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt; <wbr/></span></p></td></tr></tbody></table> 注意后面有一个“L”跟着。   3) 布尔型:bool  True   False 在这里要说的是布尔型是一个特殊的整型,但是要把它放在一个数字的环境中时,Treu用1表示,而False用0表示 4) 浮点型:float  3.1423 5) 复数:complex  0+73j   -32-549j 6.字符串  在Python中字符串被定义成引号之间字符的集合。Python中可以对字符串进行多种操作,包括索引,切片,串联字符串,重复字符串等等。 [![四、Python语言简单介绍](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "四、Python语言简单介绍")![](https://box.kancloud.cn/6c492e551820ebb7686c1d365cb402bb_521x69.jpg)](http://photo.blog.sina.com.cn/showpic.html#blogid=d6cca93e0101ejcj&url=http://album.sina.com.cn/pic/d6cca93egx6DbIRU3oW66&690) 绿色的部分就是字符串 7. 列表和元组(list andtuple)  列表和元组可以被认为是普通的数组,它能够包含任意数量,任意类型的Python对象,但是列表和元组之间还是有一定的区别。列表用中括号([])来包括,列表中元素的个数和元素的值均可以被改变,而元组则用小括号(())包括,而且元素不可以被更改。下边是列表和元组的例子: <table cellspacing="0" cellpadding="0" style="background-color:#ffffff; border-collapse:collapse"><tbody><tr><td valign="top" style="width:30.1px; height:109px; border-style:solid; border-width:1px 3px 1px 1px; border-color:rgb(0,0,0) rgb(66,146,135) rgb(0,0,0) rgb(0,0,0); padding:2.5px"><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><span style="letter-spacing:0.0px">6</span></p></td><td valign="top" style="width:400.4px; height:109px; border-style:solid; border-width:1px 1px 1px 3px; border-color:rgb(0,0,0) rgb(0,0,0) rgb(0,0,0) rgb(66,146,135); padding:2.5px"><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;alist=[1, 2, 3, 4, 'hello']</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;alist</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">[1, 2, 3, 4,'hello']</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;atuple=(1, 2, 3, 4, 'hello', 'Python')</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;atuple</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">(1, 2, 3, 4, 'hello','Python')</span></p></td></tr></tbody></table> 现在看上去似乎列表和元组没有什么区别,但是在之后的内容中我会想大家详细的介绍各种对于列表和元组的操作。 8. 字典  字典是Python中的映射数据类型,其实简单上来说就是把一些对象赋值,这些对象在字典中称为键,而赋的值也可以是任意类型的Python对象,字典元素用大括号({})包括,如果要取出一些字典中的键,则用中括号来包裹元素。在接下来的内容中会向大家详细介绍字典。例如: <table cellspacing="0" cellpadding="0" style="background-color:#ffffff; border-collapse:collapse"><tbody><tr><td valign="top" style="width:30.1px; height:90px; border-style:solid; border-width:1px 3px 1px 1px; border-color:rgb(0,0,0) rgb(66,146,135) rgb(0,0,0) rgb(0,0,0); padding:2.5px"><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><span style="letter-spacing:0.0px">5</span></p></td><td valign="top" style="width:400.4px; height:90px; border-style:solid; border-width:1px 1px 1px 3px; border-color:rgb(0,0,0) rgb(0,0,0) rgb(0,0,0) rgb(66,146,135); padding:2.5px"><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;first_Dict={ 'Hello': 'Python', 'First': 1}</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;first_Dict</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">{'First': 1, 'Hello':'Python'}</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;first_Dict ['Hello']</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">'Python'</span></p></td></tr></tbody></table> 9. Python语法中的缩进对齐   Python中经常用缩进对齐来表示代码的逻辑,而不是使用大括号。这使得程序的可读性更高,但是对于一个Python的初学者来说,仅使用缩进可能会使他感到奇怪并且十分的困惑,而且会非常的不适应。在我一开始学习的时候就经常因为缩进的距离错误导致程序无法运行。但是在我使用这种方式一段时间以后,我发现其实并不是太困难去接受。 10. if 语句 if语句是Python中最为基础的语句,它的意思简单来说就是如果一个条件被满足,就会向下执行下面的条件,这就相当于是一个条件语句,我们可以来看几个例子: <table cellspacing="0" cellpadding="0" style="background-color:#ffffff; border-collapse:collapse"><tbody><tr><td valign="top" style="width:30.1px; height:90px; border-style:solid; border-width:1px 3px 1px 1px; border-color:rgb(0,0,0) rgb(66,146,135) rgb(0,0,0) rgb(0,0,0); padding:2.5px"><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><span style="letter-spacing:0.0px">5</span></p></td><td valign="top" style="width:400.4px; height:90px; border-style:solid; border-width:1px 1px 1px 3px; border-color:rgb(0,0,0) rgb(0,0,0) rgb(0,0,0) rgb(66,146,135); padding:2.5px"><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;x=-1</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;if x&lt;0:</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">... <wbr> <wbr>print('hello')</wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><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(2,28,161)"><span style="letter-spacing:0.0px">hello</span></p></td></tr></tbody></table>  这是一段十分简单的代码,那么大家就可以看到了,if语句就相当一个条件满足了之后就会运行下一层的代码,但是在if语句下的一层代码要注意缩进。 11. while循环  while循环语句的语法与if语句的时分类似,但是while是一个循环语句,它会一直不断的执行循环,直到表达式的值变成0或者False,我们来看下例子: <table cellspacing="0" cellpadding="0" style="background-color:#ffffff; border-collapse:collapse"><tbody><tr><td valign="top" style="width:30.1px; height:166px; border-style:solid; border-width:1px 3px 1px 1px; border-color:rgb(0,0,0) rgb(66,146,135) rgb(0,0,0) rgb(0,0,0); padding:2.5px"><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><span style="letter-spacing:0.0px">9</span></p></td><td valign="top" style="width:400.4px; height:166px; border-style:solid; border-width:1px 1px 1px 3px; border-color:rgb(0,0,0) rgb(0,0,0) rgb(0,0,0) rgb(66,146,135); padding:2.5px"><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;x=1</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;while x&lt;5:</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">... <wbr> <wbr>print('circle#% x' % x)</wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">... <wbr> <wbr>x+=1</wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><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(2,28,161)"><span style="letter-spacing:0.0px">circle # 1</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">circle # 2</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">circle # 3</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">circle # 4</span></p></td></tr></tbody></table>  这就是最简单的while循环语句,如果不给它一个结束条件的话,循环就会一直进行下去,而且也要注意缩进。接下来的内容会向大家详细介绍while循环。 12. for循环 for循环不同于while循环的是for循环通常是包含一个徐序列,就像是在以区间内执行一个循环一样,例如range(),列表和元组甚至是字典内执行循环,例如: <table cellspacing="0" cellpadding="0" style="background-color:#ffffff; border-collapse:collapse"><tbody><tr><td valign="top" style="width:30.1px; height:147px; border-style:solid; border-width:1px 3px 1px 1px; border-color:rgb(0,0,0) rgb(66,146,135) rgb(0,0,0) rgb(0,0,0); padding:2.5px"><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><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(2,28,161)"><span style="letter-spacing:0.0px">8</span></p></td><td valign="top" style="width:400.4px; height:147px; border-style:solid; border-width:1px 1px 1px 3px; border-color:rgb(0,0,0) rgb(0,0,0) rgb(0,0,0) rgb(66,146,135); padding:2.5px"><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;alist=[1, 2, 3, 4]</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">&gt;&gt;&gt;for i in alist:</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">... <wbr> <wbr>print(i)</wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><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(2,28,161)"><span style="letter-spacing:0.0px">1</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">2</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">3</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(2,28,161)"><span style="letter-spacing:0.0px">4</span></p></td></tr></tbody></table> 就像上面这个循环中所说的,for循环是在给定的alist之中执行的,而while循环则没有这些条件。 我的更多文章: - Python程序调试的一些体会(2013-10-06 22:57:35) - 十四、Python编程计算24点(之二)(2013-10-03 22:18:28) - 十三、Python编程计算24点(之一)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含图片") (2013-10-02 22:15:46) - 十二、Python简单数据结构应用(之二)(2013-10-02 22:10:41) - 十一、Python简单数据结构应用(之一)(2013-09-23 23:31:49) - 十、Python编程解决组合问题(之二)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含图片") (2013-09-21 23:37:27) - 九、Python编程解决组合问题(之一)(2013-09-21 23:32:54) - 八、Python的函数编程(之二)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含视频") (2013-09-20 23:09:39) - 七、Python的函数编程(之一)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含视频") (2013-09-20 23:09:10) - 高中生如何学编程(2013-09-02 19:26:01)