- 本教程為python3文檔解讀
- 本教程面向完全型小白,只要你會在電腦上打字,那你就可以看懂。
- 參考視頻觀看,味道更加:https://www.bilibili.com/video/av13774085/
- 建議優(yōu)先閱讀本系列的《編程的本質(zhì)》這一章節(jié)。
貫穿始終的理念:別廢話,就是干!
往期回顧:
- 有特殊作用的注釋代碼
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
- EOF
- 導(dǎo)入sys模塊
import sys
- 參數(shù)傳遞
sys.argv[ 0 ]
- 變量
我們在上一章說給大廈取名字,其實就是一個賦值操作。
我們在常見的教科書上,可能會看到類似這樣的寫法:
>>> a = 100
>>> a
100
>>> b = [ 123, 234, 1, a ]
>>> b
[123, 234, 1, 100]
>>> b[3]
100
>>> b[2]
1
>>> a = 99
>>> a
99
>>> b
[123, 234, 1, 100]
>>> b[3]
100
這幾行代碼的意思是:
把100賦值給a
取a值 # 注釋:在編程中,取a值一般會說成:打印a的值。
得到100
把列表[123,234,1,a]賦值給b
打印b的值
得到 [123, 234, 1, 100] #注釋:注意3號值
打印列表b的3號值
得到100
打印列表b的2號值
得到1
把99賦值給a #注釋:注意這里,a的值改變了。這就是變量這個名字的由來
打印a值
得到99
打印b值
得到列表 [123, 234, 1, 100] #注釋:注意3號值,等到我們專門講解到變量的時候,我們會在深入,現(xiàn)在只要記住結(jié)果就好。
打印b的3號值
得到100
大家能理解嗎?
試著自己動手輸入看看,就像下圖:(此處可參考視頻演示)

文檔解讀
python3文檔第二小節(jié)鏈接地址:2. Using the Python Interpreter
本章主要講解2.1.2. Interactive Mode(交互模式)與2.2.1. Source Code Encoding(源程序的編碼)這兩個小節(jié)
段落截?。ㄒ唬?/h2>
When commands are read from a tty, the interpreter is said to be in interactive mode. In this mode it prompts for the next command with the primary prompt, usually three greater-than signs (>>>);
$ python3.6
Python 3.6 (default, Sep 16 2015, 09:25:04)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
名詞解釋
-
tty
初學(xué)者可以理解為我們的shell,也就是終端。
-
interactive mode
中文翻譯為:交互模式
看名字也能理解,就是人與機器互動的意思。比如你輸入1 + 1,機器回復(fù)你等于2 。
- python提示符
>>>
When commands are read from a tty, the interpreter is said to be in interactive mode. In this mode it prompts for the next command with the primary prompt, usually three greater-than signs (>>>);
$ python3.6
Python 3.6 (default, Sep 16 2015, 09:25:04)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
初學(xué)者可以理解為我們的shell,也就是終端。
中文翻譯為:交互模式
看名字也能理解,就是人與機器互動的意思。比如你輸入1 + 1,機器回復(fù)你等于2 。
>>>
這個符號不用多說了吧,python解釋器的提示符,用以表明python解釋器已經(jīng)成功啟動,可以輸入了。
段落大意
啟動python解釋器后就可以進入python的交互模式進行操作。輸入提示符是>>>。
段落截取(二):
for continuation lines it prompts with the secondary prompt, by default three dots ( ... ).
Continuation lines are needed when entering a multi-line construct. As an example, take a look at this if statement :>>> the_world_is_flat = True >>> if the_world_is_flat : ... print("Be careful not to fall off!") ... Be careful not to fall off!
名詞解釋
-
...
三個點也是提示符,不過它表示的是python解釋器的多行輸入。 -
if statement
中文翻譯:if語句。
statement就是語句的意思,你在編程時寫的每一行代碼基本上都可以看成是一個語句。
段落大意
當我們進行多行輸入時,提示符會變成三個點。連續(xù)按兩下回車會退出多行輸入。
這里要說一下,如果我們想在交互模式下進行多行輸入,那么有4個要注意的點:
- 首行語句要以分號:結(jié)尾。
- 提示符會變?yōu)?strong>三個點...
- 第二行要有縮進
- 連續(xù)兩次回車退出多行輸入
2.2. The Interpreter and Its Environment(解釋器及其環(huán)境)
2.2.1. Source Code Encoding(源代碼編碼)
這個小節(jié)的內(nèi)容我應(yīng)該不用多說了,我們會看到非常熟悉的代碼
#!/usr/bin/env python3
# -*- coding: cp-1252 -*-
如果不認識,把cp-1252換成utf-8,與往期回顧對照看下。
不過這里還是要摘抄一下:
段落截?。ㄈ?/h3>
To declare an encoding other than the default one, a special comment line should be added as the first line of the file.
名詞解釋
- comment
中文翻譯:注釋。
段落大意
To declare an encoding other than the default one, a special comment line should be added as the first line of the file.
中文翻譯:注釋。
有特殊作用的注釋語句必須放在文件開頭。
python3文檔第二小節(jié)就此結(jié)束。
本期記憶點
- 變量的賦值
- 多行輸入條件
- 特殊注釋