Python

Examples Of Python Keywords And Identifiers – Detailed Answers

With the aid of examples, we will go over Python’s keywords and identifiers in this article.

What does Python stand for?

Python keywords

A reserved term in Python is one that you cannot use as the name of a variable, class, function, etc. In the Python programming language, certain keywords serve specific functions and have unique meanings. For instance, since the Python term “while” is used for the while loop, you should avoid naming a variable “while” in order to avoid compilation errors.

Python 3.6 has 33 keywords in all. Open command prompt (terminal on Mac OS) and enter “Python” to acquire the operating system’s keyword list. Then, type help() and press Enter. To get a list of the keywords for the Python version that is currently running on your operating system, enter keywords.

Chaitanyas-MacBook-Pro:~ chaitanyasingh$ Python 3.6
...
>>> help()
Welcome to Python 3.6's help utility!
...
help> keywords

Here is a list of the Python keywords.  Enter any keyword to get more help.

False               def                 if                  raise
None                del                 import              return
True                elif                in                  try
and                 else                is                  while
as                  except              lambda              with
assert              finally             nonlocal            yield
break               for                 not                 
class               from                or                  
continue            global              pass

A Python keyword example

The while keyword is used in the example below to build a loop that displays the values of the num variables as long as they are larger than 5.

num = 10
while num>5:
    print(num)
    num -= 1

Output:
Python keyword example

Identifiers in Python

Python variables were covered in the last article.

Id is another name for the variable name. While naming variables in Python, there are a few guidelines that you must adhere to.

Here, for instance, the variable is of the integer type and contains the number 10. Identifier is the name of the variable, whose name is num.

num = 10

1. Either a letter or an underscore (_) must always come before the variable name. For instance, the names _str, str, num, and _num are all acceptable names for variables.
2. A number cannot be the first character in the variable’s name. For instance, the variable name 9num is not acceptable.
3. Only alphanumeric letters and underscores (A to Z, a to z, 0-9, or _) may be used in the variable name; special characters such as %, $, and # are not permitted.
4. Python’s case-sensitivity for variable names means that num and NUM are two separate variables.

Example of a Python identifier

There are three variables in the example that follows. The identifiers are the names of the variables num, _x, and a b.

# few examples of identifiers

num = 10
print(num)

_x = 100
print(_x)

a_b = 99
print(a_b)

Output:
Python Identifiers Example

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button