## 五、Python的語言基礎
----From a high schoolstudent's view to learn Python
關鍵字:
python 高中生學編程 MIT公開課視頻Python語言基礎 數據類型 變量 關鍵字
1.基本的語句和語法規則
(#)表示之后的代碼均為注釋(在同一行內)。
(\n)表示換行,是行與行之間的分隔符。
(\)表示繼續上一行的代碼
(;)表示將兩句語言連在一起,但通常是一句一行。
(:)表示將一個程序的開頭和身體部分連在一起(十分重要)
縮進對齊:這在之前的內容中已經向大家介紹了,這在Python中十分重要,用來表示語句的邏輯關系,稍有不慎就會造成程序無法運行。
2.標識符,變量,關鍵字
(1)變量:什么是變量呢?變量就是可變的量,也就是說你可以隨便改變它們,給它們賦不同的值,而在Python中,你可以通過將兩個元素通過“=”來連接,通俗來說也就是把一個舊的東西貼上一個新的標簽,但是這個標簽可以隨時更改。我們來看個例子:
<table cellspacing="0" cellpadding="0" style="background-color: #ffffff; border-collapse: collapse"><tbody><tr><td valign="top" style="width: 26.8px; height: 128px; 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: 362.9px; height: 128px; 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">>>>MyExample= 6</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">>>>print(MyExample)</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">6</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">>>>MyExample=7</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">>>>print(MyExample)</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">7</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/></span></p></td></tr></tbody></table>
增量賦值:增量賦值就是把等號和一個算數操作符連在一起,從而將計算結果重新賦給左邊的元素。就像:x=x+1,也可以寫成:x+=1,這種增量賦值法也被使用在給列表賦值的時候。
多重賦值:多重賦值就是同時把幾個變量賦成同一個值。
多元賦值:多元賦值就是同時把多個變量賦成不同的各自的值。
(2) 標識符:
標識符是電腦語言中允許作為名字的有效字符串集合,說白了就是一個名字,一個和身份。其中有一部分是關鍵字,也就是構成語言的標識符。這種關鍵字舉例來說就是像:and,class, elif, if, while, for, import,not等,雖然這其中有許多大家可能不認識,但是向for這些關鍵字是在Python中本身就有特殊含義的詞,因此不能用于其他用途,否則會引起語法錯誤(SyntaxError)。除了關鍵字以外,Python中還有一些被稱為“內建”的標識符的集合(built-in)例如:int,float, long, bool,complex等。雖然這些不是關鍵字,但是我們應該把它們當成關鍵字,盡量避免來使用它們,因為不這樣做的話經常會混淆并產生歧義。
正確的標識符命名方法:
υ第一個字符必須是字母或者下劃線。
υ剩下的字母可以是數字,字母或者下劃線
υ大小寫是有區別的
3. 簡單的數據類型
首先要向大家介紹的是數字,內容來自[www.python.org](http://www.python.org)
(1) Numeric literals
There are four types of numericliterals: plain integers, long integers, floating point numbers,and imaginary numbers. There are no complex literals (complexnumbers can be formed by adding a real number and an imaginarynumber).
Note that numeric literals donot include a sign; a phraselike?-1?is actually an expressioncomposed of the unary operator ‘-‘ and theliteral?1.
Integer and long integerliterals
Integer and long integerliterals are described by the following lexicaldefinitions:
longinteger???::=??integer?("l"| "L")
integer?? ???::=??decimalinteger?|?octinteger?|?hexinteger?|?bininteger
decimalinteger?::=??nonzerodigit?digit*| "0"
octinteger ???::=?"0" ("o" | "O")?octdigit+| "0"?octdigit+
hexinteger ???::=?"0" ("x" | "X")?hexdigit+
bininteger ???::=?"0" ("b" | "B")?bindigit+
nonzerodigit??::=?"1"..."9"
octdigit ?? ??::=?"0"..."7"
bindigit ?? ??::=?"0" | "1"
hexdigit ?? ??::=??digit?|"a"..."f" | "A"..."F"
其中“longinteger”指的是長整型,通常用“l”,“L”來表示,decimalinteger指的是十進制數,也就是我們通常在數學中用到的計算方法。Octinteger指的是八進制數,也就是說從只有0到8,到9之后就會進位。Hexinterger指的是16進制數,也就是各位包含0到15,而在9之后用數字無法表示,就需要用A到F來表示,也就是用1到F來表示。Bininteger表示是二進制數,也應該是大家除了十進制數外最熟悉的計算方式,因為是二進制,所以各位只有0和1兩個數,之后就會進位。
關于十進制與其它進制之間的轉換方法:
在Python中,進制的轉換是不需要人為計算的,只需在打出固定形式的進制數,然后在打印出來,就是十進制的數字了。例如:八進制數:0o+進制數??
16進制數:0x+進制數???
二進制數0b+進制數。
下面來舉幾個例子:
a=0xff
a
255
這是16進制數的轉換
a=0o10
a
8
這是8進制數的轉換
a=0b00101
a
5?????
這是2進制數的轉換
(2) Floating pointliterals(浮點數)
Floating point literals aredescribed by the following lexical definitions:
floatnumber??::=??pointfloat?|?exponentfloat
pointfloat???::=?[intpart]?fraction?|?intpart?"."
exponentfloat?::=?(intpart?|?pointfloat)?exponent
intpart ?? ??::=??digit+
fraction?? ??::=?"."?digit+
exponent?? ??::=?("e" | "E") ["+" | "-"]?digit+
Note that the integer andexponent parts of floating point numbers can look like octalintegers, but are interpreted using radix 10. Forexample,?077e010?is legal, anddenotes the same number as?77e10. The allowedrange of floating point literals is implementation-dependent. Someexamples of floating point literals:
3.14?? 10.? ?.001? ? 1e100?? 3.14e-10? ?0e0
Note that numeric literals donot include a sign; a phraselike?-1?is actually an expressioncomposed of the unaryoperator?-?and theliteral?1.
(3) Imaginaryliterals(虛數)
Imaginary literals aredescribed by the following lexical definitions:
imagnumber?::=?(floatnumber?|?intpart)("j" | "J")
An imaginary literal yields acomplex number with a real part of 0.0. Complex numbers arerepresented as a pair of floating point numbers and have the samerestrictions on their range. To create a complex number with anonzero real part, add a floating point number to it,e.g.,?(3+4j). Some examples of imaginaryliterals:
3.14j ?10.j? ? 10j ?? .001j ?1e100j? 3.14e-10j
當然在認識完數字之后,我們還需要用數字來進行運算,這就不可避免的要介紹一下運算符。運算符分為兩類:算術運算符,比較運算符和邏輯運算符兩種。
(1)算術運算符:之前給大家大概介紹過算術運算符,這里就在補充幾點,就是關于運算的順序問題,其實這和數學中的運算順序差不多:()的優先級最高,如果有嵌套的圓括號,則先計算最內層的圓括號內的表達式。如果同一層內有多對并列的而非嵌套的圓括號,則是從左向右計算。*,/,%(求余)的優先級次之,若果有多個,則從左向右計算。+,-的優先級最低,如果有多個,則從左向右計算。雖然這些很基礎,但是大家在寫程序的時候也要注意。例如:
<table cellspacing="0" cellpadding="0" style="background-color: #ffffff; border-collapse: collapse"><tbody><tr><td valign="top" style="width: 26.8px; 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: 362.9px; 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">>>>(1+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">16</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+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">13</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*4/(4*7)</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">0.42857142857142855</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*4/4*7</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">21.0</span></p></td></tr></tbody></table>
(2)比較運算符:比較運算符在前面也向大家較為詳細的講過了,主要有相等運算符==,! =和關系運算符>< >=<=兩種,但是要注意的就是==和=不要相互混淆,=是賦值的意思。
(3) 邏輯運算符:這個也在之前講過,主要有and, or,not三種。and就是兩者全部正確才是true。or就是兩者中有一個正確就算true。not就是取非的意思。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)
(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)
(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)
(2013-09-20 23:09:39)
- [七、Python的函數編程(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101ekwg.html)
(2013-09-20 23:09:10)
- [高中生如何學編程](http://blog.sina.com.cn/s/blog_d6cca93e0101e8fn.html)(2013-09-02 19:26:01)