作為第一次嘗試,這里我們會非常細致的介紹每一個步驟,
任務1 :在終端中輸出“Hello World!”
第一步 啟動終端:在windows系統中,我們可以使用快捷鍵 win + r 啟動命令視窗,然后輸入cmd回車,啟動命令提示符(終端)

(這是在windows系統中命令提示符打開后的樣子)

(這是在Mac系統中終端打開后的樣子)
第二步 啟動python:在當前游標下輸入python回車

正常情況,你將進入python環境,看到當前python的版本資訊,如圖Python 2.7.16等等,
Warning:如果你看到“‘python ’不是內部或外部命令,也不是可運行的程式,”說明python解釋器的路徑還沒有寫入環境變數,請參考下面文章鏈接:https://blog.csdn.net/sui_152/article/details/81210295,
第三步:鍵盤輸入print(“Hello world”)

Well done!你已經成功的輸出了hello world,如果你愿意,你還可以嘗試print一些其他的東西,
有時我們也會犯一些小錯誤,例如在python3.x版本中,print是函式,必須要加上括號,不然就會出現下面的提示,
>>> print "hello world!"
File "<stdin>", line 1
print "hello world!"
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("hello world!")?
SyntaxError 指的是語法錯誤,python 會提示你可能發生了什么錯誤,以及錯誤在哪,這些錯誤提示我們慢慢就會熟悉起來,
接下來,我們再看一個錯誤,
>>> print ("hello world!)
File "<stdin>", line 1
print("hello world!)
^
SyntaxError: invalid character '(' (U+FF08)
這個錯誤的原因是錯誤使用了中文括號,在python中通常使用的符號都應該是英文格式,
繼續糾錯……試著不要直接看結果自己找到出錯的原因,
>>> print("hello world!)
File "<stdin>", line 1
print("hello world!)
^
SyntaxError: EOL while scanning string literal
這個錯誤的原因是因為引號沒有成對出現,
關于引號的使用:
#引號應成對出現,形式有雙引號和單引號兩種
雙引號: “I Love Python“
單引號: ‘Good Luck!'
#雙引號和單引號可以內外嵌套,但仍需成對出現,
>>> print('"hello world"')
"hello world"
>>> print("'hello python'")
'hello python'
#三引號內的字串按原樣輸出,
>>> print('''
... hello world
... hello python
... I love coding''')
hello world
hello python
I love coding
任務2:使用python IDLE新建并運行一個.py檔案
IDLE是python自帶的集成開發環境,可以方便的創建、運行、除錯python程式,
輸出以下內容(步驟三正常執行后,將看到下圖中的結果):
bindeAir:documents binhu$ python hello_world.py
hello world
***********
* *
***********
步驟一:找到并打開python IDLE,新建一個檔案
步驟二:在檔案中寫入以下代碼:
print("hello world")
print('''
***********
* *
***********''')
保存命名為hello_world.py,選擇檔案存盤位置(這個位置要便于你找到.py檔案,最好檔案路徑都是英文),
步驟三:執行hello_world.py
打開命令提示符/終端,利用cd命令切換到剛剛存放.py檔案的檔案夾下,輸入命令:python hello_world.py.
(大家在完成本任務后,可以使用PyCharm再做一次,感受一下兩者在程式撰寫與執行時的區別,)
作業部分:
(作為初學者,希望你能親手鍵入每一個代碼,感受編程的樂趣,而不是直接復制代碼)
作業:使用print函式正確輸出以下內容
Hello World!
Hello Again
I like typing this.
This is fun.
Yay! Printing.
I'd much rather you 'not'.
I "said" do not touch this.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/185007.html
標籤:其他
下一篇:python入門(二)小康小白
