Python is a high-level, interpreted, and general-purpose programming language that places a strong emphasis on code readability. Its clear syntax allows developers to express concepts in fewer lines of code than might be possible in other languages. Two essential aspects of Python’s syntax are its set of reserved words, known as “keywords”, and the user-defined symbols called “identifiers”. This article delves deeply into these two foundational components.
1. Python Keywords
Keywords are reserved words in Python that cannot be used as variable names, function names, or any other identifiers. They are used to define the language’s syntax and structure.
List of Python Keywords:
False await else import pass None break except in raise True class finally is return and continue for lambda try as def from nonlocal while assert del global not with async elif if or yield
Each keyword has a specific meaning and serves a unique purpose. Let’s touch upon a few vital ones:
None– These represent Python’s boolean and null values respectively.
- Control Structures –
tryare used for creating loops and conditional statements.
- Functions and Classes –
classare used for defining functions, lambda functions, and classes respectively.
- Imports –
fromallow modules to be imported into the current namespace.
- Exception Handling –
assertare used for catching and raising exceptions.
- Scope –
nonlocalare used to modify variable scope.
Points to Remember:
- Python is case-sensitive, which means
trueare distinct identifiers.
- New keywords might be added in future versions, so always consult the official documentation when in doubt.
2. Python Identifiers
An identifier in Python is a name used to identify a variable, function, class, module, or other objects. They give meaning to our code and allow for human-readable references to segments of memory or functionality.
Rules for Writing Identifiers:
- Start with a Letter or Underscore – Identifiers can start with a letter (a-z, A-Z) or an underscore (_). They can’t start with a digit.
- Followed by Letters, Digits, or Underscores – After the first character, an identifier can have any combination of letters, digits, and underscores.
- No Punctuation Characters – Characters like
%, etc., are not allowed.
- Case-sensitive – Python identifiers are case-sensitive. This means
MYVARIABLEare all different.
- Private Variables – Variables starting with an underscore (e.g.,
_privateVar) are considered private and should not be accessed outside of their module or class.
- Strongly Private Variables – Those starting with double underscores (e.g.,
__strongPrivateVar) get their names mangled, making them even harder to access from outside.
- Built-in Names – Never use names that are already used for built-in functions or classes (e.g.,
- Camel Case – For class names, use the CamelCase convention (e.g.,
- Snake Case – For functions and variables, use underscores to separate words (e.g.,
Points to Remember:
- Although Python allows for almost any character combination for identifiers, it’s a good practice to use meaningful names. This makes your code more readable and maintainable.
- Avoid using names that are too similar or only differ in case, as this can lead to confusion.
Understanding Python’s keywords and identifiers is fundamental to writing effective Python code. While keywords define the core rules and structure of the language, identifiers give developers the freedom to craft their scripts, functions, and modules. By adhering to the conventions and rules set for both, one can ensure that their code is readable, maintainable, and free from unnecessary errors.