沐之静夏 發表於 2019-6-10 10:37:00

Python 从入门到进阶之路(一)

<p>人生苦短,我用 Python。</p>
<p>Python 无疑是目前最火的语言之一,在这里就不再夸他的 NB 之处了,本着对计算机编程的浓厚兴趣,便开始了对 Python 的自学之路,并记录下此学习记录的心酸历程。</p>
<p>Python2.x 目前还是主流,但历史总是向前看的,所以就装了 Python3.x 的版本,但是版本不重要,重要的是学习里面的精髓,对于我这个 Python 小白来说,版本不重要,反正都是新语言。</p>
<p>现从最基本的打印 "hello world" 开始。</p>
<p>python3中print是一个内置函数,有多个参数,而python2中print是一个语法结构;</p>
<p>已经说过不再拿 py2 和 py3 做比较了,我们就直接开始操作:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> python2 </span>
<span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 255, 1)">print</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">hello world</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> python3</span>
<span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">hello world</span><span style="color: rgba(128, 0, 0, 1)">"</span>)</pre>
</div>
<p>在 py2 中打印是不需要带括号的,当然也可以带上货号,但是会被认为是其他内容了,这里不做深究,以免自己走入误区。网上有很多关于 py2 和 py3 print 的区别。</p>
<p>在 py3 中,打印需要加括号。</p>
<p>在 python 的语法后面可以加 ; 也可以不加 ; ,在程序上都不会报错,类似于 js,但是 java 中就必须要加 ; 做结尾。</p>
<p>在上面的代码中,有以 # 开头的内容,这是 Python 中的注释语法</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 这是单行注释</span>
<span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">hello world</span><span style="color: rgba(128, 0, 0, 1)">"</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> hello world </span>
<span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(128, 0, 0, 1)">'''</span>
<span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(128, 0, 0, 1)">    这是多行注释,
</span><span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(128, 0, 0, 1)">    可以写多行注释在里面
</span><span style="color: rgba(0, 128, 128, 1)">6</span> <span style="color: rgba(128, 0, 0, 1)">    注释的内容不会被执行
</span><span style="color: rgba(0, 128, 128, 1)">7</span> <span style="color: rgba(128, 0, 0, 1)">'''</span></pre>
</div>
<p>再来看一下 Python 中如何定义变量</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> title = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">hello world</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 128, 128, 1)">2</span> money = 100
<span style="color: rgba(0, 128, 128, 1)">3</span> price = 10
<span style="color: rgba(0, 128, 128, 1)">4</span> money = money -<span style="color: rgba(0, 0, 0, 1)"> price
</span><span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(0, 0, 255, 1)">print</span>(money)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 90</span></pre>
</div>
<p>在 Python 中,可以直接定义变量,不需要像其他语言那样在变量名前面加修饰符。如果变量用 "" 包住的话为 string 类型,如果直接写数字为 number 类型,当然还有数据,元组等类型,这个后续再说。Python 定义变量:变量名:变量内容。</p>
<p>在 Python 中,有一些变量名称为关键字,我们在平时定义时不能用做变量名,如下:</p>
<p><img src="https://img2018.cnblogs.com/blog/1345718/201904/1345718-20190415134405065-1026912186.jpg" alt="" width="665" height="108"></p>
<p>我们再来看一下 Python 中的 input 输入功能:</p>
<p><img src="https://img2018.cnblogs.com/blog/1345718/201904/1345718-20190415114050812-51853671.jpg" alt="" width="304" height="167"></p>
<p>在 Python2 中与 Python3 中的 input 方法一致的是 raw_input(),不做 py2 深究。</p>
<p>接下来看一下 Python 中的 if 判断语句:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> name = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">张三</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 128, 128, 1)">2</span> age = 55
<span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 0, 255, 1)">if</span> age &gt;= 10 <span style="color: rgba(0, 0, 255, 1)">and</span> age &lt; 40<span style="color: rgba(0, 0, 0, 1)">:
</span><span style="color: rgba(0, 128, 128, 1)">4</span>   <span style="color: rgba(0, 0, 255, 1)">print</span>(1, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">%s的年龄是%d</span><span style="color: rgba(128, 0, 0, 1)">"</span> %<span style="color: rgba(0, 0, 0, 1)"> (name, age))
</span><span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(0, 0, 255, 1)">elif</span> age &gt;= 40 <span style="color: rgba(0, 0, 255, 1)">and</span> age &lt; 60<span style="color: rgba(0, 0, 0, 1)">:
</span><span style="color: rgba(0, 128, 128, 1)">6</span>   <span style="color: rgba(0, 0, 255, 1)">print</span>(2, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">%s的年龄是%d</span><span style="color: rgba(128, 0, 0, 1)">"</span> %<span style="color: rgba(0, 0, 0, 1)"> (name, age))
</span><span style="color: rgba(0, 128, 128, 1)">7</span> <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">:
</span><span style="color: rgba(0, 128, 128, 1)">8</span>   <span style="color: rgba(0, 0, 255, 1)">print</span>(3, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">%s的年龄是%d</span><span style="color: rgba(128, 0, 0, 1)">"</span> % (name, age))</pre>
</div>
<p>运行结果:</p>
<p><img src="https://img2018.cnblogs.com/blog/1345718/201904/1345718-20190415124801990-428926787.jpg" alt="" width="380" height="253"></p>
<p>在上面的代码中,我们使用了 if 判断语句,if 判断语句的连贯语法是 if 条件:elif 条件:else:,条件不需要用括号括起来,每个条件结束后需加 :冒号结尾,如果有多个判断条件 且 并列时用 and 连接,或 条件用 or 连接。</p>
<p>在 if 判断语句中判断执行的内容需要按一下 Tab 键,如果和 if 判断语句对其会报错。</p>
<p>在上面的 print 打印中引入了格式化变量输出格式,可以是代码看起来更加优雅一些,具体的格式化如下:</p>
<p><img src="https://img2018.cnblogs.com/blog/1345718/201904/1345718-20190415125026326-1133598682.jpg" alt="" width="459" height="629"></p>
<p>再来看一下 Python 中的循环</p>
<p><img src="https://img2018.cnblogs.com/blog/1345718/201904/1345718-20190415153103193-422055046.jpg" alt="" width="330" height="381"></p>
<p>上面的 range() 是 Python 自带的一个方法,生成 0-4 五个数字,for in 循环输出,然后 print() 打印,在打印中,如果 print( i ) 或者 print( i, ) 都是打印换行,默认情况下 print() 会换行,如果不想换行,在打印内容后面加 end="" 即可。</p>
<p>再来看一下 Python 中的运算符号:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> a, b, c = 3, 2<span style="color: rgba(0, 0, 0, 1)">, 0
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(a, b)
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span> c = a +<span style="color: rgba(0, 0, 0, 1)"> b
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">a+b的值为</span><span style="color: rgba(128, 0, 0, 1)">"</span>, c)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 5</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span> c = a -<span style="color: rgba(0, 0, 0, 1)"> b
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">a-b的值为</span><span style="color: rgba(128, 0, 0, 1)">"</span>, c)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 1</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span> c = a *<span style="color: rgba(0, 0, 0, 1)"> b
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">a*b的值为</span><span style="color: rgba(128, 0, 0, 1)">"</span>, c)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 6</span>
<span style="color: rgba(0, 128, 128, 1)"> 9</span> c = a /<span style="color: rgba(0, 0, 0, 1)"> b
</span><span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">a/b的值为</span><span style="color: rgba(128, 0, 0, 1)">"</span>, c)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 1.5</span>
<span style="color: rgba(0, 128, 128, 1)">11</span> c = a %<span style="color: rgba(0, 0, 0, 1)"> b
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">a%b的值为</span><span style="color: rgba(128, 0, 0, 1)">"</span>, c)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 1</span>
<span style="color: rgba(0, 128, 128, 1)">13</span> c = a **<span style="color: rgba(0, 0, 0, 1)"> b
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">a**b的值为</span><span style="color: rgba(128, 0, 0, 1)">"</span>, c)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 9</span>
<span style="color: rgba(0, 128, 128, 1)">15</span> c = a //<span style="color: rgba(0, 0, 0, 1)"> b
</span><span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">a//b的值为</span><span style="color: rgba(128, 0, 0, 1)">"</span>, c)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 1</span></pre>
</div>
<p>在 Python 定义变量时,我们也可以如上面的代码第 1 行那样进行定义。Python 中的运算符号如下:</p>
<p><img src="https://img2018.cnblogs.com/blog/1345718/201904/1345718-20190415164551647-1253861904.jpg" alt="" width="582" height="440"></p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/weijiutao/p/10709313.html
頁: [1]
查看完整版本: Python 从入门到进阶之路(一)