讓我們來談談Python的關鍵字和標識符號。最近,我們在這個Python教程中還介紹了一個完整的安裝和設置Python的教程,專為初學者準備。
Python關鍵字
簡單來說,Python關鍵字是保留的詞彙。這意味著你不能將它們用作變量、類和函數的名稱。
所以你可能會想知道這些關鍵字的用途是什麼。它們用於定義Python語言的語法和結構。
你應該知道,截至撰寫本教程時,Python編程語言中有33個關鍵字。雖然這個數字可能會隨著時間的推移而有所變化。此外,Python中的關鍵字是區分大小寫的。所以它們應該按照原樣書寫。下面是Python編程中所有關鍵字的列表。
如果你查看所有的關鍵字,並試圖一次性弄清楚它們的用途,你會感到不知所措。所以現在只需知道這些是關鍵字即可。我們將分別學習它們的用途。你可以通過Python shell的幫助獲取Python關鍵字的列表。
所有Python關鍵字的列表
and | Logical operator |
as | Alias |
assert | For debugging |
break | Break out of Python loops |
class | Used for defining Classes in Python |
continue | Keyword used to continue with the Python loop by skipping the existing |
def | Keyword used for defining a function |
del | Used for deleting objects in Python |
elif | Part of the if-elif-else conditional statement in Python |
else | Same as above |
except | A Python keyword used to catch exceptions |
FALSE | Boolean value |
finally | This keyword is used to run a code snippet when no exceptions occur |
for | Define a Python for loop |
from | Used when you need to import only a specific section of a module |
global | Specify a variable scope as global |
if | Used for defining an “if” condition |
import | Python keyword used to import modules |
in | Checks if specified values are present in an iterable object |
is | This keyword is used to test for equality. |
lambda | Create anonymous functions |
None | The None keyword represents a Null value in PYthon |
nonlocal | Declare a variable with non-local scope |
not | Logical operator to negate a condition |
or | A logical operator used when either one of the conditions needs to be true |
pass | This Python keyword passes and lets the function continue further |
raise | Raises an exception when called with the specified value |
return | Exits a running function and returns the value specified |
TRUE | Boolean value |
try | Part of the try…except statement |
while | Used for defining a Python while loop |
with | Creates a block to make exception handling and file operations easy |
yield | Ends a function and returns a generator object |
以下是在 Python 程式中使用 if-else 的簡單範例。
var = 1;
if(var==1):
print("odd")
else:
print("even")
當我們執行上述程式時,Python 會因為固定的關鍵字和語法而理解 if-else 區塊,然後進行進一步的處理。
Python 識別符是什麼?
Python 識別符是我們給變量、函數、類、模塊或其他對象的名稱。這意味著每當我們想給一個實體一個名稱時,那就是識別符。
有時變量和識別符被誤解為相同的,但它們並不相同。好吧,為了澄清,讓我們看看什麼是變量?
Python 中的變量是什麼?
A variable, as the name indicates is something whose value is changeable over time. In fact a variable is a memory location where a value can be stored. Later we can retrieve the value to use. But for doing it we need to give a nickname to that memory location so that we can refer to it. That’s identifier, the nickname.
撰寫識別符的規則
有一些撰寫識別符的規則。但首先您必須知道 Python 是區分大小寫的。這意味著 Name 和 name 在 Python 中是兩個不同的識別符。以下是 Python 中撰寫識別符的一些規則。
- 標識符可以是大寫字母、小寫字母、數字或下劃線(_)的組合。所以myVariable、variable_1、variable_for_print都是有效的Python標識符。
- 標識符不能以數字開頭。因此,雖然variable1是有效的,1variable則不有效。
- 我們不能在標識符中使用特殊符號,如 !,#,@,%,$ 等。
- 標識符可以是任意長度。
雖然這些是寫作標識符的硬性規則,但也有一些命名慣例,雖不強制,但遵循它們是一種好的實踐。
- 類名稱以大寫字母開頭。所有其他標識符以小寫字母開頭。
- 標識符以單個下劃線開頭表示該標識符是私有的。
- 如果標識符以兩個下劃線開始並以兩個下劃線結束,那表示該標識符是語言定義的特殊名稱。
- 雖然c = 10是有效的,但寫作count = 10會更有意義,即使在很長時間後再看你的代碼時,也更容易理解它的作用。
- 多個單詞可以使用下劃線分隔,例如this_is_a_variable。
這是一個Python變量的示例程序。
myVariable="hello world"
print(myVariable)
var1=1
print(var1)
var2=2
print(var2)
如果你運行該程序,輸出將如下圖所示。
結論
所以,今天就到這裡。在下一篇教程中,我們將學習關於Python語句和註釋。在那之前 #happy_coding 🙂
Source:
https://www.digitalocean.com/community/tutorials/python-keywords-identifiers