Python code example – Keywords and Identifiers :
# Keywords are special words which are reserved and have a specific meaning.
# All keywords in Python are case sensitive. So, you must be careful while using them in your code.
# Total keywords - 33
import keyword
print(keyword.kwlist)
print("Total number of keywords : ", len(keyword.kwlist) )
# ['False', 'None', 'True', 'and', 'as', 'assert', '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']
# Total number of keywords : 33
# Python Identifiers are user-defined names to represent a variable,
# function, class, module or any other object.
# Naming rules
# Use a sequence of letters either in lowercase (a to z) or uppercase (A to Z).
# However, you can also mix up digits (0 to 9) or an underscore (_) while writing an identifier.
# You can’t use digits to begin an identifier name. It’ll lead to the syntax error.
# the Keywords are reserved, so you should not use them as identifiers.
# for = 1
# SyntaxError: invalid syntax
# to check if some name is a keyword or not
import keyword
print(keyword.iskeyword("username")) # output : False
print(keyword.iskeyword("try")) # output : True
# we can check an identifier is valid or not is by calling the str.isidentifier() function
print("username".isidentifier()) # output : True
print("emp_id".isidentifier()) # output : True
print("1_name".isidentifier()) # output : False
# Best practices:
# The Class names starting with a capital letter.
# All other identifiers should begin with a lowercase letter.
# Declare private identifiers by using the (‘_’) underscore as their first letter.
# Avoid using names with only one character. Instead, make meaningful names.
# You can use underscore to combine multiple words to form a sensible name.