In this tutorial, you will learn about keywords (reserved words in Python) and identifiers (names given to variables, functions, etc.).
In this article, you will learn-
Python Keywords
Keywords are the reserved words in Python.
Python has a set of keywords that are reserved words that can’t be used as variable names, function names, or some other identifiers:
We can’t use a keyword as a variable name, function name, or some other identifier. They are used to characterize the punctuation and structure of the Python language.
In Python, keywords are case touchy.
There are 33 keywords in Python 3.7. This number can fluctuate somewhat through the course of time.
All the keywords except True
, False
and None
are in lowercase and they must be written as they are. The list of all the keywords is given below.
False | await | else | import | pass |
None | break | except | in | raise |
True | class | finally | is | return |
and | continue | for | lambda | try |
as | def | from | nonlocal | while |
assert | del | global | not | with |
async | elif | if | or | yield |
Looking at all the keywords at once and trying to figure out what they mean might be overwhelming.
Python Identifiers
- A Python identifier is a name used to identify a variable, function, class, module, or other objects.
An identifier is a name given to elements like class, functions, variables, and so on. It assists with separating one element from another.
Rules for writing identifiers
Rules for writing an identifier keep similar rules as variables. An identifier begins with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, and digits (0 to 9).
- Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore
_
. Names likemyClass
,var_1
andprint_this_to_screen
, all are valid examples.
2. An identifier cannot start with a digit. 1variable
is invalid, but is a valid name.
3. Keywords cannot be used as identifiers.
global = 1
Output
File "<interactive input>", line 1 global = 1 ^ SyntaxError: invalid syntax
4. We cannot use special symbols like !, @, #, $, % etc. in our identifier.
a@ = 0
Output
File "<interactive input>", line 1 a@ = 0 ^ SyntaxError: invalid syntax
5. An identifier can be of any length.
Things to Remember
Python is a case-touchy language. This implies variable and variable are not equivalents.
Continuously give the identifiers a name that makes sense. While c = 10 is a valid name, writing count = 10 would bode well, and it is simpler to make sense of what it speaks to when you take a gander at your code after a long gap.
Multiple words can be separated using an underscore, like this_is_a_long_variable
.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.