What are the Keywords in Python?

           Keywords in Python are special reserved words that have predefined meanings. These words are used to define the structure and syntax of Python code. Since they serve specific purposes, we cannot use them for naming variables, functions, or other identifiers in our programs.

A keyword is a reserved word that has a predefined meaning and cannot be used as an identifier (like a variable name, function name, etc.). These keywords define the syntax and structure of the Python language.

For example, if, else, for, while, def, class, and return are all keywords. They help in controlling the flow of a program, defining functions, and handling data structures. You can't use keywords for naming variables or other identifiers.

To see all the keywords in Python, you can use the following command:

import keyword
print(keyword.kwlist)

As of Python 3.13.0, there are 35 keywords. These keywords have specific meanings and purposes within the language, and they cannot be used as identifiers (variable names, function names, etc.).

You can check the exact number of keywords in your Python version using the following code:

import keyword
print(len(keyword.kwlist))

All Keywords in Python List is:

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', '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']
 


20+ interview questions related to Python keywords, with answers and examples

 

Question 1. What are keywords in Python?

Answer: Keywords are reserved words in Python that have a predefined meaning and cannot be used as variable names or identifiers.

Example:

 

for = 5 

Output:

for = 5 
        ^
SyntaxError: invalid syntax

 

Question 2. How many keywords are there in Python?

Answer: The number of keywords depends on the Python version. For Python 3.10, there are 35 keywords.

 

Question 3. How can you get a list of all keywords in Python?

Answer: Use the keyword module to list all Python keywords.

Example:

import keyword
print(keyword.kwlist)

Output:

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', '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']

 

Question 4. Can keywords be used as variable names?

Answer: No, keywords cannot be used as variable names.

Example:

if = 10 

Output:

invalid syntax

 

Question 5. What is the difference between a keyword and an identifier?

Answer:

  • A keyword is a reserved word with a special meaning in Python (e.g., if, else).
  • An identifier is the name used to define variables, functions, etc.

 

Question 6. What are some commonly used keywords in Python?

Answer: Commonly used keywords include:

  • Control flow: if, else, elif
  • Loops: for, while
  • Function definition: def, return
  • Data structures: class, lambda
  • Miscellaneous: import, pass, break, continue

 

Question 7. Is Python case-sensitive with keywords?

Answer: Yes, Python is case-sensitive. Keywords must be written in lowercase.

Example:

True  # Valid
true  # NameError: name 'true' is not defined

 

Question 8. What is the purpose of the None keyword in Python?

Answer: None represents the absence of a value or a null value.

Example:

x = None
print(x)

Output:

None

 

Question 9. What does the pass keyword do in Python?

Answer: The pass keyword is a placeholder that does nothing. It’s used when a statement is syntactically required but no action is needed.

Example:

if True:
    pass  # Placeholder for future code

 

Question 10. What is the role of the yield keyword?

Answer: The yield keyword is used in generators to return a value and pause execution, preserving the generator's state.

Example:

def generator():
    yield 1
    yield 2
print(list(generator()))

Output:

[1, 2]

 

Question 11. What is the purpose of the assert keyword?

Answer: The assert keyword is used to perform debugging checks, ensuring a condition is True. If the condition is False, it raises an AssertionError.

Example:

x = 5
assert x > 0  # No error
assert x < 0  # Raises AssertionError

 

Question 12. How does the with keyword work?

Answer: The with keyword simplifies resource management by automatically handling the setup and teardown process.

Example:

with open("file.txt", "r") as file:
    content = file.read()

 

Question 13. What is the use of the is keyword?

Answer: The is keyword checks whether two objects refer to the same memory location.

Example:

a = [1, 2]
b = a
print(a is b)

Output:

True

 

Question 14. What is the difference between is and ==?

Answer:

  • is: Checks object identity (same memory location).
  • ==: Checks equality of values.

Example:

a = [1, 2]
b = [1, 2]
print(a == b) 
print(a is b) 

Output:

True
False

 

Question 15. What is the purpose of the global keyword?

Answer: The global keyword allows a variable to be accessed and modified at the global scope from within a function.

Example:

x = 10
def modify():
    global x
    x = 20
modify()
print(x)

Output:

20

 

Question 16. What is the purpose of the nonlocal keyword?

Answer: The nonlocal keyword allows access to variables in the nearest enclosing scope (excluding global).

Example:

def outer():
    x = 10
    def inner():
        nonlocal x
        x = 20
    inner()
    print(x) 
outer()

Output:

20

 

Question 17. What is the del keyword used for?

Answer: The del keyword is used to delete variables, list elements, or dictionary keys.

Example:

lst = [1, 2, 3]
del lst[1]
print(lst)

Output:

 [1, 3]

 

Question 18. How is the try-except block used in Python?

Answer: The try keyword is used to test code for errors, while except handles exceptions that occur.

Example:

try:
    x = 1 / 0
except ZeroDivisionError:
    print("Cannot divide by zero!")  

Output:

Cannot divide by zero!

 

Question 19. What is the role of the lambda keyword?

Answer: The lambda keyword is used to create anonymous (nameless) functions.

Example:

square = lambda x: x ** 2
print(square(4))  

Output:

16

 

Question 20. How does the from keyword differ from import?

Answer:

  • import: Imports the entire module.
  • from: Imports specific parts of a module.

Example:

from math import sqrt
print(sqrt(16))

Output:

4.0

 

Leave a comment

You must be logged in to post a comment.

0 Comments