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 variables, functions, and classes or any other identifier.
Getting List of all Python keywords
We can also get all the keyword names using the below code.
Pythonimportkeyword# 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 aSyntaxError.Let's look at an example:
Python
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.
Category | Keywords |
---|
Value Keywords | True,False,None |
---|
Operator Keywords | and,or,not,is,in |
---|
Control Flow Keywords | if,else,elif,for,while,break,continue,pass,try,except,finally,raise,assert |
---|
Function and Class | def,return,lambda,yield,class |
---|
Context Management | with,as |
---|
Import and Module | import,from |
---|
Scope and Namespace | global,nonlocal |
---|
Async Programming | async,await |
---|
Difference Between Keywords and Identifiers
Keywords | Identifiers |
---|
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, while | Examples: 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
Variables | Keywords |
---|
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, name | Examples: 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?
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?
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?
Explanation:
class is used to define a new class in Python.
In Python, which keyword is used to indicate that a variable is global?
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?
It is used to define an exception.
It is used to start a function.
It is used in conditional statements to check additional conditions.
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?
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?
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?
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?
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?
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/101/10< Previous Next >