各位同學除夕快樂,除舊布新之時預祝能玩
你好世界 回憶上次内容- 了解了 Python
- 安裝了 python
- 進入了 Python
- 退出了 Python
- 可是我們什麼也沒有做就離開了 IDLE 遊樂場!
你好世界
#首先進入Python3
python3
我們想要來個hello world!然後直接輸入
#貌似程序都是從hello world開始的
hello world


- 好像系統爆了錯誤
- 系統還挺友好
- 告訴我錯在哪了
- 那我錯哪兒了?
加上引号- 通過報錯
- 我們知道了這是一個 SyntaxError 語法錯誤
- 他不認識 hello world


- hello world 應該是字符串
- 需要給他加上雙引号 "hello world" 引起來
- 這回真的輸出了!!! 但是好像輸出也有引号 不過是單引号
- 我想的是直出 hello world 沒有引号
- 應該怎麼樣做呢? 來胡亂嘗試一下
直接輸出- 顯示輸出英文是什麼? display 但是系統又爆了錯誤 ❌
- 其實輸出的英文是 print 這不是打印麼? 我們用的是顯示器啊 沒有用打印機


緣由- Python 誕生于 1990s 給他帶來啟發的語言誕生于 1960s、1970s 當時的機器使用電傳打字機進行輸出 代碼裡的輸出都使用 print 函數 就成了一個文化
- 我直接把 print 這個函數名放到遊樂場裡面 告訴我 print 是一個内建函數 built-in function


- 這就是交互式編程環境的好處
- 啥都告訴你
- 有來有回的
- 這個環境叫做 REPL 什麼是REPL呢? REPL
- Read - Evaluate - Print - Loop
- 讀取 - 執行 - 打印輸出 - 循環這個過程


加上括号- print 是一個函數
- 函數後面必須得加上一對小括号
- 小括号裡面放置參數
- 我們先放一個 h 但是不行 因為系統把 h當做一個變量名 不認識 h


加上引号- 必須給 h 加上雙引号 "h" 成為一個字符串 字符串就能當 print 函數的參數


符号含義- ()括号含義 意味着告訴 python 要把 print 理解成一個函數 print() 輸出空行 而且括号裡還可以放參數 print(h) h 應該對應一個變量 如果想要直接輸出h 這個時候就需要引号
- 引号含義 引号把字符引用起來形成一個字符串 就像引用名人名言一樣 所以引号叫做引号
- 整合起來 print("h") 輸出"h"字符串 "h"就是 print 函數的參數 參數放在小括号裡
#使用print函數
print("h")
#輸出hello world
print("hello world")
拼寫細節


- 拼寫錯一點兒都不行
- 叫錯名字的話就找不到這個函數了
玩解釋器- python 是個解釋器
- 可以一行行進行解釋
- 想讓他幹什麼直接輸入就可以
1 1
300*40


來點過分的
123123123123*123123131213212313


系統崩潰


- 這個數字好大
- 也難怪這個 Python 比較慢
- 看起來他對于加法、乘法做了什麼處理
- 其他程序受制于計算機字長限制
- 無法直接處理這麼大的數字乘法
- python卻可以
- 所以python能不慢麼?!
- 字符串能做乘法嗎?
字符串乘法
"hello"*10


- 想讓每個 hello 都間隔一個空格應該如何呢?
- 試試看
編輯 py 文件- 我想要編輯一個完整的 python
- 文件保存好之後再執行
- 那就要先退出解釋器
#退出解釋器
quit()
新建 py 文件- 退出 python3 的 IDLE 環境回到 shell 之後
#用vi打開并編輯oeasy.py
vi oeasy.py
- 用 vi 新建一個叫 oeasy.py 的文件 vi 是編輯神器 是我們主要的編輯工具 oeasy.py 是 python文件 的文件名 py 代表 python vi 和 oeasy.py 之間有個空格


進入 vi


- 注意寫的是 "oeasy.py" [新文件] 特别要注意左下角這個位置 就像小地圖一樣重要
編輯 py 文件- 按 i 鍵進入編輯模式 再觀察左下角 如下圖所示 顯示 --插入(粘貼)--
- 可以進行插入文字了 在上面輸入 print("hello Python world")


回到正常模式- 輸入 print("hello Python world") 之後
- 按 esc 鍵回到正常模式 再觀察左下角 --插入(粘貼)-- 不顯示了
- 如下圖


- oeasy.py 後面有個[ ]
- [ ]意味着沒保存
保存并退出- 按 : 鍵進入命令行模式 注意用的是左手小指和右手小指配合 左手小指shift 右手小指; 按下就從正常模式切換到了命令行模式 再觀察左下角
- 輸入wq w的意思是write寫文件,就是保存 q的意思是quit退出vi wq一起就是先保存後退出
- 然後按 回車 鍵
- 回到bash環境


- 如果想要對于 vi 深入了解 建議有時間的話點擊下面這個鍊接看看 oeasy教您玩轉vim 磨刀不誤砍柴工
- 編寫好了python文件我們可以查看麼?
查看 py 文件- cat oeasy.py 可以查看已經編寫的 oeasy.py 的内容
- oeasy.py 是編寫好的 python 程序
- 雖然 oeasy.py 隻有一句
- 但也是一個程序


運行 py 文件- python3 oeasy.py 運行 python3 程序 python3 是我們執行的解釋器程序 oeasy.py 是 python3 的參數 意思是用 python3 來執行 oeasy.py


- 注意大小寫的細節
- 如果要把 print 函數首字母大寫 會如何?
大寫 Print- vi oeasy.py
- 把 print 改成 Print
- 保存并退出 vim
- python3 oeasy.py


- 可以發現 python 文件中的代碼是大小寫敏感的
- 而且系統命令的執行也是大小寫敏感的
- 總之都是大小寫敏感的 (case-sensitive)
與其他語言比較


總結- 在解釋器裡玩耍 print("Hello World") 1 1
- 謎底揭曉,可以讓字符串乘法分開的 print("hello \n"*20) print("hello \t"*20)
- 編寫了py文件
- 運行了py文件
- 很簡單
- 讓我們繼續!!!
- python 還有什麼好玩的呢?
- 我們下次再說!
, 更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!