Thanks to visit codestin.com
Credit goes to www.geeksforgeeks.org

Open In App

Python Keywords

Last Updated : 12 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Keywords in Python are reserved words that have special meanings and serve specific purposes in the language syntax. Python keywords cannot be used as the names of variablesfunctions, and classes or any other identifier.

Getting List of all Python keywords

We can also get all the keyword names using the below code.

Python
import keyword

# printing all keywords at once using "kwlist()"
print("The list of keywords is : ")
print(keyword.kwlist)

Output:

The list of keywords are: 
['False', 'None', 'True',"__peg_parser__ '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']

How to Identify Python Keywords ?

  • With Syntax Highlighting - Most of IDEs provide syntax-highlight feature. You can see Keywords appearing in different color or style.
  • Look for SyntaxError - This error will encounter if you have used any keyword incorrectly. Note that keywords can not be used as identifiers (variable or a function name).

What Happens if We Use Keywords as Variable Names ?

In Python, keywords are reserved words that have special meanings and cannot be used as variable names. If you attempt to use a keyword as a variable, Python will raise a SyntaxError. Let's look at an example:

Python
for = 10 
print(for)

Output

Hangup (SIGHUP)
File "/home/guest/sandbox/Solution.py", line 1
for = 10
^
SyntaxError: invalid syntax

Let's categorize all keywords based on context for a more clear understanding.

CategoryKeywords

Value Keywords

True, False, None

Operator Keywordsand, or, not, is, in

Control Flow Keywords

if, else, elif, for, while, break, continue, pass, try, except, finally, raise, assert

Function and Classdef, return, lambda, yield, class
Context Managementwith, as
Import and Moduleimport, from
Scope and Namespaceglobal, nonlocal
Async Programmingasync, await

Difference Between Keywords and Identifiers

KeywordsIdentifiers
Reserved words in Python that have a specific meaning.Names given to variables, functions, classes, etc.
Cannot be used as variable names.Can be used as variable names (if not a keyword).
Examples: if, else, for, whileExamples: x, number, sum, result
Part of the Python syntax.User-defined, meaningful names in the code.
They cannot be redefined or changed.Can be defined and redefined by the programmer.

Difference Between Variables and Keywords

VariablesKeywords
Used to store data.Reserved words with predefined meanings in Python.
Can be created, modified, and deleted by the programmer.Cannot be modified or used as variable names.
Examples: x, age, nameExamples: if, while, for
Hold values that are manipulated in the program.Used to define the structure of Python code.
Variable names must follow naming rules but are otherwise flexible.Fixed by Python language and cannot be altered.
Suggested Quiz
10 Questions

Which of the following keywords is used to define a function in Python?

  • A

    def

  • B

    func

  • C

    define

  • D

    method

Explanation:

def is the keyword used to define a function in Python. After def, you specify the function name, followed by parentheses () and a colon : to start the function's body.

What keyword is used to handle exceptions in Python?

  • A

    except

  • B

    catch

  • C

    handle

  • D

    error

Explanation:

try is used to define a block of code that might raise an exception. except follows the try block and defines the code that runs if an exception occurs.

Which keyword is used to create a new class in Python?

  • A

    class

  • B

    new

  • C

    def

  • D

    create

Explanation:

class is used to define a new class in Python.

In Python, which keyword is used to indicate that a variable is global?

  • A

    global

  • B

    public

  • C

    shared

  • D

    external

Explanation:

In Python, the keyword global is used to indicate that a variable is global. This keyword is used inside a function to refer to a variable that is defined in the global scope, allowing the function to modify the global variable directly.

What does the elif keyword do in Python?

  • A

    It is used to define an exception.

  • B

    It is used to start a function.

  • C

    It is used in conditional statements to check additional conditions.

  • D

    It is used to define a loop.

Explanation:

elif is short for "else if" and is used after an if block to check for additional conditions when the original if condition fails.

What keyword is used to return a value from a function in Python?

  • A

    return

  • B

    yield

  • C

    output

  • D

    send

Explanation:

The return keyword is used inside a function to send back a result or value to the caller, effectively ending the function.

Which of the following keywords is used to define an object constructor in Python?

  • A

    init

  • B

    constructor

  • C

    __init__

  • D

    define

Explanation:

__init__ is a special method in Python used to initialize an object's state when a new instance of a class is created.

Which of the following keywords is used to declare an alias for a module in Python?

  • A

    import as

  • B

    using

  • C

    alias

  • D

    import

Explanation:

The import as syntax allows you to import a module and give it a shorthand alias, which is useful for convenience.

What keyword is used to define an empty function or class in Python?

  • A

    continue

  • B

    skip

  • C

    pass

  • D

    break

Explanation:

pass is a placeholder keyword that does nothing. It is used in scenarios where we need to define a function or class but haven’t implemented it yet.

Which keyword is used to define an anonymous function in Python?

  • A

    lambda

  • B

    def

  • C

    function

  • D

    anonymous

Explanation:

The lambda keyword is used to create anonymous (unnamed) functions in Python, typically for short, simple operations.

Quiz Completed Successfully
Your Score :   2/10
Accuracy :  0%
Login to View Explanation
1/10 1/10 < Previous Next >

Next Article
Article Tags :
Practice Tags :

Similar Reads