Python Keywords and Identifiers
Python Keywords are the reserved words in Python while Python Identifiers are names given to entities like variables, functions, class etc.
Python Keywords
Keywords are the reserved words in Python that have defined meanings.
You can't use a keyword as a variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language.
Python keywords are case sensitive.
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.
and
as
assert
async
await
break
class
continue
def
del
elif
else
except
False
finally
for
from
global
if
import
in
is
lambda
None
nonlocal
not
or
pass
raise
return
True
try
while
with
yield
If you want to have an overview, here is the complete list of all the keywords with examples.
For example, there are 35 keywords in Python 3.8.
Note that the number of keywords can vary slightly over the course of time according to the Python version.
Python Identifiers
An identifier is a name given to entities like variables, functions, class, etc. It helps to differentiate one entity from another.
Rules for writing identifiers
- 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 example. - An identifier can't start with a digit.
1variable
is invalid, butvariable1
is a valid name. - The first character of the variable must be a letter or an underscore (
_
). - Keywords can't be used as identifiers.
global = 1
File "<interactive input>", line 1
global = 1
^
SyntaxError: invalid syntax - You can't use special symbols such as
!
,@
,#
,$
,%
etc. in the identifier name.a@ = 0
File "<interactive input>", line 1
a@ = 0
^
SyntaxError: invalid syntax - An identifier can be of any length.
- Identifier names are case sensitive.
Important things to Remember
The following are some tips to follow:
- Python is a case-sensitive language. This means,
Variable
andvariable
are not the same. - Always assign identifiers a name that makes sense. This will save time and make it easier to understand what it represents when you look at the code after a long interval.
- Multiple words can be separated using an underscore, like
this_is_a_long_variable_identifier
, or using camel-case notation, likethisIsALongVariableIdentifier