1. 程式人生 > >Python3 入門

Python3 入門

Python下載

官網下Python3,安裝好後新增一下環境變數就可以了

cmd中輸入 path=%path%;C:\Python (C:\Python是Python的安裝路徑)

在cmd中輸入 python -V檢視python版本,如果可以說明安裝成功

Hello World

輸出"Hello World!",程式碼為print(“Hello World!”)

一:cmd命令提示符

  1. 輸入python進入互動介面
  2. 輸入程式碼 print(“Hello World!”)
  3. 按 ctrl+Z退出
    在這裡插入圖片描述

二:執行py指令碼

  1. 新建txt檔案,記事本開啟
  2. 輸入程式碼 print(“Hello World!”),儲存推出,字尾改成 .py
  3. 在檔案所在資料夾sheft+右鍵,選擇cmd/powershell,輸入python,再輸入檔名
  4. 第三步也可以直接win+Q,查詢cmd開啟,輸入python,再輸入檔案的路徑(直接拖過去也行)
    在這裡插入圖片描述

IDE下載

PyCharm下載: https://www.jetbrains.com/pycharm/download/

安裝介面: 路徑任意,勾選下面三個
在這裡插入圖片描述

Active:

找到C:\Windows\System32\drivers\etc\hosts,用記事本開啟,在檔案的最後一行的下一行,輸入 0.0.0.0 account.jetbrains.com

(如果不能修改,先另存到桌面,再拖過去將原檔案覆蓋,hosts無後綴)

回到PyCharm中選擇Activation code,輸入以下程式碼即可


K71U8DBPNE-eyJsaWNlbnNlSWQiOiJLNzFVOERCUE5FIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiIiwiYXNzaWduZWVFbWFpbCI6IiIsImxpY2Vuc2VSZXN0cmljdGlvbiI6IkZvciBlZHVjYXRpb25hbCB1c2Ugb25seSIsImNoZWNrQ29uY3VycmVudFVzZSI6ZmFsc2UsInByb2R1Y3RzIjpbeyJjb2RlIjoiSUkiLCJwYWlkVXBUbyI6IjIwMTktMDUtMDQifSx7ImNvZGUiOiJSUzAiLCJwYWlkVXBUbyI6IjIwMTktMDUtMDQifSx7ImNvZGUiOiJXUyIsInBhaWRVcFRvIjoiMjAxOS0wNS0wNCJ9LHsiY29kZSI6IlJEIiwicGFpZFVwVG8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U3yenSzr/OEur1+jfOxnQSmEcMXKXgrAQ9U55gwjcOFKrgOxEdek/Sk1VfOjvS+nuM4eyEruFMfaZHzoQiuw4IqgGc45ohFH0UUyjYcuFxxDSU9lMCv8qdHKm+wnPRb0l9l5vXsCBDuhAGYD6ss+Ga+aDY6f/qXZuUCEUOH3QUNbbCUlviSz6+GiRnt1kA9N2Qachl+2yBfaqUqr8h7Z2gsx5LcIf5kYNsqJ0GavXTVyWh7PYiKX4bs354ZQLUwwa/cG++2+wNWP+HtBhVxMRNTdVhSm38AknZlD+PTAsWGu9GyLmhti2EnVwGybSD2Dxmhxk3IPCkhKAK+pl0eWYGZWG3tJ9mZ7SowcXLWDFAk0lRJnKGFMTggrWjV8GYpw5bq23VmIqqDLgkNzuoog==

PyCharm上手

  1. Create New Project
  2. Location選擇一個project的路徑(可能彈出Error: Standard Python ‘venv’ module not found,屬於Python版本的問題,不用管)
  3. File->Settings->Project:xxx->Interpreter,<No …>改成你的Python即可
    在這裡插入圖片描述
    在這裡插入圖片描述
  4. 在Project中右鍵,New->Python File,就可以在新建的py檔案中愉快的敲程式碼了
    在這裡插入圖片描述
  5. 按shift+ctrl+ F 10 F_{10} ,在下面的run視窗中進行輸入輸出

程式示範

print("Hello\n")

for i in range(1,5):
    print(i,end=",")
    print(i+1)
print("next is ",end=": ");print(2);print()

a="12"
a+=('12')
print(a,end="\n\n")
a = 10
a+=12
print(a)

b='''


'''
print("st->"+b+'<-en' ,end='。')

Output:

Hello

1,2
2,3
3,4
4,5
next is : 2

1212

22
st->


<-en。

學C/C++後學Java感覺差別不大,但是Python看上去卻很不一樣,但是如果學過C的話,入門只需要最多三小時

縮排問題

Python 的程式碼塊不使用大括號 {} 來控制:類,函式以及其他邏輯判斷。而是使用相同的縮排來表示一塊程式碼塊

同一塊程式碼的縮排必須相同,一個空格都不能錯。且開頭塊前不能有空格

if True:
    print(1)
    print(2)
    if True:
         print(3)
    print(4)
else:
    print(5)

相當於:

if (True){
    print(1)
    print(2)
    if (True){
         print(3)
    }
    print(4)
}
else{
    print(5)
}

Output:

1
2
3
4

行問題

一行程式碼可能很長,其他語言中直接換行即可,但是Python卻以新行作為語句的結束符。如果需要一行寫成多行程式碼,在程式碼後面加 '\'即可

自帶{},[],()的程式碼可以不需要

print(\
   2+3)
   
a=["aaa",
   11]
print(a)