- Python Basics
- Python Tutorial
- Python Applications
- Python Versions
- Python environment setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control and Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break vs. continue
- Python pass vs. continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List vs. Tuple vs. Dict vs. Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date and Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes and Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class vs. Static Method
- Python @property Decorator
- Python Keywords
- Python Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
Keywords in Python | Python reserved words
Keywords in Python are pre-defined and reserved words. These words are defined by the Python creator. Every keyword has its own meaning and is defined to perform a particular task.
For example, the "if" keyword is used to allow the program to make a decision based on the given or certain conditions. Consider the following code as an illustration.
x = 10 y = 20 if x < y: print("The value of 'x' is less than the value of 'y'.")
The output should be:
The value of 'x' is less than the value of 'y'.
Because the value of "x" (10) is less than the value of "y" (20) in the above program, the condition "x < y" is true, and the "print()" statement or function was executed.
Keywords define the syntax and structure of the code and cannot be used as variable, function, or class names. A syntax error will result from doing so.
List of Python keywords (reserved words)
Here is the list of Python keywords (reserved words):
- and
- as
- assert
- break
- class
- continue
- def
- del
- elif
- else
- except
- False
- finally
- for
- from
- global
- if
- import
- in
- is
- lambda
- None
- nonlocal
- not
- or
- pass
- raise
- return
- True
- try
- while
- with
- yield
Note: Characters of all keywords are in lowercase except the three, which are: True, False, and None.
Can keywords be used as identifiers?
No, a keyword cannot be used as an identifier in Python.
What Is the Difference Between a Keyword and an Identifier?
Keywords are pre-defined by Python's creator, whereas identifiers are user-defined names given to variables, classes, functions, etc.
Advantages of the keywords in Python
- Readability: Keywords increase the code's readability and clarity. By incorporating keywords, the code becomes more structured and easier for other programmers to comprehend.
- Consistency: Keywords provide language-wide consistency and standardization, making it easier to write and maintain Python code.
- Clarity: Keywords provide code with clarity by indicating the programmer's intent. This helps to reduce errors and enhance the quality of the code overall.
- Keywords can assist compilers in optimizing code by identifying particular code patterns and structures.
Disadvantages of the keywords in Python
- Keywords have restrictions on the types of variable names and identifiers that can be used in Python code. Programmers should exercise caution when selecting variable names and refrain from using common words as identifiers because doing so can occasionally cause misunderstanding and mistakes.
- Complexity: Overusing keywords can increase the complexity and difficulty of the code. Confusion and mistakes may result from this, especially for inexperienced programmers.
- Keywords might not work with Python versions prior to 2.7 or with other programming languages. This can cause compatibility problems and make it more difficult to write code that is compatible with multiple platforms and operating systems.
- Syntax mistakes: When keywords are used incorrectly or in the wrong context, syntax mistakes can occur in the code. Programmers may find this frustrating and longer debugging times may result.
« Previous Tutorial Next Tutorial »