#注意字母大小寫
#英文標點符號
print("Hello World")
Hello World
如何尋求幫助
- 系統幫助檔案
- 例如 help(print)
- 萬能的百度
#利用系統的幫助檔案
help(print)
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
?
注釋
- 注釋夾雜在代碼中,給程式員看的內容,可以使用自然語言
- 對于復雜程序,演算法等,比較難理解的,需要加上注釋
- 注釋兩種:
- 行注釋
- 塊注釋
#Python入門陳述句
print("Hello World")
"""
多行注釋
代碼目的
代碼解釋
"""
Hello World
'\n多行注釋\n代碼目的\n代碼解釋\n'
變數
- 可以重復使用的內容,存放資料的記憶體塊
- 必須遵循先定義后使用的原則
#定義變數
#變數定義三部曲:變數名+賦值符+值或運算式
a = 100
#將變數a的值顯示在螢屏上
print(a)
b = 100 * 100
print(b)
print(B)
100
10000
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-10-286252cc0e35> in <module>()
6 b = 100 * 100
7 print(b)
----> 8 print(B)
NameError: name 'B' is not defined
變數命名規則
- 必要
- 區分大小寫
- 字母、下劃線、數字
- 不能以數字開頭
- 保留字和關鍵字不能使用
- 推薦
- 一般情況命名使用小寫字母
- 見名知意(有意義的變數名)
- 下劃線開頭一般為特殊含義和用法,多個單詞使用下劃線鏈接
- 對于類的命名,多個單詞直連,每個單詞首字母大寫
#查詢關鍵字
#匯入相關包
import keyword
#列印關鍵字
print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
變數宣告
- a = value
- a = b = c = value
- a, b, c = v1, v2, v3
a = 100
print(a)
a = b = 100 #從右向左賦值
print(a, b)
a, b, c = 1, 2, 3
print(a, b, c)
#變數名與值一一對應
#錯誤的定義方式
a, b, c = 1, 2, 3, 4
100
100 100
1 2 3
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-17-9f47cf12b0d3> in <module>()
5 a, b, c = 1, 2, 3
6 print(a, b, c)
----> 7 a, b, c = 1, 2, 3, 4
ValueError: too many values to unpack (expected 3)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/45146.html
標籤:Python
上一篇:Python全堆疊課程002
下一篇:Python代碼重用之函式入門
